本文定义了一组 Provider 方法,这些方法用来定义 DApp (Decentralized Application) 如何与 CKB 区块链的节点之间进行交互。
为防止不同的 Provider 定义不同的方法名称,导致定义和交互方式的碎片化,我们希望通过这个文档定义一组通用的 Provider 方法。
用来管理用户的公钥和私钥,为交易签名。
简单来说,Provider 是一个模块,这个模块提供了一组方法,通过这些方法可以向区块链节点发送交易。 对于 CKB 来说,可以认为 Provider 是任何的模块,只要这个模块提供了向节点发送 send_transaction RPC (Remote Procedure Call) 的能力。
Provider 就好像现实世界中的互联网服务提供商一样,为应用提供了快捷访问互联网的能力。
对于网页端来说,Provider 通常是一个 object,一般由钱包暴露给应用端。
Communication with CKB nodes is accomplished using JSON-RPC, a stateless, lightweight remote procedure call protocol that uses JSON as its data format.
注:文档中的代码由 TypeScript 来表示。
interface JsonRpcRequest {
  id: string | undefined;
  jsonrpc: '2.0';
  method: string;
  params?: Array<any>;
}interface JsonRpcResponse {
  id: string | undefined;
  jsonrpc: '2.0';
  method: string;
  result?: unknown;
  error?: Error;
}定义了错误信息的格式。
interface Error {
  message: string;
  code: number;
}Example error response:
{
  "id": 1337,
  "jsonrpc": "2.0",
  "error": {
    "code": -32003,
    "message": "Transaction rejected"
  }
}Get address list
无
type result: Address[]
interface Address {
  address: string;
  lockHash: string;
  publicKey: stirng;
  name?: string;
  capacity?: string;
}Request:
{
  "id": 2,
  "jsonrpc": "2.0",
  "method": "get_addresses",
}Response:
{
  "id": 2,
  "jsonrpc": "2.0",
  "result": [
    {
      "address": "ckt1qyqgadxhtq27ygrm62dqkdj32gl95j8gl56qum0yyn",
      "lockHash": "0x111823010653d32d36b18c9a257fe13158ca012e22b9b82f0640be187f10904b",
      "publicKey": "0x021b30b3047a645d8b6c10c513b767a3e08efa1a53df5f81bcb37af3c8c8358ae9",
      "name": "Secp256k1",
    },
    {
      "address": "ckt1qjjm395fg5uc986703vs9uqzw5gljnrslgjqd4gfulrdrhmkkphs3kenl3uxcxs6nvasjwphh2hz4cdud4ewxnrgeqn",
      "lockHash": "0x111823010653d32d36b18c9a257fe13158ca012e22b9b82f0640be187f10904b",
      "publicKey": "0x021b30b3047a645d8b6c10c513b767a3e08efa1a53df5f81bcb37af3c8c8358ae9",
      "name": "Keccak256",
    },
  ]
}Get addresses list
- Array
- [0]: lockHash: string;- Required. Lock hash.
 
- [0]: 
Cell[] - Array of Cell object
interface Script {
  codeHash: string;
  hashType: string;
  args: string;
}
interface Cell {
  blockHash: string;
  lockScript: Script | null;
  outPoint: {
    txHash: string;
    index: string;
  };
  outputData: string;
  outputDataLen: string;
  capacity: string;
  typeScript: Script | null;
  dataHash: string;
}Request:
{
  "id": 2,
  "jsonrpc": "2.0",
  "method": "get_live_cells",
  "params": [
    "0x111823010653d32d36b18c9a257fe13158ca012e22b9b82f0640be187f10904b", // lockHash
  ]
}Response:
{
  "id": 2,
  "jsonrpc": "2.0",
  "result": [
    {
      "blockHash": "0xd9027f4740c5995b17f2580ca5db9ac4ce4909e652ede2eb26d64709c26201ae",
      "lockScript": {
        "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
        "hashType": "type",
        "args": "0x8eb4d75815e2207bd29a0b3651523e5a48e8fd34",
      },
      "outPoint": {
        "txHash": "0xdb255da9ceb84c81e2053238d239e65a92076f3080aee13346d586746b3bc8ce",
        "index": "0x1",
      },
      "outputData": "0x",
      "outputDataLen": "0x0",
      "capacity": "0x535a743210",
      "typeScript": {
          "codeHash": null,
          "hashType": null,
          "args": null
      },
      "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    },
  ]
}Provider 的实现者可以添加更多的参数实现灵活的 cell 获取,例如:
- Array
- [0]: lockHash: string;- Required. Lock hash.
- [1]: capacity?: string;- Optional. How much capacity to request.
- [2]: limit?: number;- Optional. How many cells to request.
- [3]: hasData?: boolean;- Optional. Whether return cell which has output data(not 0x)
 
- [0]: 
Request:
{
  "id": 2,
  "jsonrpc": "2.0",
  "method": "get_live_cells",
  "params": [
    "0x111823010653d32d36b18c9a257fe13158ca012e22b9b82f0640be187f10904b", // lockHash
    200,  // capacity
    10, // limit
    true  // hasData
  ]
}Sign Transaction
- Array
- [0]: tx: any;- Required. Raw transaction to be signed.
- [1]: config?: Config;- Optional. How cells in inputs will be signed.
 
- [0]: 
interface Config {
  index: number;
  length: number;
}any - Signed transaction.
Request:
{
  "id": 2,
  "jsonrpc": "2.0",
  "method": "sign",
  "params": [
    TX_OBJECT, // Raw transaction, too long, only put a placeholder here
    { "index": 3, "length": 2 },  // config
  ]
}Response:
{
  "id": 2,
  "jsonrpc": "2.0",
  "result": SIGNED_TX_OBJECT  // Signed transaction, too long, only put a placeholder here
}Send Transaction
- Array
- [0]: tx: any;- Required. Signed transaction.
 
- [0]: 
string | null; - Transaction hash or null
Request:
{
  "id": 2,
  "jsonrpc": "2.0",
  "method": "send",
  "params": [
    SIGNED_TX_OBJECT, // Signed transaction(Too long, just put a placeholder here)
  ]
}Response:
{
  "id": 2,
  "jsonrpc": "2.0",
  "result": "0x892df0760d7aa7ca74f769125f47393bd6dadb59b5d66bfdb0b845165abea629"
}- https://github.com/nervosnetwork/keyper
- https://talk.nervos.org/t/keyper-scatter-dapp/4430
- https://gist.github.com/luckyyang/dd1166486b490f1bde5c7c2df5f2103f
- https://talk.nervos.org/t/keyper-agency-protocol-proposal/4743
- https://github.com/liusong1111/keypering-core
- https://github.com/nervosnetwork/rfcs
- EIP-1193: Ethereum Provider JavaScript API: https://eips.ethereum.org/EIPS/eip-1193
- EIP-2255: Wallet Permissions System: https://eips.ethereum.org/EIPS/eip-2255
- EIP-1102: Opt-in account exposure: https://eips.ethereum.org/EIPS/eip-1102
- EIP-1474: Remote procedure call specification: https://eips.ethereum.org/EIPS/eip-1474
- https://docs.metamask.io/guide/getting-started.html
- https://d2uvd02r4antif.cloudfront.net/docs/guides/web3-provider-explained
- https://web3js.readthedocs.io/en/v1.2.9/web3.html
- https://docs.ethers.io/ethers.js/v5-beta/api-providers.html