@@ -4,7 +4,13 @@ const BN = require('bn.js');
4
4
import * as bcrypto from './crypto' ;
5
5
// todo: use varuint-bitcoin??
6
6
import * as varuint from 'bip174/src/lib/converter/varint' ;
7
- import { TweakedPublicKey , TaprootLeaf , ZERO32 , EC_P , GROUP_ORDER } from './types' ;
7
+ import {
8
+ TweakedPublicKey ,
9
+ TaprootLeaf ,
10
+ ZERO32 ,
11
+ EC_P ,
12
+ GROUP_ORDER ,
13
+ } from './types' ;
8
14
9
15
// todo: !!!Temp, to be replaced. Only works because bip32 has it as dependecy. Linting will fail.
10
16
const ecc = require ( 'tiny-secp256k1' ) ;
@@ -78,7 +84,10 @@ export function tweakKey(
78
84
} ;
79
85
}
80
86
81
- export function rootHashFromPath ( controlBlock : Buffer , tapLeafMsg : Buffer ) : Buffer {
87
+ export function rootHashFromPath (
88
+ controlBlock : Buffer ,
89
+ tapLeafMsg : Buffer ,
90
+ ) : Buffer {
82
91
const k = [ tapLeafMsg ] ;
83
92
const e = [ ] ;
84
93
@@ -97,25 +106,24 @@ export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buff
97
106
}
98
107
99
108
export interface HashTree {
100
- hash : Buffer
101
- left ?: HashTree
102
- right ?: HashTree
109
+ hash : Buffer ;
110
+ left ?: HashTree ;
111
+ right ?: HashTree ;
103
112
}
104
113
105
-
106
114
export function toHashTree ( scripts : TaprootLeaf [ ] ) : HashTree {
107
115
if ( scripts . length === 1 ) {
108
116
const script = scripts [ 0 ] ;
109
117
if ( Array . isArray ( script ) ) {
110
118
return toHashTree ( script ) ;
111
119
}
112
120
script . version = script . version || LEAF_VERSION_TAPSCRIPT ;
113
- if ( ( script . version & 1 ) !== 0 ) throw new TypeError ( 'Invalid script version' ) ;
121
+ if ( ( script . version & 1 ) !== 0 )
122
+ throw new TypeError ( 'Invalid script version' ) ;
114
123
115
124
return {
116
- hash : tapLeafHash ( script . output , script . version )
117
- }
118
-
125
+ hash : tapLeafHash ( script . output , script . version ) ,
126
+ } ;
119
127
}
120
128
// todo: this is a binary tree, use zero an one index
121
129
const half = Math . trunc ( scripts . length / 2 ) ;
@@ -130,38 +138,38 @@ export function toHashTree(scripts: TaprootLeaf[]): HashTree {
130
138
return {
131
139
hash : tapBranchHash ( leftHash , rightHash ) ,
132
140
left,
133
- right
134
- }
141
+ right,
142
+ } ;
135
143
}
136
144
137
145
export function findScriptPath ( node : HashTree , hash : Buffer ) : Buffer [ ] {
138
146
if ( node . left ) {
139
- if ( node . left . hash . equals ( hash ) )
140
- return node . right ? [ node . right . hash ] : [ ]
141
- const leftPath = findScriptPath ( node . left , hash )
147
+ if ( node . left . hash . equals ( hash ) ) return node . right ? [ node . right . hash ] : [ ] ;
148
+ const leftPath = findScriptPath ( node . left , hash ) ;
142
149
if ( leftPath . length )
143
- return node . right ? [ node . right . hash ] . concat ( leftPath ) : leftPath
150
+ return node . right ? [ node . right . hash ] . concat ( leftPath ) : leftPath ;
144
151
}
145
152
146
153
if ( node . right ) {
147
- if ( node . right . hash . equals ( hash ) )
148
- return node . left ? [ node . left . hash ] : [ ]
149
- const rightPath = findScriptPath ( node . right , hash )
150
- if ( rightPath . length ) { }
151
- return node . left ? [ node . left . hash ] . concat ( rightPath ) : rightPath
154
+ if ( node . right . hash . equals ( hash ) ) return node . left ? [ node . left . hash ] : [ ] ;
155
+ const rightPath = findScriptPath ( node . right , hash ) ;
156
+ if ( rightPath . length )
157
+ return node . left ? [ node . left . hash ] . concat ( rightPath ) : rightPath ;
152
158
}
153
159
154
- return [ ]
155
-
160
+ return [ ] ;
156
161
}
157
162
158
163
export function tapLeafHash ( script : Buffer , version ?: number ) : Buffer {
159
- version = version || LEAF_VERSION_TAPSCRIPT
160
- return bcrypto . taggedHash ( TAP_LEAF_TAG , NBuffer . concat ( [ NBuffer . from ( [ version ] ) , serializeScript ( script ) ] ) ) ;
164
+ version = version || LEAF_VERSION_TAPSCRIPT ;
165
+ return bcrypto . taggedHash (
166
+ TAP_LEAF_TAG ,
167
+ NBuffer . concat ( [ NBuffer . from ( [ version ] ) , serializeScript ( script ) ] ) ,
168
+ ) ;
161
169
}
162
170
163
171
function tapBranchHash ( a : Buffer , b : Buffer ) : Buffer {
164
- return bcrypto . taggedHash ( TAP_BRANCH_TAG , NBuffer . concat ( [ a , b ] ) , ) ;
172
+ return bcrypto . taggedHash ( TAP_BRANCH_TAG , NBuffer . concat ( [ a , b ] ) ) ;
165
173
}
166
174
167
175
function serializeScript ( s : Buffer ) : Buffer {
0 commit comments