Skip to content

Commit ef5457e

Browse files
committed
Improve comments and code clarity
1 parent 5610d45 commit ef5457e

File tree

7 files changed

+57
-48
lines changed

7 files changed

+57
-48
lines changed

src/payments/p2tr.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function p2tr(a, opts) {
163163
[
164164
buffer_1.Buffer.from([o.redeemVersion | outputKey.parity]),
165165
a.internalPubkey,
166-
].concat(path.reverse()),
166+
].concat(path),
167167
);
168168
return [a.redeem.output, controlBock];
169169
}

src/payments/taprootutils.d.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ interface HashBranch {
1010
left: HashTree;
1111
right: HashTree;
1212
}
13+
/**
14+
* Binary tree representing leaf, branch, and root node hashes of a Taptree.
15+
* Each node contains a hash, and potentially left and right branch hashes.
16+
* This tree is used for 2 purposes: Providing the root hash for tweaking,
17+
* and calculating merkle inclusion proofs when constructing a control block.
18+
*/
1319
export declare type HashTree = HashLeaf | HashBranch;
1420
/**
15-
* Build the hash tree from the scripts binary tree.
16-
* The binary tree can be balanced or not.
17-
* @param scriptTree - is a list representing a binary tree where an element can be:
18-
* - a taproot leaf [(output, version)], or
19-
* - a pair of two taproot leafs [(output, version), (output, version)], or
20-
* - one taproot leaf and a list of elements
21+
* Build a hash tree of merkle nodes from the scripts binary tree.
22+
* @param scriptTree - the tree of scripts to pairwise hash.
2123
*/
2224
export declare function toHashTree(scriptTree: Taptree): HashTree;
2325
/**
24-
* Given a MAST tree, it finds the path of a particular hash.
26+
* Given a HashTree, finds the path from a particular hash to the root.
2527
* @param node - the root of the tree
2628
* @param hash - the hash to search for
27-
* @returns - and array of hashes representing the path, undefined if no path is found
29+
* @returns - array of sibling hashes, from leaf (inclusive) to root
30+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
31+
* path is found
2832
*/
2933
export declare function findScriptPath(node: HashTree, hash: Buffer): Buffer[] | undefined;
3034
export declare function tapleafHash(leaf: Tapleaf): Buffer;

src/payments/taprootutils.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ function rootHashFromPath(controlBlock, leafHash) {
2222
exports.rootHashFromPath = rootHashFromPath;
2323
const isHashBranch = ht => 'left' in ht && 'right' in ht;
2424
/**
25-
* Build the hash tree from the scripts binary tree.
26-
* The binary tree can be balanced or not.
27-
* @param scriptTree - is a list representing a binary tree where an element can be:
28-
* - a taproot leaf [(output, version)], or
29-
* - a pair of two taproot leafs [(output, version), (output, version)], or
30-
* - one taproot leaf and a list of elements
25+
* Build a hash tree of merkle nodes from the scripts binary tree.
26+
* @param scriptTree - the tree of scripts to pairwise hash.
3127
*/
3228
function toHashTree(scriptTree) {
3329
if ((0, types_1.isTapleaf)(scriptTree))
@@ -43,23 +39,22 @@ function toHashTree(scriptTree) {
4339
}
4440
exports.toHashTree = toHashTree;
4541
/**
46-
* Given a MAST tree, it finds the path of a particular hash.
42+
* Given a HashTree, finds the path from a particular hash to the root.
4743
* @param node - the root of the tree
4844
* @param hash - the hash to search for
49-
* @returns - and array of hashes representing the path, undefined if no path is found
45+
* @returns - array of sibling hashes, from leaf (inclusive) to root
46+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
47+
* path is found
5048
*/
5149
function findScriptPath(node, hash) {
52-
if (!isHashBranch(node)) {
53-
if (node.hash.equals(hash)) {
54-
return [];
55-
} else {
56-
return undefined;
57-
}
50+
if (isHashBranch(node)) {
51+
const leftPath = findScriptPath(node.left, hash);
52+
if (leftPath !== undefined) return [...leftPath, node.right.hash];
53+
const rightPath = findScriptPath(node.right, hash);
54+
if (rightPath !== undefined) return [...rightPath, node.left.hash];
55+
} else if (node.hash.equals(hash)) {
56+
return [];
5857
}
59-
const leftPath = findScriptPath(node.left, hash);
60-
if (leftPath !== undefined) return [node.right.hash, ...leftPath];
61-
const rightPath = findScriptPath(node.right, hash);
62-
if (rightPath !== undefined) return [node.left.hash, ...rightPath];
6358
return undefined;
6459
}
6560
exports.findScriptPath = findScriptPath;

src/types.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export interface Tapleaf {
2020
}
2121
export declare const TAPLEAF_VERSION_MASK = 254;
2222
export declare function isTapleaf(o: any): o is Tapleaf;
23+
/**
24+
* Binary tree repsenting script path spends for a Taproot input.
25+
* Each node is either a single Tapleaf, or a pair of Tapleaf | Taptree.
26+
* The tree has no balancing requirements.
27+
*/
2328
export declare type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
2429
export declare function isTaptree(scriptTree: any): scriptTree is Taptree;
2530
export interface TinySecp256k1Interface {

ts_src/payments/p2tr.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
181181
[
182182
NBuffer.from([o.redeemVersion! | outputKey.parity]),
183183
a.internalPubkey,
184-
].concat(path.reverse()),
184+
].concat(path),
185185
);
186186
return [a.redeem.output, controlBock];
187187
}

ts_src/payments/taprootutils.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ interface HashBranch {
3838
const isHashBranch = (ht: HashTree): ht is HashBranch =>
3939
'left' in ht && 'right' in ht;
4040

41+
/**
42+
* Binary tree representing leaf, branch, and root node hashes of a Taptree.
43+
* Each node contains a hash, and potentially left and right branch hashes.
44+
* This tree is used for 2 purposes: Providing the root hash for tweaking,
45+
* and calculating merkle inclusion proofs when constructing a control block.
46+
*/
4147
export type HashTree = HashLeaf | HashBranch;
4248

4349
/**
44-
* Build the hash tree from the scripts binary tree.
45-
* The binary tree can be balanced or not.
46-
* @param scriptTree - is a list representing a binary tree where an element can be:
47-
* - a taproot leaf [(output, version)], or
48-
* - a pair of two taproot leafs [(output, version), (output, version)], or
49-
* - one taproot leaf and a list of elements
50+
* Build a hash tree of merkle nodes from the scripts binary tree.
51+
* @param scriptTree - the tree of scripts to pairwise hash.
5052
*/
5153
export function toHashTree(scriptTree: Taptree): HashTree {
5254
if (isTapleaf(scriptTree)) return { hash: tapleafHash(scriptTree) };
@@ -63,29 +65,27 @@ export function toHashTree(scriptTree: Taptree): HashTree {
6365
}
6466

6567
/**
66-
* Given a MAST tree, it finds the path of a particular hash.
68+
* Given a HashTree, finds the path from a particular hash to the root.
6769
* @param node - the root of the tree
6870
* @param hash - the hash to search for
69-
* @returns - and array of hashes representing the path, undefined if no path is found
71+
* @returns - array of sibling hashes, from leaf (inclusive) to root
72+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
73+
* path is found
7074
*/
7175
export function findScriptPath(
7276
node: HashTree,
7377
hash: Buffer,
7478
): Buffer[] | undefined {
75-
if (!isHashBranch(node)) {
76-
if (node.hash.equals(hash)) {
77-
return [];
78-
} else {
79-
return undefined;
80-
}
79+
if (isHashBranch(node)) {
80+
const leftPath = findScriptPath(node.left, hash);
81+
if (leftPath !== undefined) return [...leftPath, node.right.hash];
82+
83+
const rightPath = findScriptPath(node.right, hash);
84+
if (rightPath !== undefined) return [...rightPath, node.left.hash];
85+
} else if (node.hash.equals(hash)) {
86+
return [];
8187
}
8288

83-
const leftPath = findScriptPath(node.left, hash);
84-
if (leftPath !== undefined) return [node.right.hash, ...leftPath];
85-
86-
const rightPath = findScriptPath(node.right, hash);
87-
if (rightPath !== undefined) return [node.left.hash, ...rightPath];
88-
8989
return undefined;
9090
}
9191

ts_src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ export function isTapleaf(o: any): o is Tapleaf {
8686
return true;
8787
}
8888

89+
/**
90+
* Binary tree repsenting script path spends for a Taproot input.
91+
* Each node is either a single Tapleaf, or a pair of Tapleaf | Taptree.
92+
* The tree has no balancing requirements.
93+
*/
8994
export type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
9095

9196
export function isTaptree(scriptTree: any): scriptTree is Taptree {

0 commit comments

Comments
 (0)