{"id":18674078,"url":"https://github.com/nervosnetwork/ckb-sdk-go","last_synced_at":"2025-04-09T13:08:06.480Z","repository":{"id":37103429,"uuid":"225023789","full_name":"nervosnetwork/ckb-sdk-go","owner":"nervosnetwork","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-17T04:27:11.000Z","size":2060,"stargazers_count":22,"open_issues_count":7,"forks_count":24,"subscribers_count":11,"default_branch":"v2","last_synced_at":"2025-04-02T06:56:02.223Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nervosnetwork.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-30T14:24:40.000Z","updated_at":"2024-12-17T04:27:16.000Z","dependencies_parsed_at":"2024-06-18T18:16:16.320Z","dependency_job_id":"67e3c4c7-546e-4fe1-9306-b8c405dd035a","html_url":"https://github.com/nervosnetwork/ckb-sdk-go","commit_stats":{"total_commits":312,"total_committers":7,"mean_commits":44.57142857142857,"dds":0.5512820512820513,"last_synced_commit":"6b6211fa0b82c673e9f2ae9301c093cb425ae9a2"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fckb-sdk-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fckb-sdk-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fckb-sdk-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fckb-sdk-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nervosnetwork","download_url":"https://codeload.github.com/nervosnetwork/ckb-sdk-go/tar.gz/refs/heads/v2","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045233,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T09:17:25.613Z","updated_at":"2025-04-09T13:08:06.460Z","avatar_url":"https://github.com/nervosnetwork.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CKB SDK Golang\n\n[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/nervosnetwork/ckb-sdk-go/blob/master/LICENSE)\n[![Go version](https://img.shields.io/badge/go-1.11.5-blue.svg)](https://github.com/moovweb/gvm)\n[![Telegram Group](https://cdn.rawgit.com/Patrolavia/telegram-badge/8fe3382b/chat.svg)](https://t.me/nervos_ckb_dev)\n\nGolang SDK for Nervos [CKB](https://github.com/nervosnetwork/ckb).\n\nThe ckb-sdk-go is still under development and considered to be a work in progress. You should get familiar with CKB transaction\nstructure and RPC before using it.\n\n## Installation\n\n### Minimum requirements\n\n| Components | Version | Description |\n|----------|-------------|-------------|\n| [Golang](https://golang.org) | \u0026ge; 1.11.5 | Go programming language |\n\n### Install\n\n```bash\ngo get -v github.com/nervosnetwork/ckb-sdk-go/v2\n```\n\n## Quick start\n\n### Setup\n\nckb-sdk-go provides a convenient client to help you easily interact with [CKB](https://github.com/nervosnetwork/ckb), [CKB-indexer](https://github.com/nervosnetwork/ckb-indexer) or [Mercury](https://github.com/nervosnetwork/mercury) node.\n\n```go\nckbClient, err := rpc.Dial(\"http://127.0.0.1:8114\")\n//!!NOTE: \n// Indexer RPCs are now integrated into CKB, people now should directly use CKB clients, not the legacy indexer client\n// check https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md#module-indexer for equivalent RPCs\n//indexerClient, err := indexer.Dial(\"http://127.0.0.1:8114\")\nmercuryClient , err := mercury.Dial(\"http://127.0.0.1:8116\")\n```\n\nYou can call JSON-RPC APIs via these clients.\n\n```go\nblock, err := ckbClient.GetBlock(context.Background(), types.HexToHash(\"0x77fdd22f6ae8a717de9ae2b128834e9b2a1424378b5fc95606ba017aab5fed75\"))\n```\n\nFor examples, you can check [collector usage examples](./collector/example)\n\n\nFor more details about JSON-RPC APIs, please check:\n\n- [CKB RPC doc](https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md)\n- [CKB Indexer Module RPC doc](https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md#module-indexer)\n- [Mercury RPC doc](https://github.com/nervosnetwork/mercury/blob/main/core/rpc/README.md).\n\n### Build transaction manually\n\nCkb-sdk-go provides [a signer mechanism](#sign-and-send-transaction) to sign transaction. The only thing you need to provide is an instance of `TransactionWithScriptGroups` and transaction signer will do all signing jobs for you. Here is the code to construct a `TransactionWithScriptGroups` by manual.\n\n```go\ntx := \u0026types.Transaction{\n\tVersion: 0,\n\tCellDeps: []*types.CellDep{\n\t\t\u0026types.CellDep{\n\t\t\tOutPoint: \u0026types.OutPoint{\n\t\t\t\tTxHash: types.HexToHash(\"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37\"),\n\t\t\t\tIndex:  0,\n\t\t\t},\n\t\t\tDepType: types.DepTypeDepGroup,\n\t\t},\n\t},\n\tHeaderDeps: nil,\n\tInputs: []*types.CellInput{\n\t\t\u0026types.CellInput{\n\t\t\tSince: 0,\n\t\t\tPreviousOutput: \u0026types.OutPoint{\n\t\t\t\tTxHash: types.HexToHash(\"0x2ff7f46d509c85e1878cf091aef0ba0b89f34f9fea9e8bc868aed2d627490512\"),\n\t\t\t\tIndex:  1,\n\t\t\t},\n\t\t},\n\t},\n\tOutputs: []*types.CellOutput{\n\t\t\u0026types.CellOutput{\n\t\t\tCapacity: 10000000000,\n\t\t\tLock: \u0026types.Script{\n\t\t\t\tCodeHash: types.HexToHash(\"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8\"),\n\t\t\t\tHashType: types.HashTypeType,\n\t\t\t\tArgs:     common.FromHex(\"0x3f1573b44218d4c12a91919a58a863be415a2bc3\"),\n\t\t\t},\n\t\t\tType: nil,\n\t\t},\n\t\t\u0026types.CellOutput{\n\t\t\tCapacity: 90000000000,\n\t\t\tLock: \u0026types.Script{\n\t\t\t\tCodeHash: types.HexToHash(\"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8\"),\n\t\t\t\tHashType: types.HashTypeType,\n\t\t\t\tArgs:     common.FromHex(\"0xb1d41a1fb06f782cf10a87f3e49e80711af63fcf\"),\n\t\t\t},\n\t\t\tType: nil,\n\t\t},\n\t},\n\tOutputsData: make([][]byte, 2),\n\tWitnesses: [][]byte{\n\t\tcommon.FromHex(\"0x55000000100000005500000055000000410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"),\n\t},\n}\n\nscriptGroups := []*transaction.ScriptGroup{\n\t\u0026transaction.ScriptGroup{\n\t\tScript: types.Script{\n\t\t\tCodeHash: types.HexToHash(\"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8\"),\n\t\t\tHashType: types.HashTypeType,\n\t\t\tArgs:     common.FromHex(\"0x3f1573b44218d4c12a91919a58a863be415a2bc3\"),\n\t\t},\n\t\tGroupType:    types.ScriptTypeLock,\n\t\tInputIndices: []uint32{0},\n\t},\n}\ntxWithScriptGroups := \u0026transaction.TransactionWithScriptGroups{\n\tTxView:       tx,\n\tScriptGroups: scriptGroups,\n}\n```\n\nRefer [here](#sign-and-send-transaction) to see how to sign and send transaction once you have the instance of `TransactionWithScriptGroups`.\n\n\n### Build transaction with Mercury\n\n[Mercury](https://github.com/nervosnetwork/mercury) is an application for better interaction with CKB chain, providing many useful [JSON-RPC APIs](https://github.com/nervosnetwork/mercury/blob/main/core/rpc/README.md) for development like querying transactions or getting UDT asset information. You need to deploy your own mercury server and sync data with the latest network before using it.\n\nMercury is another way to build transaction. With the help of Mercury, you can build a transaction by simply calling a JSON-RPC API. Here we show how to build a CKB transfer transaction with mercury.\n\n```go\nsender := \"ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0yvcdtsu5wcr2jldtl72fhkruf0w5vymsp6rk9r\"\nreceiver := \"ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvglkprurm00l7hrs3rfqmmzyy3ll7djdsujdm6z\"\nckbAmount := amount.CkbToShannon(100)  // Convert CKB to Shannon (1 CKB = 10^8 Shannon)\nreq := \u0026model.SimpleTransferPayload{\n\tAssetInfo: model.NewCkbAsset(),\n\tFrom:       []string{sender},\n\tTo:          []*model.ToInfo{{receiver, ckbAmount}},\n\tFeeRate:   1000,\n}\n// Get an unsigned raw transaction with the help of Mercury\ntxWithScriptGroups, err := mercuryClient.BuildSimpleTransferTransaction(req)\n```\n\nFor more use cases of Mercury, please refer to [Mercury test cases](./mercury/client_test.go) and [Mercury JSON-RPC documentation](https://github.com/nervosnetwork/mercury/blob/dev-0.4/core/rpc/README.md).\n\n### Sign and send transaction\n\nOnce the `TransactionWithScriptGroups` is prepared, you can follow these steps to sign and send transaction to CKB network.\n\n1. sign transaction with your private key.\n2. send signed transaction to CKB node, and wait for it to be confirmed.\n\n```go\n// You can get txWithScriptGroups by manual or by mercury\nvar txWithScriptGroups *transaction.TransactionWithScriptGroups\n\n// 0. Set your private key\nprivKey := \"0xccb083b37aa346c5ce2e1f99a687a153baa04052f26db6ab3c26d6a4cc15c5f1\"\n// 1. Sign transaction with your private key\ntxSigner := signer.GetTransactionSignerInstance(types.NetworkTest)\ntxSigner.SignTransactionByPrivateKeys(txWithScriptGroups, privKey)\n// 2. Send transaction to CKB node\ntxHash, err := ckbClient.SendTransaction(context.Background(), txWithScriptGroups.TxView)\n```\n\nPlease note that before signing and sending transaction, you need to prepare a raw transaction represented by an instance of struct `TransactionWithScriptGroups`. You can get it [by Mercury](#Build-transaction-with-Mercury) or by ckb-indexer.\n\n### Generate a new address\nIn CKB world, a lock script can be represented as an address. `secp256k1_blake160_signhash_all` is the most common used address and here we show how to generate it.\n\n```go\n// Generate a new address randomly\nkey, err := secp256k1.RandomNew()\nif err != nil {\n\t// handle error\n}\nscript := systemscript.Secp256K1Blake160SignhashAll(key)\naddr := \u0026address.Address{Script: script, Network: types.NetworkTest}\nencodedAddr, err := addr.Encode()\n```\n\nFor more details please about CKB address refer to [CKB rfc 0021](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md).\n\n### Convert public key to address\n\nConvert elliptic curve public key to an address (`secp256k1_blake160_signash_all`)\n\n```go\n// You should provide an elliptic curve public key of compressed format, with 33 bytes.\nscript, err := systemscript.Secp256K1Blake160SignhashAllByPublicKey(\"0x03a0a7a7597b019828a1dda6ed52ab25181073ec3a9825d28b9abbb932fe1ec83d\")\nif err != nil {\n\t// handle error\n}\naddr := \u0026address.Address{Script: script, Network: types.NetworkTest}\n```\n\n### Parse address\n\nShort address and full bech32 address are deprecated. The standard address encoded way is bech32m. You can still parse address\nfrom an encoded string address and then get its network, script and encoded string of other format.\n\n```go\naddr, err := address.Decode(\"ckt1qyqxgp7za7dajm5wzjkye52asc8fxvvqy9eqlhp82g\")\nif err != nil {\n\t// handle error\n}\nscript := addr.Script\nnetwork := addr.Network\n```\n\n## License\n\nThe SDK is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervosnetwork%2Fckb-sdk-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnervosnetwork%2Fckb-sdk-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervosnetwork%2Fckb-sdk-go/lists"}