From 5184804b6652056183babb47ef254d98765c9b8c Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 12 Jan 2021 12:25:44 -0800 Subject: [PATCH 1/2] tx: don't initialize v,r,s if undefined, add tests for isSigned --- packages/tx/src/transaction.ts | 16 ++++++++------- packages/tx/test/api.ts | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/tx/src/transaction.ts b/packages/tx/src/transaction.ts index 7359538c6c..99b82722fc 100644 --- a/packages/tx/src/transaction.ts +++ b/packages/tx/src/transaction.ts @@ -59,6 +59,8 @@ export default class Transaction { const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = values + const emptyBuffer = Buffer.from([]) + return new Transaction( { nonce: new BN(nonce), @@ -66,10 +68,10 @@ export default class Transaction { gasLimit: new BN(gasLimit), to: to && to.length > 0 ? new Address(to) : undefined, value: new BN(value), - data: data || Buffer.from([]), - v: v ? new BN(v) : undefined, - r: r ? new BN(r) : undefined, - s: s ? new BN(s) : undefined, + data: data ?? emptyBuffer, + v: !v?.equals(emptyBuffer) ? new BN(v) : undefined, + r: !r?.equals(emptyBuffer) ? new BN(r) : undefined, + s: !s?.equals(emptyBuffer) ? new BN(s) : undefined, }, opts ) @@ -89,9 +91,9 @@ export default class Transaction { this.to = to ? new Address(toBuffer(to)) : undefined this.value = new BN(toBuffer(value)) this.data = toBuffer(data) - this.v = new BN(toBuffer(v)) - this.r = new BN(toBuffer(r)) - this.s = new BN(toBuffer(s)) + this.v = v ? new BN(toBuffer(v)) : undefined + this.r = r ? new BN(toBuffer(r)) : undefined + this.s = s ? new BN(toBuffer(s)) : undefined const validateCannotExceedMaxInteger = { nonce: this.nonce, diff --git a/packages/tx/test/api.ts b/packages/tx/test/api.ts index 891329a8ba..78ce543efc 100644 --- a/packages/tx/test/api.ts +++ b/packages/tx/test/api.ts @@ -335,6 +335,43 @@ tape('[Transaction]: Basic functions', function (t) { st.end() }) + t.test('returns correct values for isSigned', function (st) { + let tx = Transaction.fromTxData({}) + st.notOk(tx.isSigned()) + + const txData: TxData = { + data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005', + gasLimit: '0x15f90', + gasPrice: '0x1', + nonce: '0x01', + to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc', + value: '0x0', + } + const privateKey = Buffer.from( + '4646464646464646464646464646464646464646464646464646464646464646', + 'hex' + ) + tx = Transaction.fromTxData(txData) + st.notOk(tx.isSigned()) + tx = tx.sign(privateKey) + st.ok(tx.isSigned()) + + tx = new Transaction(txData) + st.notOk(tx.isSigned()) + const rawUnsigned = tx.serialize() + tx = tx.sign(privateKey) + const rawSigned = tx.serialize() + st.ok(tx.isSigned()) + + tx = Transaction.fromRlpSerializedTx(rawUnsigned) + st.notOk(tx.isSigned()) + tx = tx.sign(privateKey) + st.ok(tx.isSigned()) + tx = Transaction.fromRlpSerializedTx(rawSigned) + st.ok(tx.isSigned()) + st.end() + }) + t.test( 'throws when creating a a transaction with incompatible chainid and v value', function (st) { From afbd077a1d464464bdbd37914ca6b7676e57e436 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 12 Jan 2021 12:55:34 -0800 Subject: [PATCH 2/2] update error message for "This transaction is not signed" --- packages/vm/tests/api/runTx.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vm/tests/api/runTx.spec.ts b/packages/vm/tests/api/runTx.spec.ts index 408d99a663..dc7ef0dd07 100644 --- a/packages/vm/tests/api/runTx.spec.ts +++ b/packages/vm/tests/api/runTx.spec.ts @@ -32,7 +32,7 @@ tape('runTx', (t) => { t.test('should fail to run without signature', async (st) => { const tx = getTransaction(false) shouldFail(st, suite.runTx({ tx }), (e: Error) => - st.ok(e.message.includes('Invalid Signature'), 'should fail with appropriate error') + st.ok(e.message.includes('not signed'), 'should fail with appropriate error') ) st.end() })