Swiftpack.co - Package - nervosnetwork/ckb-sdk-swift


Platform Azure Pipelines TravisCI Codecov Telegram Group

Swift SDK for Nervos CKB.


  • Xcode 10.2 with Swift 5, or higher
  • Target of iOS 11 or higher / macOS 10.13 or higher
  • CocoaPods or SPM



Install CocoaPods 1.7.0 or later.

Add this to your Podfile:

platform :osx, "10.13"

target "MyApp" do

  pod "CKB", git: "https://github.com/nervosnetwork/ckb-sdk-swift.git", tag: "v0.14.0"

Swift Package Manager(SPM)

You can also use Swift Package Manager. In your Package.swift file, specify this in dependencies:

dependencies: [
  .package(url: "https://github.com/nervosnetwork/ckb-sdk-swift", from: "0.14.0")

Getting Started

Note: Many API methods would throw exception on failure. The following examples all assume that there's a surrounding do {} catch {} block or throwing function.

The APIClient class provides JSONRPC access to a CKB node.

import CKB

// Connect to local node
let nodeUrl = URL(string: "http://localhost:8114")!
let apiClient = APIClient(url: nodeUrl)

// Fetch local node info
let nodeInfo = try apiClient.localNodeInfo()
print(nodeInfo.version)                        // "0.12.0-pre (rylai30-1-g3e765560 2019-05-16)"

// Get current height
let height = try apiClient.getTipBlockNumber() // Numbers are represented as strings
print(height)                                  // "10420"

Send Capacity Example

// Fetch system script which we'll use to generate lock for address
let nodeUrl = URL(string: "http://localhost:8114")!
let systemScript = try SystemScript.loadFromGenesisBlock(nodeUrl: nodeUrl)
// Fill in the sender's private key
let privateKey: Data = Data(hex: "your private key (hex string)")

// Push system script's out point into deps
let deps = [systemScript.outPoint]

// Gather inputs. For an simple example of how to gather inputs, see our Testnet Faucet's
// [CellService module](https://github.com/nervosnetwork/ckb-testnet-faucet/blob/68205ac338fa3ea3a3f007d8485c15de52cb3abd/faucet-server/Sources/App/Services/CellService.swift#L30-L48).
let inputs: [CellInput] = [/*...*/]
let witnesses: [Witness] = [/*...*/]

// Generate lock script for the receiver's address
let toAddress = "ckt1q9gry5zgw2q74lpmm03tw9snpqph2myqkkpyfss95qs228"
let addressHash = Utils.prefixHex(AddressGenerator(network: .testnet).publicKeyHash(for: toAddress)!) // "0x7281eafc3bdbe2b716130803756c80b58244c205"
let lockScript = Script(args: [addressHash], codeHash: systemScript.codeHash)
// Construct the outputs
let outputs = [CellOutput(capacity: 500_00_000_000.description, data: "0x", lock: lockScript, type: nil)]

// Generate the transaction
let tx = Transaction(deps: deps, inputs: inputs, outputs: outputs, witnesses: witnesses)
// For now we need to call the `computeTransactionHash` to get the tx hash
let apiClient = APIClient(url: nodeUrl)
let txHash = try apiClient.computeTransactionHash(transaction: tx)
let signedTx = Transaction.sign(tx: tx, with: privateKey, txHash: txHash)

// Now send out the capacity
let hash = try apiClient.sendTransaction(transaction: signedTx)
print(hash) // hash should equal to txHash

Getting Help


See CHANGELOG.md file.


Nervos CKB SDK is released under the terms of the MIT license. See LICENSE for more information or see https://opensource.org/licenses/MIT.


Stars: 10
Help us keep the lights on


v0.18.0 - Aug 10, 2019

v0.18.0 (2019-08-10)


  • Add new chain RPC method get_cellbase_output_capacity_details (85b2f8a)
  • Add new chain RPC methods get_header, get_header_by_number (0a9f7af)
  • Add new net RPC methods set_ban, get_banned_addresses (df4a7a5)

v0.17.0 - Jul 27, 2019

v0.17.0 (2019-07-27)


  • Add dao field of Header (6c884dd)
  • Add hash type support (6b01ab8)
  • Update address generator as per recent RFC 21 change (b5e73bb)


  • A public key will derive different address from previous implementation. As the code hash index has been changed from 4 bytes to 1 byte, the first serveral fixed characters will become ckt1qyq from ckb1q9gry5zg and be shorter.

v0.16.0 - Jul 13, 2019

v0.16.0 (2019-07-13)

  • Update to support CKB v0.16.0 (Testnet rylai-v5).

v0.15.0 - Jun 29, 2019

v0.15.0 (2019-06-29)

Bug Fixes

  • Condition for azure pipelines rpc tests job (a201c0a)
  • Add missing AlertMessage source file (b007aed)
  • Optional block number param for index_lock_hash rpc method (fc4a56d)


  • Implement indexer RPC (93b09b6)
  • Implement Secp256k1 recoverable sign (a5f490c)
  • RPC get_blockchain_info show alerts (5213e6a)
  • Update Epoch schema (b98fc04)
  • example: Update send tx (602bdad)
  • Use recoverable sign for tx (be88790)


  • get_blockchain_info change warnings to alerts

v0.14.0 - Jun 15, 2019

v0.14.0 (2019-06-15)


  • Add total_tx_cycles/total_tx_size to tx_pool_info RPC response (8257cfd)
  • Change base project type from iOS to macOS (675ad86)
  • Remove args property from CellInput (947c89b)
  • Throw exception when transaction sign fails (a344a98)
  • Update transaction signing based on new verification model (ab81c6d)