Skip to content

Commit f7d01b8

Browse files
committed
refactor: extract tapBranchHash() rename leafHash() to tapBranchHash()
1 parent 2a4e64b commit f7d01b8

File tree

5 files changed

+41
-54
lines changed

5 files changed

+41
-54
lines changed

src/payments/p2tr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ function p2tr(a, opts) {
8282
const controlBlock = w[w.length - 1];
8383
const leafVersion = controlBlock[0] & 0b11111110;
8484
const script = w[w.length - 2];
85-
const tapLeafHash = (0, taproot_1.leafHash)(script, leafVersion);
86-
return (0, taproot_1.rootHashFromPath)(controlBlock, tapLeafHash);
85+
const leafHash = (0, taproot_1.tapLeafHash)(script, leafVersion);
86+
return (0, taproot_1.rootHashFromPath)(controlBlock, leafHash);
8787
}
8888
return null;
8989
});
@@ -200,8 +200,8 @@ function p2tr(a, opts) {
200200
throw new TypeError('Invalid internalPubkey for p2tr witness');
201201
const leafVersion = controlBlock[0] & 0b11111110;
202202
const script = witness[witness.length - 2];
203-
const tapLeafHash = (0, taproot_1.leafHash)(script, leafVersion);
204-
const hash = (0, taproot_1.rootHashFromPath)(controlBlock, tapLeafHash);
203+
const leafHash = (0, taproot_1.tapLeafHash)(script, leafVersion);
204+
const hash = (0, taproot_1.rootHashFromPath)(controlBlock, leafHash);
205205
const outputKey = (0, taproot_1.tweakKey)(internalPubkey, hash);
206206
if (!outputKey)
207207
// todo: needs test data

src/taproot.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import { TweakedPublicKey, TaprootLeaf } from './types';
33
export declare function liftX(buffer: Buffer): Buffer | null;
44
export declare function tweakKey(pubKey: Buffer, h: Buffer | undefined): TweakedPublicKey | null;
5-
export declare function leafHash(script: Buffer, version: number): Buffer;
65
export declare function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer;
7-
export interface HashTree {
8-
rootHash: Buffer;
9-
scritptPath?: Buffer;
10-
}
116
export declare function rootHashFromTree(scripts: TaprootLeaf[]): Buffer;
7+
export declare function tapLeafHash(script: Buffer, version: number): Buffer;

src/taproot.js

Lines changed: 18 additions & 23 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.rootHashFromTree = exports.rootHashFromPath = exports.leafHash = exports.tweakKey = exports.liftX = void 0;
3+
exports.tapLeafHash = exports.rootHashFromTree = exports.rootHashFromPath = exports.tweakKey = exports.liftX = void 0;
44
const buffer_1 = require('buffer');
55
const BN = require('bn.js');
66
const bcrypto = require('./crypto');
@@ -63,32 +63,16 @@ function tweakKey(pubKey, h) {
6363
};
6464
}
6565
exports.tweakKey = tweakKey;
66-
function leafHash(script, version) {
67-
return bcrypto.taggedHash(
68-
TAP_LEAF_TAG,
69-
buffer_1.Buffer.concat([
70-
buffer_1.Buffer.from([version]),
71-
serializeScript(script),
72-
]),
73-
);
74-
}
75-
exports.leafHash = leafHash;
7666
function rootHashFromPath(controlBlock, tapLeafMsg) {
7767
const k = [tapLeafMsg];
7868
const e = [];
7969
const m = (controlBlock.length - 33) / 32;
8070
for (let j = 0; j < m; j++) {
8171
e[j] = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
8272
if (k[j].compare(e[j]) < 0) {
83-
k[j + 1] = bcrypto.taggedHash(
84-
TAP_BRANCH_TAG,
85-
buffer_1.Buffer.concat([k[j], e[j]]),
86-
);
73+
k[j + 1] = tapBranchHash(k[j], e[j]);
8774
} else {
88-
k[j + 1] = bcrypto.taggedHash(
89-
TAP_BRANCH_TAG,
90-
buffer_1.Buffer.concat([e[j], k[j]]),
91-
);
75+
k[j + 1] = tapBranchHash(e[j], k[j]);
9276
}
9377
}
9478
return k[m];
@@ -103,20 +87,31 @@ function rootHashFromTree(scripts) {
10387
script.version = script.version || LEAF_VERSION_TAPSCRIPT;
10488
if ((script.version & 1) !== 0)
10589
throw new TypeError('Invalid script version');
106-
return leafHash(script.output, script.version);
90+
return tapLeafHash(script.output, script.version);
10791
}
10892
// todo: this is a binary tree, use zero an one index
10993
const half = Math.trunc(scripts.length / 2);
11094
let leftHash = rootHashFromTree(scripts.slice(0, half));
11195
let rightHash = rootHashFromTree(scripts.slice(half));
11296
if (leftHash.compare(rightHash) === 1)
11397
[leftHash, rightHash] = [rightHash, leftHash];
98+
return tapBranchHash(leftHash, rightHash);
99+
}
100+
exports.rootHashFromTree = rootHashFromTree;
101+
// todo: rename to tapLeafHash
102+
function tapLeafHash(script, version) {
114103
return bcrypto.taggedHash(
115-
TAP_BRANCH_TAG,
116-
buffer_1.Buffer.concat([leftHash, rightHash]),
104+
TAP_LEAF_TAG,
105+
buffer_1.Buffer.concat([
106+
buffer_1.Buffer.from([version]),
107+
serializeScript(script),
108+
]),
117109
);
118110
}
119-
exports.rootHashFromTree = rootHashFromTree;
111+
exports.tapLeafHash = tapLeafHash;
112+
function tapBranchHash(a, b) {
113+
return bcrypto.taggedHash(TAP_BRANCH_TAG, buffer_1.Buffer.concat([a, b]));
114+
}
120115
function serializeScript(s) {
121116
const varintLen = varuint.encodingLength(s.length);
122117
const buffer = buffer_1.Buffer.allocUnsafe(varintLen); // better

ts_src/payments/p2tr.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { typeforce as typef } from '../types';
44
import {
55
rootHashFromTree,
66
rootHashFromPath,
7-
leafHash,
7+
tapLeafHash,
88
tweakKey,
99
liftX,
1010
} from '../taproot';
@@ -94,8 +94,8 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
9494
const controlBlock = w[w.length - 1];
9595
const leafVersion = controlBlock[0] & 0b11111110;
9696
const script = w[w.length - 2];
97-
const tapLeafHash = leafHash(script, leafVersion);
98-
return rootHashFromPath(controlBlock, tapLeafHash);
97+
const leafHash = tapLeafHash(script, leafVersion);
98+
return rootHashFromPath(controlBlock, leafHash);
9999
}
100100
return null;
101101
});
@@ -226,8 +226,8 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
226226
const leafVersion = controlBlock[0] & 0b11111110;
227227
const script = witness[witness.length - 2];
228228

229-
const tapLeafHash = leafHash(script, leafVersion);
230-
const hash = rootHashFromPath(controlBlock, tapLeafHash);
229+
const leafHash = tapLeafHash(script, leafVersion);
230+
const hash = rootHashFromPath(controlBlock, leafHash);
231231

232232
const outputKey = tweakKey(internalPubkey, hash);
233233
if (!outputKey)

ts_src/taproot.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ export function tweakKey(
7878
};
7979
}
8080

81-
export function leafHash(script: Buffer, version: number): Buffer {
82-
return bcrypto.taggedHash(TAP_LEAF_TAG, NBuffer.concat([NBuffer.from([version]), serializeScript(script)]));
83-
}
84-
8581
export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer {
8682
const k = [tapLeafMsg];
8783
const e = [];
@@ -91,15 +87,9 @@ export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buff
9187
for (let j = 0; j < m; j++) {
9288
e[j] = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
9389
if (k[j].compare(e[j]) < 0) {
94-
k[j + 1] = bcrypto.taggedHash(
95-
TAP_BRANCH_TAG,
96-
NBuffer.concat([k[j], e[j]]),
97-
);
90+
k[j + 1] = tapBranchHash(k[j], e[j]);
9891
} else {
99-
k[j + 1] = bcrypto.taggedHash(
100-
TAP_BRANCH_TAG,
101-
NBuffer.concat([e[j], k[j]]),
102-
);
92+
k[j + 1] = tapBranchHash(e[j], k[j]);
10393
}
10494
}
10595

@@ -115,7 +105,7 @@ export function rootHashFromTree(scripts: TaprootLeaf[]): Buffer {
115105
script.version = script.version || LEAF_VERSION_TAPSCRIPT;
116106
if ((script.version & 1) !== 0) throw new TypeError('Invalid script version');
117107

118-
return leafHash(script.output, script.version);
108+
return tapLeafHash(script.output, script.version);
119109
}
120110
// todo: this is a binary tree, use zero an one index
121111
const half = Math.trunc(scripts.length / 2);
@@ -124,10 +114,16 @@ export function rootHashFromTree(scripts: TaprootLeaf[]): Buffer {
124114

125115
if (leftHash.compare(rightHash) === 1)
126116
[leftHash, rightHash] = [rightHash, leftHash];
127-
return bcrypto.taggedHash(
128-
TAP_BRANCH_TAG,
129-
NBuffer.concat([leftHash, rightHash]),
130-
);
117+
return tapBranchHash(leftHash, rightHash);
118+
}
119+
120+
// todo: rename to tapLeafHash
121+
export function tapLeafHash(script: Buffer, version: number): Buffer {
122+
return bcrypto.taggedHash(TAP_LEAF_TAG, NBuffer.concat([NBuffer.from([version]), serializeScript(script)]));
123+
}
124+
125+
function tapBranchHash(a: Buffer, b: Buffer): Buffer {
126+
return bcrypto.taggedHash(TAP_BRANCH_TAG, NBuffer.concat([a, b]), );
131127
}
132128

133129
function serializeScript(s: Buffer): Buffer {

0 commit comments

Comments
 (0)