diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index e18ce65a9b..4c2124bf75 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -400,7 +400,7 @@ export class Transaction { */ serialize(): Buffer { const {signatures} = this; - if (!signatures) { + if (!signatures || signatures.length === 0 || !this.verifySignatures()) { throw new Error('Transaction has not been signed'); } diff --git a/web3.js/test/transaction.test.js b/web3.js/test/transaction.test.js index 630b07296a..30c3815b23 100644 --- a/web3.js/test/transaction.test.js +++ b/web3.js/test/transaction.test.js @@ -212,231 +212,31 @@ test('serialize unsigned transaction', () => { }); const expectedTransaction = new Transaction({recentBlockhash}).add(transfer); - const wireTransactionArray = [ - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 0, - 1, - 3, - 19, - 152, - 246, - 44, - 109, - 26, - 69, - 124, - 81, - 186, - 106, - 75, - 95, - 61, - 189, - 47, - 105, - 252, - 169, - 50, - 22, - 33, - 141, - 200, - 153, - 126, - 65, - 107, - 209, - 125, - 147, - 202, - 253, - 67, - 159, - 204, - 182, - 103, - 39, - 242, - 137, - 197, - 198, - 222, - 59, - 196, - 168, - 254, - 93, - 213, - 215, - 119, - 112, - 188, - 143, - 241, - 92, - 62, - 238, - 220, - 177, - 74, - 243, - 252, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 196, - 154, - 231, - 118, - 3, - 120, - 32, - 84, - 241, - 122, - 157, - 236, - 234, - 67, - 180, - 68, - 235, - 160, - 237, - 177, - 44, - 111, - 29, - 49, - 198, - 224, - 228, - 168, - 75, - 240, - 82, - 235, - 1, - 2, - 2, - 0, - 1, - 12, - 2, - 0, - 0, - 0, - 49, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ]; - - const wireTransaction = Buffer.from(wireTransactionArray); - expect(wireTransaction).toEqual(expectedTransaction.serialize()); - expect(Transaction.from(wireTransaction)).toEqual(expectedTransaction); - expect(Transaction.from(wireTransactionArray)).toEqual(expectedTransaction); - expect(Transaction.from(Uint8Array.from(wireTransactionArray))).toEqual( - expectedTransaction, + // Empty signature array fails. + expect(expectedTransaction.signatures.length).toBe(0); + expect(() => { + expectedTransaction.serialize(); + }).toThrow(Error); + expect(expectedTransaction.signatures.length).toBe(0); + // Signature array populated with null signatures fails. + expectedTransaction.serializeMessage(); + expect(expectedTransaction.signatures.length).toBe(1); + expect(() => { + expectedTransaction.serialize(); + }).toThrow(Error); + expect(expectedTransaction.signatures.length).toBe(1); + // Properly signed transaction succeeds + expectedTransaction.sign(sender); + expect(expectedTransaction.signatures.length).toBe(1); + const expectedSerialization = Buffer.from( + 'AVuErQHaXv0SG0/PchunfxHKt8wMRfMZzqV0tkC5qO6owYxWU2v871AoWywGoFQr4z+q/7mE8lIufNl/' + + 'kxj+nQ0BAAEDE5j2LG0aRXxRumpLXz29L2n8qTIWIY3ImX5Ba9F9k8r9Q5/Mtmcn8onFxt47xKj+XdXX' + + 'd3C8j/FcPu7csUrz/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAxJrndgN4IFTxep3s6kO0' + + 'ROug7bEsbx0xxuDkqEvwUusBAgIAAQwCAAAAMQAAAAAAAAA=', + 'base64', ); + expect(expectedTransaction.serialize()).toStrictEqual(expectedSerialization); + expect(expectedTransaction.signatures.length).toBe(1); }); test('get sign data for transaction', () => {