Skip to content

Commit d987d8d

Browse files
committed
refactor: move taproot utils file
1 parent e64c2d8 commit d987d8d

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

src/payments/p2tr.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const buffer_1 = require('buffer');
55
const networks_1 = require('../networks');
66
const bscript = require('../script');
77
const types_1 = require('../types');
8-
const taproot_1 = require('../taproot');
8+
const taprootutils_1 = require('./taprootutils');
99
const lazy = require('./lazy');
1010
const bech32_1 = require('bech32');
1111
const OPS = bscript.OPS;
@@ -75,14 +75,15 @@ function p2tr(ecc) {
7575
});
7676
lazy.prop(o, 'hash', () => {
7777
if (a.hash) return a.hash;
78-
if (a.scriptsTree) return (0, taproot_1.toHashTree)(a.scriptsTree).hash;
78+
if (a.scriptsTree)
79+
return (0, taprootutils_1.toHashTree)(a.scriptsTree).hash;
7980
const w = _witness();
8081
if (w && w.length > 1) {
8182
const controlBlock = w[w.length - 1];
8283
const leafVersion = controlBlock[0] & 0b11111110;
8384
const script = w[w.length - 2];
84-
const leafHash = (0, taproot_1.tapLeafHash)(script, leafVersion);
85-
return (0, taproot_1.rootHashFromPath)(controlBlock, leafHash);
85+
const leafHash = (0, taprootutils_1.tapLeafHash)(script, leafVersion);
86+
return (0, taprootutils_1.rootHashFromPath)(controlBlock, leafHash);
8687
}
8788
return null;
8889
});
@@ -119,12 +120,12 @@ function p2tr(ecc) {
119120
if (a.witness) return a.witness;
120121
if (a.scriptsTree && a.scriptLeaf && a.internalPubkey) {
121122
// todo: optimize/cache
122-
const hashTree = (0, taproot_1.toHashTree)(a.scriptsTree);
123-
const leafHash = (0, taproot_1.tapLeafHash)(
123+
const hashTree = (0, taprootutils_1.toHashTree)(a.scriptsTree);
124+
const leafHash = (0, taprootutils_1.tapLeafHash)(
124125
a.scriptLeaf.output,
125126
a.scriptLeaf.version,
126127
);
127-
const path = (0, taproot_1.findScriptPath)(hashTree, leafHash);
128+
const path = (0, taprootutils_1.findScriptPath)(hashTree, leafHash);
128129
const outputKey = tweakKey(a.internalPubkey, hashTree.hash);
129130
if (!outputKey) return;
130131
const version = a.scriptLeaf.version || 0xc0;
@@ -177,7 +178,7 @@ function p2tr(ecc) {
177178
throw new TypeError('Invalid pubkey for p2tr');
178179
}
179180
if (a.hash && a.scriptsTree) {
180-
const hash = (0, taproot_1.toHashTree)(a.scriptsTree).hash;
181+
const hash = (0, taprootutils_1.toHashTree)(a.scriptsTree).hash;
181182
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
182183
}
183184
const witness = _witness();
@@ -216,8 +217,11 @@ function p2tr(ecc) {
216217
throw new TypeError('Invalid internalPubkey for p2tr witness');
217218
const leafVersion = controlBlock[0] & 0b11111110;
218219
const script = witness[witness.length - 2];
219-
const leafHash = (0, taproot_1.tapLeafHash)(script, leafVersion);
220-
const hash = (0, taproot_1.rootHashFromPath)(controlBlock, leafHash);
220+
const leafHash = (0, taprootutils_1.tapLeafHash)(script, leafVersion);
221+
const hash = (0, taprootutils_1.rootHashFromPath)(
222+
controlBlock,
223+
leafHash,
224+
);
221225
const outputKey = tweakKey(internalPubkey, hash);
222226
if (!outputKey)
223227
// todo: needs test data
@@ -235,7 +239,7 @@ function p2tr(ecc) {
235239
if (!buffer_1.Buffer.isBuffer(pubKey)) return null;
236240
if (pubKey.length !== 32) return null;
237241
if (h && h.length !== 32) return null;
238-
const tweakHash = (0, taproot_1.tapTweakHash)(pubKey, h);
242+
const tweakHash = (0, taprootutils_1.tapTweakHash)(pubKey, h);
239243
const res = ecc.xOnlyPointAddTweak(pubKey, tweakHash);
240244
if (!res || res.xOnlyPubkey === null) return null;
241245
return {

src/taproot.d.ts renamed to src/payments/taprootutils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="node" />
2-
import { TaprootLeaf } from './types';
2+
import { TaprootLeaf } from '../types';
33
export declare function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer;
44
export interface HashTree {
55
hash: Buffer;

src/taproot.js renamed to src/payments/taprootutils.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
Object.defineProperty(exports, '__esModule', { value: true });
33
exports.tapTweakHash = exports.tapLeafHash = exports.findScriptPath = exports.toHashTree = exports.rootHashFromPath = void 0;
44
const buffer_1 = require('buffer');
5-
const bcrypto = require('./crypto');
6-
// todo: use varuint-bitcoin??
7-
const varuint = require('bip174/src/lib/converter/varint');
8-
// todo: !!!Temp, to be replaced. Only works because bip32 has it as dependecy. Linting will fail.
9-
// const ecc = require('tiny-secp256k1');
5+
const bcrypto = require('../crypto');
6+
const bufferutils_1 = require('../bufferutils');
107
const LEAF_VERSION_TAPSCRIPT = 0xc0;
118
const TAP_LEAF_TAG = 'TapLeaf';
129
const TAP_BRANCH_TAG = 'TapBranch';
@@ -92,8 +89,8 @@ function tapTweakHash(pubKey, h) {
9289
}
9390
exports.tapTweakHash = tapTweakHash;
9491
function serializeScript(s) {
95-
const varintLen = varuint.encodingLength(s.length);
92+
const varintLen = bufferutils_1.varuint.encodingLength(s.length);
9693
const buffer = buffer_1.Buffer.allocUnsafe(varintLen); // better
97-
varuint.encode(s.length, buffer);
94+
bufferutils_1.varuint.encode(s.length, buffer);
9895
return buffer_1.Buffer.concat([buffer, s]);
9996
}

ts_src/payments/p2tr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
findScriptPath,
99
tapLeafHash,
1010
tapTweakHash,
11-
} from '../taproot';
11+
} from './taprootutils';
1212
import { Payment, PaymentOpts, PaymentCreator } from './index';
1313
import * as lazy from './lazy';
1414
import { bech32m } from 'bech32';
15-
const OPS = bscript.OPS;
1615

16+
const OPS = bscript.OPS;
1717
const TAPROOT_VERSION = 0x01;
1818
const ANNEX_PREFIX = 0x50;
1919

ts_src/taproot.ts renamed to ts_src/payments/taprootutils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { Buffer as NBuffer } from 'buffer';
2-
import * as bcrypto from './crypto';
2+
import * as bcrypto from '../crypto';
33

4-
// todo: use varuint-bitcoin??
5-
import * as varuint from 'bip174/src/lib/converter/varint';
6-
import { TaprootLeaf } from './types';
7-
8-
// todo: !!!Temp, to be replaced. Only works because bip32 has it as dependecy. Linting will fail.
9-
// const ecc = require('tiny-secp256k1');
4+
import { varuint } from '../bufferutils';
5+
import { TaprootLeaf } from '../types';
106

117
const LEAF_VERSION_TAPSCRIPT = 0xc0;
128
const TAP_LEAF_TAG = 'TapLeaf';

0 commit comments

Comments
 (0)