Build a Simple Lock
- CKB dev environment: OffCKB (β₯v0.4.0)
- JavaScript SDK: CCC (β₯v0.0.14-alpha.0)
Tutorial Overviewβ
In this tutorial, you will learn how to create a full-stack dApp, including both the frontend and the Script, to deepen your understanding of CKB blockchain development.
Our example dApp will use a simple toy lock. You will create a Lock Script named hash_lock to secure some CKB tokens and build a web interface for users to transfer tokens from this hash_lock.
The hash_lock project involves specifying a hash in the Script's script_args. To unlock it, users must provide the preimage that matches this hash.
This toy lock example isn't intended for production. Use it ONLY in Testnet or Devnet as a starting point for learning the basics.
Setup Devnet & Run Exampleβ
Step 1: Download the Source Codeβ
To get started with the tutorial dApp, download or clone the repository and navigate to the appropriate directory.
Step 2: Start the Devnetβ
To interact with the dApp, ensure that your Devnet is up and running. After installing @offckb/cli, open a terminal and start the Devnet with the following command:
- Command
- Response
offckb node
Launching CKB devnet Node...
CKB devnet RPC Proxy server running on http://127.0.0.1:28114
You might want to check pre-funded accounts and copy private keys for later use. Open another terminal and execute:
- Command
- Response
offckb accounts
Print account list, each account is funded with 42_000_000_00000000 capacity in the genesis block.
[
{
privkey: '0x6109170b275a09ad54877b82f7d9930f88cab5717d484fb4741ae9d1dd078cd6',
pubkey: '0x02025fa7b61b2365aa459807b84df065f1949d58c0ae590ff22dd2595157bffefa',
lockScript: {
codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
hashType: 'type',
args: '0x8e42b1999f265a0078503c4acec4d5e134534297'
},
address: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvwg2cen8extgq8s5puft8vf40px3f599cytcyd8',
args: '0x8e42b1999f265a0078503c4acec4d5e134534297'
},
{
privkey: '0x9f315d5a9618a39fdc487c7a67a8581d40b045bd7a42d83648ca80ef3b2cb4a1',
pubkey: '0x026efa0579f09cc7c1129b78544f70098c90b2ab155c10746316f945829c034a2d',
lockScript: {
codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
hashType: 'type',
args: '0x758d311c8483e0602dfad7b69d9053e3f917457d'
},
address: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqt435c3epyrupszm7khk6weq5lrlyt52lg48ucew',
args: '0x758d311c8483e0602dfad7b69d9053e3f917457d'
},
#...
]
Step 3: Build and Deploy the Scriptβ
In this tutorial, the hash-lock contract is implemented in TypeScript.
To compile the TypeScript contract, navigate to the root directory and run:
- Command
- Response
pnpm install
pnpm build
> node scripts/build-all.js
Building 1 contract(s): hash-lock
π¦ Building contract: hash-lock
Building hash-lock from contracts/hash-lock/src/index.ts...
π¦ Bundling with esbuild...
π§ Compiling to bytecode...
β
Contract 'hash-lock' built successfully!
π JavaScript: dist/hash-lock.js
π Bytecode: dist/hash-lock.bc
β
Successfully built: hash-lock
π All contracts built successfully!
Once compiled, the contract files will be available in the simple-lock/dist directory.
Deploy the Script binary to the Devnet:
- Command
- Response
pnpm run deploy --network devnet
> node scripts/build-all.js && node scripts/deploy.js "--network" "devnet"
Building 1 contract(s): hash-lock
π¦ Building contract: hash-lock
Building hash-lock from contracts/hash-lock/src/index.ts...
π¦ Bundling with esbuild...