Skip to content

Commit 2f55aad

Browse files
committed
refactor: rename rootHash to rootHashFromPath and computeMastRoot to rootHashFromTree
1 parent 2feff5d commit 2f55aad

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

src/payments/p2tr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ function p2tr(a, opts) {
7272
});
7373
lazy.prop(o, 'hash', () => {
7474
if (a.hash) return a.hash;
75-
if (a.scriptsTree) return (0, taproot_1.computeMastRoot)(a.scriptsTree);
75+
if (a.scriptsTree) return (0, taproot_1.rootHashFromTree)(a.scriptsTree);
7676
const w = _witness();
7777
if (w && w.length > 1) {
7878
const controlBlock = w[w.length - 1];
7979
const leafVersion = controlBlock[0] & 0b11111110;
8080
const script = w[w.length - 2];
8181
const tapLeafHash = (0, taproot_1.leafHash)(script, leafVersion);
82-
return (0, taproot_1.rootHash)(controlBlock, tapLeafHash);
82+
return (0, taproot_1.rootHashFromPath)(controlBlock, tapLeafHash);
8383
}
8484
return null;
8585
});
@@ -155,7 +155,7 @@ function p2tr(a, opts) {
155155
throw new TypeError('Invalid pubkey for p2tr');
156156
}
157157
if (a.hash && a.scriptsTree) {
158-
const hash = (0, taproot_1.computeMastRoot)(a.scriptsTree);
158+
const hash = (0, taproot_1.rootHashFromTree)(a.scriptsTree);
159159
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
160160
}
161161
// todo: review cache
@@ -195,7 +195,7 @@ function p2tr(a, opts) {
195195
const leafVersion = controlBlock[0] & 0b11111110;
196196
const script = witness[witness.length - 2];
197197
const tapLeafHash = (0, taproot_1.leafHash)(script, leafVersion);
198-
const hash = (0, taproot_1.rootHash)(controlBlock, tapLeafHash);
198+
const hash = (0, taproot_1.rootHashFromPath)(controlBlock, tapLeafHash);
199199
const outputKey = (0, taproot_1.tweakKey)(internalPubkey, hash);
200200
if (!outputKey)
201201
// todo: needs test data

src/taproot.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { TweakedPublicKey } from './types';
33
export declare function liftX(buffer: Buffer): Buffer | null;
44
export declare function tweakKey(pubKey: Buffer, h: Buffer | undefined): TweakedPublicKey | null;
55
export declare function leafHash(script: Buffer, version: number): Buffer;
6-
export declare function rootHash(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer;
7-
export declare function computeMastRoot(scripts: any): Buffer;
6+
export declare function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer;
7+
export declare function rootHashFromTree(scripts: any): Buffer;

src/taproot.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.computeMastRoot = exports.rootHash = exports.leafHash = exports.tweakKey = exports.liftX = void 0;
3+
exports.rootHashFromTree = exports.rootHashFromPath = exports.leafHash = exports.tweakKey = exports.liftX = void 0;
44
const buffer_1 = require('buffer');
55
const BN = require('bn.js');
66
const bcrypto = require('./crypto');
@@ -70,7 +70,7 @@ function leafHash(script, version) {
7070
]);
7171
}
7272
exports.leafHash = leafHash;
73-
function rootHash(controlBlock, tapLeafMsg) {
73+
function rootHashFromPath(controlBlock, tapLeafMsg) {
7474
const k = [];
7575
const e = [];
7676
const m = (controlBlock.length - 33) / 32;
@@ -91,13 +91,13 @@ function rootHash(controlBlock, tapLeafMsg) {
9191
}
9292
return k[m];
9393
}
94-
exports.rootHash = rootHash;
94+
exports.rootHashFromPath = rootHashFromPath;
9595
// todo: solve any[]
96-
function computeMastRoot(scripts) {
96+
function rootHashFromTree(scripts) {
9797
if (scripts.length === 1) {
9898
const script = scripts[0];
9999
if (Array.isArray(script)) {
100-
return computeMastRoot(script);
100+
return rootHashFromTree(script);
101101
}
102102
script.version = script.version || LEAF_VERSION_TAPSCRIPT;
103103
if ((script.version & 1) !== 0) throw new Error('Invalid script version'); // todo typedef error
@@ -113,16 +113,16 @@ function computeMastRoot(scripts) {
113113
}
114114
// todo: this is a binary tree, use zero an one index
115115
const half = Math.trunc(scripts.length / 2);
116-
let leftHash = computeMastRoot(scripts.slice(0, half));
117-
let rightHash = computeMastRoot(scripts.slice(half));
116+
let leftHash = rootHashFromTree(scripts.slice(0, half));
117+
let rightHash = rootHashFromTree(scripts.slice(half));
118118
if (leftHash.compare(rightHash) === 1)
119119
[leftHash, rightHash] = [rightHash, leftHash];
120120
return bcrypto.taggedHash(
121121
TAP_BRANCH_TAG,
122122
buffer_1.Buffer.concat([leftHash, rightHash]),
123123
);
124124
}
125-
exports.computeMastRoot = computeMastRoot;
125+
exports.rootHashFromTree = rootHashFromTree;
126126
function serializeScript(s) {
127127
const varintLen = varuint.encodingLength(s.length);
128128
const buffer = buffer_1.Buffer.allocUnsafe(varintLen); // better

ts_src/payments/p2tr.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { bitcoin as BITCOIN_NETWORK } from '../networks';
22
import * as bscript from '../script';
33
import { typeforce as typef } from '../types';
44
import {
5-
computeMastRoot,
5+
rootHashFromTree,
6+
rootHashFromPath,
67
leafHash,
7-
rootHash,
88
tweakKey,
99
liftX,
1010
} from '../taproot';
@@ -83,14 +83,14 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
8383

8484
lazy.prop(o, 'hash', () => {
8585
if (a.hash) return a.hash;
86-
if (a.scriptsTree) return computeMastRoot(a.scriptsTree);
86+
if (a.scriptsTree) return rootHashFromTree(a.scriptsTree);
8787
const w = _witness();
8888
if (w && w.length > 1) {
8989
const controlBlock = w[w.length - 1];
9090
const leafVersion = controlBlock[0] & 0b11111110;
9191
const script = w[w.length - 2];
9292
const tapLeafHash = leafHash(script, leafVersion);
93-
return rootHash(controlBlock, tapLeafHash);
93+
return rootHashFromPath(controlBlock, tapLeafHash);
9494
}
9595
return null;
9696
});
@@ -172,11 +172,10 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
172172
}
173173

174174
if (a.hash && a.scriptsTree) {
175-
const hash = computeMastRoot(a.scriptsTree);
175+
const hash = rootHashFromTree(a.scriptsTree);
176176
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
177177
}
178178

179-
// todo: review cache
180179
const witness = _witness();
181180

182181
if (witness && witness.length) {
@@ -220,7 +219,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
220219
const script = witness[witness.length - 2];
221220

222221
const tapLeafHash = leafHash(script, leafVersion);
223-
const hash = rootHash(controlBlock, tapLeafHash);
222+
const hash = rootHashFromPath(controlBlock, tapLeafHash);
224223

225224
const outputKey = tweakKey(internalPubkey, hash);
226225
if (!outputKey)

ts_src/taproot.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function leafHash(script: Buffer, version: number): Buffer {
8282
return NBuffer.concat([NBuffer.from([version]), serializeScript(script)]);
8383
}
8484

85-
export function rootHash(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer {
85+
export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer {
8686
const k = [];
8787
const e = [];
8888

@@ -108,11 +108,11 @@ export function rootHash(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer {
108108
}
109109

110110
// todo: solve any[]
111-
export function computeMastRoot(scripts: any): Buffer {
111+
export function rootHashFromTree(scripts: any): Buffer {
112112
if (scripts.length === 1) {
113113
const script = scripts[0];
114114
if (Array.isArray(script)) {
115-
return computeMastRoot(script);
115+
return rootHashFromTree(script);
116116
}
117117
script.version = script.version || LEAF_VERSION_TAPSCRIPT;
118118
if ((script.version & 1) !== 0) throw new Error('Invalid script version'); // todo typedef error
@@ -128,8 +128,8 @@ export function computeMastRoot(scripts: any): Buffer {
128128
}
129129
// todo: this is a binary tree, use zero an one index
130130
const half = Math.trunc(scripts.length / 2);
131-
let leftHash = computeMastRoot(scripts.slice(0, half));
132-
let rightHash = computeMastRoot(scripts.slice(half));
131+
let leftHash = rootHashFromTree(scripts.slice(0, half));
132+
let rightHash = rootHashFromTree(scripts.slice(half));
133133

134134
if (leftHash.compare(rightHash) === 1)
135135
[leftHash, rightHash] = [rightHash, leftHash];

0 commit comments

Comments
 (0)