From e0a668e4d4c40d32507ab1faccf260a2e7cbfc1e Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Tue, 8 Apr 2025 11:45:00 +0200 Subject: [PATCH] fix: make SignPsbtResult type more precise Shape the sign result to indicate the type of signatures that were done: Ecdsa or Schnorr. Update tests to use the new type definitions and add type checking with 'satisfies'. Issue: BTC-1966 Co-authored-by: llm-git --- packages/wasm-miniscript/js/index.ts | 4 +++- packages/wasm-miniscript/test/psbtFromDescriptor.ts | 13 +++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/wasm-miniscript/js/index.ts b/packages/wasm-miniscript/js/index.ts index 8152113..a25b238 100644 --- a/packages/wasm-miniscript/js/index.ts +++ b/packages/wasm-miniscript/js/index.ts @@ -8,8 +8,10 @@ export type DescriptorPkType = "derivable" | "definite" | "string"; export type ScriptContext = "tap" | "segwitv0" | "legacy"; +export type SignPsbtInputResult = { Ecdsa: string[] } | { Schnorr: string[] }; + export type SignPsbtResult = { - [inputIndex: number]: [pubkey: string][]; + [inputIndex: number]: SignPsbtInputResult; }; declare module "./wasm/wasm_miniscript" { diff --git a/packages/wasm-miniscript/test/psbtFromDescriptor.ts b/packages/wasm-miniscript/test/psbtFromDescriptor.ts index e7485ea..418c390 100644 --- a/packages/wasm-miniscript/test/psbtFromDescriptor.ts +++ b/packages/wasm-miniscript/test/psbtFromDescriptor.ts @@ -4,7 +4,7 @@ import { getKey } from "@bitgo/utxo-lib/dist/src/testutil"; import { DescriptorNode, formatNode } from "../js/ast"; import { mockPsbtDefault } from "./psbtFromDescriptor.util"; -import { Descriptor } from "../js"; +import { Descriptor, SignPsbtInputResult, SignPsbtResult } from "../js"; import { toWrappedPsbt } from "./psbt.util"; function toKeyWithPath(k: BIP32Interface, path = "*"): string { @@ -59,12 +59,9 @@ function describeSignDescriptor( ), }); - function getSigResult(keys: (BIP32Interface | ECPairInterface)[]) { - return { - [isTaproot ? "Schnorr" : "Ecdsa"]: keys.map((key) => - key.publicKey.subarray(isTaproot ? 1 : 0).toString("hex"), - ), - }; + function getSigResult(keys: (BIP32Interface | ECPairInterface)[]): SignPsbtInputResult { + const pks = keys.map((key) => key.publicKey.subarray(isTaproot ? 1 : 0).toString("hex")); + return isTaproot ? { Schnorr: pks } : { Ecdsa: pks }; } signBip32.forEach((signSeq, i) => { @@ -100,7 +97,7 @@ function describeSignDescriptor( assert.deepStrictEqual(wrappedPsbt.signWithPrv(key.privateKey), { 0: getSigResult([key]), 1: getSigResult([key]), - }); + } satisfies SignPsbtResult); }); wrappedPsbt.finalize(); });