forked from LedgerHQ/app-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_psbt.js
More file actions
142 lines (121 loc) · 4.91 KB
/
Copy pathprint_psbt.js
File metadata and controls
142 lines (121 loc) · 4.91 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { PsbtV2 } = require('ledger-bitcoin');
const { fromHex } = require('uint8array-tools');
const { fromBase64 } = require('uint8array-tools');
const bitcoin = require('bitcoinjs-lib');
let {
toHex
} = require('./utils.js');
const H = 0x80000000;
const unsafeToHex = toHex;
toHex = function (buffer) {
if (!buffer) {
return 'undefined';
}
return unsafeToHex(buffer);
}
Object.defineProperty(String.prototype, 'capitalize', {
value: function() {
return this.charAt(0).toUpperCase() + this.slice(1);
},
enumerable: false
});
function pathLevelToString(pathLevel) {
return (pathLevel & H) ? `${pathLevel & ~H}'` : `${pathLevel}`;
}
function safe(f) {
try {
return f();
} catch (e) {
return 'undefined';
}
}
function witnessUtxoToString(witnessUtxo) {
return (`\n Amount: ${witnessUtxo.amount}\n Script: ${toHex(witnessUtxo.scriptPubKey)}`);
}
function printBip32Derivations(psbt, in_out, prefix, index) {
const funcName = `get${in_out.capitalize()}Bip32Derivation`;
const mapName = `${in_out}Maps`;
let firstDerivation = true;
const map = psbt[mapName][index];
if (!map) {
return;
}
for (const pubkey of map.keys()) {
if (!pubkey.startsWith(prefix))
continue;
if (firstDerivation)
console.log(` Bip32 derivations:`);
console.log(` Public key: ${pubkey.slice(2)}`);
let derivation = psbt[funcName](index, fromHex(pubkey.slice(2)));
if (!derivation)
continue;
let path = derivation.path.map(pathLevelToString).join('/');
console.log(` Path: [${toHex(derivation.masterFingerprint)}]${path}`);
firstDerivation = false;
}
}
function printInputPartialSigs(psbt, index) {
console.log(' Partial signatures: (not supported by the printer)');
}
function printPSBT(psbt, network) {
// getInputPartialSig(inputIndex: number, pubkey: Buffer): Buffer | undefined;
// getInputTapKeySig(inputIndex: number): Buffer | undefined;
// getInputTapBip32Derivation(inputIndex: number, pubkey: Buffer): {
// getOutputRedeemScript(outputIndex: number): Buffer;
// getOutputBip32Derivation(outputIndex: number, pubkey: Buffer): {
// getOutputAmount(outputIndex: number): number;
// getOutputScript(outputIndex: number): Buffer;
// getOutputTapBip32Derivation(outputIndex: number, pubkey: Buffer): {
console.log('Global');
console.log(` Tx version: ${psbt.getGlobalTxVersion()}`);
console.log(` Locktime: ${psbt.getGlobalFallbackLocktime()}`);
console.log(` Input count: ${psbt.getGlobalInputCount()}`);
console.log(` Output count: ${psbt.getGlobalOutputCount()}`);
console.log(` PSBT version: ${psbt.getGlobalPsbtVersion()}`);
console.log(` Modifiable: ${toHex(psbt.getGlobalTxModifiable())}`);
console.log();
console.log('Inputs');
for (let i = 0; i < psbt.getGlobalInputCount(); i++) {
console.log(` Input ${i}`);
console.log(` Previous txid: ${toHex(psbt.getInputPreviousTxid(i))}`);
console.log(` Output index: ${psbt.getInputOutputIndex(i)}`);
console.log(` Sequence: ${psbt.getInputSequence(i)}`);
console.log(` Sighash type: ${psbt.getInputSighashType(i)}`);
console.log(` Final scriptsig: ${toHex(psbt.getInputFinalScriptsig(i))}`);
console.log(` Final witness: ${safe(() => toHex(psbt.getInputFinalScriptwitness(i)))}`);
console.log(` Non-witness UTXO: ${safe(() => toHex(psbt.getInputNonWitnessUtxo(i)))}`);
console.log(` Witness UTXO: ${witnessUtxoToString(psbt.getInputWitnessUtxo(i))}`);
console.log(` Redeem script: ${safe(() => toHex(psbt.getInputRedeemScript(i)))}`);
printBip32Derivations(psbt, 'input', '06', i);
printBip32Derivations(psbt, 'input', '16', i);
console.log(` Witness script: ${safe(() => toHex(psbt.getInputWitnessScript(i)))}`);
console.log(` Final scriptsig: ${safe(() => toHex(psbt.getInputFinalScriptsig(i)))}`);
console.log(` Final witness: ${safe(() => toHex(psbt.getInputFinalScriptwitness(i)))}`);
printInputPartialSigs(psbt, i);
console.log();
}
console.log('Outputs');
for (let i = 0; i < psbt.getGlobalOutputCount(); i++) {
console.log(` Output ${i}`);
console.log(` Amount: ${psbt.getOutputAmount(i)}`);
console.log(` Script: ${toHex(psbt.getOutputScript(i))}`);
printBip32Derivations(psbt, 'output', '02', i);
console.log();
}
}
if (require.main === module) {
if (process.argv.length < 3) {
console.error('Usage: node print_psbt.js base64');
process.exit(1);
}
try {
psbt = new PsbtV2()
psbt.deserialize(Buffer.copyBytesFrom(fromBase64(process.argv[2])))
printPSBT(psbt, bitcoin.networks.testnet);
} catch (error) {
console.error('Error:', error);
}
}
module.exports = {
printPSBT
};