From c2d8d19c616e816c418f1283579010218a523fe9 Mon Sep 17 00:00:00 2001 From: junderw Date: Thu, 21 May 2020 12:54:49 +0900 Subject: [PATCH] Simplify chunkHasUncompressedPubkey and remove lazy load of output script --- src/payments/p2wsh.js | 18 +++++++++++------- ts_src/payments/p2wsh.ts | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/payments/p2wsh.js b/src/payments/p2wsh.js index 1177f3c..f9ae90b 100644 --- a/src/payments/p2wsh.js +++ b/src/payments/p2wsh.js @@ -16,9 +16,13 @@ function stacksEqual(a, b) { }); } function chunkHasUncompressedPubkey(chunk) { - if (Buffer.isBuffer(chunk) && chunk.length === 65) { - if (ecc.isPoint(chunk)) return true; - else return false; + if ( + Buffer.isBuffer(chunk) && + chunk.length === 65 && + chunk[0] === 0x04 && + ecc.isPoint(chunk) + ) { + return true; } else { return false; } @@ -60,9 +64,6 @@ function p2wsh(a, opts) { const _rchunks = lazy.value(() => { return bscript.decompile(a.redeem.input); }); - const _rochunks = lazy.value(() => { - return bscript.decompile(a.redeem.output); - }); let network = a.network; if (!network) { network = (a.redeem && a.redeem.network) || networks_1.bitcoin; @@ -180,7 +181,10 @@ function p2wsh(a, opts) { throw new TypeError('Witness and redeem.witness mismatch'); if ( (a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) || - (a.redeem.output && _rochunks().some(chunkHasUncompressedPubkey)) + (a.redeem.output && + (bscript.decompile(a.redeem.output) || []).some( + chunkHasUncompressedPubkey, + )) ) { throw new TypeError( 'redeem.input or redeem.output contains uncompressed pubkey', diff --git a/ts_src/payments/p2wsh.ts b/ts_src/payments/p2wsh.ts index 173767d..a72be94 100644 --- a/ts_src/payments/p2wsh.ts +++ b/ts_src/payments/p2wsh.ts @@ -20,9 +20,13 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean { } function chunkHasUncompressedPubkey(chunk: StackElement): boolean { - if (Buffer.isBuffer(chunk) && chunk.length === 65) { - if (ecc.isPoint(chunk)) return true; - else return false; + if ( + Buffer.isBuffer(chunk) && + chunk.length === 65 && + chunk[0] === 0x04 && + ecc.isPoint(chunk) + ) { + return true; } else { return false; } @@ -69,9 +73,6 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment { const _rchunks = lazy.value(() => { return bscript.decompile(a.redeem!.input!); }) as StackFunction; - const _rochunks = lazy.value(() => { - return bscript.decompile(a.redeem!.output!); - }) as StackFunction; let network = a.network; if (!network) { @@ -202,7 +203,10 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment { throw new TypeError('Witness and redeem.witness mismatch'); if ( (a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) || - (a.redeem.output && _rochunks().some(chunkHasUncompressedPubkey)) + (a.redeem.output && + (bscript.decompile(a.redeem.output) || []).some( + chunkHasUncompressedPubkey, + )) ) { throw new TypeError( 'redeem.input or redeem.output contains uncompressed pubkey',