Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/internal/btc/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async function btcSign(
requireVersion(info, { major: 9, minor: 24, patch: 0 });
}

const signatures: Uint8Array[] = [];
const signatures = new Array<Uint8Array>(transaction.inputs.length);
let nextResponse = await queryBtcSignNext(channel, {
case: 'btcSignInit',
value: create(BTCSignInitRequestSchema, {
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
41 changes: 40 additions & 1 deletion test/btc-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<EncryptedChannel['query']>(async () => {
throw new Error('unexpected device query');
});
const info: Info = {
Expand Down Expand Up @@ -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'),
});
}
});
});
Loading