Skip to content

Commit

Permalink
feat(utils): format the outputs of the utils module
Browse files Browse the repository at this point in the history
hexilize the outputs

BREAKING CHANGE: hexilize the outputs of the utils module
  • Loading branch information
Keith-CY committed Sep 12, 2019
1 parent 996a67e commit a30071c
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 215 deletions.
13 changes: 1 addition & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,7 @@ module.exports = {
}],
"max-len": [2, 120],
"object-curly-newline": ["error", {
"ObjectExpression": {
"minProperties": 1,
"multiline": true
},
"ObjectPattern": {
"consistent": true
},
"ImportDeclaration": "never",
"ExportDeclaration": {
"multiline": true,
"minProperties": 3
}
"consistent": true
}]
},
"globals": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const ECPair = require('../lib/ecpair').default
const { sigFixtures, signRecoverableFixtures } = require('./signature-fixtures.json')
const { hexToBytes } = require('../lib')
const ECPair = require('../../lib/ecpair').default
const { HexStringShouldStartWith0x } = require('../../lib/exceptions/hexStringShouldStartWith0x')
const { sigFixtures, signRecoverableFixtures } = require('./signature.fixtures.json')
const { hexToBytes } = require('../../lib')

describe('ECPair', () => {
it('new ecpair', () => {
const fixture = {
privateKey: 'e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
privateKey: '0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
compressed: true,
publicKey: '024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
publicKey: '0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
}

const ecpair = new ECPair(hexToBytes(fixture.privateKey), {
Expand All @@ -22,9 +23,9 @@ describe('ECPair', () => {

it('new ecpair with empty options, default compressed should be true', () => {
const fixture = {
privateKey: 'e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
privateKey: '0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
compressed: true,
publicKey: '024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
publicKey: '0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
}

const ecpair = new ECPair(hexToBytes(fixture.privateKey), {})
Expand All @@ -35,9 +36,9 @@ describe('ECPair', () => {

it('new ecpair with default options which should be { compressed: true }', () => {
const fixture = {
privateKey: 'e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
privateKey: '0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
compressed: true,
publicKey: '024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
publicKey: '0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01',
}

const ecpair = new ECPair(hexToBytes(fixture.privateKey))
Expand All @@ -46,21 +47,26 @@ describe('ECPair', () => {
expect(ecpair.publicKey).toBe(fixture.publicKey)
})

it('Initialize by private key without 0x should throw an error', () => {
const privateKey = 'e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3'
expect(() => new ECPair(privateKey, {}).toThrow(new HexStringShouldStartWith0x(privateKey)))
})

it('sign and verify message', () => {
sigFixtures.forEach(fixture => {
const ecpair = new ECPair(fixture.privkey)
const sig = ecpair.sign(fixture.msg)
const ecpair = new ECPair(`0x${fixture.privkey}`)
const sig = ecpair.sign(`0x${fixture.msg}`)
// slice sig from 0, -2 to ignore the recovery param
expect(sig).toBe(fixture.sig.slice(0, -2))
expect(ecpair.verify(fixture.msg, fixture.sig.slice(0, -2))).toBe(true)
expect(sig).toBe(`0x${fixture.sig.slice(0, -2)}`)
expect(ecpair.verify(`0x${fixture.msg}`, `0x${fixture.sig.slice(0, -2)}`)).toBe(true)
})
})

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

Expand Down
42 changes: 21 additions & 21 deletions packages/ckb-sdk-utils/__tests__/serialization/index/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"serializeArray": [
{
"array": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
"expected": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
"expected": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
},
{
"array": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
"expected": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
"array": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
"expected": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
},
{
"array": [
Expand Down Expand Up @@ -43,55 +43,55 @@
142,
136
],
"expected": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
"expected": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
}
],
"serializeStruct": [
{
"struct": [["f1", "ab"]],
"expected": "ab"
"struct": [["0xf1", "0xab"]],
"expected": "0xab"
},
{
"struct": [["f1", "ab"], ["f2", "0x030201"]],
"expected": "ab030201"
"struct": [["0xf1", "0xab"], ["0xf2", "0x030201"]],
"expected": "0xab030201"
}
],
"serializeFixVec": [
{
"fixvec": [],
"expected": "00000000"
"expected": "0x00000000"
},
{
"fixvec": ["0x12"],
"expected": "0100000012"
"expected": "0x0100000012"
},
{
"fixvec": [[18]],
"expected": "0100000012"
"expected": "0x0100000012"
},
{
"fixvec": "0x1234567890abcdef",
"expected": "080000001234567890abcdef"
"expected": "0x080000001234567890abcdef"
}
],
"serializeDynVec": [
{
"dynvec": [],
"expected": "04000000"
"expected": "0x04000000"
},
{
"dynvec": ["020000001234"],
"expected": "0e00000008000000020000001234"
"dynvec": ["0x020000001234"],
"expected": "0x0e00000008000000020000001234"
},
{
"dynvec": ["020000001234", "00000000", "020000000567", "0x0100000089", "03000000abcdef"],
"expected": "34000000180000001e00000022000000280000002d00000002000000123400000000020000000567010000008903000000abcdef"
"dynvec": ["0x020000001234", "0x00000000", "0x020000000567", "0x0100000089", "0x03000000abcdef"],
"expected": "0x34000000180000001e00000022000000280000002d00000002000000123400000000020000000567010000008903000000abcdef"
}
],
"serializeTable": [
{
"table": [["f1", "ab"], ["f2", "0x030201"]],
"expected": "100000000c0000000d000000ab030201"
"table": [["0xf1", "0xab"], ["0xf2", "0x030201"]],
"expected": "0x100000000c0000000d000000ab030201"
}
],
"serializeOption": [
Expand All @@ -100,8 +100,8 @@
"expected": ""
},
{
"option": "0c0000000800000000000000",
"expected": "0c0000000800000000000000"
"option": "0x0c0000000800000000000000",
"expected": "0x0c0000000800000000000000"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,31 @@
"serializeArgs": [
{
"args": [],
"expected": "04000000"
"expected": "0x04000000"
},
{
"args": ["0x10", "0x01"],
"expected": "160000000c0000001100000001000000100100000001"
"expected": "0x160000000c0000001100000001000000100100000001"
},
{
"args": ["0x3954acece65096bfa81258983ddb83915fc56bd8"],
"expected": "2000000008000000140000003954acece65096bfa81258983ddb83915fc56bd8"
"expected": "0x2000000008000000140000003954acece65096bfa81258983ddb83915fc56bd8"
}
],
"serializeCodeHash": [
{
"codeHash": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
"expected": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
},
{
"codeHash": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
"expected": "68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
"expected": "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
}
],
"serializeHashType": [
{
"hashType": "data",
"expected": "00"
"expected": "0x00"
},
{
"hashType": "type",
"expected": "01"
"expected": "0x01"
}
],
"serializeScript": [
Expand All @@ -40,7 +36,7 @@
"args": ["0x3954acece65096bfa81258983ddb83915fc56bd8"],
"hashType": "type"
},
"expected": "5100000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88012000000008000000140000003954acece65096bfa81258983ddb83915fc56bd8"
"expected": "0x5100000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88012000000008000000140000003954acece65096bfa81258983ddb83915fc56bd8"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
serializeHashType,
serializeScript,
} = require('../../../lib/serialization/script')
const { ArgumentRequired } = require('../../../lib/exceptions')
const fixtures = require('./fixtures.json')

describe('Test Script Serialization', () => {
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('Test Script Serialization', () => {
})

it('throw errors if the script is not passed into the method', () => {
expect(() => serializeScript()).toThrow(new Error('Script is required'))
expect(() => serializeScript()).toThrow(new ArgumentRequired('Script'))
})
})
})

0 comments on commit a30071c

Please sign in to comment.