Skip to content

Commit 5b1500e

Browse files
committed
feat(p2tr): expose internalPubkey
required for verifying PSBT outputs Issue: BG-54756
1 parent 9989088 commit 5b1500e

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

src/payments/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Payment {
2222
input?: Buffer;
2323
signatures?: Buffer[];
2424
pubkey?: Buffer;
25+
internalPubkey?: Buffer;
2526
signature?: Buffer;
2627
address?: string;
2728
hash?: Buffer;

src/payments/p2tr.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ function p2tr(a, opts) {
103103
if (parsedWitness && parsedWitness.spendType === 'Script')
104104
return taproot.parseControlBlock(ecc, parsedWitness.controlBlock);
105105
});
106-
const _internalPubkey = lazy.value(() => {
106+
lazy.prop(o, 'internalPubkey', () => {
107107
if (a.pubkey) {
108108
// single pubkey
109109
return a.pubkey;
110110
} else if (a.pubkeys && a.pubkeys.length === 1) {
111111
return a.pubkeys[0];
112112
} else if (a.pubkeys && a.pubkeys.length > 1) {
113113
// multiple pubkeys
114-
return taproot.aggregateMuSigPubkeys(ecc, a.pubkeys);
114+
return Buffer.from(taproot.aggregateMuSigPubkeys(ecc, a.pubkeys));
115115
} else if (_parsedControlBlock()) {
116116
return _parsedControlBlock().internalPubkey;
117117
} else {
@@ -153,7 +153,7 @@ function p2tr(a, opts) {
153153
);
154154
}
155155
if (!taptreeRoot && _taprootPaths()) taptreeRoot = _taprootPaths().root;
156-
return taproot.tapTweakPubkey(ecc, _internalPubkey(), taptreeRoot);
156+
return taproot.tapTweakPubkey(ecc, o.internalPubkey, taptreeRoot);
157157
});
158158
lazy.prop(o, 'tapTree', () => {
159159
if (!a.redeems) return;
@@ -192,7 +192,7 @@ function p2tr(a, opts) {
192192
if (!taprootPaths || !taprootPubkey || a.redeemIndex === undefined) return;
193193
return taproot.getControlBlock(
194194
taprootPubkey.parity,
195-
_internalPubkey(),
195+
o.internalPubkey,
196196
taprootPaths.paths[a.redeemIndex],
197197
);
198198
});
@@ -289,7 +289,7 @@ function p2tr(a, opts) {
289289
throw new TypeError('mismatch between address and taproot pubkey');
290290
const parsedControlBlock = _parsedControlBlock();
291291
if (parsedControlBlock) {
292-
if (!parsedControlBlock.internalPubkey.equals(_internalPubkey()))
292+
if (!parsedControlBlock.internalPubkey.equals(o.internalPubkey))
293293
throw new TypeError('Internal pubkey mismatch');
294294
if (taprootPubkey && parsedControlBlock.parity !== taprootPubkey.parity)
295295
throw new TypeError('Parity mismatch');

ts_src/payments/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Payment {
2222
input?: Buffer;
2323
signatures?: Buffer[];
2424
pubkey?: Buffer;
25+
internalPubkey?: Buffer;
2526
signature?: Buffer;
2627
address?: string;
2728
hash?: Buffer;

ts_src/payments/p2tr.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
117117
if (parsedWitness && parsedWitness.spendType === 'Script')
118118
return taproot.parseControlBlock(ecc, parsedWitness.controlBlock);
119119
});
120-
const _internalPubkey = lazy.value(() => {
120+
121+
lazy.prop(o, 'internalPubkey', () => {
121122
if (a.pubkey) {
122123
// single pubkey
123124
return a.pubkey;
124125
} else if (a.pubkeys && a.pubkeys.length === 1) {
125126
return a.pubkeys[0];
126127
} else if (a.pubkeys && a.pubkeys.length > 1) {
127128
// multiple pubkeys
128-
return taproot.aggregateMuSigPubkeys(ecc, a.pubkeys);
129+
return Buffer.from(taproot.aggregateMuSigPubkeys(ecc, a.pubkeys));
129130
} else if (_parsedControlBlock()) {
130131
return _parsedControlBlock()!.internalPubkey;
131132
} else {
@@ -169,7 +170,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
169170
}
170171
if (!taptreeRoot && _taprootPaths()) taptreeRoot = _taprootPaths()!.root;
171172

172-
return taproot.tapTweakPubkey(ecc, _internalPubkey(), taptreeRoot);
173+
return taproot.tapTweakPubkey(ecc, o.internalPubkey!, taptreeRoot);
173174
});
174175

175176
lazy.prop(o, 'tapTree', () => {
@@ -209,7 +210,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
209210
if (!taprootPaths || !taprootPubkey || a.redeemIndex === undefined) return;
210211
return taproot.getControlBlock(
211212
taprootPubkey.parity,
212-
_internalPubkey(),
213+
o.internalPubkey!,
213214
taprootPaths.paths[a.redeemIndex],
214215
);
215216
});
@@ -319,7 +320,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
319320

320321
const parsedControlBlock = _parsedControlBlock();
321322
if (parsedControlBlock) {
322-
if (!parsedControlBlock.internalPubkey.equals(_internalPubkey()))
323+
if (!parsedControlBlock.internalPubkey.equals(o.internalPubkey!))
323324
throw new TypeError('Internal pubkey mismatch');
324325
if (taprootPubkey && parsedControlBlock.parity !== taprootPubkey.parity)
325326
throw new TypeError('Parity mismatch');

0 commit comments

Comments
 (0)