fix: serializing an unsigned tx should throw

This commit is contained in:
Trent Nelson 2020-07-07 16:56:50 -06:00 committed by Trent Nelson
parent 57576b07ef
commit 3e3241125f
2 changed files with 25 additions and 225 deletions

View File

@ -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');
}

View File

@ -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', () => {