@@ -6,6 +6,7 @@ const bscript = require('../script');
66const  lazy  =  require ( './lazy' ) ; 
77const  typef  =  require ( 'typeforce' ) ; 
88const  OPS  =  bscript . OPS ; 
9+ const  ecc  =  require ( 'tiny-secp256k1' ) ; 
910const  bech32  =  require ( 'bech32' ) ; 
1011const  EMPTY_BUFFER  =  Buffer . alloc ( 0 ) ; 
1112function  stacksEqual ( a ,  b )  { 
@@ -14,6 +15,18 @@ function stacksEqual(a, b) {
1415    return  x . equals ( b [ i ] ) ; 
1516  } ) ; 
1617} 
18+ function  chunkHasUncompressedPubkey ( chunk )  { 
19+   if  ( 
20+     Buffer . isBuffer ( chunk )  && 
21+     chunk . length  ===  65  && 
22+     chunk [ 0 ]  ===  0x04  && 
23+     ecc . isPoint ( chunk ) 
24+   )  { 
25+     return  true ; 
26+   }  else  { 
27+     return  false ; 
28+   } 
29+ } 
1730// input: <> 
1831// witness: [redeemScriptSig ...] {redeemScript} 
1932// output: OP_0 {sha256(redeemScript)} 
@@ -166,14 +179,27 @@ function p2wsh(a, opts) {
166179        ! stacksEqual ( a . witness ,  a . redeem . witness ) 
167180      ) 
168181        throw  new  TypeError ( 'Witness and redeem.witness mismatch' ) ; 
182+       if  ( 
183+         ( a . redeem . input  &&  _rchunks ( ) . some ( chunkHasUncompressedPubkey ) )  || 
184+         ( a . redeem . output  && 
185+           ( bscript . decompile ( a . redeem . output )  ||  [ ] ) . some ( 
186+             chunkHasUncompressedPubkey , 
187+           ) ) 
188+       )  { 
189+         throw  new  TypeError ( 
190+           'redeem.input or redeem.output contains uncompressed pubkey' , 
191+         ) ; 
192+       } 
169193    } 
170-     if  ( a . witness )  { 
194+     if  ( a . witness  &&  a . witness . length  >  0 )  { 
195+       const  wScript  =  a . witness [ a . witness . length  -  1 ] ; 
196+       if  ( a . redeem  &&  a . redeem . output  &&  ! a . redeem . output . equals ( wScript ) ) 
197+         throw  new  TypeError ( 'Witness and redeem.output mismatch' ) ; 
171198      if  ( 
172-         a . redeem  && 
173-         a . redeem . output  && 
174-         ! a . redeem . output . equals ( a . witness [ a . witness . length  -  1 ] ) 
199+         a . witness . some ( chunkHasUncompressedPubkey )  || 
200+         ( bscript . decompile ( wScript )  ||  [ ] ) . some ( chunkHasUncompressedPubkey ) 
175201      ) 
176-         throw  new  TypeError ( 'Witness and redeem.output mismatch ' ) ; 
202+         throw  new  TypeError ( 'Witness contains uncompressed pubkey ' ) ; 
177203    } 
178204  } 
179205  return  Object . assign ( o ,  a ) ; 
0 commit comments