Skip to content

Commit

Permalink
feat(core): use secp256k1 dep group instead of secp256k1 cell
Browse files Browse the repository at this point in the history
BREAKING CHANGE: use secp256k1 dep group instead of secp256k1 cell
  • Loading branch information
Keith-CY committed Aug 21, 2019
1 parent 63577b9 commit 578eb43
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 61 deletions.
10 changes: 5 additions & 5 deletions packages/ckb-sdk-core/__tests__/ckb-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const core = new Core(url)

describe('ckb-core', () => {
describe('success', () => {
it('load the system cell', async () => {
const fixture = successFixtures.loadSystemCell
expect(core.config.systemCellInfo).toEqual(fixture.emptyInfo)
it('load the secp256k1 dep', async () => {
const fixture = successFixtures.loadSecp256k1Dep
expect(core.config.loadSecp256k1Dep).toEqual(undefined)

const systemCellInfo = await core.loadSystemCell()
expect(systemCellInfo).toEqual(fixture.target)
const secp256k1Dep = await core.loadSecp256k1Dep()
expect(secp256k1Dep).toEqual(fixture.target)
})

it('sign witnesses', () => {
Expand Down
13 changes: 3 additions & 10 deletions packages/ckb-sdk-core/__tests__/successFixtures.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
{
"loadSystemCell": {
"emptyInfo": {
"codeHash": "",
"outPoint": {
"txHash": "",
"index": "0"
}
},
"loadSecp256k1Dep": {
"target": {
"codeHash": "54811ce986d5c3e57eaafab22cdd080e32209e39590e204a99b32935f835a13c",
"outPoint": {
"txHash": "d0608a90cc3fd5bec9807263d3c7c93fad48a1afa0e44b38a664fbe79d4a1400",
"index": "1"
"index": "0"
}
}
},
Expand Down Expand Up @@ -64,7 +57,7 @@
"type": null
}
],
"outputData": ["0x"],
"outputsData": ["0x"],
"version": "0",
"witnesses": [{ "data": [] }]
},
Expand Down
31 changes: 7 additions & 24 deletions packages/ckb-sdk-core/examples/sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,7 @@ const bootstrap = async () => {

const core = new Core(nodeUrl) // instantiate the JS SDK with provided node url

const systemCellInfo = await core.loadSystemCell() // load system cell, which contains the secp256k1 algorithm used to verify the signature in transaction's witnesses.

/**
* const SYSTEM_ENCRYPTION_CODE_HASH = 0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08
* The system encryption code hash is the hash of system cell's data by blake2b algorithm
*/
const SYSTEM_ENCRYPTION_CODE_HASH = core.rpc.paramsFormatter.toHash(systemCellInfo.codeHash)

/**
* const SYSTEM_ENCRYPTION_OUT_POINT = {
* txHash: '0x7c77c04b904bd937bd371ab0d413ed6eb887661e2484bc198aca6934ba5ea4e3',
* index: '1',
* }
*/
const SYSTEM_ENCRYPTION_OUT_POINT = {
txHash: core.rpc.paramsFormatter.toHash(systemCellInfo.outPoint.cell.txHash),
index: systemCellInfo.outPoint.cell.index,
}
const secp256k1Dep = await core.loadSecp256k1Dep() // load the dep group of secp256k1 algorithm which is used to verify the signature in transaction's witnesses.

/**
* genereat address object, who has peroperties like private key, public key, sign method and verify mehtod
Expand All @@ -44,12 +27,12 @@ const bootstrap = async () => {
/**
* calculate the lockhash by the address
* 1. the identifier of the address is required in the args field of lock script
* 2. compose the lock script with SYSTEM_ENCRYPTION_CODE_HASH, and args
* 2. compose the lock script with secp256k1Dep.codeHash, and args
* 3. calculate the hash of lock script
*/

const script = {
codeHash: SYSTEM_ENCRYPTION_CODE_HASH,
codeHash: secp256k1Dep.codeHash,
args: [`0x${myAddressObj.identifier}`],
}
/**
Expand Down Expand Up @@ -133,7 +116,7 @@ const bootstrap = async () => {
const targetOutput = {
capacity: targetCapacity,
lock: {
codeHash: SYSTEM_ENCRYPTION_CODE_HASH,
codeHash: secp256k1Dep.codeHash,
args: [targetIdentifier],
},
}
Expand All @@ -144,7 +127,7 @@ const bootstrap = async () => {
const changeOutput = {
capacity: 0n,
lock: {
codeHash: SYSTEM_ENCRYPTION_CODE_HASH,
codeHash: secp256k1Dep.codeHash,
args: [`0x${myAddressObj.identifier}`],
},
}
Expand Down Expand Up @@ -197,8 +180,8 @@ const bootstrap = async () => {
const tx = {
version: '0',
cellDeps: [{
outPoint: SYSTEM_ENCRYPTION_OUT_POINT,
isDepGroup: false,
outPoint: secp256k1Dep.outPoint,
isDepGroup: true,
}, ],
headerDeps: [],
inputs,
Expand Down
46 changes: 24 additions & 22 deletions packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import RPC from '@nervosnetwork/ckb-sdk-rpc'
import Address from '@nervosnetwork/ckb-sdk-address'
import * as utils from '@nervosnetwork/ckb-sdk-utils'

interface DepCellInfo {
codeHash: CKBComponents.Hash256
outPoint: CKBComponents.OutPoint
}
class Core {
public rpc: RPC

public utils = utils

private _node: CKBComponents.Node

public config = {
systemCellInfo: {
codeHash: '',
outPoint: {
txHash: '',
index: '0',
},
},
public config: {
secp256k1Dep: DepCellInfo | undefined
} = {
secp256k1Dep: undefined,
}

constructor(nodeUrl: string) {
Expand Down Expand Up @@ -69,23 +69,25 @@ class Core {
codeHashIndex,
})

public loadSystemCell = async () => {
public loadSecp256k1Dep = async () => {
/**
* cell list
* @link https://github.com/nervosnetwork/ckb/blob/dbadf484cea6bdba0329d58102726068be997a50/docs/hashes.toml
*/
const block = await this.rpc.getBlockByNumber('0')
if (!block) throw new Error('Cannot load the genesis block')
const cellTx = block.transactions[1]
if (!cellTx) throw new Error('Cannot load the transaction which has the system cell')
if (!cellTx.outputs[0]) throw new Error('Cannot load the system cell because the specific output is not found')

const { codeHash } = cellTx.outputs[0].lock
const outPoint = {
txHash: cellTx.hash.replace(/^0x/, ''),
index: '1',
}
this.config.systemCellInfo = {
codeHash,
outPoint,
const tx = block.transactions[1]
if (!tx) throw new Error('Cannot load the transaction which has the secp256k1 dep cell')
if (!tx.outputs[0]) throw new Error('Cannot load the secp256k1 dep because the specific output is not found')

this.config.secp256k1Dep = {
codeHash: tx.outputs[0].lock.codeHash,
outPoint: {
txHash: tx.hash.replace(/^0x/, ''),
index: '0',
},
}
return this.config.systemCellInfo
return this.config.secp256k1Dep
}

public signWitnesses = (key: string | Address) => ({
Expand Down

0 comments on commit 578eb43

Please sign in to comment.