TITLE: Install CKB SDK Go DESCRIPTION: This command installs the ckb-sdk-go library using Go's module system. It fetches the latest version 2 of the SDK. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_0 LANGUAGE: bash CODE: ``` go get -v github.com/nervosnetwork/ckb-sdk-go/v2 ``` ---------------------------------------- TITLE: Initialize CKB, Indexer, and Mercury Clients in Go DESCRIPTION: Demonstrates how to establish connections to CKB, CKB-indexer (legacy, now integrated into CKB), and Mercury nodes using their respective Go RPC dialers. This allows interaction with their JSON-RPC APIs. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_1 LANGUAGE: go CODE: ``` ckbClient, err := rpc.Dial("http://127.0.0.1:8114") //!!NOTE: // Indexer RPCs are now integrated into CKB, people now should directly use CKB clients, not the legacy indexer client // check https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md#module-indexer for equivalent RPCs //indexerClient, err := indexer.Dial("http://127.0.0.1:8114") mercuryClient , err := mercury.Dial("http://127.0.0.1:8116") ``` ---------------------------------------- TITLE: Build CKB Transfer Transaction with Mercury in Go DESCRIPTION: This snippet demonstrates how to build a simple CKB transfer transaction using the Mercury JSON-RPC API. It initializes a `SimpleTransferPayload` with sender, receiver, CKB amount, and fee rate, then calls `BuildSimpleTransferTransaction` on a Mercury client to get an unsigned raw transaction. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_5 LANGUAGE: Go CODE: ``` sender := "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0yvcdtsu5wcr2jldtl72fhkruf0w5vymsp6rk9r" receiver := "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvglkprurm00l7hrs3rfqmmzyy3ll7djdsujdm6z" ckbAmount := amount.CkbToShannon(100) // Convert CKB to Shannon (1 CKB = 10^8 Shannon) req := &model.SimpleTransferPayload{ AssetInfo: model.NewCkbAsset(), From: []string{sender}, To: []*model.ToInfo{{receiver, ckbAmount}}, FeeRate: 1000, } // Get an unsigned raw transaction with the help of Mercury txWithScriptGroups, err := mercuryClient.BuildSimpleTransferTransaction(req) ``` ---------------------------------------- TITLE: Sign CKB Transactions with ScriptGroup and TransactionSigner in Go DESCRIPTION: This snippet illustrates the new transaction signing mechanism in ckb-sdk-go, which utilizes `ScriptGroup` and `TransactionSigner`. It contrasts the manual iteration through script groups and individual signing with the simplified approach using `signer.GetTransactionSignerInstance` and `SignTransaction`. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/docs/migration-guide.md#_snippet_4 LANGUAGE: go CODE: ``` // Before: addressWithKeys := make(map[string]string) // omit the code to put private key to `addressWithKeys` txWithGroup, err := mercuryClient.BuildSimpleTransferTransaction(req) scriptGroups := txWithGroup.GetScriptGroup() tx := txWithGroup.GetTransaction() for _, group := range scriptGroups { key, _ := secp256k1.HexToKey(addressWithKeys[group.GetAddress()]) resp.SignTransaction(tx, group, key) } txHash, err := ckbClient.SendTransaction(context.Background(), txWithScriptGroup.TxView) // Now: txWithScriptGroups, err := mercuryClient.BuildSimpleTransferTransaction(req) txSigner := signer.GetTransactionSignerInstance(types.NetworkTest) txSigner.SignTransaction(txWithScriptGroup, "0x6c9ed03816e31...") txHash, err := ckbClient.SendTransaction(context.Background(), txWithScriptGroup.TxView) ``` ---------------------------------------- TITLE: Sign and Send CKB Transaction in Go DESCRIPTION: This snippet illustrates the process of signing a prepared `TransactionWithScriptGroups` using a private key and then sending the signed transaction to the CKB network. It uses `GetTransactionSignerInstance` to sign and `SendTransaction` on a CKB client to broadcast. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_6 LANGUAGE: Go CODE: ``` // You can get txWithScriptGroups by manual or by mercury var txWithScriptGroups *transaction.TransactionWithScriptGroups // 0. Set your private key privKey := "0xccb083b37aa346c5ce2e1f99a687a153baa04052f26db6ab3c26d6a4cc15c5f1" // 1. Sign transaction with your private key txSigner := signer.GetTransactionSignerInstance(types.NetworkTest) txSigner.SignTransactionByPrivateKeys(txWithScriptGroups, privKey) // 2. Send transaction to CKB node txHash, err := ckbClient.SendTransaction(context.Background(), txWithScriptGroups.TxView) ``` ---------------------------------------- TITLE: Construct a CKB TransactionWithScriptGroups Manually in Go DESCRIPTION: Illustrates the manual construction of a `TransactionWithScriptGroups` object, which is required for signing transactions. This involves defining transaction version, cell dependencies, inputs, outputs with capacities and lock scripts, output data, and witnesses, along with associated script groups. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_3 LANGUAGE: go CODE: ``` tx := &types.Transaction{ Version: 0, CellDeps: []*types.CellDep{ &types.CellDep{ OutPoint: &types.OutPoint{ TxHash: types.HexToHash("0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37"), Index: 0, }, DepType: types.DepTypeDepGroup, }, }, HeaderDeps: nil, Inputs: []*types.CellInput{ &types.CellInput{ Since: 0, PreviousOutput: &types.OutPoint{ TxHash: types.HexToHash("0x2ff7f46d509c85e1878cf091aef0ba0b89f34f9fea9e8bc868aed2d627490512"), Index: 1, }, }, }, Outputs: []*types.CellOutput{ &types.CellOutput{ Capacity: 10000000000, Lock: &types.Script{ CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), HashType: types.HashTypeType, Args: common.FromHex("0x3f1573b44218d4c12a91919a58a863be415a2bc3"), }, Type: nil, }, &types.CellOutput{ Capacity: 90000000000, Lock: &types.Script{ CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), HashType: types.HashTypeType, Args: common.FromHex("0xb1d41a1fb06f782cf10a87f3e49e80711af63fcf"), }, Type: nil, }, }, OutputsData: make([][]byte, 2), Witnesses: [][]byte{ common.FromHex("0x55000000100000005500000055000000410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), }, } scriptGroups := []*transaction.ScriptGroup{ &transaction.ScriptGroup{ Script: types.Script{ CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), HashType: types.HashTypeType, Args: common.FromHex("0x3f1573b44218d4c12a91919a58a863be415a2bc3"), }, GroupType: types.ScriptTypeLock, InputIndices: []uint32{0}, }, } txWithScriptGroups := &transaction.TransactionWithScriptGroups{ TxView: tx, ScriptGroups: scriptGroups, } ``` ---------------------------------------- TITLE: Generate New CKB Address (secp256k1_blake160_signhash_all) in Go DESCRIPTION: This snippet shows how to generate a new CKB address, specifically the common `secp256k1_blake160_signhash_all` type. It generates a random private key, derives the lock script, and then encodes it into an address. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_7 LANGUAGE: Go CODE: ``` // Generate a new address randomly key, err := secp256k1.RandomNew() if err != nil { // handle error } script := systemscript.Secp256K1Blake160SignhashAll(key) addr := &address.Address{Script: script, Network: types.NetworkTest} encodedAddr, err := addr.Encode() ``` ---------------------------------------- TITLE: Convert Public Key to CKB Address (secp256k1_blake160_signhash_all) in Go DESCRIPTION: This snippet demonstrates how to convert an elliptic curve public key (compressed format, 33 bytes) into a CKB address of type `secp256k1_blake160_signhash_all`. It uses `Secp256K1Blake160SignhashAllByPublicKey` to derive the script and then creates the address object. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_8 LANGUAGE: Go CODE: ``` // You should provide an elliptic curve public key of compressed format, with 33 bytes. script, err := systemscript.Secp256K1Blake160SignhashAllByPublicKey("0x03a0a7a7597b019828a1dda6ed52ab25181073ec3a9825d28b9abbb932fe1ec83d") if err != nil { // handle error } addr := &address.Address{Script: script, Network: types.NetworkTest} ``` ---------------------------------------- TITLE: Retrieve CKB Block by Hash using Go Client DESCRIPTION: Shows an example of calling a JSON-RPC API method, `GetBlock`, on the CKB client to retrieve block details using a hexadecimal hash. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_2 LANGUAGE: go CODE: ``` block, err := ckbClient.GetBlock(context.Background(), types.HexToHash("0x77fdd22f6ae8a717de9ae2b128834e9b2a1424378b5fc95606ba017aab5fed75")) ``` ---------------------------------------- TITLE: Parse CKB Address from Encoded String in Go DESCRIPTION: This snippet shows how to parse a CKB address from its encoded string representation (bech32m format). It uses `address.Decode` to retrieve the address object, from which the underlying script and network information can be extracted. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_9 LANGUAGE: Go CODE: ``` addr, err := address.Decode("ckt1qyqxgp7za7dajm5wzjkye52asc8fxvvqy9eqlhp82g") if err != nil { // handle error } script := addr.Script network := addr.Network ``` ---------------------------------------- TITLE: Decode CKB Address in Go DESCRIPTION: This snippet demonstrates how to decode a CKB address using the new ckb-sdk-go. It shows the updated `address.Decode` method compared to the deprecated `address.Parse` method, illustrating how to extract the script and network from the decoded address. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/docs/migration-guide.md#_snippet_0 LANGUAGE: go CODE: ``` encoded := "ckt1qzda..." // Before parsedAddr, err := address.Parse(encoded) script := parsedAddr.Script network := parsedAddr.Mode // Now: addr, err := address.Decode(encoded) script := addr.Script network := addr.Network ``` ---------------------------------------- TITLE: Encode CKB Address from Script and Network in Go DESCRIPTION: This snippet illustrates the updated method for encoding a CKB address from a script and network using the new ckb-sdk-go. It contrasts the previous `address.ConvertScriptToAddress` with the new approach of creating an `address.Address` struct and calling its `Encode` method. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/docs/migration-guide.md#_snippet_1 LANGUAGE: go CODE: ``` // Before: encoded, err := address.ConvertScriptToAddress(address.Testnet, script) // Now: addr := &address.Address{Script: script, Network: types.NetworkTest} encoded := addr.Encode() ``` ---------------------------------------- TITLE: Create Mercury JSON-RPC Request Payload in Go (Without Builder) DESCRIPTION: This snippet demonstrates the new way to create a Mercury JSON-RPC request payload in ckb-sdk-go, which removes the builder pattern for simplicity. It contrasts the verbose builder chain with the new direct struct initialization for `model.QueryTransactionsPayload`. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/docs/migration-guide.md#_snippet_3 LANGUAGE: go CODE: ``` // Before: payload := model.NewQueryTransactionsPayloadBuilder(). SetItem(item). AddAssetInfo(common.NewUdtAsset(constant.UDT_HASH)). SetExtra(&extra). AddBlockRange(&model.BlockRange{ From: 0, To: 4592529, }).Build() payload.StructureType = model.DoubleEntry // Now: payload := model.QueryTransactionsPayload{ Item: item, AssetInfos: []*model.AssetInfo{model.NewUdtAsset(constant.UDT_HASH)}, Extra: &extra, BlockRange: &model.BlockRange{ From: 0, To: 4592529, }, StructureType: model.StructureTypeDoubleEntry, } ``` ---------------------------------------- TITLE: Update Go Package Imports for ckb-sdk-go Mercury Model DESCRIPTION: This snippet shows the updated import paths for the Mercury model package in ckb-sdk-go. The previous multiple imports from `mercury/model/*` are now consolidated into a single import from `mercury/model` for simplicity. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/docs/migration-guide.md#_snippet_2 LANGUAGE: go CODE: ``` // Before: import ( "github.com/nervosnetwork/ckb-sdk-go/mercury/model/common" "github.com/nervosnetwork/ckb-sdk-go/mercury/model/resp" ) // Now: import ( "github.com/nervosnetwork/ckb-sdk-go/mercury/model" ) ``` ---------------------------------------- TITLE: CKB Ecosystem JSON-RPC API Documentation References DESCRIPTION: Provides links to external documentation for JSON-RPC APIs related to CKB, CKB Indexer Module, and Mercury. These resources detail the available methods, parameters, and return types for interacting with the respective nodes. SOURCE: https://github.com/nervosnetwork/ckb-sdk-go/blob/v2/README.md#_snippet_4 LANGUAGE: APIDOC CODE: ``` CKB RPC doc: https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md CKB Indexer Module RPC doc: https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md#module-indexer Mercury RPC doc: https://github.com/nervosnetwork/mercury/blob/main/core/rpc/README.md ```