Skip to content

Commit e17e2bd

Browse files
committed
feat: do taproot checks for updateOutput()
1 parent 8ca34c0 commit e17e2bd

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

src/psbt.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,12 @@ class Psbt {
884884
return this;
885885
}
886886
updateOutput(outputIndex, updateData) {
887-
// const outputData = this.data.outputs[outputIndex];
888-
// checkTaprootOutputFields(outputData, updateData, 'updateOutput');
887+
const outputData = this.data.outputs[outputIndex];
888+
(0, bip371_1.checkTaprootOutputFields)(
889+
outputData,
890+
updateData,
891+
'updateOutput',
892+
);
889893
this.data.updateOutput(outputIndex, updateData);
890894
return this;
891895
}

test/fixtures/psbt.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,36 @@
247247
}
248248
],
249249
"result": "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAEAuwIAAAABqtc5MQGL0l+ErkALaISL4J23BurCrBgpi6vucatlb4sAAAAASEcwRAIgWPb8fGoz4bMVSNSByCbAFb0wE1qtQs1neQ2rZtKtJDsCIEoc7SYExnNbY5PltBaR3XiwDwxZQvufdRhW+qk4FX26Af7///8CgPD6AgAAAAAXqRQPuUY0IWlrgsgzryQceMF9295JNIfQ8gonAQAAABepFCnKdPigj4GZlCgYXJe12FLkBj9hh2UAAAABAwQBAAAAAQRHUiEClYO/Oa4KYJdHrRma3dY0+mEIVZ1sXNObTCGD8auW4H8hAtq2H/SaFNtqfQKwzR+7ePxLGDErW05U2uTbovv+9TbXUq4iBgKVg785rgpgl0etGZrd1jT6YQhVnWxc05tMIYPxq5bgfxDZDGpPAAAAgAAAAIAAAACAIgYC2rYf9JoU22p9ArDNH7t4/EsYMStbTlTa5Nui+/71NtcQ2QxqTwAAAIAAAACAAQAAgAABASAAwusLAAAAABepFLf1+vQOPUClpFmx2zU18rcvqSHohwEDBAEAAAABBCIAIIwjUxc3Q7WV37Sge3K6jkLjeX2nTof+fZ10l+OyAokDAQVHUiEDCJ3BDHrG21T5EymvYXMz2ziM6tDCMfcjN50bmQMLAtwhAjrdkE89bc9Z3bkGsN7iNSm3/7ntUOXoYVGSaGAiHw5zUq4iBgI63ZBPPW3PWd25BrDe4jUpt/+57VDl6GFRkmhgIh8OcxDZDGpPAAAAgAAAAIADAACAIgYDCJ3BDHrG21T5EymvYXMz2ziM6tDCMfcjN50bmQMLAtwQ2QxqTwAAAIAAAACAAgAAgAAiAgOppMN/WZbTqiXbrGtXCvBlA5RJKUJGCzVHU+2e7KWHcRDZDGpPAAAAgAAAAIAEAACAACICAn9jmXV9Lv9VoTatAsaEsYOLZVbl8bazQoKpS2tQBRCWENkMak8AAACAAAAAgAUAAIAA"
250+
},
251+
{
252+
"description": "Update taproot output",
253+
"isTaproot": true,
254+
"psbt": "cHNidP8BAF4CAAAAAbc48X2AGPRzFoMvXHqldFViBB89ZrPfTH6yxr42pJsWAAAAAAAKAAAAAZBBBgAAAAAAIlEgwjn5jxVDmCN6B+wTPt4zCjlF+5KOEDapZ4S9Y3HZxdwAAAAAAAEBK6BoBgAAAAAAIlEgOJumKbXWO2VGchS5k1l1NzOsAyg8aBVQbDNJ88BPtH5iFcF6ESg3Xx4VxbYhRfEEyJc6dumg3zlxfNO8piELt1JMShpSnJ+zzX53bWG2IltsYQ6JBvuPqmxZrFw+lbX4LSnWGlKcn7PNfndtYbYiW2xhDokG+4+qbFmsXD6VtfgtKdYmWrJ1IAOmFFKDvSwulomGXZP6LYOYOGBHzSRnrh2w9cb0vzIGrMAAAA==",
255+
"outputData": [
256+
{
257+
"tapInternalKey": "Buffer.from('b3310106b13af5def8fc341fe7120e844ab73d4437649aefcadd306d974c9958', 'hex')",
258+
"tapTree": {
259+
"leaves": [
260+
{
261+
"depth": 1,
262+
"leafVersion": 192,
263+
"script": "Buffer.from('2050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0ac', 'hex')"
264+
},
265+
{
266+
"depth": 2,
267+
"leafVersion": 192,
268+
"script": "Buffer.from('2050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0ac', 'hex')"
269+
},
270+
{
271+
"depth": 2,
272+
"leafVersion": 192,
273+
"script": "Buffer.from('5ab2752003a6145283bd2c2e9689865d93fa2d8398386047cd2467ae1db0f5c6f4bf3206ac', 'hex')"
274+
}
275+
]
276+
}
277+
}
278+
],
279+
"result": "cHNidP8BAF4CAAAAAbc48X2AGPRzFoMvXHqldFViBB89ZrPfTH6yxr42pJsWAAAAAAAKAAAAAZBBBgAAAAAAIlEgwjn5jxVDmCN6B+wTPt4zCjlF+5KOEDapZ4S9Y3HZxdwAAAAAAAEBK6BoBgAAAAAAIlEgOJumKbXWO2VGchS5k1l1NzOsAyg8aBVQbDNJ88BPtH5iFcF6ESg3Xx4VxbYhRfEEyJc6dumg3zlxfNO8piELt1JMShpSnJ+zzX53bWG2IltsYQ6JBvuPqmxZrFw+lbX4LSnWGlKcn7PNfndtYbYiW2xhDokG+4+qbFmsXD6VtfgtKdYmWrJ1IAOmFFKDvSwulomGXZP6LYOYOGBHzSRnrh2w9cb0vzIGrMAAAQUgszEBBrE69d74/DQf5xIOhEq3PUQ3ZJrvyt0wbZdMmVgBBnIBwCIgUJKbdMGgSVS3i0tgNel6XgeKWg8o7JbVR7/ums6AOsCsAsAiIFCSm3TBoElUt4tLYDXpel4HiloPKOyW1Ue/7prOgDrArALAJVqydSADphRSg70sLpaJhl2T+i2DmDhgR80kZ64dsPXG9L8yBqwA"
250280
}
251281
],
252282
"signer": [

test/integration/taproot.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
322322
redeemVersion: 192,
323323
};
324324

325-
const { output, address, witness } = bitcoin.payments.p2tr({
325+
const { output, witness } = bitcoin.payments.p2tr({
326326
internalPubkey: toXOnly(internalKey.publicKey),
327327
scriptTree,
328328
redeem,
@@ -352,7 +352,21 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
352352
},
353353
],
354354
});
355-
psbt.addOutput({ value: sendAmount, address: address! });
355+
356+
const sendInternalKey = bip32.fromSeed(rng(64), regtest);
357+
const sendPubKey = toXOnly(sendInternalKey.publicKey);
358+
const { address: sendAddress } = bitcoin.payments.p2tr({
359+
internalPubkey: sendPubKey,
360+
scriptTree: scriptTree,
361+
network: regtest,
362+
});
363+
364+
psbt.addOutput({ value: sendAmount, address: sendAddress! });
365+
// just to test that updateOutput works as expected
366+
psbt.updateOutput(0, {
367+
tapInternalKey: sendPubKey,
368+
tapTree: { leaves: tapTreeToList(scriptTree) },
369+
});
356370

357371
await psbt.signInputAsync(0, leafKey);
358372

@@ -376,7 +390,7 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
376390
await regtestUtils.broadcast(hex);
377391
await regtestUtils.verify({
378392
txId: tx.getId(),
379-
address: address!,
393+
address: sendAddress!,
380394
vout: 0,
381395
value: sendAmount,
382396
});

test/psbt.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ describe(`Psbt`, () => {
132132

133133
fixtures.bip174.updater.forEach(f => {
134134
it('Updates PSBT to the expected result', () => {
135+
if (f.isTaproot) initEccLib(ecc);
135136
const psbt = Psbt.fromBase64(f.psbt);
136137

137138
for (const inputOrOutput of ['input', 'output']) {

ts_src/psbt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,8 @@ export class Psbt {
11021102
}
11031103

11041104
updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this {
1105-
// const outputData = this.data.outputs[outputIndex];
1106-
// checkTaprootOutputFields(outputData, updateData, 'updateOutput');
1105+
const outputData = this.data.outputs[outputIndex];
1106+
checkTaprootOutputFields(outputData, updateData, 'updateOutput');
11071107

11081108
this.data.updateOutput(outputIndex, updateData);
11091109
return this;

0 commit comments

Comments
 (0)