Skip to content

Commit fa8b104

Browse files
committed
feat: export default tapscript version
Issue: BG-54756
1 parent 68e4f8c commit fa8b104

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/taproot.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TinySecp256k1Interface, XOnlyPointAddTweakResult } from './types';
55
* on all 32 byte x-only pub keys as defined in BIP340.
66
*/
77
export declare const EVEN_Y_COORD_PREFIX: Buffer;
8+
export declare const INITIAL_TAPSCRIPT_VERSION = 192;
89
/**
910
* Aggregates a list of public keys into a single MuSig2* public key
1011
* according to the MuSig2 paper.
@@ -24,7 +25,7 @@ export declare function serializeScriptSize(script: Buffer): Buffer;
2425
* @param script
2526
* @returns
2627
*/
27-
export declare function hashTapLeaf(script: Buffer): Buffer;
28+
export declare function hashTapLeaf(script: Buffer, leafVersion?: number): Buffer;
2829
/**
2930
* Creates a lexicographically sorted tapbranch from two child taptree nodes
3031
* and returns its tagged hash.
@@ -62,7 +63,7 @@ export interface Taptree {
6263
* @returns {Taptree} the tree, represented by its root hash, and the paths to that root from each of the input scripts
6364
*/
6465
export declare function getHuffmanTaptree(scripts: Buffer[], weights: Array<number | undefined>): Taptree;
65-
export declare function getControlBlock(parity: 0 | 1, pubkey: Uint8Array, path: Buffer[]): Buffer;
66+
export declare function getControlBlock(parity: 0 | 1, pubkey: Uint8Array, path: Buffer[], leafVersion?: number): Buffer;
6667
export interface KeyPathWitness {
6768
spendType: 'Key';
6869
signature: Buffer;

src/taproot.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
44
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
55
Object.defineProperty(exports, '__esModule', { value: true });
6-
exports.getTaptreeRoot = exports.getTapleafHash = exports.parseControlBlock = exports.parseTaprootWitness = exports.getControlBlock = exports.getHuffmanTaptree = exports.tapTweakPubkey = exports.tapTweakPrivkey = exports.hashTapBranch = exports.hashTapLeaf = exports.serializeScriptSize = exports.aggregateMuSigPubkeys = exports.EVEN_Y_COORD_PREFIX = void 0;
6+
exports.getTaptreeRoot = exports.getTapleafHash = exports.parseControlBlock = exports.parseTaprootWitness = exports.getControlBlock = exports.getHuffmanTaptree = exports.tapTweakPubkey = exports.tapTweakPrivkey = exports.hashTapBranch = exports.hashTapLeaf = exports.serializeScriptSize = exports.aggregateMuSigPubkeys = exports.INITIAL_TAPSCRIPT_VERSION = exports.EVEN_Y_COORD_PREFIX = void 0;
77
const assert = require('assert');
88
const FastPriorityQueue = require('fastpriorityqueue');
99
const bcrypto = require('./crypto');
@@ -14,7 +14,7 @@ const varuint = require('varuint-bitcoin');
1414
* on all 32 byte x-only pub keys as defined in BIP340.
1515
*/
1616
exports.EVEN_Y_COORD_PREFIX = Buffer.of(0x02);
17-
const INITIAL_TAPSCRIPT_VERSION = Buffer.of(0xc0);
17+
exports.INITIAL_TAPSCRIPT_VERSION = 0xc0;
1818
/**
1919
* Aggregates a list of public keys into a single MuSig2* public key
2020
* according to the MuSig2 paper.
@@ -82,11 +82,11 @@ exports.serializeScriptSize = serializeScriptSize;
8282
* @param script
8383
* @returns
8484
*/
85-
function hashTapLeaf(script) {
85+
function hashTapLeaf(script, leafVersion = exports.INITIAL_TAPSCRIPT_VERSION) {
8686
const size = serializeScriptSize(script);
8787
return bcrypto.taggedHash(
8888
'TapLeaf',
89-
Buffer.concat([INITIAL_TAPSCRIPT_VERSION, size, script]),
89+
Buffer.concat([Buffer.of(leafVersion), size, script]),
9090
);
9191
}
9292
exports.hashTapLeaf = hashTapLeaf;
@@ -217,8 +217,13 @@ function getHuffmanTaptree(scripts, weights) {
217217
return { root: rootNode.taggedHash, paths };
218218
}
219219
exports.getHuffmanTaptree = getHuffmanTaptree;
220-
function getControlBlock(parity, pubkey, path) {
221-
const parityVersion = INITIAL_TAPSCRIPT_VERSION[0] + parity;
220+
function getControlBlock(
221+
parity,
222+
pubkey,
223+
path,
224+
leafVersion = exports.INITIAL_TAPSCRIPT_VERSION,
225+
) {
226+
const parityVersion = leafVersion + parity;
222227
return Buffer.concat([Buffer.of(parityVersion), pubkey, ...path]);
223228
}
224229
exports.getControlBlock = getControlBlock;

ts_src/taproot.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const varuint = require('varuint-bitcoin');
1414
* on all 32 byte x-only pub keys as defined in BIP340.
1515
*/
1616
export const EVEN_Y_COORD_PREFIX = Buffer.of(0x02);
17-
const INITIAL_TAPSCRIPT_VERSION = Buffer.of(0xc0);
17+
export const INITIAL_TAPSCRIPT_VERSION = 0xc0;
1818

1919
/**
2020
* Aggregates a list of public keys into a single MuSig2* public key
@@ -95,11 +95,11 @@ export function serializeScriptSize(script: Buffer): Buffer {
9595
* @param script
9696
* @returns
9797
*/
98-
export function hashTapLeaf(script: Buffer): Buffer {
98+
export function hashTapLeaf(script: Buffer, leafVersion = INITIAL_TAPSCRIPT_VERSION): Buffer {
9999
const size = serializeScriptSize(script);
100100
return bcrypto.taggedHash(
101101
'TapLeaf',
102-
Buffer.concat([INITIAL_TAPSCRIPT_VERSION, size, script]),
102+
Buffer.concat([Buffer.of(leafVersion), size, script]),
103103
);
104104
}
105105

@@ -275,8 +275,9 @@ export function getControlBlock(
275275
parity: 0 | 1,
276276
pubkey: Uint8Array,
277277
path: Buffer[],
278+
leafVersion = INITIAL_TAPSCRIPT_VERSION,
278279
): Buffer {
279-
const parityVersion = INITIAL_TAPSCRIPT_VERSION[0] + parity;
280+
const parityVersion = leafVersion + parity;
280281

281282
return Buffer.concat([Buffer.of(parityVersion), pubkey, ...path]);
282283
}

0 commit comments

Comments
 (0)