Skip to content

Commit

Permalink
Merge pull request #251 from nervosnetwork/add-sign-transaction-method
Browse files Browse the repository at this point in the history
feat(core): add signWitnesses and signTransaction methods in the core…
  • Loading branch information
Keith-CY authored Jun 5, 2019
2 parents cfc811e + c20d36c commit 5e90ab4
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 84 deletions.
105 changes: 78 additions & 27 deletions packages/ckb-sdk-core/__tests__/ckb-core.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,86 @@
const successFixtures = require('./successFixtures.json')
const exceptionFixtures = require('./exceptionFixtures.json')

const Core = require('../lib').default

const url = 'http://localhost:8114'
const core = new Core(url)

describe('ckb-core', () => {
const fixture = {
empty: {
codeHash: '',
outPoint: {
blockHash: '',
cell: {
txHash: '',
index: '0',
},
},
},
target: {
codeHash: '9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08',
outPoint: {
blockHash: 'aad9b82caa07f5989dfb8caa44927f0bab515a96ccaaceba82c7bea609fec205',
cell: {
txHash: 'bffab7ee0a050e2cb882de066d3dbf3afdd8932d6a26eda44f06e4b23f0f4b5a',
index: '1',
},
},
},
}
it('load the system cell', async () => {
expect(core.config.systemCellInfo).toEqual(fixture.empty)

const systemCellInfo = await core.loadSystemCell()
expect(systemCellInfo).toEqual(fixture.target)
describe('success', () => {
it('load the system cell', async () => {
const fixture = successFixtures.loadSystemCell
expect(core.config.systemCellInfo).toEqual(fixture.emptyInfo)

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

it('sign witnesses', () => {
const fixture = successFixtures.signWitnesses
const signedWitnessesByPrivateKey = core.signWitnesses(fixture.privateKey)(fixture.message)
expect(signedWitnessesByPrivateKey).toEqual(fixture.target)

const signedWitnessesByAddressObject = core.signWitnesses(core.generateAddress(fixture.privateKey))(
fixture.message
)
expect(signedWitnessesByAddressObject).toEqual(fixture.target)
})

it('sign transaction', async () => {
const fixture = successFixtures.signTransaction
const signedTransactionWithPrivateKey = await core.signTransaction(fixture.privateKey)(fixture.transaction)
const signedTransactionWithAddressObj = await core.signTransaction(core.generateAddress(fixture.privateKey))(
fixture.transaction
)
expect(signedTransactionWithPrivateKey).toEqual(fixture.target)
expect(signedTransactionWithAddressObj).toEqual(fixture.target)
})
})

describe('exceptions', () => {
describe('sign witneses', () => {
it('throw an error when key is missing', () => {
const fixture = exceptionFixtures.signWitnessesWithoutKey
expect(() => core.signWitnesses(fixture.privateKey)(fixture.message)).toThrowError(fixture.exception)
})

it('throw an error when transaction hash is missing', () => {
const fixture = exceptionFixtures.signWitnessesWithoutTransactionHash
expect(() => core.signWitnesses(fixture.privateKey)(fixture.message)).toThrowError(fixture.exception)
})
})

describe('sign transaction', () => {
it('throw an error when key is missing', () => {
const fixture = exceptionFixtures.signTransactionWithoutKey
expect(core.signTransaction(fixture.privateKey)(fixture.transaction)).rejects.toEqual(
new Error(fixture.exception)
)
})

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

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)
)
})
})
})
})
156 changes: 156 additions & 0 deletions packages/ckb-sdk-core/__tests__/exceptionFixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"signWitnessesWithoutKey": {
"privateKey": null,
"message": {
"transactionHash": "0xac1bb95455cdfb89b6e977568744e09b6b80e08cab9477936a09c4ca07f5b8ab",
"witnesses": [
{
"data": []
}
]
},
"exception": "Private key or address object is required"
},

"signWitnessesWithoutTransactionHash": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"message": {
"transactionHash": null,
"witnesses": [
{
"data": []
}
]
},
"exception": "Transaction hash is required"
},

"signTransactionWithoutKey": {
"privateKey": null,
"transaction": {
"deps": [
{
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"inputs": [
{
"previousOutput": {
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0"
}
],
"outputs": [
{
"capacity": "5000000000000",
"data": "0x",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001"
},
"type": null
}
],
"version": "0",
"witnesses": [{ "data": [] }]
},
"exception": "Private key or address object is required"
},

"signTransactionWithoutTransaction": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"transaction": null,
"exception": "Transaction is required"
},

"signTransactionWithoutWitnesses": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"transaction": {
"deps": [
{
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"inputs": [
{
"previousOutput": {
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0"
}
],
"outputs": [
{
"capacity": "5000000000000",
"data": "0x",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001"
},
"type": null
}
],
"version": "0",
"witnesses": null
},
"exception": "Witnesses is required"
},

"signTransactionWithInvalidCountOfWitnesses": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"transaction": {
"deps": [
{
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"inputs": [
{
"previousOutput": {
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0"
}
],
"outputs": [
{
"capacity": "5000000000000",
"data": "0x",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001"
},
"type": null
}
],
"version": "0",
"witnesses": []
},
"exception": "Invalid count of witnesses"
}
}
127 changes: 127 additions & 0 deletions packages/ckb-sdk-core/__tests__/successFixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"loadSystemCell": {
"emptyInfo": {
"codeHash": "",
"outPoint": {
"blockHash": "",
"cell": {
"txHash": "",
"index": "0"
}
}
},
"target": {
"codeHash": "f1951123466e4479842387a66fabfd6b65fc87fd84ae8e6cd3053edb27fff2fd",
"outPoint": {
"blockHash": "fc3ad90e38598032598c90b4ad4fefb420bacabaa7c5b40111daca7dfcc1f9d4",
"cell": {
"txHash": "3f09b95f8886723cc850db0beb9c153169151c663f3e8f832dc04421fbb1f382",
"index": "1"
}
}
}
},

"signWitnesses": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"message": {
"transactionHash": "0xac1bb95455cdfb89b6e977568744e09b6b80e08cab9477936a09c4ca07f5b8ab",
"witnesses": [
{
"data": []
}
]
},
"target": [
{
"data": [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x304402202c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe02202dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b6952408"
]
}
]
},

"signTransaction": {
"privateKey": "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3",
"transaction": {
"deps": [
{
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"inputs": [
{
"previousOutput": {
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0"
}
],
"outputs": [
{
"capacity": "5000000000000",
"data": "0x",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001"
},
"type": null
}
],
"version": "0",
"witnesses": [{ "data": [] }]
},
"target": {
"deps": [
{
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"inputs": [
{
"previousOutput": {
"cell": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "4294967295"
},
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0"
}
],
"outputs": [
{
"capacity": "5000000000000",
"data": "0x",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001"
},
"type": null
}
],
"version": "0",
"witnesses": [
{
"data": [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x3045022100aa659c1d2920d144b76e4d03eb4d0c56b22e66501c68bb330bc58b5af1ba411c02203728e526386d4186955049405a669a9ce63ec0e3b233ca7696f9a5d95651a256"
]
}
]
}
}
}
Loading

0 comments on commit 5e90ab4

Please sign in to comment.