caver-js
Version:
caver-js is a JavaScript API library that allows developers to interact with a Kaia node
831 lines (662 loc) • 39.1 kB
JavaScript
/*
Copyright 2020 The caver-js Authors
This file is part of the caver-js library.
The caver-js library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The caver-js library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the caver-js. If not, see <http://www.gnu.org/licenses/>.
*/
const chai = require('chai')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const chaiAsPromised = require('chai-as-promised')
const RLP = require('eth-lib/lib/rlp')
chai.use(chaiAsPromised)
chai.use(sinonChai)
const expect = chai.expect
const { propertiesForUnnecessary } = require('../utils')
const testRPCURL = require('../../testrpc')
const Caver = require('../../../index')
const Keyring = require('../../../packages/caver-wallet/src/keyring/keyringFactory')
const SingleKeyring = require('../../../packages/caver-wallet/src/keyring/singleKeyring')
const TransactionHasher = require('../../../packages/caver-transaction/src/transactionHasher/transactionHasher')
const { generateRoleBasedKeyring, checkSignature } = require('../utils')
let caver
let sender
let testKeyring
let roleBasedKeyring
const txWithExpectedValues = {}
const sandbox = sinon.createSandbox()
before(() => {
caver = new Caver(testRPCURL)
sender = caver.wallet.add(caver.wallet.keyring.generate())
testKeyring = caver.wallet.add(caver.wallet.keyring.generate())
roleBasedKeyring = generateRoleBasedKeyring([3, 3, 3])
txWithExpectedValues.tx = {
from: '0xa94f5374Fce5edBC8E2a8697C15331677e6EbF0B',
to: '0x7b65B75d204aBed71587c9E519a89277766EE1d0',
value: '0xa',
gas: '0xf4240',
nonce: 1234,
gasPrice: '0x19',
input: '0x68656c6c6f',
signatures: [
[
'0x25',
'0x7d2b0c89ee8afa502b3186413983bfe9a31c5776f4f820210cffe44a7d568d1c',
'0x2b1cbd587c73b0f54969f6b76ef2fd95cea0c1bb79256a75df9da696278509f3',
],
],
chainId: '0x1',
}
txWithExpectedValues.rlpEncodingForSigning =
'0xf841b83cf83a108204d219830f4240947b65b75d204abed71587c9e519a89277766ee1d00a94a94f5374fce5edbc8e2a8697c15331677e6ebf0b8568656c6c6f018080'
txWithExpectedValues.senderTxHash = '0x6c7ee543c24e5b928b638a9f4502c1eca69103f5467ed4b6a2ed0ea5aede2e6b'
txWithExpectedValues.transactionHash = '0x6c7ee543c24e5b928b638a9f4502c1eca69103f5467ed4b6a2ed0ea5aede2e6b'
txWithExpectedValues.rlpEncoding =
'0x10f8808204d219830f4240947b65b75d204abed71587c9e519a89277766ee1d00a94a94f5374fce5edbc8e2a8697c15331677e6ebf0b8568656c6c6ff845f84325a07d2b0c89ee8afa502b3186413983bfe9a31c5776f4f820210cffe44a7d568d1ca02b1cbd587c73b0f54969f6b76ef2fd95cea0c1bb79256a75df9da696278509f3'
})
describe('TxTypeValueTransferMemo', () => {
let transactionObj
let getGasPriceSpy
let getHeaderSpy
let getNonceSpy
let getChainIdSpy
beforeEach(() => {
transactionObj = {
from: sender.address,
to: testKeyring.address,
value: 1,
gas: '0x3b9ac9ff',
input: '0x68656c6c6f',
}
getGasPriceSpy = sandbox.stub(caver.transaction.klaytnCall, 'getGasPrice')
getGasPriceSpy.returns('0x5d21dba00')
getHeaderSpy = sandbox.stub(caver.transaction.klaytnCall, 'getHeaderByNumber')
getHeaderSpy.returns({ baseFeePerGas: '0x5d21dba00' })
getNonceSpy = sandbox.stub(caver.transaction.klaytnCall, 'getTransactionCount')
getNonceSpy.returns('0x3a')
getChainIdSpy = sandbox.stub(caver.transaction.klaytnCall, 'getChainId')
getChainIdSpy.returns('0x7e3')
})
afterEach(() => {
sandbox.restore()
})
context('create valueTransferMemo instance', () => {
it('CAVERJS-UNIT-TRANSACTION-102: If valueTransferMemo not define from, return error', () => {
delete transactionObj.from
const expectedError = '"from" is missing'
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-103: If valueTransferMemo not define to, return error', () => {
delete transactionObj.to
const expectedError = '"to" is missing'
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-104: If valueTransferMemo not define value, return error', () => {
delete transactionObj.value
const expectedError = '"value" is missing'
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-105: If valueTransferMemo not define gas, return error', () => {
delete transactionObj.gas
const expectedError = '"gas" is missing'
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-106: If valueTransferMemo not define input, return error', () => {
delete transactionObj.input
const expectedError = '"input" is missing'
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-107: If valueTransferMemo define from property with invalid address, return error', () => {
transactionObj.from = 'invalid'
const expectedError = `Invalid address of from: ${transactionObj.from}`
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-108: If valueTransferMemo define to property with invalid address, return error', () => {
transactionObj.to = 'invalid address'
const expectedError = `Invalid address of to: ${transactionObj.to}`
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-109: If valueTransferMemo define unnecessary property, return error', () => {
const unnecessaries = [
propertiesForUnnecessary.codeFormat,
propertiesForUnnecessary.failKey,
propertiesForUnnecessary.feePayer,
propertiesForUnnecessary.feePayerSignatures,
propertiesForUnnecessary.feeRatio,
propertiesForUnnecessary.account,
propertiesForUnnecessary.key,
propertiesForUnnecessary.legacyKey,
propertiesForUnnecessary.publicKey,
propertiesForUnnecessary.failKey,
propertiesForUnnecessary.multisig,
propertiesForUnnecessary.roleTransactionKey,
propertiesForUnnecessary.roleAccountUpdateKey,
propertiesForUnnecessary.roleFeePayerKey,
propertiesForUnnecessary.humanReadable,
propertiesForUnnecessary.accessList,
propertiesForUnnecessary.maxPriorityFeePerGas,
propertiesForUnnecessary.maxFeePerGas,
]
for (let i = 0; i < unnecessaries.length; i++) {
if (i > 0) delete transactionObj[unnecessaries[i - 1].name]
transactionObj[unnecessaries[i].name] = unnecessaries[i].value
const expectedError = `"${unnecessaries[i].name}" cannot be used with ${caver.transaction.type.TxTypeValueTransferMemo} transaction`
expect(() => caver.transaction.valueTransferMemo.create(transactionObj)).to.throw(expectedError)
}
})
})
context('valueTransferMemo.getRLPEncoding', () => {
it('CAVERJS-UNIT-TRANSACTION-110: Returns RLP-encoded string', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
expect(tx.getRLPEncoding()).to.equal(txWithExpectedValues.rlpEncoding)
})
it('CAVERJS-UNIT-TRANSACTION-111: getRLPEncoding should throw error when nonce is undefined', () => {
transactionObj.chainId = 2019
transactionObj.gasPrice = '0x5d21dba00'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `nonce is undefined. Define nonce in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getRLPEncoding()).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-112: getRLPEncoding should throw error when gasPrice is undefined', () => {
transactionObj.chainId = 2019
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `gasPrice is undefined. Define gasPrice in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getRLPEncoding()).to.throw(expectedError)
})
})
context('valueTransferMemo.sign', () => {
const txHash = '0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550'
let fillTransactionSpy
let createFromPrivateKeySpy
let senderSignSpy
let appendSignaturesSpy
let hasherSpy
let tx
beforeEach(() => {
tx = caver.transaction.valueTransferMemo.create(transactionObj)
fillTransactionSpy = sandbox.spy(tx, 'fillTransaction')
createFromPrivateKeySpy = sandbox.spy(Keyring, 'createFromPrivateKey')
senderSignSpy = sandbox.spy(sender, 'sign')
appendSignaturesSpy = sandbox.spy(tx, 'appendSignatures')
hasherSpy = sandbox.stub(TransactionHasher, 'getHashForSignature')
hasherSpy.returns(txHash)
})
afterEach(() => {
sandbox.restore()
})
function checkFunctionCall(customHasher = false) {
expect(fillTransactionSpy).to.have.been.calledOnce
expect(appendSignaturesSpy).to.have.been.calledOnce
if (!customHasher) expect(hasherSpy).to.have.been.calledWith(tx)
}
it('CAVERJS-UNIT-TRANSACTION-114: input: keyring. should sign transaction.', async () => {
await tx.sign(sender)
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(senderSignSpy).to.have.been.calledWith(txHash, '0x7e3', 0, undefined)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-115: input: private key string. should sign transaction.', async () => {
const signProtoSpy = sandbox.spy(SingleKeyring.prototype, 'sign')
await tx.sign(sender.key.privateKey)
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).to.have.been.calledOnce
expect(signProtoSpy).to.have.been.calledWith(txHash, '0x7e3', 0, undefined)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-116: input: KlaytnWalletKey. should sign transaction.', async () => {
const signProtoSpy = sandbox.spy(SingleKeyring.prototype, 'sign')
await tx.sign(sender.getKlaytnWalletKey())
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).to.have.been.calledOnce
expect(signProtoSpy).to.have.been.calledWith(txHash, '0x7e3', 0, undefined)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-117: input: keyring, index. should sign transaction with specific index.', async () => {
const roleBasedSignSpy = sandbox.spy(roleBasedKeyring, 'sign')
tx.from = roleBasedKeyring.address
await tx.sign(roleBasedKeyring, 1)
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(roleBasedSignSpy).to.have.been.calledWith(txHash, '0x7e3', 0, 1)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-118: input: keyring, custom hasher. should use custom hasher.', async () => {
const hashForCustomHasher = '0x9e4b4835f6ea5ce55bd1037fe92040dd070af6154aefc30d32c65364a1123cae'
const customHasher = () => hashForCustomHasher
await tx.sign(sender, customHasher)
checkFunctionCall(true)
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(senderSignSpy).to.have.been.calledWith(hashForCustomHasher, '0x7e3', 0, undefined)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-119: input: keyring, index, custom hasher. should use custom hasher when sign transaction.', async () => {
const hashForCustomHasher = '0x9e4b4835f6ea5ce55bd1037fe92040dd070af6154aefc30d32c65364a1123cae'
const customHasher = () => hashForCustomHasher
const roleBasedSignSpy = sandbox.spy(roleBasedKeyring, 'sign')
tx.from = roleBasedKeyring.address
await tx.sign(roleBasedKeyring, 1, customHasher)
checkFunctionCall(true)
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(roleBasedSignSpy).to.have.been.calledWith(hashForCustomHasher, '0x7e3', 0, 1)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-120: input: keyring. should throw error when from is different.', async () => {
transactionObj.from = roleBasedKeyring.address
tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `The from address of the transaction is different with the address of the keyring to use.`
await expect(tx.sign(sender)).to.be.rejectedWith(expectedError)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-121: input: rolebased keyring, index out of range. should throw error.', async () => {
transactionObj.from = roleBasedKeyring.address
tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `Invalid index(10): index must be less than the length of keys(${roleBasedKeyring.keys[0].length}).`
await expect(tx.sign(roleBasedKeyring, 10)).to.be.rejectedWith(expectedError)
}).timeout(200000)
})
context('valueTransferMemo.sign with multiple keys', () => {
const txHash = '0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550'
let fillTransactionSpy
let createFromPrivateKeySpy
let senderSignWithKeysSpy
let appendSignaturesSpy
let hasherSpy
let tx
beforeEach(() => {
tx = caver.transaction.valueTransferMemo.create(transactionObj)
fillTransactionSpy = sandbox.spy(tx, 'fillTransaction')
createFromPrivateKeySpy = sandbox.spy(Keyring, 'createFromPrivateKey')
senderSignWithKeysSpy = sandbox.spy(sender, 'sign')
appendSignaturesSpy = sandbox.spy(tx, 'appendSignatures')
hasherSpy = sandbox.stub(TransactionHasher, 'getHashForSignature')
hasherSpy.returns(txHash)
})
afterEach(() => {
sandbox.restore()
})
function checkFunctionCall(customHasher = false) {
expect(fillTransactionSpy).to.have.been.calledOnce
expect(appendSignaturesSpy).to.have.been.calledOnce
if (!customHasher) expect(hasherSpy).to.have.been.calledWith(tx)
}
it('CAVERJS-UNIT-TRANSACTION-122: input: keyring. should sign transaction.', async () => {
await tx.sign(sender)
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(senderSignWithKeysSpy).to.have.been.calledWith(txHash, '0x7e3', 0)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-123: input: private key string. should sign transaction.', async () => {
const signProtoSpy = sandbox.spy(SingleKeyring.prototype, 'sign')
await tx.sign(sender.key.privateKey)
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).to.have.been.calledOnce
expect(signProtoSpy).to.have.been.calledWith(txHash, '0x7e3', 0)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-124: input: KlaytnWalletKey. should sign transaction.', async () => {
const signProtoSpy = sandbox.spy(SingleKeyring.prototype, 'sign')
await tx.sign(sender.getKlaytnWalletKey())
checkFunctionCall()
checkSignature(tx)
expect(createFromPrivateKeySpy).to.have.been.calledOnce
expect(signProtoSpy).to.have.been.calledWith(txHash, '0x7e3', 0)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-125: input: keyring, custom hasher. should use custom hasher when sign transaction.', async () => {
const hashForCustomHasher = '0x9e4b4835f6ea5ce55bd1037fe92040dd070af6154aefc30d32c65364a1123cae'
const customHasher = () => hashForCustomHasher
await tx.sign(sender, customHasher)
checkFunctionCall(true)
checkSignature(tx)
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(senderSignWithKeysSpy).to.have.been.calledWith(hashForCustomHasher, '0x7e3', 0)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-126: input: keyring. should throw error when from is different.', async () => {
transactionObj.from = roleBasedKeyring.address
tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `The from address of the transaction is different with the address of the keyring to use.`
await expect(tx.sign(sender)).to.be.rejectedWith(expectedError)
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-127: input: roleBased keyring. should sign with multiple keys and append signatures', async () => {
const roleBasedSignWithKeysSpy = sandbox.spy(roleBasedKeyring, 'sign')
tx.from = roleBasedKeyring.address
await tx.sign(roleBasedKeyring)
checkFunctionCall(true)
checkSignature(tx, { expectedLength: roleBasedKeyring.keys[0].length })
expect(createFromPrivateKeySpy).not.to.have.been.calledOnce
expect(roleBasedSignWithKeysSpy).to.have.been.calledWith(txHash, '0x7e3', 0)
}).timeout(200000)
})
context('valueTransferMemo.appendSignatures', () => {
afterEach(() => {
sandbox.restore()
})
it('CAVERJS-UNIT-TRANSACTION-128: If signatures is empty, appendSignatures append signatures in transaction', () => {
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const sig = [
'0x0fea',
'0xade9480f584fe481bf070ab758ecc010afa15debc33e1bd75af637d834073a6e',
'0x38160105d78cef4529d765941ad6637d8dcf6bd99310e165fee1c39fff2aa27e',
]
tx.appendSignatures(sig)
checkSignature(tx)
})
it('CAVERJS-UNIT-TRANSACTION-129: If signatures is empty, appendSignatures append signatures with two-dimensional signature array', () => {
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const sig = [
[
'0x0fea',
'0xade9480f584fe481bf070ab758ecc010afa15debc33e1bd75af637d834073a6e',
'0x38160105d78cef4529d765941ad6637d8dcf6bd99310e165fee1c39fff2aa27e',
],
]
tx.appendSignatures(sig)
checkSignature(tx)
})
it('CAVERJS-UNIT-TRANSACTION-130: If signatures is not empty, appendSignatures should append signatures', () => {
transactionObj.signatures = [
'0x0fea',
'0xade9480f584fe481bf070ab758ecc010afa15debc33e1bd75af637d834073a6e',
'0x38160105d78cef4529d765941ad6637d8dcf6bd99310e165fee1c39fff2aa27e',
]
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const sig = [
'0x0fea',
'0x7a5011b41cfcb6270af1b5f8aeac8aeabb1edb436f028261b5add564de694700',
'0x23ac51660b8b421bf732ef8148d0d4f19d5e29cb97be6bccb5ae505ebe89eb4a',
]
tx.appendSignatures(sig)
checkSignature(tx, { expectedLength: 2 })
})
it('CAVERJS-UNIT-TRANSACTION-131: appendSignatures should append multiple signatures', () => {
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const sig = [
[
'0x0fea',
'0xbde66cceed35a576010966338b7ded961f2c160c96f928e193b47aaf4480aa07',
'0x546eb193ec138523b7fd34c4f12a1a04d0f74470e8f3bbe91ce0b4ec16e7f0d2',
],
[
'0x0fea',
'0xade9480f584fe481bf070ab758ecc010afa15debc33e1bd75af637d834073a6e',
'0x38160105d78cef4529d765941ad6637d8dcf6bd99310e165fee1c39fff2aa27e',
],
]
tx.appendSignatures(sig)
checkSignature(tx, { expectedLength: 2 })
})
})
context('valueTransferMemo.combineSignedRawTransactions', () => {
beforeEach(() => {
transactionObj = {
from: '0x7d0104ac150f749d36bb34999bcade9f2c0bd2e6',
to: '0x8723590d5D60e35f7cE0Db5C09D3938b26fF80Ae',
value: 1,
gas: 90000,
input: '0x68656c6c6f',
nonce: '0x3a',
gasPrice: '0x5d21dba00',
chainId: 2019,
}
})
afterEach(() => {
sandbox.restore()
})
it('CAVERJS-UNIT-TRANSACTION-132: combineSignedRawTransactions combines single signature and sets signatures in transaction', () => {
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const appendSignaturesSpy = sandbox.spy(tx, 'appendSignatures')
const getRLPEncodingSpy = sandbox.spy(tx, 'getRLPEncoding')
const rlpEncoded =
'0x10f8853a8505d21dba0083015f90948723590d5d60e35f7ce0db5c09d3938b26ff80ae01947d0104ac150f749d36bb34999bcade9f2c0bd2e68568656c6c6ff847f845820fe9a02aea3bb7c0632f1991b0b0b7a51cd6537a35554b74c198ebd79069c72a591832a0617d2942861f2c4280e793f2bdb107751e88c43048983823110eb044d7572254'
const combined = tx.combineSignedRawTransactions([rlpEncoded])
const expectedSignatures = [
[
'0x0fe9',
'0x2aea3bb7c0632f1991b0b0b7a51cd6537a35554b74c198ebd79069c72a591832',
'0x617d2942861f2c4280e793f2bdb107751e88c43048983823110eb044d7572254',
],
]
expect(appendSignaturesSpy).to.have.been.calledOnce
expect(getRLPEncodingSpy).to.have.been.calledOnce
expect(combined).to.equal(rlpEncoded)
checkSignature(tx, { expectedSignatures })
})
it('CAVERJS-UNIT-TRANSACTION-133: combineSignedRawTransactions combines multiple signatures and sets signatures in transaction', () => {
transactionObj.signatures = [
[
'0x0fe9',
'0x2aea3bb7c0632f1991b0b0b7a51cd6537a35554b74c198ebd79069c72a591832',
'0x617d2942861f2c4280e793f2bdb107751e88c43048983823110eb044d7572254',
],
]
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const rlpEncodedStrings = [
'0x10f8853a8505d21dba0083015f90948723590d5d60e35f7ce0db5c09d3938b26ff80ae01947d0104ac150f749d36bb34999bcade9f2c0bd2e68568656c6c6ff847f845820feaa0eda88095a7e349facbb40cc68c8c082aab3c21fbdbb05dca7fce6ab6c0a92866a03420efb785a186cda7f5bf99473bff57c18f9c4384126bec6f9172d6dcce2565',
'0x10f8853a8505d21dba0083015f90948723590d5d60e35f7ce0db5c09d3938b26ff80ae01947d0104ac150f749d36bb34999bcade9f2c0bd2e68568656c6c6ff847f845820fe9a08d80151db0b7195adfef41443ddacd5ca57a6a479eb31fb0fea9f1c98596d4c9a079f37b400123c6a8415d8a851e8519102a02345feff6e2b3fb3b28699712e7e4',
]
const appendSignaturesSpy = sandbox.spy(tx, 'appendSignatures')
const getRLPEncodingSpy = sandbox.spy(tx, 'getRLPEncoding')
const combined = tx.combineSignedRawTransactions(rlpEncodedStrings)
const expectedRLPEncoded =
'0x10f901133a8505d21dba0083015f90948723590d5d60e35f7ce0db5c09d3938b26ff80ae01947d0104ac150f749d36bb34999bcade9f2c0bd2e68568656c6c6ff8d5f845820fe9a02aea3bb7c0632f1991b0b0b7a51cd6537a35554b74c198ebd79069c72a591832a0617d2942861f2c4280e793f2bdb107751e88c43048983823110eb044d7572254f845820feaa0eda88095a7e349facbb40cc68c8c082aab3c21fbdbb05dca7fce6ab6c0a92866a03420efb785a186cda7f5bf99473bff57c18f9c4384126bec6f9172d6dcce2565f845820fe9a08d80151db0b7195adfef41443ddacd5ca57a6a479eb31fb0fea9f1c98596d4c9a079f37b400123c6a8415d8a851e8519102a02345feff6e2b3fb3b28699712e7e4'
const expectedSignatures = [
[
'0x0fe9',
'0x2aea3bb7c0632f1991b0b0b7a51cd6537a35554b74c198ebd79069c72a591832',
'0x617d2942861f2c4280e793f2bdb107751e88c43048983823110eb044d7572254',
],
[
'0x0fea',
'0xeda88095a7e349facbb40cc68c8c082aab3c21fbdbb05dca7fce6ab6c0a92866',
'0x3420efb785a186cda7f5bf99473bff57c18f9c4384126bec6f9172d6dcce2565',
],
[
'0x0fe9',
'0x8d80151db0b7195adfef41443ddacd5ca57a6a479eb31fb0fea9f1c98596d4c9',
'0x79f37b400123c6a8415d8a851e8519102a02345feff6e2b3fb3b28699712e7e4',
],
]
expect(appendSignaturesSpy).to.have.been.callCount(rlpEncodedStrings.length)
expect(getRLPEncodingSpy).to.have.been.calledOnce
expect(combined).to.equal(expectedRLPEncoded)
checkSignature(tx, { expectedSignatures })
})
it('CAVERJS-UNIT-TRANSACTION-134: If decode transaction has different values, combineSignedRawTransactions should throw error', () => {
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
tx.value = 10000
const rlpEncoded =
'0x10f8853a8505d21dba0083015f90948723590d5d60e35f7ce0db5c09d3938b26ff80ae01947d0104ac150f749d36bb34999bcade9f2c0bd2e68568656c6c6ff847f845820feaa0eda88095a7e349facbb40cc68c8c082aab3c21fbdbb05dca7fce6ab6c0a92866a03420efb785a186cda7f5bf99473bff57c18f9c4384126bec6f9172d6dcce2565'
const expectedError = `Transactions containing different information cannot be combined.`
expect(() => tx.combineSignedRawTransactions([rlpEncoded])).to.throw(expectedError)
})
})
context('valueTransferMemo.getRawTransaction', () => {
afterEach(() => {
sandbox.restore()
})
it('CAVERJS-UNIT-TRANSACTION-135: getRawTransaction should call getRLPEncoding function', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
const getRLPEncodingSpy = sandbox.spy(tx, 'getRLPEncoding')
const rawTransaction = tx.getRawTransaction()
expect(getRLPEncodingSpy).to.have.been.calledOnce
expect(rawTransaction).to.equal(txWithExpectedValues.rlpEncoding)
})
})
context('CAVERJS-UNIT-TRANSACTION-136: valueTransferMemo.getTransactionHash', () => {
afterEach(() => {
sandbox.restore()
})
it('getTransactionHash should call getRLPEncoding function and return hash of RLPEncoding', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
const getRLPEncodingSpy = sandbox.spy(tx, 'getRLPEncoding')
const txHash = tx.getTransactionHash()
expect(getRLPEncodingSpy).to.have.been.calledOnce
expect(txHash).to.equal(txWithExpectedValues.transactionHash)
expect(caver.utils.isValidHashStrict(txHash)).to.be.true
})
it('CAVERJS-UNIT-TRANSACTION-137: getTransactionHash should throw error when nonce is undefined', () => {
transactionObj.chainId = 2019
transactionObj.gasPrice = '0x5d21dba00'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `nonce is undefined. Define nonce in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getTransactionHash()).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-138: getTransactionHash should throw error when gasPrice is undefined', () => {
transactionObj.chainId = 2019
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `gasPrice is undefined. Define gasPrice in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getTransactionHash()).to.throw(expectedError)
})
})
context('valueTransferMemo.getSenderTxHash', () => {
afterEach(() => {
sandbox.restore()
})
it('CAVERJS-UNIT-TRANSACTION-140: getSenderTxHash should call getRLPEncoding function and return hash of RLPEncoding', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
const getRLPEncodingSpy = sandbox.spy(tx, 'getRLPEncoding')
const senderTxHash = tx.getSenderTxHash()
expect(getRLPEncodingSpy).to.have.been.calledOnce
expect(senderTxHash).to.equal(txWithExpectedValues.senderTxHash)
expect(caver.utils.isValidHashStrict(senderTxHash)).to.be.true
})
it('CAVERJS-UNIT-TRANSACTION-141: getSenderTxHash should throw error when nonce is undefined', () => {
transactionObj.chainId = 2019
transactionObj.gasPrice = '0x5d21dba00'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `nonce is undefined. Define nonce in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getSenderTxHash()).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-142: getSenderTxHash should throw error when gasPrice is undefined', () => {
transactionObj.chainId = 2019
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `gasPrice is undefined. Define gasPrice in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getSenderTxHash()).to.throw(expectedError)
})
})
context('valueTransferMemo.getRLPEncodingForSignature', () => {
afterEach(() => {
sandbox.restore()
})
it('CAVERJS-UNIT-TRANSACTION-144: getRLPEncodingForSignature should return RLP-encoded transaction string for signing', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
const commonRLPForSigningSpy = sandbox.spy(tx, 'getCommonRLPEncodingForSignature')
const rlpEncodingForSign = tx.getRLPEncodingForSignature()
expect(rlpEncodingForSign).to.equal(txWithExpectedValues.rlpEncodingForSigning)
expect(commonRLPForSigningSpy).to.have.been.calledOnce
})
it('CAVERJS-UNIT-TRANSACTION-145: getRLPEncodingForSignature should throw error when nonce is undefined', () => {
transactionObj.gasPrice = '0x5d21dba00'
transactionObj.chainId = 2019
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `nonce is undefined. Define nonce in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getRLPEncodingForSignature()).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-146: getRLPEncodingForSignature should throw error when gasPrice is undefined', () => {
transactionObj.chainId = 2019
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `gasPrice is undefined. Define gasPrice in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getRLPEncodingForSignature()).to.throw(expectedError)
})
it('CAVERJS-UNIT-TRANSACTION-147: getRLPEncodingForSignature should throw error when chainId is undefined', () => {
transactionObj.gasPrice = '0x5d21dba00'
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
const expectedError = `chainId is undefined. Define chainId in transaction or use 'transaction.fillTransaction' to fill values.`
expect(() => tx.getRLPEncodingForSignature()).to.throw(expectedError)
})
})
context('valueTransferMemo.getCommonRLPEncodingForSignature', () => {
it('CAVERJS-UNIT-TRANSACTION-148: getRLPEncodingForSignature should return RLP-encoded transaction string for signing', () => {
const tx = caver.transaction.valueTransferMemo.create(txWithExpectedValues.tx)
const commonRLPForSign = tx.getCommonRLPEncodingForSignature()
const decoded = RLP.decode(txWithExpectedValues.rlpEncodingForSigning)
expect(commonRLPForSign).to.equal(decoded[0])
})
})
context('valueTransferMemo.fillTransaction', () => {
it('CAVERJS-UNIT-TRANSACTION-149: fillTransaction should call klay_getGasPrice to fill gasPrice when gasPrice is undefined', async () => {
transactionObj.nonce = '0x3a'
transactionObj.chainId = 2019
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
await tx.fillTransaction()
expect(getGasPriceSpy).to.have.been.calledOnce
expect(getHeaderSpy).not.to.have.been.calledOnce
expect(getNonceSpy).not.to.have.been.calledOnce
expect(getChainIdSpy).not.to.have.been.calledOnce
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-150: fillTransaction should call klay_getTransactionCount to fill nonce when nonce is undefined', async () => {
transactionObj.gasPrice = '0x5d21dba00'
transactionObj.chainId = 2019
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
await tx.fillTransaction()
expect(getGasPriceSpy).not.to.have.been.calledOnce
expect(getHeaderSpy).not.to.have.been.calledOnce
expect(getNonceSpy).to.have.been.calledOnce
expect(getChainIdSpy).not.to.have.been.calledOnce
}).timeout(200000)
it('CAVERJS-UNIT-TRANSACTION-151: fillTransaction should call klay_getChainid to fill chainId when chainId is undefined', async () => {
transactionObj.gasPrice = '0x5d21dba00'
transactionObj.nonce = '0x3a'
const tx = caver.transaction.valueTransferMemo.create(transactionObj)
await tx.fillTransaction()
expect(getGasPriceSpy).not.to.have.been.calledOnce
expect(getHeaderSpy).not.to.have.been.calledOnce
expect(getNonceSpy).not.to.have.been.calledOnce
expect(getChainIdSpy).to.have.been.calledOnce
}).timeout(200000)
})
context('valueTransferMemo.recoverPublicKeys', () => {
const expectedPublicKeyArray = [
'0x8bb6aaeb2d96d024754d3b50babf116cece68977acbe8ba6a66f14d5217c60d96af020a0568661e7c72e753e80efe084a3aed9f9ac87bf44d09ce67aad3d4e01',
'0xc7751c794337a93e4db041fb5401c2c816cf0a099d8fd4b1f3f555aab5dfead2417521bb0c03d8637f350df15ef6a6cb3cdb806bd9d10bc71982dd03ff5d9ddd',
'0x3919091ba17c106dd034af508cfe00b963d173dffab2c7702890e25a96d107ca1bb4f148ee1984751e57d2435468558193ce84ab9a7731b842e9672e40dc0f22',
]
it('CAVERJS-UNIT-TRANSACTION-423: should return public key string recovered from signatures in ValueTransferMemo', async () => {
const tx = caver.transaction.valueTransferMemo.create({
from: '0xf21460730845e3652aa3cc9bc13b345e4f53984a',
to: '0x59177716c34ac6e49e295a0e78e33522f14d61ee',
value: '0x1',
chainId: '0x7e3',
gasPrice: '0x5d21dba00',
nonce: '0x0',
gas: '0x2faf080',
input: '0x68656c6c6f',
signatures: [
[
'0x0fea',
'0x11a1ccec9b1bd489c46f1fc34e102be17355ebd373048ef458fd57e4e5df7e8f',
'0x124e8a23b6316f1e308aed1daed99d991bf40ea16b8a146dce464c1c0462d0cb',
],
[
'0x0fea',
'0xfb4bb3b834ddb9d3d3dd0aed8fa0565f8fa4dd12a10a1e2c43a23ba58b254d23',
'0x5e3ee8d665a5d1097654149976963fbb8224e96c05cf7846ee3b511f75e633b4',
],
[
'0x0fe9',
'0xf7df849f0f2bf4c4743465c2049830b2a27b143bb2799ad211d2a6e07fc83899',
'0x5ccef241bcbe6c25d8affbacfb8fe02e5971cd32980ff2df2e627696d6368162',
],
],
})
const publicKeys = tx.recoverPublicKeys()
expect(publicKeys.length).to.equal(expectedPublicKeyArray.length)
for (let i = 0; i < publicKeys.length; i++) {
expect(publicKeys[i].toLowerCase()).to.equal(expectedPublicKeyArray[i].toLowerCase())
}
}).timeout(200000)
})
})