diff --git a/src/internal/btc/methods.ts b/src/internal/btc/methods.ts index 96a3335..ccf4e75 100644 --- a/src/internal/btc/methods.ts +++ b/src/internal/btc/methods.ts @@ -260,7 +260,7 @@ async function btcSign( requireVersion(info, { major: 9, minor: 24, patch: 0 }); } - const signatures: Uint8Array[] = []; + const signatures = new Array(transaction.inputs.length); let nextResponse = await queryBtcSignNext(channel, { case: 'btcSignInit', value: create(BTCSignInitRequestSchema, { @@ -282,7 +282,7 @@ async function btcSign( case BTCSignNextResponse_Type.INPUT: { const inputIndex = nextResponse.index; const input = transaction.inputs[inputIndex]; - if (input === undefined) { + if (input === undefined || signatures[inputIndex] !== undefined) { throw unexpectedResponse(); } const scriptConfig = transaction.scriptConfigs[input.scriptConfigIndex]; @@ -328,7 +328,10 @@ async function btcSign( if (!nextResponse.hasSignature) { throw unexpectedResponse(); } - signatures.push(nextResponse.signature); + if (nextResponse.signature.length !== 64) { + throw btcSignError('signature must be 64 bytes'); + } + signatures[inputIndex] = nextResponse.signature; } if (inputIndex === transaction.inputs.length - 1) { isInputsPass2 = true; @@ -406,6 +409,11 @@ async function btcSign( break; } case BTCSignNextResponse_Type.DONE: + for (const signature of signatures) { + if (signature === undefined || signature.length === 0) { + throw btcSignError('missing signature for transaction input'); + } + } return signatures; case BTCSignNextResponse_Type.HOST_NONCE: case BTCSignNextResponse_Type.PAYMENT_REQUEST: diff --git a/test/btc-methods.test.ts b/test/btc-methods.test.ts index 9795702..f6e8776 100644 --- a/test/btc-methods.test.ts +++ b/test/btc-methods.test.ts @@ -3,11 +3,14 @@ import { readFileSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; +import { create, toBinary } from '@bufbuild/protobuf'; import { Psbt } from 'bitcoinjs-lib'; import { describe, expect, it, vi } from 'vitest'; import { PairedBitBox } from '../src/index.js'; import type { Info } from '../src/internal/hww.js'; import type { EncryptedChannel } from '../src/internal/pairing.js'; +import { BTCSignNextResponse_Type } from '../src/proto/gen/btc_pb.js'; +import { ResponseSchema } from '../src/proto/gen/hww_pb.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const vectors = JSON.parse(readFileSync( @@ -17,7 +20,7 @@ const vectors = JSON.parse(readFileSync( const validPsbt = Psbt.fromHex(vectors.vectors[0]!.psbt.transaction).toBase64(); function paired(version = '9.26.0') { - const query = vi.fn(async () => { + const query = vi.fn(async () => { throw new Error('unexpected device query'); }); const info: Info = { @@ -145,3 +148,39 @@ describe('Bitcoin host validation', () => { expect(query).not.toHaveBeenCalled(); }); }); + +describe('Bitcoin signature validation', () => { + it.each([0, 1, 31, 32, 63, 64, 65, 66])('validates a %i-byte Schnorr signature', async (length) => { + const { bitbox, query } = paired(); + const psbt = Psbt.fromBase64(validPsbt); + const fingerprint = psbt.data.inputs[0]!.tapBip32Derivation![0]!.masterFingerprint; + const signature = new Uint8Array(length).fill(1); + const otherSignature = new Uint8Array(64).fill(2); + const responses = [ + create(ResponseSchema, { response: { case: 'fingerprint', value: { fingerprint } } }), + ...[ + { type: BTCSignNextResponse_Type.INPUT, index: 0 }, + { type: BTCSignNextResponse_Type.INPUT, index: 1 }, + { type: BTCSignNextResponse_Type.INPUT, index: 0 }, + { type: BTCSignNextResponse_Type.INPUT, index: 1, hasSignature: true, signature }, + { type: BTCSignNextResponse_Type.DONE, hasSignature: true, signature: otherSignature }, + ].map(value => create(ResponseSchema, { response: { case: 'btcSignNext', value } })), + ]; + for (const response of responses) { + query.mockResolvedValueOnce(toBinary(ResponseSchema, response)); + } + + const result = bitbox.btcSignPSBT('tbtc', validPsbt, undefined, 'default'); + if (length === 64) { + const signedPsbt = Psbt.fromBase64(await result); + expect(signedPsbt.data.inputs.map(input => input.tapKeySig)) + .toEqual([signature, otherSignature]); + expect(query).toHaveBeenCalledTimes(responses.length); + } else { + await expect(result).rejects.toMatchObject({ + code: 'btc-sign', + message: expect.stringContaining('signature must be 64 bytes'), + }); + } + }); +});