|
| 1 | +import assert from "node:assert"; |
| 2 | +import * as utxolib from "@bitgo/utxo-lib"; |
| 3 | +import { fixedScriptWallet } from "../../js/index.js"; |
| 4 | +import { |
| 5 | + loadPsbtFixture, |
| 6 | + getPsbtBuffer, |
| 7 | + getExtractedTransactionHex, |
| 8 | + type Fixture, |
| 9 | +} from "./fixtureUtil.js"; |
| 10 | + |
| 11 | +describe("finalize and extract transaction", function () { |
| 12 | + const supportedNetworks = utxolib.getNetworkList().filter((network) => { |
| 13 | + return ( |
| 14 | + utxolib.isMainnet(network) && |
| 15 | + network !== utxolib.networks.bitcoincash && |
| 16 | + network !== utxolib.networks.bitcoingold && |
| 17 | + network !== utxolib.networks.bitcoinsv && |
| 18 | + network !== utxolib.networks.ecash && |
| 19 | + network !== utxolib.networks.zcash |
| 20 | + ); |
| 21 | + }); |
| 22 | + |
| 23 | + supportedNetworks.forEach((network) => { |
| 24 | + const networkName = utxolib.getNetworkName(network); |
| 25 | + |
| 26 | + describe(`network: ${networkName}`, function () { |
| 27 | + let fullsignedFixture: Fixture; |
| 28 | + let fullsignedPsbtBuffer: Buffer; |
| 29 | + let fullsignedBitgoPsbt: fixedScriptWallet.BitGoPsbt; |
| 30 | + |
| 31 | + before(function () { |
| 32 | + fullsignedFixture = loadPsbtFixture(networkName, "fullsigned"); |
| 33 | + fullsignedPsbtBuffer = getPsbtBuffer(fullsignedFixture); |
| 34 | + fullsignedBitgoPsbt = fixedScriptWallet.BitGoPsbt.fromBytes( |
| 35 | + fullsignedPsbtBuffer, |
| 36 | + networkName, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should serialize and deserialize PSBT (round-trip)", function () { |
| 41 | + const serialized = fullsignedBitgoPsbt.serialize(); |
| 42 | + |
| 43 | + // Verify we can deserialize what we serialized (functional round-trip) |
| 44 | + const deserialized = fixedScriptWallet.BitGoPsbt.fromBytes(serialized, networkName); |
| 45 | + |
| 46 | + // Verify the deserialized PSBT has the same unsigned txid |
| 47 | + assert.strictEqual( |
| 48 | + deserialized.unsignedTxid(), |
| 49 | + fullsignedBitgoPsbt.unsignedTxid(), |
| 50 | + "Deserialized PSBT should have same unsigned txid after round-trip", |
| 51 | + ); |
| 52 | + |
| 53 | + // Verify the re-deserialized PSBT can be serialized back to bytes |
| 54 | + const reserialized = deserialized.serialize(); |
| 55 | + |
| 56 | + // Verify functional equivalence by deserializing again and checking txid |
| 57 | + const redeserialized = fixedScriptWallet.BitGoPsbt.fromBytes(reserialized, networkName); |
| 58 | + assert.strictEqual( |
| 59 | + redeserialized.unsignedTxid(), |
| 60 | + fullsignedBitgoPsbt.unsignedTxid(), |
| 61 | + "PSBT should maintain consistency through multiple serialize/deserialize cycles", |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should finalize all inputs and be extractable", function () { |
| 66 | + // Create a fresh instance for finalization |
| 67 | + const psbt = fixedScriptWallet.BitGoPsbt.fromBytes(fullsignedPsbtBuffer, networkName); |
| 68 | + |
| 69 | + // Finalize all inputs |
| 70 | + psbt.finalizeAllInputs(); |
| 71 | + |
| 72 | + // Serialize the finalized PSBT |
| 73 | + const serialized = psbt.serialize(); |
| 74 | + |
| 75 | + // Verify we can deserialize the finalized PSBT |
| 76 | + const deserialized = fixedScriptWallet.BitGoPsbt.fromBytes(serialized, networkName); |
| 77 | + |
| 78 | + // Verify it can be extracted (which confirms finalization worked) |
| 79 | + const extractedTx = deserialized.extractTransaction(); |
| 80 | + const extractedTxHex = Buffer.from(extractedTx).toString("hex"); |
| 81 | + const expectedTxHex = getExtractedTransactionHex(fullsignedFixture); |
| 82 | + |
| 83 | + assert.strictEqual( |
| 84 | + extractedTxHex, |
| 85 | + expectedTxHex, |
| 86 | + "Extracted transaction from finalized PSBT should match expected transaction", |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should extract transaction from finalized PSBT", function () { |
| 91 | + // Create a fresh instance for extraction |
| 92 | + const psbt = fixedScriptWallet.BitGoPsbt.fromBytes(fullsignedPsbtBuffer, networkName); |
| 93 | + |
| 94 | + // Finalize all inputs |
| 95 | + psbt.finalizeAllInputs(); |
| 96 | + |
| 97 | + // Extract transaction |
| 98 | + const extractedTx = psbt.extractTransaction(); |
| 99 | + const extractedTxHex = Buffer.from(extractedTx).toString("hex"); |
| 100 | + |
| 101 | + // Get expected transaction hex from fixture |
| 102 | + const expectedTxHex = getExtractedTransactionHex(fullsignedFixture); |
| 103 | + |
| 104 | + assert.strictEqual( |
| 105 | + extractedTxHex, |
| 106 | + expectedTxHex, |
| 107 | + "Extracted transaction should match expected transaction", |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments