@@ -5,10 +5,10 @@ const buffer_1 = require('buffer');
5
5
const networks_1 = require ( '../networks' ) ;
6
6
const bscript = require ( '../script' ) ;
7
7
const types_1 = require ( '../types' ) ;
8
+ const ecc_lib_1 = require ( '../ecc_lib' ) ;
8
9
const taprootutils_1 = require ( './taprootutils' ) ;
9
10
const lazy = require ( './lazy' ) ;
10
11
const bech32_1 = require ( 'bech32' ) ;
11
- const verifyecc_1 = require ( './verifyecc' ) ;
12
12
const OPS = bscript . OPS ;
13
13
const TAPROOT_WITNESS_VERSION = 0x01 ;
14
14
const ANNEX_PREFIX = 0x50 ;
@@ -22,11 +22,6 @@ function p2tr(a, opts) {
22
22
)
23
23
throw new TypeError ( 'Not enough data' ) ;
24
24
opts = Object . assign ( { validate : true } , opts || { } ) ;
25
- const _ecc = lazy . value ( ( ) => {
26
- if ( ! opts . eccLib ) throw new Error ( 'ECC Library is missing for p2tr.' ) ;
27
- ( 0 , verifyecc_1 . verifyEcc ) ( opts . eccLib ) ;
28
- return opts . eccLib ;
29
- } ) ;
30
25
( 0 , types_1 . typeforce ) (
31
26
{
32
27
address : types_1 . typeforce . maybe ( types_1 . typeforce . String ) ,
@@ -132,7 +127,7 @@ function p2tr(a, opts) {
132
127
if ( a . output ) return a . output . slice ( 2 ) ;
133
128
if ( a . address ) return _address ( ) . data ;
134
129
if ( o . internalPubkey ) {
135
- const tweakedKey = tweakKey ( o . internalPubkey , o . hash , _ecc ( ) ) ;
130
+ const tweakedKey = tweakKey ( o . internalPubkey , o . hash ) ;
136
131
if ( tweakedKey ) return tweakedKey . x ;
137
132
}
138
133
} ) ;
@@ -157,7 +152,7 @@ function p2tr(a, opts) {
157
152
} ) ;
158
153
const path = ( 0 , taprootutils_1 . findScriptPath ) ( hashTree , leafHash ) ;
159
154
if ( ! path ) return ;
160
- const outputKey = tweakKey ( a . internalPubkey , hashTree . hash , _ecc ( ) ) ;
155
+ const outputKey = tweakKey ( a . internalPubkey , hashTree . hash ) ;
161
156
if ( ! outputKey ) return ;
162
157
const controlBock = buffer_1 . Buffer . concat (
163
158
[
@@ -198,13 +193,13 @@ function p2tr(a, opts) {
198
193
else pubkey = a . output . slice ( 2 ) ;
199
194
}
200
195
if ( a . internalPubkey ) {
201
- const tweakedKey = tweakKey ( a . internalPubkey , o . hash , _ecc ( ) ) ;
196
+ const tweakedKey = tweakKey ( a . internalPubkey , o . hash ) ;
202
197
if ( pubkey . length > 0 && ! pubkey . equals ( tweakedKey . x ) )
203
198
throw new TypeError ( 'Pubkey mismatch' ) ;
204
199
else pubkey = tweakedKey . x ;
205
200
}
206
201
if ( pubkey && pubkey . length ) {
207
- if ( ! _ecc ( ) . isXOnlyPoint ( pubkey ) )
202
+ if ( ! ( 0 , ecc_lib_1 . getEccLib ) ( ) . isXOnlyPoint ( pubkey ) )
208
203
throw new TypeError ( 'Invalid pubkey for p2tr' ) ;
209
204
}
210
205
const hashTree = _hashTree ( ) ;
@@ -267,7 +262,7 @@ function p2tr(a, opts) {
267
262
const internalPubkey = controlBlock . slice ( 1 , 33 ) ;
268
263
if ( a . internalPubkey && ! a . internalPubkey . equals ( internalPubkey ) )
269
264
throw new TypeError ( 'Internal pubkey mismatch' ) ;
270
- if ( ! _ecc ( ) . isXOnlyPoint ( internalPubkey ) )
265
+ if ( ! ( 0 , ecc_lib_1 . getEccLib ) ( ) . isXOnlyPoint ( internalPubkey ) )
271
266
throw new TypeError ( 'Invalid internalPubkey for p2tr witness' ) ;
272
267
const leafVersion = controlBlock [ 0 ] & types_1 . TAPLEAF_VERSION_MASK ;
273
268
const script = witness [ witness . length - 2 ] ;
@@ -279,7 +274,7 @@ function p2tr(a, opts) {
279
274
controlBlock ,
280
275
leafHash ,
281
276
) ;
282
- const outputKey = tweakKey ( internalPubkey , hash , _ecc ( ) ) ;
277
+ const outputKey = tweakKey ( internalPubkey , hash ) ;
283
278
if ( ! outputKey )
284
279
// todo: needs test data
285
280
throw new TypeError ( 'Invalid outputKey for p2tr witness' ) ;
@@ -293,12 +288,12 @@ function p2tr(a, opts) {
293
288
return Object . assign ( o , a ) ;
294
289
}
295
290
exports . p2tr = p2tr ;
296
- function tweakKey ( pubKey , h , eccLib ) {
291
+ function tweakKey ( pubKey , h ) {
297
292
if ( ! buffer_1 . Buffer . isBuffer ( pubKey ) ) return null ;
298
293
if ( pubKey . length !== 32 ) return null ;
299
294
if ( h && h . length !== 32 ) return null ;
300
295
const tweakHash = ( 0 , taprootutils_1 . tapTweakHash ) ( pubKey , h ) ;
301
- const res = eccLib . xOnlyPointAddTweak ( pubKey , tweakHash ) ;
296
+ const res = ( 0 , ecc_lib_1 . getEccLib ) ( ) . xOnlyPointAddTweak ( pubKey , tweakHash ) ;
302
297
if ( ! res || res . xOnlyPubkey === null ) return null ;
303
298
return {
304
299
parity : res . parity ,
0 commit comments