From 2c83e99df72fc5489aa9f2df3602b96668a308f8 Mon Sep 17 00:00:00 2001 From: Bhavi Dhingra Date: Tue, 29 Sep 2026 17:04:51 +0530 Subject: [PATCH 1/4] feat(sdk-coin-sol): add v1 message decoder Refs: CHALO-1602 --- .../lib/serialization/codecs/v1/message.ts | 156 +++++++++++++++++- .../lib/serialization/parseWireTransaction.ts | 75 +++++++++ .../unit/serialization/decodeV1Message.ts | 66 ++++++++ .../serialization/parseWireTransaction.ts | 93 +++++++++++ 4 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts create mode 100644 modules/sdk-coin-sol/test/unit/serialization/decodeV1Message.ts create mode 100644 modules/sdk-coin-sol/test/unit/serialization/parseWireTransaction.ts diff --git a/modules/sdk-coin-sol/src/lib/serialization/codecs/v1/message.ts b/modules/sdk-coin-sol/src/lib/serialization/codecs/v1/message.ts index 9e6c7eb9059..7ff8ee43f22 100644 --- a/modules/sdk-coin-sol/src/lib/serialization/codecs/v1/message.ts +++ b/modules/sdk-coin-sol/src/lib/serialization/codecs/v1/message.ts @@ -1,7 +1,14 @@ import bs58 from 'bs58'; import { PublicKey } from '@solana/web3.js'; import { BuildTransactionError } from '@bitgo/sdk-core'; -import { encodeConfigMaskAndValues, ConfigValue } from './config'; +import { + encodeConfigMaskAndValues, + ConfigValue, + PRIORITY_FEE_BITS, + COMPUTE_UNIT_LIMIT_BIT, + LOADED_ACCOUNTS_DATA_SIZE_LIMIT_BIT, + HEAP_SIZE_BIT, +} from './config'; import { encodeInstructions, CompiledInstruction } from './instruction'; /** @@ -72,3 +79,150 @@ export function encodeV1Message(args: { } export { encodeConfigMaskAndValues }; + +const V1_VERSION_PREFIX = 0x81; +const KNOWN_CONFIG_MASK = + PRIORITY_FEE_BITS | COMPUTE_UNIT_LIMIT_BIT | LOADED_ACCOUNTS_DATA_SIZE_LIMIT_BIT | HEAP_SIZE_BIT; +const MAX_ACCOUNTS = 64; +const MAX_INSTRUCTIONS = 64; +const MAX_ACCOUNTS_PER_INSTRUCTION = 255; +const STATIC_ACCOUNT_BYTES = 32; + +export type V1DecodedMessage = { + header: V1MessageHeader; + configMask: number; + configValues: ConfigValue[]; + blockhash: string; + staticAccounts: string[]; + compiledInstructions: CompiledInstruction[]; + byteLength: number; +}; + +function readU8(b: Uint8Array, o: number): number { + return b[o]; +} +function readU16LE(b: Uint8Array, o: number): number { + return b[o] | (b[o + 1] << 8); +} +function readU32LE(b: Uint8Array, o: number): number { + return (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0; +} +function readU64LE(b: Uint8Array, o: number): bigint { + let v = 0n; + for (let i = 0; i < 8; i++) { + v |= BigInt(b[o + i]) << BigInt(8 * i); + } + return v; +} + +function ensureAvailable(b: Uint8Array, off: number, size: number): void { + if (off + size > b.length) { + throw new BuildTransactionError('Invalid v1 message: truncated'); + } +} + +export function decodeV1Message(bytes: Uint8Array): V1DecodedMessage { + if (!bytes || bytes.length < 42) { + throw new BuildTransactionError('Invalid v1 message: too short'); + } + let off = 0; + if (bytes[off++] !== V1_VERSION_PREFIX) { + throw new BuildTransactionError(`Invalid v1 version prefix: 0x${bytes[0].toString(16)}`); + } + const numSignerAccounts = readU8(bytes, off++); + const numReadonlySignerAccounts = readU8(bytes, off++); + const numReadonlyNonSignerAccounts = readU8(bytes, off++); + const configMask = readU32LE(bytes, off); + off += 4; + if ((configMask & ~KNOWN_CONFIG_MASK) !== 0) { + throw new BuildTransactionError(`Unknown config mask bits: ${(configMask & ~KNOWN_CONFIG_MASK).toString(2)}`); + } + const blockhash = bs58.encode(bytes.slice(off, off + STATIC_ACCOUNT_BYTES)); + off += STATIC_ACCOUNT_BYTES; + const numInstructions = readU8(bytes, off++); + const numStaticAccounts = readU8(bytes, off++); + if (numStaticAccounts > MAX_ACCOUNTS) { + throw new BuildTransactionError('Invalid v1 message: too many static accounts'); + } + if (numInstructions > MAX_INSTRUCTIONS) { + throw new BuildTransactionError('Invalid v1 message: too many instructions'); + } + if (numReadonlySignerAccounts > numSignerAccounts) { + throw new BuildTransactionError('Invalid v1 header: readonly signers exceed signers'); + } + if (numSignerAccounts + numReadonlyNonSignerAccounts > numStaticAccounts) { + throw new BuildTransactionError('Invalid v1 header: signers + readonly non-signers exceed static accounts'); + } + + const staticAccounts: string[] = []; + for (let i = 0; i < numStaticAccounts; i++) { + ensureAvailable(bytes, off, STATIC_ACCOUNT_BYTES); + staticAccounts.push(bs58.encode(bytes.slice(off, off + STATIC_ACCOUNT_BYTES))); + off += STATIC_ACCOUNT_BYTES; + } + + const configValues: ConfigValue[] = []; + if (configMask & PRIORITY_FEE_BITS) { + ensureAvailable(bytes, off, 8); + configValues.push({ kind: 'u64', value: readU64LE(bytes, off) }); + off += 8; + } + if (configMask & COMPUTE_UNIT_LIMIT_BIT) { + ensureAvailable(bytes, off, 4); + configValues.push({ kind: 'u32', value: readU32LE(bytes, off) }); + off += 4; + } + if (configMask & LOADED_ACCOUNTS_DATA_SIZE_LIMIT_BIT) { + ensureAvailable(bytes, off, 4); + configValues.push({ kind: 'u32', value: readU32LE(bytes, off) }); + off += 4; + } + if (configMask & HEAP_SIZE_BIT) { + ensureAvailable(bytes, off, 4); + configValues.push({ kind: 'u32', value: readU32LE(bytes, off) }); + off += 4; + } + + const headers: Array<{ programAddressIndex: number; accountCount: number; dataLength: number }> = []; + for (let i = 0; i < numInstructions; i++) { + ensureAvailable(bytes, off, 4); + const programAddressIndex = readU8(bytes, off++); + const accountCount = readU8(bytes, off++); + const dataLength = readU16LE(bytes, off); + off += 2; + if (programAddressIndex >= numStaticAccounts) { + throw new BuildTransactionError('Invalid v1 instruction: program index out of range'); + } + if (accountCount > MAX_ACCOUNTS_PER_INSTRUCTION) { + throw new BuildTransactionError('Invalid v1 instruction: too many accounts'); + } + headers.push({ programAddressIndex, accountCount, dataLength }); + } + + const compiledInstructions: CompiledInstruction[] = []; + for (const h of headers) { + ensureAvailable(bytes, off, h.accountCount); + const accountIndices: number[] = []; + for (let i = 0; i < h.accountCount; i++) { + const idx = readU8(bytes, off++); + if (idx >= numStaticAccounts) { + throw new BuildTransactionError('Invalid v1 instruction: account index out of range'); + } + accountIndices.push(idx); + } + ensureAvailable(bytes, off, h.dataLength); + const data = bytes.slice(off, off + h.dataLength); + off += h.dataLength; + compiledInstructions.push({ programAddressIndex: h.programAddressIndex, accountIndices, data }); + } + + return { + header: { numSignerAccounts, numReadonlySignerAccounts, numReadonlyNonSignerAccounts }, + configMask, + configValues, + blockhash, + staticAccounts, + compiledInstructions, + byteLength: off, + }; +} diff --git a/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts b/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts new file mode 100644 index 00000000000..11d3dfcb108 --- /dev/null +++ b/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts @@ -0,0 +1,75 @@ +import { BuildTransactionError } from '@bitgo/sdk-core'; +import nacl from 'tweetnacl'; +import { PublicKey } from '@solana/web3.js'; +import { decodeV1Message } from './codecs/v1/message'; + +const SIGNATURE_BYTE_LENGTH = 64; +const V1_VERSION_PREFIX = 0x81; +const V1_TRANSACTION_SIZE_LIMIT = 4096; + +export type ParsedWireTransaction = { + messageBytes: Uint8Array; + signatures: Uint8Array[]; + signerPublicKeys: string[]; +}; + +export function parseWireTransaction(wireBytes: Uint8Array): ParsedWireTransaction { + if (!wireBytes || wireBytes.length === 0) { + throw new BuildTransactionError('Invalid v1 transaction: empty'); + } + if (wireBytes[0] !== V1_VERSION_PREFIX) { + throw new BuildTransactionError( + `Invalid v1 transaction: expected version prefix 0x${V1_VERSION_PREFIX.toString( + 16 + )}, got 0x${wireBytes[0].toString(16)}` + ); + } + if (wireBytes.length < 42) { + throw new BuildTransactionError('Invalid v1 transaction: too short'); + } + if (wireBytes.length > V1_TRANSACTION_SIZE_LIMIT) { + throw new BuildTransactionError(`Invalid v1 transaction: exceeds the ${V1_TRANSACTION_SIZE_LIMIT}-byte size limit`); + } + const decoded = decodeV1Message(wireBytes); + const messageLength = decoded.byteLength; + const numSignatures = decoded.header.numSignerAccounts; + const expectedSigBytes = numSignatures * SIGNATURE_BYTE_LENGTH; + + if (wireBytes.length - messageLength !== expectedSigBytes) { + throw new BuildTransactionError( + `Invalid v1 transaction: expected ${expectedSigBytes} signature bytes after message, got ${ + wireBytes.length - messageLength + }` + ); + } + + const messageBytes = wireBytes.slice(0, messageLength); + const signatures: Uint8Array[] = []; + for (let i = 0; i < numSignatures; i++) { + signatures.push( + wireBytes.slice(messageLength + i * SIGNATURE_BYTE_LENGTH, messageLength + (i + 1) * SIGNATURE_BYTE_LENGTH) + ); + } + return { messageBytes, signatures, signerPublicKeys: decoded.staticAccounts.slice(0, numSignatures) }; +} + +export function verifyV1Signatures( + messageBytes: Uint8Array, + signatures: Uint8Array[], + signerPublicKeys: string[] +): void { + if (signatures.length !== signerPublicKeys.length) { + throw new BuildTransactionError('Signature count does not match signer count'); + } + for (let i = 0; i < signatures.length; i++) { + const pubkey = new PublicKey(signerPublicKeys[i]).toBuffer(); + if (!nacl.sign.detached.verify(messageBytes, signatures[i], pubkey)) { + throw new BuildTransactionError(`Signature verification failed for signer ${i}`); + } + } +} + +export function validateRawTransactionV1(wireBytes: Uint8Array): void { + const { messageBytes, signatures, signerPublicKeys } = parseWireTransaction(wireBytes); + verifyV1Signatures(messageBytes, signatures, signerPublicKeys); +} diff --git a/modules/sdk-coin-sol/test/unit/serialization/decodeV1Message.ts b/modules/sdk-coin-sol/test/unit/serialization/decodeV1Message.ts new file mode 100644 index 00000000000..39444380c93 --- /dev/null +++ b/modules/sdk-coin-sol/test/unit/serialization/decodeV1Message.ts @@ -0,0 +1,66 @@ +import 'should'; +import { BuildTransactionError } from '@bitgo/sdk-core'; +import { decodeV1Message, encodeV1Message } from '../../../src/lib/serialization/codecs/v1/message'; +import { encodeConfigMaskAndValues } from '../../../src/lib/serialization/codecs/v1/config'; + +const A = '3h1zGmCwsRJn5kU4LdYD8QfUoXjW8YqZcZsRsYxVvNpq'; +const B = '9xQeWvGcCvsftH8sN4jWQcBpkT2yLdMfZqNpVxKjRwSb'; +const BLOCKHASH = 'GHtXQBsoZHVnNFa9YevAzFr17DJjgHXk3ycTKD5xD3Zi'; + +function sampleMessage(): Uint8Array { + const config = encodeConfigMaskAndValues({ + computeUnitLimit: 200_000, + heapSize: 32_768, + loadedAccountsDataSizeLimit: 65_536, + priorityFee: 5_000, + }); + return encodeV1Message({ + header: { numSignerAccounts: 1, numReadonlySignerAccounts: 0, numReadonlyNonSignerAccounts: 1 }, + configMask: config.mask, + configValues: config.values, + blockhash: BLOCKHASH, + staticAccounts: [A, B], + compiledInstructions: [{ programAddressIndex: 1, accountIndices: [0, 1], data: new Uint8Array([1, 2, 3]) }], + }); +} + +describe('decodeV1Message', () => { + it('round-trips an encoded v1 message', () => { + const bytes = sampleMessage(); + const decoded = decodeV1Message(bytes); + decoded.header.numSignerAccounts.should.equal(1); + decoded.header.numReadonlySignerAccounts.should.equal(0); + decoded.header.numReadonlyNonSignerAccounts.should.equal(1); + decoded.configMask.should.equal(0b11 | 0b100 | 0b1000 | 0b10000); + decoded.configValues.should.eql([ + { kind: 'u64', value: 5_000n }, + { kind: 'u32', value: 200_000 }, + { kind: 'u32', value: 65_536 }, + { kind: 'u32', value: 32_768 }, + ]); + decoded.blockhash.should.equal(BLOCKHASH); + decoded.staticAccounts.should.eql([A, B]); + decoded.compiledInstructions.should.eql([ + { programAddressIndex: 1, accountIndices: [0, 1], data: new Uint8Array([1, 2, 3]) }, + ]); + decoded.byteLength.should.equal(bytes.length); + }); + + it('rejects an unknown config mask bit', () => { + const bytes = sampleMessage(); + bytes[4] = 0xff; + (() => decodeV1Message(bytes)).should.throw(BuildTransactionError, { message: /Unknown config mask/ }); + }); + + it('rejects an out-of-range program index', () => { + const bytes = sampleMessage(); + const programIdxByte = 42 + 2 * 32 + 4 + 8 + 4 + 4; + bytes[programIdxByte] = 0xff; + (() => decodeV1Message(bytes)).should.throw(BuildTransactionError, { message: /program index/ }); + }); + + it('rejects a truncated message', () => { + const bytes = sampleMessage(); + (() => decodeV1Message(bytes.subarray(0, bytes.length - 1))).should.throw(BuildTransactionError); + }); +}); diff --git a/modules/sdk-coin-sol/test/unit/serialization/parseWireTransaction.ts b/modules/sdk-coin-sol/test/unit/serialization/parseWireTransaction.ts new file mode 100644 index 00000000000..1c100c23b25 --- /dev/null +++ b/modules/sdk-coin-sol/test/unit/serialization/parseWireTransaction.ts @@ -0,0 +1,93 @@ +import 'should'; +import nacl from 'tweetnacl'; +import bs58 from 'bs58'; +import { BuildTransactionError } from '@bitgo/sdk-core'; +import { parseWireTransaction, validateRawTransactionV1 } from '../../../src/lib/serialization/parseWireTransaction'; +import { encodeV1Message } from '../../../src/lib/serialization/codecs/v1/message'; +import { encodeConfigMaskAndValues } from '../../../src/lib/serialization/codecs/v1/config'; +import { serializeWireTransaction } from '../../../src/lib/serialization/wire-transaction'; + +const BLOCKHASH = 'GHtXQBsoZHVnNFa9YevAzFr17DJjgHXk3ycTKD5xD3Zi'; +const kp = nacl.sign.keyPair(); +const A = bs58.encode(kp.publicKey); +const B = bs58.encode(nacl.sign.keyPair().publicKey); + +function sampleMessage(): Uint8Array { + const config = encodeConfigMaskAndValues({ + computeUnitLimit: 200_000, + heapSize: null, + loadedAccountsDataSizeLimit: null, + priorityFee: 5_000, + }); + return encodeV1Message({ + header: { numSignerAccounts: 1, numReadonlySignerAccounts: 0, numReadonlyNonSignerAccounts: 1 }, + configMask: config.mask, + configValues: config.values, + blockhash: BLOCKHASH, + staticAccounts: [A, B], + compiledInstructions: [{ programAddressIndex: 1, accountIndices: [0, 1], data: new Uint8Array([1]) }], + }); +} + +function sign(messageBytes: Uint8Array): Uint8Array[] { + return [nacl.sign.detached(messageBytes, kp.secretKey)]; +} + +describe('parseWireTransaction', () => { + it('splits a signed v1 transaction into message + signatures + signer pubkeys', () => { + const messageBytes = sampleMessage(); + const signatures = sign(messageBytes); + const wire = serializeWireTransaction(messageBytes, signatures); + const parsed = parseWireTransaction(wire); + parsed.messageBytes.should.eql(messageBytes); + parsed.signatures.length.should.equal(1); + parsed.signatures[0].should.eql(signatures[0]); + parsed.signerPublicKeys.should.eql([A]); + }); + + it('rejects a wire tx whose tail is not the expected number of signatures', () => { + const messageBytes = sampleMessage(); + const wire = serializeWireTransaction(messageBytes, sign(messageBytes)); + const truncated = wire.subarray(0, wire.length - 1); + (() => parseWireTransaction(truncated)).should.throw(BuildTransactionError); + }); + + it('rejects a non-v1 wire tx', () => { + const bytes = new Uint8Array([0x00, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + (() => parseWireTransaction(bytes)).should.throw(BuildTransactionError, { message: /version prefix/ }); + }); +}); + +describe('validateRawTransactionV1', () => { + it('accepts a fully signed, valid v1 transaction', () => { + const messageBytes = sampleMessage(); + const wire = serializeWireTransaction(messageBytes, sign(messageBytes)); + (() => validateRawTransactionV1(wire)).should.not.throw(); + }); + + it('rejects a tampered signature', () => { + const messageBytes = sampleMessage(); + const wire = serializeWireTransaction(messageBytes, sign(messageBytes)); + const tampered = new Uint8Array(wire); + tampered[tampered.length - 1] ^= 0xff; + (() => validateRawTransactionV1(tampered)).should.throw(BuildTransactionError, { message: /verification failed/ }); + }); + + it('rejects when a required signature is missing', () => { + const config = encodeConfigMaskAndValues({ + computeUnitLimit: 200_000, + heapSize: null, + loadedAccountsDataSizeLimit: null, + priorityFee: 5_000, + }); + const msg = encodeV1Message({ + header: { numSignerAccounts: 1, numReadonlySignerAccounts: 0, numReadonlyNonSignerAccounts: 1 }, + configMask: config.mask, + configValues: config.values, + blockhash: BLOCKHASH, + staticAccounts: [A, B], + compiledInstructions: [{ programAddressIndex: 1, accountIndices: [0, 1], data: new Uint8Array([1]) }], + }); + (() => validateRawTransactionV1(msg)).should.throw(BuildTransactionError, { message: /signature bytes/ }); + }); +}); From 4f21e6459172db6c99a1e394a7d286d236145a44 Mon Sep 17 00:00:00 2001 From: Bhavi Dhingra Date: Tue, 29 Sep 2026 17:10:18 +0530 Subject: [PATCH 2/4] feat(sdk-coin-sol): validate v1 transaction signatures on broadcast Refs: CHALO-1602 --- modules/sdk-coin-sol/src/sol.ts | 22 ++++++- modules/sdk-coin-sol/test/unit/sol.ts | 91 ++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/modules/sdk-coin-sol/src/sol.ts b/modules/sdk-coin-sol/src/sol.ts index 284a1ec0695..695152cb08b 100644 --- a/modules/sdk-coin-sol/src/sol.ts +++ b/modules/sdk-coin-sol/src/sol.ts @@ -99,9 +99,12 @@ import { isValidPublicKey, validateRawTransaction, } from './lib/utils'; +import { validateRawTransactionV1 } from './lib/serialization/parseWireTransaction'; export const DEFAULT_SCAN_FACTOR = 20; // default number of receive addresses to scan for funds +const V1_TRANSACTION_SIZE_LIMIT = 4096; // v1 (SIMD-0296/0385) wire size limit in bytes + export interface TransactionFee { fee: string; } @@ -2159,7 +2162,24 @@ export class Sol extends BaseCoin { async broadcastTransaction({ serializedSignedTransaction, }: BaseBroadcastTransactionOptions): Promise { - validateRawTransaction(serializedSignedTransaction, true, true); + const rawBytes = Buffer.from(serializedSignedTransaction, 'base64'); + // v1 (SIMD-0296/0385) transactions start with the 0x81 version prefix; the legacy + // parser cannot deserialize them, so validate the v1 size limit directly instead. + const isV1 = rawBytes.length > 0 && rawBytes[0] === 0x81; + + if (isV1) { + if (rawBytes.length > V1_TRANSACTION_SIZE_LIMIT) { + throw new Error( + `v1 transaction exceeds the ${V1_TRANSACTION_SIZE_LIMIT}-byte size limit: ${rawBytes.length} bytes` + ); + } + // Structural + signature validation parity with the legacy path (which uses + // validateRawTransaction(raw, true, true)). + validateRawTransactionV1(rawBytes); + } else { + validateRawTransaction(serializedSignedTransaction, true, true); + } + const response = await this.getDataFromNode({ payload: { id: '1', diff --git a/modules/sdk-coin-sol/test/unit/sol.ts b/modules/sdk-coin-sol/test/unit/sol.ts index 507e2ba8769..d0d4be1b1cf 100644 --- a/modules/sdk-coin-sol/test/unit/sol.ts +++ b/modules/sdk-coin-sol/test/unit/sol.ts @@ -5,10 +5,13 @@ import * as should from 'should'; import * as sinon from 'sinon'; import { getExtraAccountMetaAddress, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'; -import { PublicKey, SystemProgram } from '@solana/web3.js'; +import { Keypair, PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js'; import * as base58 from 'bs58'; import * as nacl from 'tweetnacl'; +import { compileTransactionMessage } from '../../src/lib/serialization/compileTransactionMessage'; +import { serializeWireTransaction } from '../../src/lib/serialization/wire-transaction'; + import { BitGoAPI, encrypt } from '@bitgo/sdk-api'; import { common, @@ -4663,6 +4666,92 @@ describe('SOL:', function () { }); }); + describe('broadcastTransaction - v1', function () { + const sandBox = sinon.createSandbox(); + const recentBlockhash = 'GHtXQBsoZHVnNFa9YevAzFr17DJjgHXk3ycTKD5xD3Zi'; + + afterEach(() => { + sandBox.restore(); + }); + + // Build a real signed v1 (SIMD-0296/0385) transaction using the PR-A v1 + // serializer: compile the message (0x81 prefix), sign the message bytes with + // the fee payer's ed25519 key, and append the signature at the tail. + function buildV1SignedTransaction(): string { + const signer = Keypair.generate(); + const feePayer = signer.publicKey; + const tokenAddress = Keypair.generate().publicKey; + + const instruction = new TransactionInstruction({ + keys: [ + { pubkey: feePayer, isSigner: true, isWritable: true }, + { pubkey: tokenAddress, isSigner: false, isWritable: true }, + ], + programId: TOKEN_2022_PROGRAM_ID, + data: Buffer.from([27, 5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 6]), + }); + + const messageBytes = compileTransactionMessage({ + version: 1, + instructions: [instruction], + feePayer, + recentBlockhash, + transactionConfig: { + computeUnitLimit: 200_000, + heapSize: 32_768, + loadedAccountsDataSizeLimit: 65_536, + priorityFee: 5_000, + }, + }); + + const signature = nacl.sign.detached(messageBytes, signer.secretKey); + const wireBytes = serializeWireTransaction(messageBytes, [signature]); + return Buffer.from(wireBytes).toString('base64'); + } + + it('broadcasts a valid signed v1 transaction (0x81)', async function () { + const serializedSignedTransaction = buildV1SignedTransaction(); + const rawBytes = Buffer.from(serializedSignedTransaction, 'base64'); + assert.strictEqual(rawBytes[0], 0x81); + + const broadcastStub = sandBox + .stub(Sol.prototype, 'getDataFromNode' as keyof Sol) + .withArgs({ + payload: { + id: '1', + jsonrpc: '2.0', + method: 'sendTransaction', + params: [serializedSignedTransaction, { encoding: 'base64' }], + }, + }) + .resolves(testData.SolResponses.broadcastTransactionResponse); + + const broadcastTxn = await basecoin.broadcastTransaction({ serializedSignedTransaction }); + assert.ok(broadcastTxn); + assert.ok(broadcastTxn.txId); + assert.strictEqual(broadcastStub.callCount, 1); + }); + + it('rejects a v1 transaction with a tampered signature', async function () { + const serializedSignedTransaction = buildV1SignedTransaction(); + const rawBytes = Buffer.from(serializedSignedTransaction, 'base64'); + // Flip the last byte: it lies inside the trailing signature, so the + // structure stays valid but the ed25519 signature no longer verifies. + rawBytes[rawBytes.length - 1] ^= 0xff; + const tampered = rawBytes.toString('base64'); + + const broadcastStub = sandBox + .stub(Sol.prototype, 'getDataFromNode' as keyof Sol) + .resolves(testData.SolResponses.broadcastTransactionResponse); + + await assert.rejects(async () => { + await basecoin.broadcastTransaction({ serializedSignedTransaction: tampered }); + }, /Signature verification failed/); + // Validation must fail before any node call is made. + assert.strictEqual(broadcastStub.callCount, 0); + }); + }); + describe('AuditKey', () => { const { key: keyString, commonKeychain } = solBackupKey; const key = keyString.replace(/\s/g, ''); From ae81e9bca88f8d2082ae969442f92e3dc6290e05 Mon Sep 17 00:00:00 2001 From: Bhavi Dhingra Date: Tue, 29 Sep 2026 17:32:37 +0530 Subject: [PATCH 3/4] refactor(sdk-coin-sol): enforce v1 size limit in validateRawTransactionV1 Refs: CHALO-1602 --- .../src/lib/serialization/parseWireTransaction.ts | 4 +++- modules/sdk-coin-sol/src/sol.ts | 10 ++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts b/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts index 11d3dfcb108..47c690ccaa4 100644 --- a/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts +++ b/modules/sdk-coin-sol/src/lib/serialization/parseWireTransaction.ts @@ -28,7 +28,9 @@ export function parseWireTransaction(wireBytes: Uint8Array): ParsedWireTransacti throw new BuildTransactionError('Invalid v1 transaction: too short'); } if (wireBytes.length > V1_TRANSACTION_SIZE_LIMIT) { - throw new BuildTransactionError(`Invalid v1 transaction: exceeds the ${V1_TRANSACTION_SIZE_LIMIT}-byte size limit`); + throw new BuildTransactionError( + `Invalid v1 transaction: exceeds the ${V1_TRANSACTION_SIZE_LIMIT}-byte size limit: ${wireBytes.length} bytes` + ); } const decoded = decodeV1Message(wireBytes); const messageLength = decoded.byteLength; diff --git a/modules/sdk-coin-sol/src/sol.ts b/modules/sdk-coin-sol/src/sol.ts index 695152cb08b..52b23db8380 100644 --- a/modules/sdk-coin-sol/src/sol.ts +++ b/modules/sdk-coin-sol/src/sol.ts @@ -103,8 +103,6 @@ import { validateRawTransactionV1 } from './lib/serialization/parseWireTransacti export const DEFAULT_SCAN_FACTOR = 20; // default number of receive addresses to scan for funds -const V1_TRANSACTION_SIZE_LIMIT = 4096; // v1 (SIMD-0296/0385) wire size limit in bytes - export interface TransactionFee { fee: string; } @@ -2168,13 +2166,9 @@ export class Sol extends BaseCoin { const isV1 = rawBytes.length > 0 && rawBytes[0] === 0x81; if (isV1) { - if (rawBytes.length > V1_TRANSACTION_SIZE_LIMIT) { - throw new Error( - `v1 transaction exceeds the ${V1_TRANSACTION_SIZE_LIMIT}-byte size limit: ${rawBytes.length} bytes` - ); - } // Structural + signature validation parity with the legacy path (which uses - // validateRawTransaction(raw, true, true)). + // validateRawTransaction(raw, true, true)). The 4096-byte v1 size limit is + // enforced inside validateRawTransactionV1 (parseWireTransaction). validateRawTransactionV1(rawBytes); } else { validateRawTransaction(serializedSignedTransaction, true, true); From 3c2048d13a8d51cc94585b81e90242466cd68064 Mon Sep 17 00:00:00 2001 From: Bhavi Dhingra Date: Tue, 29 Sep 2026 18:52:25 +0530 Subject: [PATCH 4/4] fix(sdk-coin-sol): guard v1 detection against malformed broadcast input Refs: CHALO-1602 --- modules/sdk-coin-sol/src/sol.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/sdk-coin-sol/src/sol.ts b/modules/sdk-coin-sol/src/sol.ts index 52b23db8380..97168a0c397 100644 --- a/modules/sdk-coin-sol/src/sol.ts +++ b/modules/sdk-coin-sol/src/sol.ts @@ -2160,16 +2160,22 @@ export class Sol extends BaseCoin { async broadcastTransaction({ serializedSignedTransaction, }: BaseBroadcastTransactionOptions): Promise { - const rawBytes = Buffer.from(serializedSignedTransaction, 'base64'); - // v1 (SIMD-0296/0385) transactions start with the 0x81 version prefix; the legacy - // parser cannot deserialize them, so validate the v1 size limit directly instead. - const isV1 = rawBytes.length > 0 && rawBytes[0] === 0x81; + let rawBytes: Buffer | undefined; + let isV1 = false; + try { + rawBytes = Buffer.from(serializedSignedTransaction, 'base64'); + // v1 (SIMD-0296/0385) transactions start with the 0x81 version prefix. + isV1 = rawBytes.length > 0 && rawBytes[0] === 0x81; + } catch { + // Malformed or non-string input: not a v1 transaction. Fall through to the + // legacy validateRawTransaction path, which raises a descriptive error. + } if (isV1) { // Structural + signature validation parity with the legacy path (which uses // validateRawTransaction(raw, true, true)). The 4096-byte v1 size limit is // enforced inside validateRawTransactionV1 (parseWireTransaction). - validateRawTransactionV1(rawBytes); + validateRawTransactionV1(rawBytes!); } else { validateRawTransaction(serializedSignedTransaction, true, true); }