Skip to content
Swift SDK for CKB
Branch: develop
Clone or download
ashchan Merge pull request #94 from nervosnetwork/address-from-pubkeyhash
feat: Add public key hash (blake160) to address methods
Latest commit 9986a1b May 19, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
CKB.xcodeproj feat: Add system script loading May 16, 2019
CKB.xcworkspace Add Podfile and workspace Dec 13, 2018
Examples/Example-iOS chore: Update example to use released v0.12.0 May 18, 2019
Source feat: Add public key hash (blake160) to address methods May 19, 2019
.gitignore Add gitignore file Dec 13, 2018
.gitmodules refactor: delete the no longer used VerifyScript class Mar 20, 2019
.swiftlint.yml Do not treat TODOs as error Dec 20, 2018
.travis.yml chore: Update CocoaPods to 1.7.0.rc.1 May 8, 2019 docs: Draft v0.12.0 changelog May 16, 2019
CKB.podspec chore: Pre-release v0.12.0 May 16, 2019
Package.swift chore: pre-release v0.9.0 Apr 9, 2019
Podfile chore: Move iOS example to separate workspace Apr 28, 2019
Podfile.lock chore: Update CocoaPods to newest 1.7.0.rc.2 May 16, 2019 fix: Minor doc issue May 17, 2019


TravisCI Telegram Group

Swift SDK for Nervos CKB.


  • Xcode 10.2 with Swift 5, or higher
  • Target of iOS 11 or higher(we have plan to add macOS support in the near future)
  • CocoaPods or SPM



Install CocoaPods 1.7.0.rc.2 or later.

Add this to your Podfile:

platform :ios, "11.0"

target "MyApp" do

  pod "CKB", git: "", tag: "v0.12.0"

Swift Package Manager(SPM)

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

dependencies: [
  .package(url: "", from: "0.12.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](
let inputs: [CellInput] = [/*...*/]

// 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)
// 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


Nervos CKB SDK is released under the terms of the MIT license. See LICENSE for more information or see


See file.

You can’t perform that action at this time.