From fea09b36c98733b040db39c18dc86abcfd9f502b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Landabaso=20D=C3=ADaz?= Date: Tue, 12 Sep 2023 17:10:33 +0200 Subject: [PATCH] fix: Properly compare Buffer objects in Descriptors - Introduced a utility function, `eqBuffers`, to accurately compare two Buffer objects or their fallback values. - Updated buffer comparisons for `getWitnessScript` and `getRedeemScript` to use the new utility function, ensuring correct and more readable comparisons. Resolves #20. --- src/descriptors.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/descriptors.ts b/src/descriptors.ts index bc96a4d..279c6cd 100644 --- a/src/descriptors.ts +++ b/src/descriptors.ts @@ -765,12 +765,16 @@ export function DescriptorsFactory(ecc: TinySecp256k1Interface) { let sequence = this.getSequence(); if (sequence === undefined && locktime !== 0) sequence = 0xfffffffe; if (sequence === undefined && locktime === 0) sequence = 0xffffffff; + const eqBuffers = (buf1: Buffer | undefined, buf2: Buffer | undefined) => + buf1 instanceof Buffer && buf2 instanceof Buffer + ? Buffer.compare(buf1, buf2) === 0 + : buf1 === buf2; if ( Buffer.compare(scriptPubKey, this.getScriptPubKey()) !== 0 || sequence !== inputSequence || locktime !== psbt.locktime || - this.getWitnessScript() !== input.witnessScript || - this.getRedeemScript() !== input.redeemScript + !eqBuffers(this.getWitnessScript(), input.witnessScript) || + !eqBuffers(this.getRedeemScript(), input.redeemScript) ) { throw new Error( `Error: cannot finalize psbt index ${index} since it does not correspond to this descriptor`