Skip to content

Commit

Permalink
chore(examples): writing custom data to cell (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Jan 3, 2024
1 parent 204184c commit ccd8213
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions examples/misc/inscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This example demonstrates how to write custom data to the cell to simulate the inscription

import { Cell, Indexer, RPC, hd, Script } from "@ckb-lumos/lumos";
import { generateGenesisScriptConfigs, initializeConfig } from "@ckb-lumos/lumos/config";
import { hexify } from "@ckb-lumos/lumos/codec";
import { common } from "@ckb-lumos/lumos/common-scripts";
import {
encodeToAddress,
minimalCellCapacityCompatible,
sealTransaction,
TransactionSkeleton,
} from "@ckb-lumos/lumos/helpers";

const ENDPOINT = "https://testnet.ckb.dev";
const PRIVATE_KEY = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";

async function main() {
const indexer = new Indexer(ENDPOINT);
const rpc = new RPC(ENDPOINT);

const genesisBlock = await rpc.getBlockByNumber("0x0");
const scriptConfigs = generateGenesisScriptConfigs(genesisBlock);

initializeConfig({ PREFIX: "ckt", SCRIPTS: scriptConfigs });

const lock: Script = {
codeHash: scriptConfigs.SECP256K1_BLAKE160.CODE_HASH,
hashType: scriptConfigs.SECP256K1_BLAKE160.HASH_TYPE,
args: hd.key.privateKeyToBlake160(PRIVATE_KEY),
};

const address = encodeToAddress(lock);
console.log("Inscribe to the address:", address);

const encoder = new TextEncoder();
const mint = {
p: "brc-20",
op: "mint",
tick: "ordi",
amt: "1000",
};
const message = hexify(encoder.encode(JSON.stringify(mint)));
console.log("Inscription message:", mint);
console.log("Inscription message(binary format):", message);

const inscriptionCell: Cell = {
cellOutput: { capacity: "0x0", lock },
data: message,
};
// set the minimal capacity for the inscription cell
const capacity = minimalCellCapacityCompatible(inscriptionCell).toHexString();
inscriptionCell.cellOutput.capacity = capacity;

const txSkeleton = TransactionSkeleton({
cellProvider: {
// IMPORTANT: avoid collecting cell with type or data
collector: (queryOptions) =>
indexer.collector({
...queryOptions,
type: "empty",
data: { data: "0x", searchMode: "exact" },
}),
},
}).asMutable();

txSkeleton.update("outputs", (outputs) => outputs.push(inscriptionCell));
// inject capacity to fill the inscription cell
await common.injectCapacity(
txSkeleton,
[address],
capacity,
undefined,
undefined,
// IMPORTANT: avoid deducting capacity from the inscription cell
{ enableDeductCapacity: false }
);
await common.payFeeByFeeRate(txSkeleton, [address], 1000, undefined);
common.prepareSigningEntries(txSkeleton);

const signatures = txSkeleton
.get("signingEntries")
.map((entry) => hd.key.signRecoverable(entry.message, PRIVATE_KEY))
.toArray();

const signedTx = sealTransaction(txSkeleton, signatures);

const txHash = await rpc.sendTransaction(signedTx);
console.log("Sent transaction hash:", txHash);
}

main();

2 comments on commit ccd8213

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-ccd8213-20240103083351

npm install @ckb-lumos/[email protected]

@vercel
Copy link

@vercel vercel bot commented on ccd8213 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.