Skip to content

Commit

Permalink
feat: Sign support multisig. (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Jun 20, 2022
1 parent 706b4ff commit 3ad7abd
Show file tree
Hide file tree
Showing 10 changed files with 1,041 additions and 23 deletions.
149 changes: 149 additions & 0 deletions packages/ckb-sdk-core/__tests__/multisig/fixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"serializeMultisigConfig": {
"exception with config is less than 0": {
"config": {
"r": -1,
"m": 2,
"n": 3,
"blake160s": []
},
"exception": "For multisig sign, signer should between 0 and 255"
},
"exception with config is grater than 255": {
"config": {
"r": 256,
"m": 2,
"n": 3,
"blake160s": []
},
"exception": "For multisig sign, signer should between 0 and 255"
},
"exception with r shouldn't be greater than n": {
"config": {
"r": 3,
"m": 2,
"n": 3,
"blake160s": []
},
"exception": "For m of n multisig sign, r shouldn't be greater than m"
},
"exception with m is greater than n": {
"config": {
"r": 1,
"m": 4,
"n": 3,
"blake160s": []
},
"exception": "For m of n multisig sign, m shouldn't be greater than n"
},
"exception blake160s length not equal with n": {
"config": {
"r": 1,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"]
},
"exception": "For m of n multisig sign, signer's length should equal with n"
},
"test serializeMultisigConfig": {
"config": {
"r": 1,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xb7672fcde903607f6bb150a730085c2a43c422fa", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"]
},
"expected": "0x000102037c021957a27000e794f25828270f187c791443e3b7672fcde903607f6bb150a730085c2a43c422fad93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"
}
},
"hashMultisig": {
"normal": {
"config": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"expected": "0xe7db180742a9b5c4f2d9319d74982503fbc88a37"
}
},
"getMultisigStatus": {
"Unsigned": {
"config": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"signatures": [],
"expected": "Unsigned"
},
"PartiallySigned": {
"config": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"signatures": ["0x7c021957a27000e794f25828270f187c791443e3"],
"expected": "PartiallySigned"
},
"Signed": {
"config": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"signatures": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"],
"expected": "Signed"
},
"exception with More signature for multisig, all signatures overflow": {
"config": {
"r": 1,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"signatures": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"],
"exception": "More signature for multisig"
},
"exception with More signature for multisig, m signatures overflow": {
"config": {
"r": 1,
"m": 1,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"signatures": ["0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"],
"exception": "More signature for multisig"
}
},
"isMultisigConfig": {
"false with loss field": {
"config": {
"m": "2",
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3"]
},
"expected": false
},
"false with unmatch field type": {
"config": {
"r": "a",
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3"]
},
"expected": false
},
"success": {
"config": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3"]
},
"expected": true
}
}
}
85 changes: 85 additions & 0 deletions packages/ckb-sdk-core/__tests__/multisig/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const { serializeMultisigConfig, hashMultisig, getMultisigStatus, isMultisigConfig } = require('../../lib/multisig')
const fixtures = require('./fixtures.json')

describe('test serializeMultisigConfig', () => {
const serializeMultisigConfigTable = Object.entries(fixtures.serializeMultisigConfig).map(
([title, { config, expected, exception }]) => [
title,
config,
exception,
expected
],
)
test.each(serializeMultisigConfigTable)(
'%s',
(_title, config, exception, expected) => {
if (exception !== undefined) {
expect(() =>serializeMultisigConfig(config)).toThrowError(exception)
} else {
const result = serializeMultisigConfig(config)
expect(result).toEqual(expected)
}
},
)
})
describe('test hashMultisig', () => {
const hashMultisigTable = Object.entries(fixtures.hashMultisig).map(
([title, { config, expected, exception }]) => [
title,
config,
exception,
expected
],
)

test.each(hashMultisigTable)(
'%s',
(_title, config, exception, expected) => {
if (exception !== undefined) {
expect(() => hashMultisig(config)).toThrowError(exception)
} else {
const result = hashMultisig(config)
expect(result).toEqual(expected)
}
},
)
})
describe('test getMultisigStatus', () => {
const table = Object.entries(fixtures.getMultisigStatus).map(
([title, { config, signatures, expected, exception }]) => [
title,
config,
signatures,
exception,
expected
],
)

test.each(table)(
'%s',
(_title, config, signatures, exception, expected) => {
if (exception !== undefined) {
expect(() =>getMultisigStatus(config, signatures)).toThrowError(exception)
} else {
const result = getMultisigStatus(config, signatures)
expect(result).toEqual(expected)
}
},
)
})
describe('test isMultisigConfig', () => {
const table = Object.entries(fixtures.isMultisigConfig).map(
([title, { config, expected }]) => [
title,
config,
expected
],
)

test.each(table)(
'%s',
(_title, config, expected) => {
expect(isMultisigConfig(config)).toEqual(expected)
},
)
})
34 changes: 34 additions & 0 deletions packages/ckb-sdk-core/__tests__/signWitnessGroup/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,39 @@
"outputType": ""
}
]
},
"sign with multisig config": {
"privateKey": "0xdcec27d0d975b0378471183a03f7071dea8532aaf968be796719ecd20af6988f",
"transactionHash": "0x4a4bcfef1b7448e27edf533df2f1de9f56be05eba645fb83f42d55816797ad2a",
"witnesses": [
{
"lock": "",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
],
"multisigConfig": {
"r": 0,
"m": 2,
"n": 3,
"blake160s": ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1", "0xb7672fcde903607f6bb150a730085c2a43c422fa"]
},
"expected": [
{
"lock": "0x52e62abe0aa34889eef6a27f29e78a500e6534e91c3ddbeaab0ea4f5a4a53a12628773309a9b18d047f318d3647315626bfab7c64ca95a21a1e1fe32a8eec6a201",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
]
}
}
90 changes: 86 additions & 4 deletions packages/ckb-sdk-core/__tests__/signWitnessGroup/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,104 @@ const fixtures = require('./fixtures.json')
describe('test sign witness group', () => {
const fixtureTable = Object.entries(
fixtures,
).map(([title, { privateKey, transactionHash, witnesses, expected, exception }]) => [
).map(([title, { privateKey, transactionHash, witnesses, multisigConfig, expected, exception }]) => [
title,
privateKey,
transactionHash,
witnesses,
multisigConfig,
exception,
expected,
])

test.each(fixtureTable)('%s', (_title, privateKey, transactionHash, witnesses, exception, expected) => {
test.each(fixtureTable)('%s', (_title, privateKey, transactionHash, witnesses, multisigConfig, exception, expected) => {
expect.assertions(1)
if (exception !== undefined) {
expect(() => signWitnessGroup(privateKey, transactionHash, witnesses)).toThrowError(exception)
expect(() => signWitnessGroup(privateKey, transactionHash, witnesses, multisigConfig)).toThrowError(exception)
} else if (privateKey !== undefined) {
const signedWitnessGroup = signWitnessGroup(privateKey, transactionHash, witnesses)
const signedWitnessGroup = signWitnessGroup(privateKey, transactionHash, witnesses, multisigConfig)
expect(signedWitnessGroup).toEqual(expected)
}
})


describe('sk is function', () => {
const transactionHash = '0x4a4bcfef1b7448e27edf533df2f1de9f56be05eba645fb83f42d55816797ad2a'
const witnesses = [
{
"lock": "",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
]
const multisigConfig = {
r: 1,
m: 1,
n: 2,
blake160s: ["0x7c021957a27000e794f25828270f187c791443e3", "0xd93b3564ef1b2dcf7bca781f968b3c7d2db85fd1"]
}
it('sk is sync', () => {
const privateKey = v => v
const signedWitnessGroup = signWitnessGroup(privateKey, transactionHash, witnesses)
expect(signedWitnessGroup).toEqual([
'0x34000000100000003400000034000000200000007739c6307c4e3698a8a8ebfdb3908a29a7cb5a382040c89806cace1ddc538b0e',
{
"lock": "",
"inputType": "",
"outputType": ""
}
])
})
it('sk is sync with multisigConfig', () => {
const privateKey = v => v
const signedWitnessGroup = signWitnessGroup(privateKey, transactionHash, witnesses, multisigConfig)
expect(signedWitnessGroup).toEqual([
{
"lock": "0x040db42399af0c6e32cb68160079cc40c4e8d207052fec335c52b76e3442a8a3",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
])
})

it('sk result is promise', async () => {
const privateKey = (v) => Promise.resolve(v)
const signedWitnessGroup = await signWitnessGroup(privateKey, transactionHash, witnesses)
expect(signedWitnessGroup).toEqual([
'0x34000000100000003400000034000000200000007739c6307c4e3698a8a8ebfdb3908a29a7cb5a382040c89806cace1ddc538b0e',
{
"lock": "",
"inputType": "",
"outputType": ""
}
])
})

it('sk result is promise with multisigConfig', async () => {
const privateKey = (v) => Promise.resolve(v)
const signedWitnessGroup = await signWitnessGroup(privateKey, transactionHash, witnesses, multisigConfig)
expect(signedWitnessGroup).toEqual([
{
"lock": "0x040db42399af0c6e32cb68160079cc40c4e8d207052fec335c52b76e3442a8a3",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
])
})
})
})

0 comments on commit 3ad7abd

Please sign in to comment.