|
| 1 | +# Tutorial Target |
| 2 | +The target of this tutorial is very simple: use [Lumos](https://github.com/ckb-js/lumos) to write "Common Knowledge: Hello world!" into a cell on CKB testnet and check it on CKB explorer. |
| 3 | + |
| 4 | +TODO: In this tutorial, a browser-based runtime called [WebContainers](https://webcontainers.io/) is leveraged to create a minimal development environment only in the browser to acheive interactive tutorial experiences. Let's use the latest web capabilities to deliver a nice browser-based development experience for a new generation of interactive courses. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | +1. Node.js |
| 8 | + .... |
| 9 | + |
| 10 | +2. Lomos |
| 11 | +Lumos provides a high-level API for interacting with CKB, which makes it easier to develop dApps. |
| 12 | +You can install Lumos by running the following command: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @ckb-lumos/base @ckb-lumos/indexer @ckb-lumos/common @ckb-lumos/hd-indexer @ckb-lumos/transaction @ckb-lumos/wallet |
| 16 | +``` |
| 17 | + |
| 18 | +## Pseudo Code |
| 19 | +After installing Lumos, you can use it to send a transaction to CKB testnet, write a simple `Common Knowledge`: "Hello World" into a [cell](https://docs.nervos.org/docs/reference/cell/) on CKB. |
| 20 | +Here's an example of how you can do that: |
| 21 | + |
| 22 | +```js |
| 23 | +const { RPC } = require("@ckb-lumos/rpc"); |
| 24 | +const { Reader } = require("ckb-js-toolkit"); |
| 25 | +const { CellCollector } = require("@ckb-lumos/indexer"); |
| 26 | +const { generateAddress, key } = require("@ckb-lumos/hd-wallet"); |
| 27 | +const { sealTransaction } = require("@ckb-lumos/helpers"); |
| 28 | +const { Script } = require("@ckb-lumos/base"); |
| 29 | + |
| 30 | +// create a new RPC instance pointing to a CKB node |
| 31 | +const rpc = new RPC("https://testnet.ckb.dev/rpc"); |
| 32 | + |
| 33 | +// create a new key |
| 34 | +const privateKey = key.generatePrivateKey(); |
| 35 | +const publicKey = key.publicKeyFromPrivate(privateKey); |
| 36 | + |
| 37 | +// generate an address from the public key |
| 38 | +const address = generateAddress(publicKey); |
| 39 | + |
| 40 | +// define the lock script for the cell |
| 41 | +const lockScript = new Script({ |
| 42 | + code_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 43 | + hash_type: "type", |
| 44 | + args: address, |
| 45 | +}); |
| 46 | + |
| 47 | +// define the type script for the cell |
| 48 | +const typeScript = new Script({ |
| 49 | + code_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 50 | + hash_type: "type", |
| 51 | + args: new Reader("0x"), |
| 52 | +}); |
| 53 | + |
| 54 | +// create a new transaction that adds a cell with the message "Common Knowledge: Hello world!" |
| 55 | +const tx = { |
| 56 | + version: "0x0", |
| 57 | + cell_deps: [], |
| 58 | + header_deps: [], |
| 59 | + inputs: [], |
| 60 | + outputs: [ |
| 61 | + { |
| 62 | + capacity: "0x" + BigInt(10000000000).toString(16), |
| 63 | + lock: lockScript, |
| 64 | + type: typeScript, |
| 65 | + }, |
| 66 | + ], |
| 67 | + witnesses: [], |
| 68 | + outputs_data: [new Reader("0x" + Buffer.from("Common Knowledge: Hello world!").toString("hex")).serializeJson()], |
| 69 | +}; |
| 70 | + |
| 71 | +// sign the transaction with the private key |
| 72 | +const signedTx = sealTransaction(tx, [privateKey]); |
| 73 | + |
| 74 | +// send the transaction to the CKB node |
| 75 | +const txHash = await rpc.sendTransaction(signedTx); |
| 76 | + |
| 77 | +console.log(`Transaction sent with hash: \${txHash}`); |
| 78 | +``` |
| 79 | + |
| 80 | +## TODO |
| 81 | +- [ ] interactive tutorial experiences |
0 commit comments