Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #136 from jimni1222/callback
Browse files Browse the repository at this point in the history
Added callback parameter for combineSignatures
  • Loading branch information
jimni1222 authored Oct 21, 2019
2 parents dd0c44a + 891699b commit 9352460
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/caver-klay/caver-klay-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,15 +900,24 @@ Accounts.prototype.getRawTransactionWithSignatures = function getRawTransactionW
*
* @method combineSignatures
* @param {Array} rawTransactions The array of raw transaction string to combine.
* @param {Function} callback The callback function to call.
* @return {Object}
*/
Accounts.prototype.combineSignatures = function combineSignatures(rawTransactions) {
Accounts.prototype.combineSignatures = function combineSignatures(rawTransactions, callback) {
let decodedTx
let senders = []
let feePayers = []
let feePayer

if (!_.isArray(rawTransactions)) throw new Error(`The parameter of the combineSignatures function must be an array of RLP encoded transaction strings.`)
callback = callback || function () {}

let handleError = (e) => {
e = e instanceof Error? e : new Error(e)
if (callback) callback(e)
return Promise.reject(e)
}

if (!_.isArray(rawTransactions)) return handleError(`The parameter of the combineSignatures function must be an array of RLP encoded transaction strings.`)

for (const raw of rawTransactions) {
const { senderSignatures, feePayerSignatures, decodedTransaction } = extractSignatures(raw)
Expand Down Expand Up @@ -944,7 +953,7 @@ Accounts.prototype.combineSignatures = function combineSignatures(rawTransaction
break
}
}
if (!isSame) throw new Error(`Failed to combineSignatures: Signatures that sign to different transaction cannot be combined.`)
if (!isSame) return handleError(`Failed to combineSignatures: Signatures that sign to different transaction cannot be combined.`)
} else {
decodedTx = decodedTransaction
}
Expand All @@ -960,7 +969,7 @@ Accounts.prototype.combineSignatures = function combineSignatures(rawTransaction
parsedTxObject.feePayerSignatures = feePayers
}
}
return this.getRawTransactionWithSignatures(parsedTxObject)
return this.getRawTransactionWithSignatures(parsedTxObject, callback)
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/packages/caver.klay.accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,32 @@ describe('caver.klay.accounts.combineSignatures', () => {
expect(decoded.signatures.length).to.equals(3)
}).timeout(200000)
})

context('CAVERJS-UNIT-WALLET-387: input: not array', () => {
it('should throw error when parameter is not array', async () => {
let signResult = await caver.klay.accounts.signTransaction(feeDelegatedTx)

const expectedError = 'The parameter of the combineSignatures function must be an array of RLP encoded transaction strings.'
await expect(caver.klay.accounts.combineSignatures(signResult.rawTransaction, (error, result) => {
expect(error).not.to.be.undefined
expect(result).to.be.undefined
})).to.be.rejectedWith(expectedError)
}).timeout(200000)
})

context('CAVERJS-UNIT-WALLET-388: input: different RLP encoded transaction', () => {
it('should throw error when contents of transaction is not same', async () => {
let signResult = await caver.klay.accounts.signTransaction(feeDelegatedTx, sender.transactionKey[0])
feeDelegatedTx.value = 2
let signResult2 = await caver.klay.accounts.signTransaction(feeDelegatedTx, sender.transactionKey[2])

const expectedError = 'Failed to combineSignatures: Signatures that sign to different transaction cannot be combined.'
await expect(caver.klay.accounts.combineSignatures([signResult.rawTransaction, signResult2.rawTransaction], (error, result) => {
expect(error).not.to.be.undefined
expect(result).to.be.undefined
})).to.be.rejectedWith(expectedError)
}).timeout(200000)
})
})

describe('caver.klay.accounts.recoverTransaction', () => {
Expand Down

0 comments on commit 9352460

Please sign in to comment.