Skip to content

Commit

Permalink
feat(utils): add signRecoverable method to the ECPair class
Browse files Browse the repository at this point in the history
add signRecoverable method to the ECPair class, and use it to sign the transactions.

BREAKING CHANGE: use signRecoverable instead of sign method to sign the transactions.
  • Loading branch information
Keith-CY committed Jun 12, 2019
1 parent d3ba579 commit 3c1f334
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
2 changes: 0 additions & 2 deletions packages/ckb-sdk-core/__tests__/ckb-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ describe('ckb-core', () => {

it('throw an error when witnesses is missing', () => {
const fixture = exceptionFixtures.signTransactionWithoutWitnesses
console.log(!fixture.transaction.witnesses)
expect(core.signTransaction(fixture.privateKey)(fixture.transaction)).rejects.toEqual(
new Error(fixture.exception)
)
})

it('throw an error with invalid cound of witnesses', () => {
const fixture = exceptionFixtures.signTransactionWithInvalidCountOfWitnesses
console.log(!fixture.transaction.witnesses)
expect(core.signTransaction(fixture.privateKey)(fixture.transaction)).rejects.toEqual(
new Error(fixture.exception)
)
Expand Down
6 changes: 2 additions & 4 deletions packages/ckb-sdk-core/__tests__/successFixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"target": [
{
"data": [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x304402202c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe02202dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b6952408"
"0x2c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe2dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b695240800"
]
}
]
Expand Down Expand Up @@ -117,8 +116,7 @@
"witnesses": [
{
"data": [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x3045022100aa659c1d2920d144b76e4d03eb4d0c56b22e66501c68bb330bc58b5af1ba411c02203728e526386d4186955049405a669a9ce63ec0e3b233ca7696f9a5d95651a256"
"0xaa659c1d2920d144b76e4d03eb4d0c56b22e66501c68bb330bc58b5af1ba411c3728e526386d4186955049405a669a9ce63ec0e3b233ca7696f9a5d95651a25600"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Core {
s.update(this.utils.hexToBytes(datum))
})
const message = s.digest('hex')
const data = [`0x${addrObj.publicKey}`, `0x${addrObj.sign(message)}`, ...oldData]
const data = [`0x${addrObj.signRecoverable(message)}`, ...oldData]
return {
data,
}
Expand Down
10 changes: 9 additions & 1 deletion packages/ckb-sdk-utils/__tests__/ecpair.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ECPair = require('../lib/ecpair').default
const { sigFixtures } = require('./signature-fixtures.json')
const { sigFixtures, signRecoverableFixtures } = require('./signature-fixtures.json')
const { hexToBytes } = require('../lib')

describe('ECPair', () => {
Expand Down Expand Up @@ -56,6 +56,14 @@ describe('ECPair', () => {
})
})

it('signRecoverable', () => {
signRecoverableFixtures.forEach(fixture => {
const ecpair = new ECPair(fixture.privKey)
const sig = ecpair.signRecoverable(fixture.msg)
expect(sig).toBe(fixture.sig)
})
})

it('throw error if private key is absent', () => {
expect(() => new ECPair()).toThrow('Private key is required')
})
Expand Down
10 changes: 9 additions & 1 deletion packages/ckb-sdk-utils/__tests__/signature-fixtures.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sigFixtures": [{
"sigFixtures": [
{
"msg": "403676bd85716a1e16b415093cee88c07d7cf2c2199aaf82320d354cb571f0d9",
"sig": "3045022100c795b2b3c48d370324e5053f4509d4f1f18f80aec4a8cba68ebae922b9f882d8022045ae312bd84e25eed818ef84e7ed61a774f208fe2b2fe3588b60b4686086208201",
"privkey": "e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3"
Expand Down Expand Up @@ -999,5 +1000,12 @@
"sig": "3045022100ae0f294fbc13c28259019d000a9a9817173febfb37fb0a273511add46af4226102202d2a8b8b0e058179a0baf190dc76f649075a8d3ab3832d3be859159cbe1a9ae701",
"privkey": "66dc7feb6d7c733005c5d8716b22f38819738bd1538a40f46205f3f3be191efc"
}
],
"signRecoverableFixtures": [
{
"msg": "95e919c41e1ae7593730097e9bb1185787b046ae9f47b4a10ff4e22f9c3e3eab",
"sig": "1e94db61cff452639cf7dd991cf0c856923dcf74af24b6f575b91479ad2c8ef40769812d1cf1fd1a15d2f6cb9ef3d91260ef27e65e1f9be399887e9a5447786301",
"privKey": "e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3"
}
]
}
23 changes: 17 additions & 6 deletions packages/ckb-sdk-utils/src/ecpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,30 @@ class ECPair {

public getPublicKey = (enc: 'hex' | 'array') => this.key.getPublic(this.compressed, enc)

public sign = (msg: string | Uint8Array): string => {
const message = typeof msg === 'string' ? hexToBytes(msg) : msg
public sign = (message: string | Uint8Array): string => {
const msg = typeof message === 'string' ? hexToBytes(message) : message
return this.key
.sign(message, {
.sign(msg, {
canonical: true,
})
.toDER('hex')
}

public verify = (msg: string | Buffer, sig: string | Buffer) => {
const message = typeof msg === 'string' ? hexToBytes(msg) : msg
public verify = (message: string | Buffer, sig: string | Buffer) => {
const msg = typeof message === 'string' ? hexToBytes(message) : message
const signature = typeof sig === 'string' ? hexToBytes(sig) : sig
return this.key.verify(message, signature as any)
return this.key.verify(msg, signature as any)
}

public signRecoverable = (message: string | Uint8Array): string => {
const msg = typeof message === 'string' ? hexToBytes(message) : message
const { r, s, recoveryParam } = this.key.sign(msg, {
canonical: true,
})
const fmtR = r.toString(16).padStart(64, '0')
const fmtS = s.toString(16).padStart(64, '0')
const fmtRecID = recoveryParam ? '01' : '00'
return `${fmtR}${fmtS}${fmtRecID}`
}
}

Expand Down

0 comments on commit 3c1f334

Please sign in to comment.