|
1 | 1 | import * as utxolib from "@bitgo/utxo-lib"; |
2 | 2 | import * as assert from "node:assert"; |
3 | | -import { getPsbtFixtures } from "./psbtFixtures"; |
4 | | -import { Psbt } from "../js"; |
| 3 | +import { getPsbtFixtures, toPsbtWithPrevOutOnly } from "./psbtFixtures"; |
| 4 | +import { Descriptor, Psbt } from "../js"; |
5 | 5 |
|
6 | | -getPsbtFixtures().forEach(({ psbt, name }) => { |
7 | | - describe(`PSBT fixture ${name}`, function () { |
| 6 | +import { getDescriptorForScriptType } from "./descriptorUtil"; |
| 7 | + |
| 8 | +const rootWalletKeys = new utxolib.bitgo.RootWalletKeys(utxolib.testutil.getKeyTriple("wasm")); |
| 9 | + |
| 10 | +function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | Buffer | Uint8Array) { |
| 11 | + if (psbt instanceof utxolib.bitgo.UtxoPsbt) { |
| 12 | + psbt = psbt.toBuffer(); |
| 13 | + } |
| 14 | + if (psbt instanceof Buffer || psbt instanceof Uint8Array) { |
| 15 | + return Psbt.deserialize(psbt); |
| 16 | + } |
| 17 | + throw new Error("Invalid input"); |
| 18 | +} |
| 19 | + |
| 20 | +function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) { |
| 21 | + if (psbt instanceof Psbt) { |
| 22 | + psbt = psbt.serialize(); |
| 23 | + } |
| 24 | + if (psbt instanceof Buffer || psbt instanceof Uint8Array) { |
| 25 | + return utxolib.bitgo.UtxoPsbt.fromBuffer(Buffer.from(psbt), { |
| 26 | + network: utxolib.networks.bitcoin, |
| 27 | + }); |
| 28 | + } |
| 29 | + throw new Error("Invalid input"); |
| 30 | +} |
| 31 | + |
| 32 | +const fixtures = getPsbtFixtures(rootWalletKeys); |
| 33 | + |
| 34 | +function describeUpdateInputWithDescriptor( |
| 35 | + psbt: utxolib.bitgo.UtxoPsbt, |
| 36 | + scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3, |
| 37 | +) { |
| 38 | + const fullSignedFixture = fixtures.find( |
| 39 | + (f) => f.scriptType === scriptType && f.stage === "fullsigned", |
| 40 | + ); |
| 41 | + if (!fullSignedFixture) { |
| 42 | + throw new Error("Could not find fullsigned fixture"); |
| 43 | + } |
| 44 | + |
| 45 | + describe("updateInputWithDescriptor", function () { |
| 46 | + it("should update the input with the descriptor", function () { |
| 47 | + const descriptorStr = getDescriptorForScriptType(rootWalletKeys, scriptType, "internal"); |
| 48 | + const index = 0; |
| 49 | + const descriptor = Descriptor.fromString(descriptorStr, "derivable"); |
| 50 | + const wrappedPsbt = toWrappedPsbt(toPsbtWithPrevOutOnly(psbt)); |
| 51 | + wrappedPsbt.updateInputWithDescriptor(0, descriptor.atDerivationIndex(index)); |
| 52 | + const updatedPsbt = toUtxoPsbt(wrappedPsbt); |
| 53 | + updatedPsbt.signAllInputsHD(rootWalletKeys.triple[0]); |
| 54 | + updatedPsbt.signAllInputsHD(rootWalletKeys.triple[2]); |
| 55 | + updatedPsbt.finalizeAllInputs(); |
| 56 | + assert.deepStrictEqual( |
| 57 | + fullSignedFixture.psbt |
| 58 | + .clone() |
| 59 | + .finalizeAllInputs() |
| 60 | + .extractTransaction() |
| 61 | + .toBuffer() |
| 62 | + .toString("hex"), |
| 63 | + updatedPsbt.extractTransaction().toBuffer().toString("hex"), |
| 64 | + ); |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +fixtures.forEach(({ psbt, scriptType, stage }) => { |
| 70 | + describe(`PSBT fixture ${scriptType} ${stage}`, function () { |
8 | 71 | let buf: Buffer; |
9 | 72 | let wrappedPsbt: Psbt; |
10 | 73 |
|
11 | 74 | before(function () { |
12 | 75 | buf = psbt.toBuffer(); |
13 | | - wrappedPsbt = Psbt.deserialize(buf); |
| 76 | + wrappedPsbt = toWrappedPsbt(buf); |
14 | 77 | }); |
15 | 78 |
|
16 | 79 | it("should map to same hex", function () { |
17 | | - assert.strictEqual( |
18 | | - buf.toString("hex"), |
19 | | - // it seems that the utxolib impl sometimes adds two extra bytes zero bytes at the end |
20 | | - // they probably are insignificant so we just add them here |
21 | | - Buffer.from(wrappedPsbt.serialize()).toString("hex") + (name === "empty" ? "0000" : ""), |
22 | | - ); |
| 80 | + assert.strictEqual(buf.toString("hex"), Buffer.from(wrappedPsbt.serialize()).toString("hex")); |
23 | 81 | }); |
24 | 82 |
|
25 | 83 | it("should round-trip utxolib -> ms -> utxolib", function () { |
26 | | - assert.strictEqual( |
27 | | - buf.toString("hex"), |
28 | | - utxolib.bitgo.UtxoPsbt.fromBuffer(Buffer.from(wrappedPsbt.serialize()), { |
29 | | - network: utxolib.networks.bitcoin, |
30 | | - }) |
31 | | - .toBuffer() |
32 | | - .toString("hex"), |
33 | | - ); |
| 84 | + assert.strictEqual(buf.toString("hex"), toUtxoPsbt(wrappedPsbt).toBuffer().toString("hex")); |
34 | 85 | }); |
| 86 | + |
| 87 | + if (stage === "bare") { |
| 88 | + describeUpdateInputWithDescriptor(psbt, scriptType); |
| 89 | + } |
35 | 90 | }); |
36 | 91 | }); |
0 commit comments