11import * as bcrypto from '../crypto' ;
22import { bitcoin as BITCOIN_NETWORK } from '../networks' ;
33import * as bscript from '../script' ;
4- import { Payment , PaymentOpts , StackFunction } from './index' ;
4+ import { Payment , PaymentOpts , StackElement , StackFunction } from './index' ;
55import * as lazy from './lazy' ;
66const typef = require ( 'typeforce' ) ;
77const OPS = bscript . OPS ;
8+ const ecc = require ( 'tiny-secp256k1' ) ;
89
910const bech32 = require ( 'bech32' ) ;
1011
@@ -18,6 +19,19 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
1819 } ) ;
1920}
2021
22+ function chunkHasUncompressedPubkey ( chunk : StackElement ) : boolean {
23+ if (
24+ Buffer . isBuffer ( chunk ) &&
25+ chunk . length === 65 &&
26+ chunk [ 0 ] === 0x04 &&
27+ ecc . isPoint ( chunk )
28+ ) {
29+ return true ;
30+ } else {
31+ return false ;
32+ }
33+ }
34+
2135// input: <>
2236// witness: [redeemScriptSig ...] {redeemScript}
2337// output: OP_0 {sha256(redeemScript)}
@@ -187,15 +201,28 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
187201 ! stacksEqual ( a . witness , a . redeem . witness )
188202 )
189203 throw new TypeError ( 'Witness and redeem.witness mismatch' ) ;
204+ if (
205+ ( a . redeem . input && _rchunks ( ) . some ( chunkHasUncompressedPubkey ) ) ||
206+ ( a . redeem . output &&
207+ ( bscript . decompile ( a . redeem . output ) || [ ] ) . some (
208+ chunkHasUncompressedPubkey ,
209+ ) )
210+ ) {
211+ throw new TypeError (
212+ 'redeem.input or redeem.output contains uncompressed pubkey' ,
213+ ) ;
214+ }
190215 }
191216
192- if ( a . witness ) {
217+ if ( a . witness && a . witness . length > 0 ) {
218+ const wScript = a . witness [ a . witness . length - 1 ] ;
219+ if ( a . redeem && a . redeem . output && ! a . redeem . output . equals ( wScript ) )
220+ throw new TypeError ( 'Witness and redeem.output mismatch' ) ;
193221 if (
194- a . redeem &&
195- a . redeem . output &&
196- ! a . redeem . output . equals ( a . witness [ a . witness . length - 1 ] )
222+ a . witness . some ( chunkHasUncompressedPubkey ) ||
223+ ( bscript . decompile ( wScript ) || [ ] ) . some ( chunkHasUncompressedPubkey )
197224 )
198- throw new TypeError ( 'Witness and redeem.output mismatch ' ) ;
225+ throw new TypeError ( 'Witness contains uncompressed pubkey ' ) ;
199226 }
200227 }
201228
0 commit comments