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