Skip to content

Commit 7839476

Browse files
committed
refactor: rename taprootutils to bip341
1 parent 4db8455 commit 7839476

File tree

10 files changed

+30
-36
lines changed

10 files changed

+30
-36
lines changed
File renamed without changes.
File renamed without changes.

src/payments/p2tr.js

+15-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const networks_1 = require('../networks');
66
const bscript = require('../script');
77
const types_1 = require('../types');
88
const ecc_lib_1 = require('../ecc_lib');
9-
const taprootutils_1 = require('./taprootutils');
9+
const bip341_1 = require('./bip341');
1010
const lazy = require('./lazy');
1111
const bech32_1 = require('bech32');
1212
const OPS = bscript.OPS;
@@ -69,7 +69,7 @@ function p2tr(a, opts) {
6969
return a.witness.slice();
7070
});
7171
const _hashTree = lazy.value(() => {
72-
if (a.scriptTree) return (0, taprootutils_1.toHashTree)(a.scriptTree);
72+
if (a.scriptTree) return (0, bip341_1.toHashTree)(a.scriptTree);
7373
if (a.hash) return { hash: a.hash };
7474
return;
7575
});
@@ -89,11 +89,11 @@ function p2tr(a, opts) {
8989
const controlBlock = w[w.length - 1];
9090
const leafVersion = controlBlock[0] & types_1.TAPLEAF_VERSION_MASK;
9191
const script = w[w.length - 2];
92-
const leafHash = (0, taprootutils_1.tapleafHash)({
92+
const leafHash = (0, bip341_1.tapleafHash)({
9393
output: script,
9494
version: leafVersion,
9595
});
96-
return (0, taprootutils_1.rootHashFromPath)(controlBlock, leafHash);
96+
return (0, bip341_1.rootHashFromPath)(controlBlock, leafHash);
9797
}
9898
return null;
9999
});
@@ -110,7 +110,7 @@ function p2tr(a, opts) {
110110
) {
111111
return a.redeem.redeemVersion;
112112
}
113-
return taprootutils_1.LEAF_VERSION_TAPSCRIPT;
113+
return bip341_1.LEAF_VERSION_TAPSCRIPT;
114114
});
115115
lazy.prop(o, 'redeem', () => {
116116
const witness = _witness(); // witness without annex
@@ -127,7 +127,7 @@ function p2tr(a, opts) {
127127
if (a.output) return a.output.slice(2);
128128
if (a.address) return _address().data;
129129
if (o.internalPubkey) {
130-
const tweakedKey = (0, taprootutils_1.tweakKey)(o.internalPubkey, o.hash);
130+
const tweakedKey = (0, bip341_1.tweakKey)(o.internalPubkey, o.hash);
131131
if (tweakedKey) return tweakedKey.x;
132132
}
133133
});
@@ -147,16 +147,13 @@ function p2tr(a, opts) {
147147
if (a.witness) return a.witness;
148148
const hashTree = _hashTree();
149149
if (hashTree && a.redeem && a.redeem.output && a.internalPubkey) {
150-
const leafHash = (0, taprootutils_1.tapleafHash)({
150+
const leafHash = (0, bip341_1.tapleafHash)({
151151
output: a.redeem.output,
152152
version: o.redeemVersion,
153153
});
154-
const path = (0, taprootutils_1.findScriptPath)(hashTree, leafHash);
154+
const path = (0, bip341_1.findScriptPath)(hashTree, leafHash);
155155
if (!path) return;
156-
const outputKey = (0, taprootutils_1.tweakKey)(
157-
a.internalPubkey,
158-
hashTree.hash,
159-
);
156+
const outputKey = (0, bip341_1.tweakKey)(a.internalPubkey, hashTree.hash);
160157
if (!outputKey) return;
161158
const controlBock = buffer_1.Buffer.concat(
162159
[
@@ -197,7 +194,7 @@ function p2tr(a, opts) {
197194
else pubkey = a.output.slice(2);
198195
}
199196
if (a.internalPubkey) {
200-
const tweakedKey = (0, taprootutils_1.tweakKey)(a.internalPubkey, o.hash);
197+
const tweakedKey = (0, bip341_1.tweakKey)(a.internalPubkey, o.hash);
201198
if (pubkey.length > 0 && !pubkey.equals(tweakedKey.x))
202199
throw new TypeError('Pubkey mismatch');
203200
else pubkey = tweakedKey.x;
@@ -211,11 +208,11 @@ function p2tr(a, opts) {
211208
if (!a.hash.equals(hashTree.hash)) throw new TypeError('Hash mismatch');
212209
}
213210
if (a.redeem && a.redeem.output && hashTree) {
214-
const leafHash = (0, taprootutils_1.tapleafHash)({
211+
const leafHash = (0, bip341_1.tapleafHash)({
215212
output: a.redeem.output,
216213
version: o.redeemVersion,
217214
});
218-
if (!(0, taprootutils_1.findScriptPath)(hashTree, leafHash))
215+
if (!(0, bip341_1.findScriptPath)(hashTree, leafHash))
219216
throw new TypeError('Redeem script not in tree');
220217
}
221218
const witness = _witness();
@@ -270,15 +267,12 @@ function p2tr(a, opts) {
270267
throw new TypeError('Invalid internalPubkey for p2tr witness');
271268
const leafVersion = controlBlock[0] & types_1.TAPLEAF_VERSION_MASK;
272269
const script = witness[witness.length - 2];
273-
const leafHash = (0, taprootutils_1.tapleafHash)({
270+
const leafHash = (0, bip341_1.tapleafHash)({
274271
output: script,
275272
version: leafVersion,
276273
});
277-
const hash = (0, taprootutils_1.rootHashFromPath)(
278-
controlBlock,
279-
leafHash,
280-
);
281-
const outputKey = (0, taprootutils_1.tweakKey)(internalPubkey, hash);
274+
const hash = (0, bip341_1.rootHashFromPath)(controlBlock, leafHash);
275+
const outputKey = (0, bip341_1.tweakKey)(internalPubkey, hash);
282276
if (!outputKey)
283277
// todo: needs test data
284278
throw new TypeError('Invalid outputKey for p2tr witness');

src/psbt.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const address_1 = require('./address');
88
const bufferutils_1 = require('./bufferutils');
99
const networks_1 = require('./networks');
1010
const payments = require('./payments');
11-
const taprootutils_1 = require('./payments/taprootutils');
11+
const bip341_1 = require('./payments/bip341');
1212
const bscript = require('./script');
1313
const transaction_1 = require('./transaction');
1414
const bip371_1 = require('./psbt/bip371');
@@ -1325,7 +1325,7 @@ function getTaprootHashesForSig(
13251325
const tapLeafHashes = (input.tapLeafScript || [])
13261326
.filter(tapLeaf => (0, psbtutils_1.pubkeyInScript)(pubkey, tapLeaf.script))
13271327
.map(tapLeaf => {
1328-
const hash = (0, taprootutils_1.tapleafHash)({
1328+
const hash = (0, bip341_1.tapleafHash)({
13291329
output: tapLeaf.script,
13301330
version: tapLeaf.leafVersion,
13311331
});

src/psbt/bip371.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.checkTaprootInputForSigs = exports.tapTreeFromList = exports.tapTreeToLi
44
const types_1 = require('../types');
55
const transaction_1 = require('../transaction');
66
const psbtutils_1 = require('./psbtutils');
7-
const taprootutils_1 = require('../payments/taprootutils');
7+
const bip341_1 = require('../payments/bip341');
88
const payments_1 = require('../payments');
99
const psbtutils_2 = require('./psbtutils');
1010
const toXOnly = pubKey => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
@@ -101,7 +101,7 @@ function tweakInternalPubKey(inputIndex, input) {
101101
const tapInternalKey = input.tapInternalKey;
102102
const outputKey =
103103
tapInternalKey &&
104-
(0, taprootutils_1.tweakKey)(tapInternalKey, input.tapMerkleRoot);
104+
(0, bip341_1.tweakKey)(tapInternalKey, input.tapMerkleRoot);
105105
if (!outputKey)
106106
throw new Error(
107107
`Cannot tweak tap internal key for input #${inputIndex}. Public key: ${tapInternalKey &&
@@ -175,13 +175,13 @@ function getTapKeySigFromWithness(finalScriptWitness) {
175175
if (witness.length === 64 || witness.length === 65) return witness;
176176
}
177177
function _tapTreeToList(tree, leaves = [], depth = 0) {
178-
if (depth > taprootutils_1.MAX_TAPTREE_DEPTH)
178+
if (depth > bip341_1.MAX_TAPTREE_DEPTH)
179179
throw new Error('Max taptree depth exceeded.');
180180
if (!tree) return [];
181181
if ((0, types_1.isTapleaf)(tree)) {
182182
leaves.push({
183183
depth,
184-
leafVersion: tree.version || taprootutils_1.LEAF_VERSION_TAPSCRIPT,
184+
leafVersion: tree.version || bip341_1.LEAF_VERSION_TAPSCRIPT,
185185
script: tree.output,
186186
});
187187
return leaves;
@@ -199,7 +199,7 @@ function instertLeavesInTree(leaves) {
199199
return tree;
200200
}
201201
function instertLeafInTree(leaf, tree, depth = 0) {
202-
if (depth > taprootutils_1.MAX_TAPTREE_DEPTH)
202+
if (depth > bip341_1.MAX_TAPTREE_DEPTH)
203203
throw new Error('Max taptree depth exceeded.');
204204
if (leaf.depth === depth) {
205205
if (!tree)
@@ -275,18 +275,18 @@ function checkIfTapLeafInTree(inputData, newInputData, action) {
275275
}
276276
function isTapLeafInTree(tapLeaf, merkleRoot) {
277277
if (!merkleRoot) return true;
278-
const leafHash = (0, taprootutils_1.tapleafHash)({
278+
const leafHash = (0, bip341_1.tapleafHash)({
279279
output: tapLeaf.script,
280280
version: tapLeaf.leafVersion,
281281
});
282-
const rootHash = (0, taprootutils_1.rootHashFromPath)(
282+
const rootHash = (0, bip341_1.rootHashFromPath)(
283283
tapLeaf.controlBlock,
284284
leafHash,
285285
);
286286
return rootHash.equals(merkleRoot);
287287
}
288288
function sortSignatures(input, tapLeaf) {
289-
const leafHash = (0, taprootutils_1.tapleafHash)({
289+
const leafHash = (0, bip341_1.tapleafHash)({
290290
output: tapLeaf.script,
291291
version: tapLeaf.leafVersion,
292292
});
@@ -327,7 +327,7 @@ function findTapLeafToFinalize(input, inputIndex, leafHashToFinalize) {
327327
return tapLeaf;
328328
}
329329
function canFinalizeLeaf(leaf, tapScriptSig, hash) {
330-
const leafHash = (0, taprootutils_1.tapleafHash)({
330+
const leafHash = (0, bip341_1.tapleafHash)({
331331
output: leaf.script,
332332
version: leaf.leafVersion,
333333
});

test/psbt.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ECPairFactory from 'ecpair';
66
import { describe, it } from 'mocha';
77

88
import { convertScriptTree } from './payments.utils';
9-
import { LEAF_VERSION_TAPSCRIPT } from '../src/payments/taprootutils';
9+
import { LEAF_VERSION_TAPSCRIPT } from '../src/payments/bip341';
1010
import { tapTreeToList, tapTreeFromList } from '../src/psbt/bip371';
1111
import { Taptree } from '../src/types';
1212
import { initEccLib } from '../src';
File renamed without changes.

ts_src/payments/p2tr.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
tapleafHash,
1111
tweakKey,
1212
LEAF_VERSION_TAPSCRIPT,
13-
} from './taprootutils';
13+
} from './bip341';
1414
import { Payment, PaymentOpts } from './index';
1515
import * as lazy from './lazy';
1616
import { bech32m } from 'bech32';

ts_src/psbt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { fromOutputScript, toOutputScript } from './address';
1919
import { cloneBuffer, reverseBuffer } from './bufferutils';
2020
import { bitcoin as btcNetwork, Network } from './networks';
2121
import * as payments from './payments';
22-
import { tapleafHash } from './payments/taprootutils';
22+
import { tapleafHash } from './payments/bip341';
2323
import * as bscript from './script';
2424
import { Output, Transaction } from './transaction';
2525
import {

ts_src/psbt/bip371.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
rootHashFromPath,
2323
LEAF_VERSION_TAPSCRIPT,
2424
MAX_TAPTREE_DEPTH,
25-
} from '../payments/taprootutils';
25+
} from '../payments/bip341';
2626
import { p2tr } from '../payments';
2727

2828
import { signatureBlocksAction } from './psbtutils';

0 commit comments

Comments
 (0)