11'use strict' ;
22Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
3- exports . tapTweakHash = exports . tapLeafHash = exports . findScriptPath = exports . isTapTree = exports . toHashTree = exports . rootHashFromPath = exports . LEAF_VERSION_TAPSCRIPT = void 0 ;
3+ exports . tapTweakHash = exports . tapLeafHash = exports . findScriptPath = exports . toHashTree = exports . rootHashFromPath = exports . LEAF_VERSION_TAPSCRIPT = void 0 ;
44const buffer_1 = require ( 'buffer' ) ;
55const bcrypto = require ( '../crypto' ) ;
66const bufferutils_1 = require ( '../bufferutils' ) ;
7+ const types_1 = require ( '../types' ) ;
78const TAP_LEAF_TAG = 'TapLeaf' ;
89const TAP_BRANCH_TAG = 'TapBranch' ;
910const TAP_TWEAK_TAG = 'TapTweak' ;
@@ -32,48 +33,18 @@ exports.rootHashFromPath = rootHashFromPath;
3233 * - one taproot leaf and a list of elements
3334 */
3435function toHashTree ( scriptTree ) {
35- if ( scriptTree . length === 1 ) {
36- const script = scriptTree [ 0 ] ;
37- if ( Array . isArray ( script ) ) {
38- return toHashTree ( script ) ;
39- }
40- script . version = script . version || exports . LEAF_VERSION_TAPSCRIPT ;
41- if ( ( script . version & 1 ) !== 0 )
42- throw new TypeError ( 'Invalid script version' ) ;
43- return {
44- hash : tapLeafHash ( script . output , script . version ) ,
45- } ;
46- }
47- let left = toHashTree ( [ scriptTree [ 0 ] ] ) ;
48- let right = toHashTree ( [ scriptTree [ 1 ] ] ) ;
49- if ( left . hash . compare ( right . hash ) === 1 ) [ left , right ] = [ right , left ] ;
36+ if ( ( 0 , types_1 . isTapleaf ) ( scriptTree ) )
37+ return { hash : tapLeafHash ( scriptTree ) } ;
38+ const hashes = [ toHashTree ( scriptTree [ 0 ] ) , toHashTree ( scriptTree [ 1 ] ) ] ;
39+ hashes . sort ( ( a , b ) => a . hash . compare ( b . hash ) ) ;
40+ const [ left , right ] = hashes ;
5041 return {
5142 hash : tapBranchHash ( left . hash , right . hash ) ,
5243 left,
5344 right,
5445 } ;
5546}
5647exports . toHashTree = toHashTree ;
57- /**
58- * Check if the tree is a binary tree with leafs of type Tapleaf
59- */
60- function isTapTree ( scriptTree ) {
61- if ( scriptTree . length > 2 ) return false ;
62- if ( scriptTree . length === 1 ) {
63- const script = scriptTree [ 0 ] ;
64- if ( Array . isArray ( script ) ) {
65- return isTapTree ( script ) ;
66- }
67- if ( ! script . output ) return false ;
68- script . version = script . version || exports . LEAF_VERSION_TAPSCRIPT ;
69- if ( ( script . version & 1 ) !== 0 ) return false ;
70- return true ;
71- }
72- if ( ! isTapTree ( [ scriptTree [ 0 ] ] ) ) return false ;
73- if ( ! isTapTree ( [ scriptTree [ 1 ] ] ) ) return false ;
74- return true ;
75- }
76- exports . isTapTree = isTapTree ;
7748/**
7849 * Given a MAST tree, it finds the path of a particular hash.
7950 * @param node - the root of the tree
@@ -96,13 +67,13 @@ function findScriptPath(node, hash) {
9667 return [ ] ;
9768}
9869exports . findScriptPath = findScriptPath ;
99- function tapLeafHash ( script , version ) {
100- version = version || exports . LEAF_VERSION_TAPSCRIPT ;
70+ function tapLeafHash ( leaf ) {
71+ const version = leaf . version || exports . LEAF_VERSION_TAPSCRIPT ;
10172 return bcrypto . taggedHash (
10273 TAP_LEAF_TAG ,
10374 buffer_1 . Buffer . concat ( [
10475 buffer_1 . Buffer . from ( [ version ] ) ,
105- serializeScript ( script ) ,
76+ serializeScript ( leaf . output ) ,
10677 ] ) ,
10778 ) ;
10879}
0 commit comments