TITLE: Calculating Minimum Output Capacity DESCRIPTION: This rule dictates that the total capacity of input cells must be greater than or equal to the total capacity of output cells. This ensures that no new CKB tokens are minted from thin air within a transaction, maintaining the token supply. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0022-transaction-structure/0022-transaction-structure.md#_snippet_0 LANGUAGE: mathematical_expression CODE: ``` sum(cell's capacity for each cell in inputs) ≥ sum(cell's capacity for each cell in outputs) ``` ---------------------------------------- TITLE: Example Script Structure in Nervos CKB (JSON) DESCRIPTION: This JSON snippet demonstrates the structure of a Script in Nervos CKB, which defines logic for cell ownership (`lock`) or data validation (`type`). It specifies `code_hash` to reference the RISC-V binary, `args` as input arguments for the script, and `hash_type` to determine how `code_hash` is interpreted (either `data` or `type` of a dep cell). Scripts are executed in a RISC-V VM during transaction verification. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md#_snippet_1 LANGUAGE: JSON CODE: ``` { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x0a486fb8f6fe60f76f001d6372da41be91172259", "hash_type": "type" } ``` ---------------------------------------- TITLE: Response for dry_run_transaction RPC Call (JSON) DESCRIPTION: This JSON snippet shows the expected successful response from the `dry_run_transaction` RPC call. The `result` field contains the estimated `cycles` for the provided transaction, indicating the computational cost. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_14 LANGUAGE: json CODE: ``` { "id": 2, "jsonrpc": "2.0", "result": { "cycles": "0x219" } } ``` ---------------------------------------- TITLE: Sending CKB Capacity with Ruby SDK DESCRIPTION: This Ruby snippet illustrates how to send CKB tokens (capacity) from one wallet to another. It initializes two wallets with random private keys (Alice and Bob), prints Alice's initial balance, and then uses `alice.send_capacity` to transfer a specified amount (100 CKB) to Bob's address, including a transaction fee. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_7 LANGUAGE: ruby CODE: ``` api = CKB::API.new alice = CKB::Wallet.from_hex(api, CKB::Key.random_private_key) puts "alice balance: " + alice.get_balance.to_s bob = CKB::Wallet.from_hex(api, CKB::Key.random_private_key) tx_hash = alice.send_capacity(bob.address, 100*(10**8), fee: 3000) # alice 向bob发送100个ckb,手续是 1000 ckb byte ``` ---------------------------------------- TITLE: Attempting Invalid and Valid UDT Transfers on CKB in Ruby DESCRIPTION: This Ruby code snippet first attempts an invalid User Defined Token (UDT) transfer on CKB, where the sum of output tokens exceeds the input tokens, leading to a validation error. Subsequently, it modifies the transaction to demonstrate a valid UDT transfer, ensuring that the total input tokens equal the total output tokens, adhering to the UDT's sum validation rule. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/doc2.md#_snippet_2 LANGUAGE: Ruby CODE: ``` udt_out_point = CKB::Types::OutPoint.new(tx_hash: root_udt_tx_hash, index: 0) tx = wallet.generate_tx(wallet2.address, CKB::Utils.byte_to_shannon(20000), fee: 5000) tx.cell_deps.push(duktape_out_point.dup) tx.witnesses.push(CKB::Types::Witness.new ) # 不能这么改 tx.outputs[0].type = duktape_udt_script tx.outputs_data[0] = CKB::Utils.bin_to_hex([1000000].pack("L<")) tx.inputs.push(CKB::Types::Input.new(previous_output: udt_out_point, since: 0)) tx.outputs.push(tx.outputs[1].dup) tx.outputs[2].capacity = CKB::Utils::byte_to_shannon(20000) tx.outputs[2].type = duktape_udt_script tx.outputs_data.push(CKB::Utils.bin_to_hex([1000000].pack("L<"))) signed_tx = tx.sign(wallet.key) api.send_transaction(signed_tx) # CKB::RPCError: jsonrpc error: {:code=>-3, :message=>"InvalidTx(ScriptFailure(ValidationFailure(-2)))"} tx.outputs_data[0] = CKB::Utils.bin_to_hex([900000].pack("L<")) tx.outputs_data[2] = CKB::Utils.bin_to_hex([100000].pack("L<")) signed_tx = tx.sign(wallet.key) api.send_transaction(signed_tx) ``` ---------------------------------------- TITLE: Example Cell Structure in Nervos CKB (JSON) DESCRIPTION: This JSON snippet illustrates the structure of a Cell in Nervos CKB. A Cell represents a unit of state and value, similar to Bitcoin's UTXO. It includes `capacity` for its size and CKB coin balance, a `lock` script defining ownership, and an optional `type` script for data validation. The `lock` and `type` fields are instances of the Script data structure. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md#_snippet_0 LANGUAGE: JSON CODE: ``` { "capacity": "0x19995d0ccf", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x0a486fb8f6fe60f76f001d6372da41be91172259", "hash_type": "type" }, "type": null } ``` ---------------------------------------- TITLE: Creating HTLC Cell Transaction on Nervos CKB (JavaScript) DESCRIPTION: This JavaScript snippet constructs and sends a CKB transaction to create an HTLC (Hashed Timelock Contract) cell. It defines the transaction structure, including cell dependencies, inputs, outputs (one for the HTLC and one for change), and serializes HTLC arguments using the Molecule library. The transaction is then signed and sent to the CKB network. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-02-21-ckbscript-07.md#_snippet_6 LANGUAGE: JavaScript CODE: ``` // might not want to do this. const fee = 100000000n const htlcCellCapacity = 200000000000n const customSchema = JSON.parse(fs.readFileSync('../htlc-template/src/htlc-combined.json')) const htlcArgsType = new Molecule( customSchema.declarations.find(d => d.name == "HtlcArgs")) const htlcScriptArgs = htlcArgsType.serialize([ ['a', hexStringToHexStringArray(lockHashA)], ['b', hexStringToHexStringArray(lockHashB)], ['hash', hexStringToHexStringArray('0x' + crc32('i am a secret'))] ]) const transaction = { version: '0x0', cellDeps: [ { outPoint: secp256k1Dep.outPoint, depType: 'depGroup' } ], headerDeps: [], inputs: unspentCells.map(cell => ({ previousOutput: cell.outPoint, since: '0x0' })), outputs: [ { lock: { codeHash: utils.bytesToHex(duktapeHash), hashType: 'data', args: htlcScriptArgs }, type: null, capacity: '0x' + htlcCellCapacity.toString(16) }, {B lock: lockScript, type: null, capacity: '0x' + (totalCapacity - fee - htlcCellCapacity).toString(16) } ], witnesses: [ { lock: '', inputType: '', outputType: '' } ], outputsData: [ '0x', '0x' ] } const signedTransaction = ckb.signTransaction(privateKey)(transaction) const txHash = await ckb.rpc.sendTransaction(signedTransaction, 'passthrough') console.log(`Transaction hash: ${txHash}`) fs.writeFileSync('create_htlc_cell_result.txt', txHash) } run() ``` ---------------------------------------- TITLE: Querying Address and Balance with CKB Ruby Wallet DESCRIPTION: This snippet demonstrates how to initialize a CKB wallet from a private key and query its associated address, public key, private key, Blake160 hash (lock_arg), and current balance. It showcases essential wallet functionalities for managing accounts on the CKB network. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-build-dev.md#_snippet_1 LANGUAGE: Ruby CODE: ``` _priv_key = "fe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" api = CKB::API.new wallet = CKB::Wallet.from_hex(api, "0x"+_priv_key) wallet.address wallet.key.privkey wallet.key.pubkey wallet.blake160 wallet.get_balance ``` ---------------------------------------- TITLE: Defining CKB Contract Entry Point in C DESCRIPTION: This C code snippet illustrates the basic structure of a CKB contract's `main` function, which serves as the entry point for execution within the CKB-VM. It demonstrates how to allocate memory and use `ckb_load_cell` syscalls to load input and output cells, simulating interaction with blockchain data. Contracts are treated as successful if `main` returns 0, operating in a single-threaded environment with a 4MB memory limit. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0003-ckb-vm/0003-ckb-vm.md#_snippet_0 LANGUAGE: C CODE: ``` int main(int argc, char* argv[]) { uint64_t input_cell_length = 10000; void *input_cell = malloc(input_cell_length); ckb_load_cell(input_cell, &input_cell_length, 0, 0, CKB_SOURCE_INPUT); uint64_t output_cell_length = 10000; void *output_cell = malloc(output_cell_length); ckb_load_cell(output_cell, &output_cell_length, 0, 0, CKB_SOURCE_OUTPUT); // Consume input & output cell return 0; } ``` ---------------------------------------- TITLE: Defining `no_std` Entry Point for CKB Rust Contract (Rust) DESCRIPTION: This Rust code snippet provides the essential boilerplate for a `no_std` application, which is required for CKB contracts due to the RISC-V target's limitations. It includes `no_std`, `no_main`, `start` feature, `lang_items`, a `start` function as the entry point, a `panic_handler`, and `eh_personality` for exception handling. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md#_snippet_3 LANGUAGE: Rust CODE: ``` #![no_std] #![no_main] #![feature(start)] #![feature(lang_items)] #[no_mangle] #[start] pub fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 } #[panic_handler] fn panic_handler(_: &core::panic::PanicInfo) -> ! { loop {} } #[lang = "eh_personality"] extern "C" fn eh_personality() {} ``` ---------------------------------------- TITLE: Defining C Data Structures for User Defined Token (UDT) DESCRIPTION: This C code defines the core data structures for a simplified User Defined Token (UDT) contract. It includes `balance_t` for account balances, `allowed_t` for token allowances, and `data_t` which aggregates all UDT state, including balances, allowances, owner information, and total supply. Constants like `ADDRESS_LENGTH`, `MAX_BALANCES`, and `MAX_ALLOWED` set limits for the data structures. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0003-ckb-vm/0003-ckb-vm.md#_snippet_2 LANGUAGE: C CODE: ``` #define ADDRESS_LENGTH 32 #define MAX_BALANCES 100 #define MAX_ALLOWED 100 typedef struct { char address[ADDRESS_LENGTH]; int64_t tokens; } balance_t; typedef struct { char address[ADDRESS_LENGTH]; char spender[ADDRESS_LENGTH]; int64_t tokens; } allowed_t; typedef struct { balance_t balances[MAX_BALANCES]; int used_balance; allowed_t allowed[MAX_ALLOWED]; int used_allowed; char owner[ADDRESS_LENGTH]; char newOwner[ADDRESS_LENGTH]; int64_t total_supply; } data_t; ``` ---------------------------------------- TITLE: Setting Up CKB Ruby SDK (Shell) DESCRIPTION: This snippet outlines the shell commands to clone the CKB Ruby SDK repository, navigate into its directory, run the setup script to install plugins, and finally enter the Ruby REPL environment for interactive use. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_1 LANGUAGE: shell CODE: ``` git clone https://github.com/nervosnetwork/ckb-sdk-ruby cd ckb-sdk-ruby ./bin/setup # 安装插件 ./bin/console # 进入 ruby REPL环境 ``` ---------------------------------------- TITLE: Sending CKB Transactions with Ruby Wallet DESCRIPTION: This snippet demonstrates how to send CKB tokens from one wallet to another using the `send_capacity` method. It initializes two wallets (Alice and Bob), prints Alice's balance, and then executes a transaction, specifying the recipient's address, amount, and an optional transaction fee. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-build-dev.md#_snippet_3 LANGUAGE: Ruby CODE: ``` api = CKB::API.new alice = CKB::Wallet.from_hex(api, CKB::Key.random_private_key) puts "alice balance: " + alice.get_balance.to_s bob = CKB::Wallet.from_hex(api, CKB::Key.random_private_key) tx_hash = alice.send_capacity(bob.address, 100*(10**8), fee: 3000) ``` ---------------------------------------- TITLE: Example CKB Transaction Structure (JSON) DESCRIPTION: This JSON snippet illustrates a complete CKB transaction, showcasing its various fields such as version, cell dependencies, header dependencies, inputs, outputs, output data, and witnesses. It serves as a concrete example of how a transaction is structured on the CKB blockchain. It includes details like `out_point` for dependencies and `lock` scripts for outputs. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md#_snippet_2 LANGUAGE: json CODE: ``` { "version": "0x0", "cell_deps": [ { "out_point": { "tx_hash": "0xbd864a269201d7052d4eb3f753f49f7c68b8edc386afc8bb6ef3e15a05facca2", "index": "0x0" }, "dep_type": "dep_group" } ], "header_deps": [ "0xaa1124da6a230435298d83a12dd6c13f7d58caf7853f39cea8aad992ef88a422" ], "inputs": [ { "previous_output": { "tx_hash": "0x8389eba3ae414fb6a3019aa47583e9be36d096c55ab2e00ec49bdb012c24844d", "index": "0x1" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x746a528800", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x56008385085341a6ed68decfabb3ba1f3eea7b68", "hash_type": "type" }, "type": null }, { "capacity": "0x1561d9307e88", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x886d23a7858f12ebf924baaacd774a5e2cf81132", "hash_type": "type" }, "type": null } ], "outputs_data": [ "0x", "0x" ], "witnesses": ["0x55000000100000005500000055000000410000004a975e08ff99fa0001\n 42ff3b86a836b43884b5b46f91b149f7cc5300e8607e633b7a29c94dc01c6616a12f62e74a1\n 415f57fcc5a00e41ac2d7034e90edf4fdf800"] } ``` ---------------------------------------- TITLE: CKB `since` Field Examples DESCRIPTION: This section provides examples of `since` field values for both absolute and relative time locks in CKB. It illustrates how different flag and value combinations correspond to block number, timestamp, and epoch-based lock conditions, demonstrating how transactions fail verification if the specified conditions are not met. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.md#_snippet_3 LANGUAGE: txt CODE: ``` # Absolute time lock 0x0000_0000_0000_3039 # The tx failed verification unless the block number is greater than #12345 0x4000_0000_5e83_d980 # The tx failed verification unless current blockchain date is later than 2020-04-01 0x2000_0000_0000_0400 # The tx failed verification unless the epoch number is greater than 1024 # Relative time lock 0x8000_0000_0000_0064 # The tx failed verification unless it is 100 blocks later since the input cell get confirmed on-chain 0xc000_0000_0012_7500 # The tx failed verification unless it is 14 days(blockchain time) later since the input cell get confirmed on-chain 0xa000_0000_0000_0018 # The tx failed verification unless it is 24 epochs later since the input cell get confirmed on-chain ``` ---------------------------------------- TITLE: Defining UDT Type Script and Generating Issuance Transaction - Ruby DESCRIPTION: This snippet calculates the Blake2b hash of the contract data, which serves as the `code_hash` for the UDT's type script. It then constructs the `udt_type_script` using the contract's data hash and the wallet's lock hash for access control. Finally, it generates a new transaction to issue the UDT, including initial capacity and amount. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/script/script-issue-udt.md#_snippet_3 LANGUAGE: Ruby CODE: ``` pry(main)> udt_data_hash = CKB::Blake2b.hexdigest(data) => "0x5f50913c8afb6ddb5d5189207d6e4e4f5b213fc35cb3fdea57629cb3452d295b" pry(main)> udt_type_script = CKB::Types::Script.new(code_hash: udt_data_hash, args: wallet.lock_hash) => # //生成交易 pry(main)> tx = wallet.generate_tx(wallet.address, CKB::Utils.byte_to_shannon(1000), CKB::Utils.to_hex(1000*(10**8)),fee: 5000) => #], @hash="0xd0a8b08bd1879f0dc6e871b543369bad431ee58a24c731f019981757265e8f79", @header_deps=[], @inputs=[#, @since=0>], @outputs= [#, @type=nil>, #, @type=nil>], @outputs_data=["0x174876e800", "0x"], @version=0, @witnesses= [#]> ``` ---------------------------------------- TITLE: Initializing UDT Token Data and Issuance Contract - C DESCRIPTION: This C contract demonstrates how to initialize a User-Defined Token (UDT) by setting the owner, total supply, and initial balance. The `main` function then verifies that the output cell data matches the initialized UDT data, ensuring the contract's correctness and data integrity before token issuance. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0003-ckb-vm/0003-ckb-vm.zh.md#_snippet_1 LANGUAGE: C CODE: ``` int udt_initialize(data_t *data, char owner[ADDRESS_LENGTH], int64_t total_supply) { memset(&data, 0, sizeof(data_t)); memcpy(data->owner, owner, ADDRESS_LENGTH); memcpy(data->balances[0].address, owner, ADDRESS_LENGTH); data->balances[0].tokens = total_supply; data->used_balance = 1; data->used_allowed = 0; data->total_supply = total_supply; return 0; } int main(int argc, char* argv[]) { data_t data; ret = udt_initialize(&data, "", 10000000); if (ret != 0) { return ret; } data_t *output_data = NULL; ret = ckb_read_cell(0, CKB_SOURCE_OUTPUT, (void **) &output_data, NULL); if (ret != 0) { return ret; } if (memcmp(&data, output_data, sizeof(data_t)) != 0) { return -1; } return 0; } ``` ---------------------------------------- TITLE: Constructing and Sending CKB Transaction with Duktape Script - Ruby DESCRIPTION: This code constructs and sends a CKB transaction that utilizes the previously deployed Duktape script. It defines a `TypeScript` with the script's code hash and arguments, attaches the script's cell dependency, and signs and sends the transaction. The `CKB.debug` call within the JavaScript argument demonstrates script execution and allows for debugging output. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-deploy-script.md#_snippet_1 LANGUAGE: Ruby CODE: ``` duktape_hello_type_script = CKB::Types::Script.new(code_hash: duktape_data_hash, args: CKB::Utils.bin_to_hex("CKB.debug(\"I'm running in JS!\")")) tx = wallet.generate_tx(wallet2.address, CKB::Utils.byte_to_shannon(200), fee: 5000) tx.cell_deps.push(duktape_cell_dep.dup) tx.outputs[0].type = duktape_hello_type_script.dup tx = tx.sign(wallet.key) api.send_transaction(tx) # => "0x2e4d3aab4284bc52fc6f07df66e7c8fc0e236916b8a8b8417abb2a2c60824028" ``` ---------------------------------------- TITLE: Querying CKB Address Details and Balance with Ruby SDK DESCRIPTION: This Ruby snippet demonstrates how to derive a CKB wallet from a private key and then query its associated address, public key, private key, lock_arg (blake160 hash), and current balance. It initializes `CKB::API` and `CKB::Wallet` objects to perform these operations. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_5 LANGUAGE: ruby CODE: ``` _priv_key = "fe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" api = CKB::API.new => # wallet = CKB::Wallet.from_hex(api, "0x"+_priv_key) wallet.address => "ckt1qyqrc4wkvc95f2wxguxaafwtgavpuqnqkxzqs0375w" wallet.key.privkey #私钥 => "0xfe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" wallet.key.pubkey #公钥 => "0x0351d114becc7f9ad1986b99951969c67bae6d00252c4901a6a3ef70ff1eb57dd4" wallet.blake160 #lock_arg => "0x3c55d6660b44a9c6470ddea5cb47581e0260b184" wallet.get_balance # 余额 => 103265972696 ``` ---------------------------------------- TITLE: Defining `abort` Function for `no_std` Rust Contract (Rust) DESCRIPTION: This Rust snippet defines an `abort` function, which is required to resolve an "undefined symbol: abort" compilation error in `no_std` environments. It ensures that the program can handle unrecoverable errors by panicking, preventing unexpected behavior. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md#_snippet_9 LANGUAGE: Rust CODE: ``` #[no_mangle] pub fn abort() -> ! { panic!("abort!") } ``` ---------------------------------------- TITLE: CKB Transaction Blueprint for Anyone-Can-Pay Unlock DESCRIPTION: This conceptual code snippet illustrates the detailed structure of a CKB transaction for unlocking funds via CKByte payment using the 'anyone-can-pay' lock. It specifies the input and output cells, including their capacities, lock scripts, type scripts, and data, along with the required witness for signature validation. It demonstrates how minimum CKByte and UDT transfers are handled within the transaction. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0026-anyone-can-pay/0026-anyone-can-pay.md#_snippet_5 LANGUAGE: CKB Transaction Structure CODE: ``` Inputs: Deposit Normal Cell: Capacity: 500 CKBytes Lock: code_hash: secp256k1_blake2b lock args: Type: code_hash: simple udt lock args: Data: Amount: 200000 UDT Anyone-can-pay Cell: Capacity: 1000 CKBytes Lock: code_hash: anyone-can-pay lock args: Type: code_hash: simple udt lock args: Data: Amount: 3000 UDT Outputs: Deposit Change Cell: Capacity: 489.99 CKBytes Lock: code_hash: secp256k1_blake2b lock args: Type: code_hash: simple udt lock args: Data: Amount: 200000 UDT Anyone-can-pay Cell: Capacity: 1010 CKBytes Lock: code_hash: anyone-can-pay lock args: Type: code_hash: simple udt lock args: Data: Amount: 3000 UDT Witnesses: ``` ---------------------------------------- TITLE: Serializing Molecule Data for HTLC Arguments (JavaScript) DESCRIPTION: This snippet illustrates the serialization of a Molecule data structure for HTLC arguments. It includes a utility function `hexStringToHexStringArray` to convert hexadecimal strings into an array of hex strings, which is then used as input for serializing the `HtlcArgs` Molecule type from a custom schema. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-02-21-ckbscript-07.md#_snippet_7 LANGUAGE: JavaScript CODE: ``` // ... function hexStringToHexStringArray(s) { let arr = [] for (let i = 2; i < s.length; i += 2) { arr.push('0x' + s.substr(i, 2)) } return arr } // ... const customSchema = JSON.parse(fs.readFileSync('../htlc-template/src/htlc-combined.json')) const htlcArgsType = new Molecule( customSchema.declarations.find(d => d.name == "HtlcArgs")) const htlcScriptArgs = htlcArgsType.serialize([ ['a', hexStringToHexStringArray(lockHashA)], ['b', hexStringToHexStringArray(lockHashB)], ['hash', hexStringToHexStringArray('0x' + crc32('i am a secret'))] ]) // ... ``` ---------------------------------------- TITLE: Generating New CKB Address from Random Private Key (Ruby) DESCRIPTION: This Ruby snippet demonstrates how to generate a new CKB address by first creating a random private key using `CKB::Key.random_private_key` and then deriving a `CKB::Wallet` object from it. The resulting wallet object contains the new address and associated key details. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_6 LANGUAGE: ruby CODE: ``` api = CKB::API.new new_key = CKB::Key.random_private_key #生成私钥 => "0x7b60c0ca227427836588f31291c90979b4d31f0d73d28772f172dc3f1b47a672" wallet2 = CKB::Wallet.from_hex(api, new_key) => #>, @address="ckt1qyqpzjkd4zaxrnk46ryssmlwjxrhc2ttxfvswf5pjs", @api=#, @blake160="0x114acda8ba61ced5d0c9086fee91877c296b3259", @hash_type="type", @key= #, @pubkey="0x039c730f2a61cf1e1cd9f98c916e86a5176b973d531fddd0444c780bcebe5aee93", @skip_data_and_type=true> ``` ---------------------------------------- TITLE: Generating New CKB Address and Wallet in Ruby DESCRIPTION: This snippet illustrates how to generate a new random private key and then derive a new CKB wallet and address from it. It's useful for creating new accounts programmatically for testing or development purposes on the CKB network. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-build-dev.md#_snippet_2 LANGUAGE: Ruby CODE: ``` api = CKB::API.new new_key = CKB::Key.random_private_key wallet2 = CKB::Wallet.from_hex(api, new_key) ``` ---------------------------------------- TITLE: Sending a Transaction to the Pool (Bash) DESCRIPTION: This `bash` example demonstrates how to send a new transaction to the CKB node's transaction pool using the `send_transaction` RPC method. The request includes a detailed transaction object with `cell_deps`, `header_deps`, `inputs`, `outputs`, `outputs_data`, `version`, and `witnesses`, along with an `outputs_validator` parameter. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_43 LANGUAGE: bash CODE: ``` echo '{ "id": 2, "jsonrpc": "2.0", "method": "send_transaction", "params": [ { "cell_deps": [ { "dep_type": "code", "out_point": { "index": "0x0", "tx_hash": "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3" } } ], "header_deps": [ "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed" ], "inputs": [ { "previous_output": { "index": "0x0", "tx_hash": "0x365698b50ca0da75dca2c87f9e7b563811d3b5813736b8cc62cc3b106faceb17" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x2540be400", "lock": { "args": "0x", "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", "hash_type": "data" }, "type": null } ], "outputs_data": [ "0x" ], "version": "0x0", "witnesses": [] }, "passthrough" ] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` ---------------------------------------- TITLE: Unlocking HTLC Cell with Secret String on Nervos CKB (JavaScript) DESCRIPTION: This JavaScript script outlines the process for unlocking an HTLC-protected cell on the Nervos CKB blockchain by providing the correct secret string. It initializes the CKB SDK, loads necessary dependencies, derives the public key hash, constructs the lock script, and prepares a transaction that includes the HTLC witness with the serialized secret. The script also handles command-line arguments for configuration. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-02-21-ckbscript-07.md#_snippet_8 LANGUAGE: JavaScript CODE: ``` #!/usr/bin/env node const { Molecule } = require('molecule-javascript') const crc32 = require('crc32') const CKB = require("@ ``` ---------------------------------------- TITLE: SUDT Cell Data Structure - Conceptual DESCRIPTION: This conceptual structure defines the components of a Simple UDT (SUDT) cell on Nervos CKB. It specifies that the cell data segment must store the token amount as a 128-bit unsigned integer, the type script's code_hash points to the simple_udt type script, and its args contain the owner lock script hash. The lock field is user-defined, allowing flexibility for token holders. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0025-simple-udt/0025-simple-udt.md#_snippet_0 LANGUAGE: Conceptual CODE: ``` data: amount: uint128 type: code_hash: simple_udt type script args: owner lock script hash (...) lock: ``` ---------------------------------------- TITLE: Defining SUDT Cell Structure on CKB DESCRIPTION: This snippet illustrates the fundamental data structure of a Simple User Defined Token (SUDT) cell on Nervos CKB. It specifies that the `data` field holds the token `amount` as a `uint128`, the `type` field includes the `code_hash` of the SUDT type script and `args` for the owner's lock script hash, and the `lock` field is user-defined. This structure is crucial for defining a unique SUDT. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0025-simple-udt/0025-simple-udt.zh.md#_snippet_0 LANGUAGE: CKB Cell Structure CODE: ``` data: amount: uint128 type: code_hash: simple_udt type script args: owner lock script hash (...) lock: ``` ---------------------------------------- TITLE: Verifying Relative 'Since' Locks in Nervos CKB (Rust) DESCRIPTION: This function verifies relative 'since' locks, which are calculated relative to the block where the input cell was created. It checks if the current chain state (block number, epoch, or median time) is sufficiently advanced beyond the input cell's creation time, based on the 'since' value. An `Immature` error is returned if the relative lock condition is not satisfied. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.md#_snippet_7 LANGUAGE: Rust CODE: ``` fn verify_relative_lock(&self, since: Since, cell_meta: &CellMeta) -> Result<(), Error> { if since.is_relative() { let info = match cell_meta.transaction_info { Some(ref transaction_info) => Ok(transaction_info), None => Err(TransactionError::Immature), }?; match since.extract_metric() { Some(SinceMetric::BlockNumber(block_number)) => { if self.block_number < info.block_number + block_number { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::EpochNumberWithFraction(epoch_number_with_fraction)) => { let a = self.epoch_number_with_fraction.to_rational(); let b = info.block_epoch.to_rational() + epoch_number_with_fraction.to_rational(); if a < b { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::Timestamp(timestamp)) => { // pass_median_time(current_block) starts with tip block, which is the // parent of current block. // pass_median_time(input_cell's block) starts with cell_block_number - 1, // which is the parent of input_cell's block let cell_median_timestamp = self.parent_median_time(&info.block_hash); let current_median_time = self.block_median_time(&self.parent_hash); if current_median_time < cell_median_timestamp + timestamp { return Err(TransactionError::Immature).into()); } } None => { return Err(TransactionError::InvalidSince).into()); } } } Ok(()) } ``` ---------------------------------------- TITLE: Verifying Absolute Transaction Locks in Rust DESCRIPTION: This function verifies absolute 'since' locks for a transaction input in Rust. It checks if the current block's properties (block number, epoch, or median timestamp) meet or exceed the specified absolute 'since' value. It returns an `Immature` error if the conditions are not met or `InvalidSince` for invalid flags. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.zh.md#_snippet_2 LANGUAGE: Rust CODE: ``` fn verify_absolute_lock(&self, since: Since) -> Result<(), Error> { if since.is_relative() { return Err(TransactionError::InvalidSince).into()); } match since.extract_metric() { Some(SinceMetric::BlockNumber(block_number)) => { if self.block_number < block_number { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::EpochNumberWithFraction(epoch_number_with_fraction)) => { if self.epoch_number_with_fraction < epoch_number_with_fraction { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::Timestamp(timestamp)) => { let tip_timestamp = self.block_median_time(&self.parent_hash); if tip_timestamp < timestamp { return Err(TransactionError::Immature).into()); } } None => { return Err(TransactionError::InvalidSince).into()); } } Ok(()) } ``` ---------------------------------------- TITLE: Verifying Relative Transaction Locks in Rust DESCRIPTION: This function verifies relative 'since' locks for a transaction input in Rust. It calculates the required block, epoch, or timestamp based on the input cell's creation block and the relative 'since' value. It returns an `Immature` error if the current block's properties do not satisfy the relative conditions or `InvalidSince` for invalid flags. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.zh.md#_snippet_3 LANGUAGE: Rust CODE: ``` fn verify_relative_lock(&self, since: Since, cell_meta: &CellMeta) -> Result<(), Error> { if since.is_relative() { let info = match cell_meta.transaction_info { Some(ref transaction_info) => Ok(transaction_info), None => Err(TransactionError::Immature), }?; match since.extract_metric() { Some(SinceMetric::BlockNumber(block_number)) => { if self.block_number < info.block_number + block_number { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::EpochNumberWithFraction(epoch_number_with_fraction)) => { let a = self.epoch_number_with_fraction.to_rational(); let b = info.block_epoch.to_rational() + epoch_number_with_fraction.to_rational(); if a < b { return Err(TransactionError::Immature).into()); } } Some(SinceMetric::Timestamp(timestamp)) => { // pass_median_time(current_block) starts with tip block, which is the // parent of current block. // pass_median_time(input_cell's block) starts with cell_block_number - 1, // which is the parent of input_cell's block let cell_median_timestamp = self.parent_median_time(&info.block_hash); let current_median_time = self.block_median_time(&self.parent_hash); if current_median_time < cell_median_timestamp + timestamp { return Err(TransactionError::Immature).into()); } } None => { return Err(TransactionError::InvalidSince).into()); } } } Ok(()) } ``` ---------------------------------------- TITLE: Loading Input Cell Fields - CKB Syscall (C) DESCRIPTION: The `ckb_load_input_by_field` syscall fetches a specific field from a cell input in the current transaction. It uses `addr`, `len`, and `offset` for partial loading, `index` to identify the input, `source` to specify the input type, and `field` to denote the specific data to read (e.g., `out_point` or `since`). Similar to other loading syscalls, it feeds the data into VM memory and can return errors for invalid source, index out of bounds, or an invalid field value. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md#_snippet_13 LANGUAGE: C CODE: ``` int ckb_load_input_by_field(void* addr, uint64_t* len, size_t offset, size_t index, size_t source, size_t field) { return syscall(2083, addr, len, offset, index, source, field); } ``` ---------------------------------------- TITLE: Loading CKB Cell (C) DESCRIPTION: This syscall locates a single cell in the current transaction based on `source` and `index`, serializes the entire cell into Molecule Encoding, and feeds it into VM memory using partial loading. Arguments include `addr`, `len`, `offset` for partial loading, `index` for entry selection, and `source` to specify input, output, or dep cells, with options for cells having the same running script. It wraps `syscall(2071, ...)` and can return `1` for out-of-bound index or trigger VM errors for invalid source. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md#_snippet_8 LANGUAGE: C CODE: ``` int ckb_load_cell(void* addr, uint64_t* len, size_t offset, size_t index, size_t source) { return syscall(2071, addr, len, offset, index, source, 0); } ``` ---------------------------------------- TITLE: Loading Input Cells - CKB Syscall (C) DESCRIPTION: This `ckb_load_input` syscall loads a complete cell input from the current transaction into VM memory. It serializes the entire cell input into Molecule Encoding format. Parameters include `addr` for the destination address, `len` for data length, `offset` for partial loading, `index` for the input's position, and `source` to specify the input type (e.g., input cells or input cells with the same script). Invalid source values or out-of-bound indices can lead to VM errors or specific return codes. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md#_snippet_12 LANGUAGE: C CODE: ``` int ckb_load_input(void* addr, uint64_t* len, size_t offset, size_t index, size_t source) { return syscall(2073, addr, len, offset, index, source, 0); } ``` ---------------------------------------- TITLE: Defining Merkle Proof Structure (CKB Schema) DESCRIPTION: This snippet defines the `Proof` structure, which is essential for providing Merkle Proofs in CKB. It specifies two fields: `indexes` for the item positions (in ascending order) and `nodes` for the necessary sibling hashes along the path from leaves to root (in descending order by index), enabling the reconstruction and verification of the Merkle Root. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0006-merkle-tree/0006-merkle-tree.md#_snippet_0 LANGUAGE: CKB Schema CODE: ``` table Proof { // indexes of items indexes: [uint32]; // nodes on the path which can not be calculated, in descending order by index nodes: [H256]; } ```