import '@solana/test-matchers/toBeFrozenObject'; import { Address } from '@solana/addresses'; import { Encoder, ReadonlyUint8Array, VariableSizeDecoder } from '@solana/codecs-core'; import { SOLANA_ERROR__TRANSACTION__MALFORMED_MESSAGE_BYTES, SOLANA_ERROR__TRANSACTION__MESSAGE_SIGNATURES_MISMATCH, SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED, SolanaError, } from '@solana/errors'; import { SignatureBytes } from '@solana/keys'; import { Transaction, TransactionMessageBytes } from '../../transaction'; import { getSignaturesEncoderWithLength, getSignaturesEncoderWithSizePrefix } from '../signatures-encoder'; import { getTransactionCodec, getTransactionDecoder, getTransactionEncoder } from '../transaction-codec'; jest.mock('../signatures-encoder'); describe.each([getTransactionEncoder, getTransactionCodec])('Transaction encoder %p', encoderFactory => { const mockEncodedSignatures = new Uint8Array([1, 2, 3]); let encoder: Encoder; beforeEach(() => { (getSignaturesEncoderWithSizePrefix as jest.Mock).mockReturnValue({ getSizeFromValue: jest.fn().mockReturnValue(mockEncodedSignatures.length), write: jest.fn().mockImplementation((_value, bytes: Uint8Array, offset: number) => { bytes.set(mockEncodedSignatures, offset); return offset + mockEncodedSignatures.length; }), }); encoder = encoderFactory(); }); it('should encode a legacy transaction correctly', () => { // Legacy transactions have signature number as the first byte of the message const messageBytes = new Uint8Array([4, 5, 6]) as ReadonlyUint8Array as TransactionMessageBytes; const transaction: Transaction = { messageBytes, signatures: {}, }; expect(encoder.encode(transaction)).toStrictEqual( new Uint8Array([ /* signatures */ ...mockEncodedSignatures, /* message bytes */ ...messageBytes, ]), ); }); it('should encode a v0 transaction correctly', () => { // v0 transactions have the first byte with the version bit set to 128 const messageBytes = new Uint8Array([128, 4, 5, 6]) as ReadonlyUint8Array as TransactionMessageBytes; const transaction: Transaction = { messageBytes, signatures: {}, }; expect(encoder.encode(transaction)).toStrictEqual( new Uint8Array([ /* signatures */ ...mockEncodedSignatures, /* message bytes */ ...messageBytes, ]), ); }); it('should encode a v1 transaction correctly', () => { // v1 transactions have the first byte with the version bit set to 129 const messageBytes = new Uint8Array([129, 1, 5, 6]) as ReadonlyUint8Array as TransactionMessageBytes; const mockSignature = new Uint8Array(64).fill(1); // In this case we have a VariableSizEncoder with size messageBytes + (signatures * 64) // So we will mock the signature encoder with the size of a real signature // The second byte of `messageBytes` is 1, so we encode to 1 signature (getSignaturesEncoderWithLength as jest.Mock).mockReturnValue({ fixedSize: 64, write: jest.fn().mockImplementation((_value, bytes: Uint8Array, offset: number) => { bytes.set(mockSignature, offset); return offset + mockSignature.length; }), }); const transaction: Transaction = { messageBytes, signatures: {}, }; expect(encoder.encode(transaction)).toStrictEqual( new Uint8Array([ /* message bytes */ ...messageBytes, /* signatures */ ...mockSignature, ]), ); expect(getSignaturesEncoderWithLength).toHaveBeenCalledTimes(1); expect(getSignaturesEncoderWithLength).toHaveBeenCalledWith(1); }); it('should throw for a v1 transaction if there are no other message bytes', () => { // v1 transactions have the first byte with the version bit set to 129 // the following byte is signature count, but here the message bytes is malformed const messageBytes = new Uint8Array([129]) as ReadonlyUint8Array as TransactionMessageBytes; const transaction: Transaction = { messageBytes, signatures: {}, }; expect(() => encoder.encode(transaction)).toThrow( new SolanaError(SOLANA_ERROR__TRANSACTION__MALFORMED_MESSAGE_BYTES, { messageBytes, }), ); }); }); describe.each([getTransactionDecoder, getTransactionCodec])('Transaction decoder %p', decoderFactory => { let decoder: VariableSizeDecoder; beforeEach(() => { (getSignaturesEncoderWithSizePrefix as jest.Mock).mockReturnValue({ getSizeFromValue: jest.fn().mockReturnValue(0), write: jest.fn(), }); decoder = decoderFactory(); }); describe('for a legacy transaction', () => { describe('for a transaction with a single signature', () => { const addressBytes = new Uint8Array(64).fill(11); const address = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; it('should decode the transaction correctly when the signature is defined', () => { const signature = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: signature, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when the signature is not defined', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const address = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should freeze the signatures map', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const decoded = decoder.decode(encodedTransaction); expect(decoded.signatures).toBeFrozenObject(); }); }); describe('for a transaction with multiple signatures', () => { const address1Bytes = new Uint8Array(32).fill(11); const address1 = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; const address2Bytes = new Uint8Array(32).fill(12); const address2 = 'p2Yicb86aZig616Eav2VWG9vuXR5mEqhtzshZYBxzsV' as Address; const address3Bytes = new Uint8Array(32).fill(13); const address3 = 'swqrv48gsrwpBFbftEwnP2vB4jckpvfGJfXkwaniLCC' as Address; const signature1 = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; const signature2 = new Uint8Array(64).fill(2) as ReadonlyUint8Array as SignatureBytes; const signature3 = new Uint8Array(64).fill(3) as ReadonlyUint8Array as SignatureBytes; it('should decode the transaction correctly when all the signatures are defined', () => { const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...signature1, ...signature2, ...signature3, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: signature2, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when none of the signatures are defined', () => { const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: null, [address2]: null, [address3]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when some of the signatures are defined', () => { const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...signature1, ...new Uint8Array(64).fill(0), ...signature3, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: null, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should throw when the number of signers in the header does not match the number of signatures', () => { const messageBytes = new Uint8Array([ /** NO VERSION BYTE */ /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 2, // num signatures ...signature1, ...signature2, /** MESSAGE */ ...messageBytes, ]); expect(() => decoder.decode(encodedTransaction)).toThrow( new SolanaError(SOLANA_ERROR__TRANSACTION__MESSAGE_SIGNATURES_MISMATCH, { numRequiredSignatures: 3, signaturesLength: 2, signerAddresses: [address1, address2, address3], }), ); }); }); }); describe('for a v0 transaction', () => { describe('for a transaction with a single signature', () => { const addressBytes = new Uint8Array(64).fill(11); const address = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; it('should decode the transaction correctly when the signature is defined', () => { const signature = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: signature, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when the signature is not defined', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const address = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should freeze the signatures map', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 2, // Number of static accounts ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...signature, /** MESSAGE */ ...messageBytes, ]); const decoded = decoder.decode(encodedTransaction); expect(decoded.signatures).toBeFrozenObject(); }); }); describe('for a transaction with multiple signatures', () => { const address1Bytes = new Uint8Array(32).fill(11); const address1 = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; const address2Bytes = new Uint8Array(32).fill(12); const address2 = 'p2Yicb86aZig616Eav2VWG9vuXR5mEqhtzshZYBxzsV' as Address; const address3Bytes = new Uint8Array(32).fill(13); const address3 = 'swqrv48gsrwpBFbftEwnP2vB4jckpvfGJfXkwaniLCC' as Address; const signature1 = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; const signature2 = new Uint8Array(64).fill(2) as ReadonlyUint8Array as SignatureBytes; const signature3 = new Uint8Array(64).fill(3) as ReadonlyUint8Array as SignatureBytes; it('should decode the transaction correctly when all the signatures are defined', () => { const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...signature1, ...signature2, ...signature3, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: signature2, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when none of the signatures are defined', () => { const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: null, [address2]: null, [address3]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when some of the signatures are defined', () => { const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 3, // num signatures ...signature1, ...new Uint8Array(64).fill(0), ...signature3, /** MESSAGE */ ...messageBytes, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: null, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should throw when the number of signers in the header does not match the number of signatures', () => { const messageBytes = new Uint8Array([ /** VERSION HEADER */ 128, // 0 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 4, // Number of static accounts ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(21), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 2, // num signatures ...signature1, ...signature2, /** MESSAGE */ ...messageBytes, ]); expect(() => decoder.decode(encodedTransaction)).toThrow( new SolanaError(SOLANA_ERROR__TRANSACTION__MESSAGE_SIGNATURES_MISMATCH, { numRequiredSignatures: 3, signaturesLength: 2, signerAddresses: [address1, address2, address3], }), ); }); }); }); describe('for a v1 transaction', () => { describe('for a transaction with a single signature', () => { const addressBytes = new Uint8Array(64).fill(11); const address = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; it('should decode the transaction correctly when the signature is defined', () => { const signature = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary, 4 bytes /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 2, /** STATIC ADDRESSES */ ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...signature, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: signature, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when the signature is not defined', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 2, /** STATIC ADDRESSES */ ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...signature, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should freeze the signatures map', () => { const signature = new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes; // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 2, /** STATIC ADDRESSES */ ...addressBytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...signature, ]); const decoded = decoder.decode(encodedTransaction); expect(decoded.signatures).toBeFrozenObject(); }); }); describe('for a transaction with multiple signatures', () => { const address1Bytes = new Uint8Array(32).fill(11); const address1 = 'k7FaK87WHGVXzkaoHb7CdVPgkKDQhZ29VLDeBVbDfYn' as Address; const address2Bytes = new Uint8Array(32).fill(12); const address2 = 'p2Yicb86aZig616Eav2VWG9vuXR5mEqhtzshZYBxzsV' as Address; const address3Bytes = new Uint8Array(32).fill(13); const address3 = 'swqrv48gsrwpBFbftEwnP2vB4jckpvfGJfXkwaniLCC' as Address; const signature1 = new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes; const signature2 = new Uint8Array(64).fill(2) as ReadonlyUint8Array as SignatureBytes; const signature3 = new Uint8Array(64).fill(3) as ReadonlyUint8Array as SignatureBytes; it('should decode the transaction correctly when all the signatures are defined', () => { // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 4, /** STATIC ADDRESSES */ ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...signature1, ...signature2, ...signature3, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: signature2, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when none of the signatures are defined', () => { // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 4, /** STATIC ADDRESSES */ ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), ...new Uint8Array(64).fill(0), ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: null, [address2]: null, [address3]: null, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); it('should decode the transaction correctly when some of the signatures are defined', () => { // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 129, // 1 + version mask /** MESSAGE HEADER */ 3, // numSignerAccounts 0, // numReadonlySignerAccount 1, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 4, /** STATIC ADDRESSES */ ...address1Bytes, ...address2Bytes, ...address3Bytes, ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...signature1, ...new Uint8Array(64).fill(0), ...signature3, ]); const expectedTransaction: Transaction = { messageBytes: messageBytes, signatures: { [address1]: signature1, [address2]: null, [address3]: signature3, }, }; expect(decoder.decode(encodedTransaction)).toStrictEqual(expectedTransaction); }); }); }); describe('unsupported transaction versions', () => { it('should throw for a v0-shaped transaction with a v2 version byte', () => { const messageBytes = new Uint8Array([ /** VERSION HEADER */ 130, // 2 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 0, // numReadonlyNonSignerAccounts /** STATIC ADDRESSES */ 1, // Number of static accounts ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** SIGNATURES */ 1, // num signatures ...(new Uint8Array(64).fill(1) as ReadonlyUint8Array as SignatureBytes), /** MESSAGE */ ...messageBytes, ]); expect(() => decoder.decode(encodedTransaction)).toThrow( new SolanaError(SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED, { unsupportedVersion: 2, }), ); }); it('should throw for a v1-shaped transaction with a v2 version byte', () => { // prettier-ignore const messageBytes = new Uint8Array([ /** VERSION HEADER */ 130, // 2 + version mask /** MESSAGE HEADER */ 1, // numSignerAccounts 0, // numReadonlySignerAccount 0, // numReadonlyNonSignerAccounts /** TRANSACTION CONFIG MASK */ 0, 0, 0, 0, // arbitrary /** LIFETIME SPECIFIER */ 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, // arbitrary, 32 bytes /** NUM INSTRUCTIONS */ 1, // arbitrary /** NUM ACCOUNTS */ 1, /** STATIC ADDRESSES */ ...new Uint8Array(64).fill(12), /** REST OF TRANSACTION MESSAGE (arbitrary) */ ...new Uint8Array(100).fill(1), ]) as ReadonlyUint8Array as TransactionMessageBytes; const encodedTransaction = new Uint8Array([ /** MESSAGE */ ...messageBytes, /** SIGNATURES */ ...(new Uint8Array(64).fill(0) as ReadonlyUint8Array as SignatureBytes), ]); expect(() => decoder.decode(encodedTransaction)).toThrow( new SolanaError(SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED, { unsupportedVersion: 2, }), ); }); }); });