Skip to content

Commit 4573e6c

Browse files
committed
chore: add docs, simplify code
1 parent f61371c commit 4573e6c

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

src/payments/taprootutils.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ export interface HashTree {
66
left?: HashTree;
77
right?: HashTree;
88
}
9-
export declare function toHashTree(scripts: TaprootLeaf[]): HashTree;
9+
/**
10+
* Build the hash tree from the scripts binary tree.
11+
* The binary tree can be balanced or not.
12+
* @param scriptsTree - is a list representing a binary tree where an element can be:
13+
* - a taproot leaf [(output, version)], or
14+
* - a pair of two taproot leafs [(output, version), (output, version)], or
15+
* - one taproot leaf and a list of elements
16+
*/
17+
export declare function toHashTree(scriptsTree: TaprootLeaf[]): HashTree;
18+
/**
19+
* Given a MAST tree, it finds the path of a particular hash.
20+
* @param node - the root of the tree
21+
* @param hash - the hash to search for
22+
* @returns - and array of hashes representing the path, or an empty array if no pat is found
23+
*/
1024
export declare function findScriptPath(node: HashTree, hash: Buffer): Buffer[];
1125
export declare function tapLeafHash(script: Buffer, version?: number): Buffer;
1226
export declare function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer;

src/payments/taprootutils.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ function rootHashFromPath(controlBlock, tapLeafMsg) {
2323
return k[m];
2424
}
2525
exports.rootHashFromPath = rootHashFromPath;
26-
function toHashTree(scripts) {
27-
if (scripts.length === 1) {
28-
const script = scripts[0];
26+
/**
27+
* Build the hash tree from the scripts binary tree.
28+
* The binary tree can be balanced or not.
29+
* @param scriptsTree - is a list representing a binary tree where an element can be:
30+
* - a taproot leaf [(output, version)], or
31+
* - a pair of two taproot leafs [(output, version), (output, version)], or
32+
* - one taproot leaf and a list of elements
33+
*/
34+
function toHashTree(scriptsTree) {
35+
if (scriptsTree.length === 1) {
36+
const script = scriptsTree[0];
2937
if (Array.isArray(script)) {
3038
return toHashTree(script);
3139
}
@@ -36,10 +44,8 @@ function toHashTree(scripts) {
3644
hash: tapLeafHash(script.output, script.version),
3745
};
3846
}
39-
// todo: this is a binary tree, use zero an one index
40-
const half = Math.trunc(scripts.length / 2);
41-
const left = toHashTree(scripts.slice(0, half));
42-
const right = toHashTree(scripts.slice(half));
47+
const left = toHashTree([scriptsTree[0]]);
48+
const right = toHashTree([scriptsTree[1]]);
4349
let leftHash = left.hash;
4450
let rightHash = right.hash;
4551
if (leftHash.compare(rightHash) === 1)
@@ -51,6 +57,12 @@ function toHashTree(scripts) {
5157
};
5258
}
5359
exports.toHashTree = toHashTree;
60+
/**
61+
* Given a MAST tree, it finds the path of a particular hash.
62+
* @param node - the root of the tree
63+
* @param hash - the hash to search for
64+
* @returns - and array of hashes representing the path, or an empty array if no pat is found
65+
*/
5466
function findScriptPath(node, hash) {
5567
if (node.left) {
5668
if (node.left.hash.equals(hash)) return node.right ? [node.right.hash] : [];

ts_src/payments/taprootutils.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ export interface HashTree {
3636
right?: HashTree;
3737
}
3838

39-
export function toHashTree(scripts: TaprootLeaf[]): HashTree {
40-
if (scripts.length === 1) {
41-
const script = scripts[0];
39+
/**
40+
* Build the hash tree from the scripts binary tree.
41+
* The binary tree can be balanced or not.
42+
* @param scriptsTree - is a list representing a binary tree where an element can be:
43+
* - a taproot leaf [(output, version)], or
44+
* - a pair of two taproot leafs [(output, version), (output, version)], or
45+
* - one taproot leaf and a list of elements
46+
*/
47+
export function toHashTree(scriptsTree: TaprootLeaf[]): HashTree {
48+
if (scriptsTree.length === 1) {
49+
const script = scriptsTree[0];
4250
if (Array.isArray(script)) {
4351
return toHashTree(script);
4452
}
@@ -50,10 +58,9 @@ export function toHashTree(scripts: TaprootLeaf[]): HashTree {
5058
hash: tapLeafHash(script.output, script.version),
5159
};
5260
}
53-
// todo: this is a binary tree, use zero an one index
54-
const half = Math.trunc(scripts.length / 2);
55-
const left = toHashTree(scripts.slice(0, half));
56-
const right = toHashTree(scripts.slice(half));
61+
62+
const left = toHashTree([scriptsTree[0]]);
63+
const right = toHashTree([scriptsTree[1]]);
5764

5865
let leftHash = left.hash;
5966
let rightHash = right.hash;
@@ -67,6 +74,12 @@ export function toHashTree(scripts: TaprootLeaf[]): HashTree {
6774
};
6875
}
6976

77+
/**
78+
* Given a MAST tree, it finds the path of a particular hash.
79+
* @param node - the root of the tree
80+
* @param hash - the hash to search for
81+
* @returns - and array of hashes representing the path, or an empty array if no pat is found
82+
*/
7083
export function findScriptPath(node: HashTree, hash: Buffer): Buffer[] {
7184
if (node.left) {
7285
if (node.left.hash.equals(hash)) return node.right ? [node.right.hash] : [];

0 commit comments

Comments
 (0)