Skip to content

Commit 583174d

Browse files
committed
feat: compute p2tr hash based on the script tree
1 parent f04d8b2 commit 583174d

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/payments/p2tr.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.p2tr = void 0;
44
const networks_1 = require('../networks');
55
const bscript = require('../script');
66
const types_1 = require('../types');
7+
const merkle_1 = require('../merkle');
78
const lazy = require('./lazy');
89
const bech32_1 = require('bech32');
910
const OPS = bscript.OPS;
@@ -28,6 +29,7 @@ function p2tr(a, opts) {
2829
witness: types_1.typeforce.maybe(
2930
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
3031
),
32+
// scriptsTree: typef.maybe(typef.TaprootNode), // use merkel.isMast ?
3133
},
3234
a,
3335
);
@@ -52,7 +54,7 @@ function p2tr(a, opts) {
5254
});
5355
lazy.prop(o, 'hash', () => {
5456
if (a.hash) return a.hash;
55-
// todo: if (a.redeems?.length) compute from MAST root from redeems
57+
if (a.scriptsTree) return (0, merkle_1.computeMastRoot)(a.scriptsTree);
5658
return null;
5759
});
5860
lazy.prop(o, 'output', () => {

ts_src/payments/p2tr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { bitcoin as BITCOIN_NETWORK } from '../networks';
22
import * as bscript from '../script';
33
import { liftX, tweakPublicKey, typeforce as typef } from '../types';
4+
import { computeMastRoot } from '../merkle';
45
import { Payment, PaymentOpts } from './index';
56
import * as lazy from './lazy';
67
import { bech32m } from 'bech32';
@@ -27,6 +28,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
2728
pubkey: typef.maybe(typef.BufferN(32)),
2829
signature: typef.maybe(bscript.isCanonicalScriptSignature),
2930
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
31+
// scriptsTree: typef.maybe(typef.TaprootNode), // use merkel.isMast ?
3032
},
3133
a,
3234
);
@@ -58,7 +60,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
5860

5961
lazy.prop(o, 'hash', () => {
6062
if (a.hash) return a.hash;
61-
// todo: if (a.redeems?.length) compute from MAST root from redeems
63+
if (a.scriptsTree) return computeMastRoot(a.scriptsTree)
6264
return null
6365
});
6466
lazy.prop(o, 'output', () => {

0 commit comments

Comments
 (0)