@ledgerhq/hw-app-btc 10.0.1 → 10.0.2-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/Btc.d.ts.map +1 -1
- package/lib/Btc.js +72 -97
- package/lib/Btc.js.map +1 -1
- package/lib/BtcNew.js +217 -313
- package/lib/BtcNew.js.map +1 -1
- package/lib/BtcOld.js +46 -106
- package/lib/BtcOld.js.map +1 -1
- package/lib/bip32.js +12 -12
- package/lib/bip32.js.map +1 -1
- package/lib/buffertools.js +66 -69
- package/lib/buffertools.js.map +1 -1
- package/lib/compressPublicKey.js +3 -3
- package/lib/compressPublicKey.js.map +1 -1
- package/lib/constants.js +1 -1
- package/lib/createTransaction.d.ts +1 -1
- package/lib/createTransaction.d.ts.map +1 -1
- package/lib/createTransaction.js +285 -398
- package/lib/createTransaction.js.map +1 -1
- package/lib/debug.js +11 -13
- package/lib/debug.js.map +1 -1
- package/lib/finalizeInput.js +23 -62
- package/lib/finalizeInput.js.map +1 -1
- package/lib/getAppAndVersion.d.ts +1 -1
- package/lib/getAppAndVersion.d.ts.map +1 -1
- package/lib/getAppAndVersion.js +29 -72
- package/lib/getAppAndVersion.js.map +1 -1
- package/lib/getTrustedInput.js +108 -251
- package/lib/getTrustedInput.js.map +1 -1
- package/lib/getTrustedInputBIP143.js +9 -10
- package/lib/getTrustedInputBIP143.js.map +1 -1
- package/lib/getWalletPublicKey.d.ts +1 -1
- package/lib/getWalletPublicKey.d.ts.map +1 -1
- package/lib/getWalletPublicKey.js +27 -73
- package/lib/getWalletPublicKey.js.map +1 -1
- package/lib/hashPublicKey.js +4 -4
- package/lib/hashPublicKey.js.map +1 -1
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/lib/newops/accounttype.d.ts +2 -2
- package/lib/newops/accounttype.d.ts.map +1 -1
- package/lib/newops/accounttype.js +85 -125
- package/lib/newops/accounttype.js.map +1 -1
- package/lib/newops/appClient.js +98 -205
- package/lib/newops/appClient.js.map +1 -1
- package/lib/newops/clientCommands.js +122 -213
- package/lib/newops/clientCommands.js.map +1 -1
- package/lib/newops/merkelizedPsbt.js +28 -75
- package/lib/newops/merkelizedPsbt.js.map +1 -1
- package/lib/newops/merkle.js +38 -67
- package/lib/newops/merkle.js.map +1 -1
- package/lib/newops/merkleMap.js +11 -12
- package/lib/newops/merkleMap.js.map +1 -1
- package/lib/newops/policy.d.ts +1 -1
- package/lib/newops/policy.d.ts.map +1 -1
- package/lib/newops/policy.js +17 -18
- package/lib/newops/policy.js.map +1 -1
- package/lib/newops/psbtExtractor.js +9 -9
- package/lib/newops/psbtExtractor.js.map +1 -1
- package/lib/newops/psbtFinalizer.js +22 -22
- package/lib/newops/psbtFinalizer.js.map +1 -1
- package/lib/newops/psbtv2.d.ts +1 -1
- package/lib/newops/psbtv2.d.ts.map +1 -1
- package/lib/newops/psbtv2.js +227 -286
- package/lib/newops/psbtv2.js.map +1 -1
- package/lib/serializeTransaction.js +13 -15
- package/lib/serializeTransaction.js.map +1 -1
- package/lib/shouldUseTrustedInputForSegwit.js +4 -5
- package/lib/shouldUseTrustedInputForSegwit.js.map +1 -1
- package/lib/signMessage.js +47 -99
- package/lib/signMessage.js.map +1 -1
- package/lib/signP2SHTransaction.d.ts +1 -1
- package/lib/signP2SHTransaction.d.ts.map +1 -1
- package/lib/signP2SHTransaction.js +91 -187
- package/lib/signP2SHTransaction.js.map +1 -1
- package/lib/signTransaction.js +8 -9
- package/lib/signTransaction.js.map +1 -1
- package/lib/splitTransaction.js +50 -54
- package/lib/splitTransaction.js.map +1 -1
- package/lib/startUntrustedHashTransactionInput.js +65 -167
- package/lib/startUntrustedHashTransactionInput.js.map +1 -1
- package/lib/types.js +1 -1
- package/lib/varint.js +10 -10
- package/lib/varint.js.map +1 -1
- package/lib-es/Btc.d.ts.map +1 -1
- package/lib-es/Btc.js +58 -84
- package/lib-es/Btc.js.map +1 -1
- package/lib-es/BtcNew.js +205 -302
- package/lib-es/BtcNew.js.map +1 -1
- package/lib-es/BtcOld.js +35 -96
- package/lib-es/BtcOld.js.map +1 -1
- package/lib-es/bip32.js +7 -7
- package/lib-es/bip32.js.map +1 -1
- package/lib-es/buffertools.js +62 -67
- package/lib-es/buffertools.js.map +1 -1
- package/lib-es/compressPublicKey.js +2 -2
- package/lib-es/compressPublicKey.js.map +1 -1
- package/lib-es/constants.js +12 -12
- package/lib-es/constants.js.map +1 -1
- package/lib-es/createTransaction.d.ts +1 -1
- package/lib-es/createTransaction.d.ts.map +1 -1
- package/lib-es/createTransaction.js +271 -384
- package/lib-es/createTransaction.js.map +1 -1
- package/lib-es/debug.js +10 -12
- package/lib-es/debug.js.map +1 -1
- package/lib-es/finalizeInput.js +20 -59
- package/lib-es/finalizeInput.js.map +1 -1
- package/lib-es/getAppAndVersion.d.ts +1 -1
- package/lib-es/getAppAndVersion.d.ts.map +1 -1
- package/lib-es/getAppAndVersion.js +27 -70
- package/lib-es/getAppAndVersion.js.map +1 -1
- package/lib-es/getTrustedInput.js +104 -247
- package/lib-es/getTrustedInput.js.map +1 -1
- package/lib-es/getTrustedInputBIP143.js +5 -6
- package/lib-es/getTrustedInputBIP143.js.map +1 -1
- package/lib-es/getWalletPublicKey.d.ts +1 -1
- package/lib-es/getWalletPublicKey.d.ts.map +1 -1
- package/lib-es/getWalletPublicKey.js +25 -71
- package/lib-es/getWalletPublicKey.js.map +1 -1
- package/lib-es/newops/accounttype.d.ts +2 -2
- package/lib-es/newops/accounttype.d.ts.map +1 -1
- package/lib-es/newops/accounttype.js +79 -123
- package/lib-es/newops/accounttype.js.map +1 -1
- package/lib-es/newops/appClient.js +92 -200
- package/lib-es/newops/appClient.js.map +1 -1
- package/lib-es/newops/clientCommands.js +117 -214
- package/lib-es/newops/clientCommands.js.map +1 -1
- package/lib-es/newops/merkelizedPsbt.js +25 -73
- package/lib-es/newops/merkelizedPsbt.js.map +1 -1
- package/lib-es/newops/merkle.js +36 -66
- package/lib-es/newops/merkle.js.map +1 -1
- package/lib-es/newops/merkleMap.js +8 -10
- package/lib-es/newops/merkleMap.js.map +1 -1
- package/lib-es/newops/policy.d.ts +1 -1
- package/lib-es/newops/policy.d.ts.map +1 -1
- package/lib-es/newops/policy.js +12 -14
- package/lib-es/newops/policy.js.map +1 -1
- package/lib-es/newops/psbtExtractor.js +7 -7
- package/lib-es/newops/psbtExtractor.js.map +1 -1
- package/lib-es/newops/psbtFinalizer.js +19 -19
- package/lib-es/newops/psbtFinalizer.js.map +1 -1
- package/lib-es/newops/psbtv2.d.ts +1 -1
- package/lib-es/newops/psbtv2.d.ts.map +1 -1
- package/lib-es/newops/psbtv2.js +225 -286
- package/lib-es/newops/psbtv2.js.map +1 -1
- package/lib-es/serializeTransaction.js +11 -13
- package/lib-es/serializeTransaction.js.map +1 -1
- package/lib-es/shouldUseTrustedInputForSegwit.js +1 -2
- package/lib-es/shouldUseTrustedInputForSegwit.js.map +1 -1
- package/lib-es/signMessage.js +44 -96
- package/lib-es/signMessage.js.map +1 -1
- package/lib-es/signP2SHTransaction.d.ts +1 -1
- package/lib-es/signP2SHTransaction.d.ts.map +1 -1
- package/lib-es/signP2SHTransaction.js +84 -180
- package/lib-es/signP2SHTransaction.js.map +1 -1
- package/lib-es/signTransaction.js +6 -7
- package/lib-es/signTransaction.js.map +1 -1
- package/lib-es/splitTransaction.js +46 -50
- package/lib-es/splitTransaction.js.map +1 -1
- package/lib-es/startUntrustedHashTransactionInput.js +62 -164
- package/lib-es/startUntrustedHashTransactionInput.js.map +1 -1
- package/lib-es/varint.js +9 -9
- package/lib-es/varint.js.map +1 -1
- package/package.json +5 -6
- package/src/Btc.ts +28 -5
package/lib/newops/psbtv2.js
CHANGED
|
@@ -1,46 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __values = (this && this.__values) || function(o) {
|
|
29
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
30
|
-
if (m) return m.call(o);
|
|
31
|
-
if (o && typeof o.length === "number") return {
|
|
32
|
-
next: function () {
|
|
33
|
-
if (o && i >= o.length) o = void 0;
|
|
34
|
-
return { value: o && o[i++], done: !o };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
38
|
-
};
|
|
39
|
-
exports.__esModule = true;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
3
|
exports.PsbtV2 = exports.NoSuchEntry = exports.psbtOut = exports.psbtIn = exports.psbtGlobal = void 0;
|
|
41
4
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
42
5
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
43
|
-
|
|
6
|
+
const buffertools_1 = require("../buffertools");
|
|
44
7
|
var psbtGlobal;
|
|
45
8
|
(function (psbtGlobal) {
|
|
46
9
|
psbtGlobal[psbtGlobal["TX_VERSION"] = 2] = "TX_VERSION";
|
|
@@ -74,14 +37,9 @@ var psbtOut;
|
|
|
74
37
|
psbtOut[psbtOut["SCRIPT"] = 4] = "SCRIPT";
|
|
75
38
|
psbtOut[psbtOut["TAP_BIP32_DERIVATION"] = 7] = "TAP_BIP32_DERIVATION";
|
|
76
39
|
})(psbtOut = exports.psbtOut || (exports.psbtOut = {}));
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
function NoSuchEntry() {
|
|
81
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
82
|
-
}
|
|
83
|
-
return NoSuchEntry;
|
|
84
|
-
}(Error));
|
|
40
|
+
const PSBT_MAGIC_BYTES = Buffer.from([0x70, 0x73, 0x62, 0x74, 0xff]);
|
|
41
|
+
class NoSuchEntry extends Error {
|
|
42
|
+
}
|
|
85
43
|
exports.NoSuchEntry = NoSuchEntry;
|
|
86
44
|
/**
|
|
87
45
|
* Implements Partially Signed Bitcoin Transaction version 2, BIP370, as
|
|
@@ -101,345 +59,341 @@ exports.NoSuchEntry = NoSuchEntry;
|
|
|
101
59
|
* This class is made purposefully dumb, so it's easy to add support for
|
|
102
60
|
* complemantary fields as needed in the future.
|
|
103
61
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
62
|
+
class PsbtV2 {
|
|
63
|
+
constructor() {
|
|
106
64
|
this.globalMap = new Map();
|
|
107
65
|
this.inputMaps = [];
|
|
108
66
|
this.outputMaps = [];
|
|
109
67
|
}
|
|
110
|
-
|
|
68
|
+
setGlobalTxVersion(version) {
|
|
111
69
|
this.setGlobal(psbtGlobal.TX_VERSION, uint32LE(version));
|
|
112
|
-
}
|
|
113
|
-
|
|
70
|
+
}
|
|
71
|
+
getGlobalTxVersion() {
|
|
114
72
|
return this.getGlobal(psbtGlobal.TX_VERSION).readUInt32LE(0);
|
|
115
|
-
}
|
|
116
|
-
|
|
73
|
+
}
|
|
74
|
+
setGlobalFallbackLocktime(locktime) {
|
|
117
75
|
this.setGlobal(psbtGlobal.FALLBACK_LOCKTIME, uint32LE(locktime));
|
|
118
|
-
}
|
|
119
|
-
|
|
76
|
+
}
|
|
77
|
+
getGlobalFallbackLocktime() {
|
|
120
78
|
var _a;
|
|
121
79
|
return (_a = this.getGlobalOptional(psbtGlobal.FALLBACK_LOCKTIME)) === null || _a === void 0 ? void 0 : _a.readUInt32LE(0);
|
|
122
|
-
}
|
|
123
|
-
|
|
80
|
+
}
|
|
81
|
+
setGlobalInputCount(inputCount) {
|
|
124
82
|
this.setGlobal(psbtGlobal.INPUT_COUNT, varint(inputCount));
|
|
125
|
-
}
|
|
126
|
-
|
|
83
|
+
}
|
|
84
|
+
getGlobalInputCount() {
|
|
127
85
|
return fromVarint(this.getGlobal(psbtGlobal.INPUT_COUNT));
|
|
128
|
-
}
|
|
129
|
-
|
|
86
|
+
}
|
|
87
|
+
setGlobalOutputCount(outputCount) {
|
|
130
88
|
this.setGlobal(psbtGlobal.OUTPUT_COUNT, varint(outputCount));
|
|
131
|
-
}
|
|
132
|
-
|
|
89
|
+
}
|
|
90
|
+
getGlobalOutputCount() {
|
|
133
91
|
return fromVarint(this.getGlobal(psbtGlobal.OUTPUT_COUNT));
|
|
134
|
-
}
|
|
135
|
-
|
|
92
|
+
}
|
|
93
|
+
setGlobalTxModifiable(byte) {
|
|
136
94
|
this.setGlobal(psbtGlobal.TX_MODIFIABLE, byte);
|
|
137
|
-
}
|
|
138
|
-
|
|
95
|
+
}
|
|
96
|
+
getGlobalTxModifiable() {
|
|
139
97
|
return this.getGlobalOptional(psbtGlobal.TX_MODIFIABLE);
|
|
140
|
-
}
|
|
141
|
-
|
|
98
|
+
}
|
|
99
|
+
setGlobalPsbtVersion(psbtVersion) {
|
|
142
100
|
this.setGlobal(psbtGlobal.VERSION, uint32LE(psbtVersion));
|
|
143
|
-
}
|
|
144
|
-
|
|
101
|
+
}
|
|
102
|
+
getGlobalPsbtVersion() {
|
|
145
103
|
return this.getGlobal(psbtGlobal.VERSION).readUInt32LE(0);
|
|
146
|
-
}
|
|
147
|
-
|
|
104
|
+
}
|
|
105
|
+
setInputNonWitnessUtxo(inputIndex, transaction) {
|
|
148
106
|
this.setInput(inputIndex, psbtIn.NON_WITNESS_UTXO, b(), transaction);
|
|
149
|
-
}
|
|
150
|
-
|
|
107
|
+
}
|
|
108
|
+
getInputNonWitnessUtxo(inputIndex) {
|
|
151
109
|
return this.getInputOptional(inputIndex, psbtIn.NON_WITNESS_UTXO, b());
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
110
|
+
}
|
|
111
|
+
setInputWitnessUtxo(inputIndex, amount, scriptPubKey) {
|
|
112
|
+
const buf = new buffertools_1.BufferWriter();
|
|
155
113
|
buf.writeSlice(amount);
|
|
156
114
|
buf.writeVarSlice(scriptPubKey);
|
|
157
115
|
this.setInput(inputIndex, psbtIn.WITNESS_UTXO, b(), buf.buffer());
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
116
|
+
}
|
|
117
|
+
getInputWitnessUtxo(inputIndex) {
|
|
118
|
+
const utxo = this.getInputOptional(inputIndex, psbtIn.WITNESS_UTXO, b());
|
|
161
119
|
if (!utxo)
|
|
162
120
|
return undefined;
|
|
163
|
-
|
|
121
|
+
const buf = new buffertools_1.BufferReader(utxo);
|
|
164
122
|
return { amount: buf.readSlice(8), scriptPubKey: buf.readVarSlice() };
|
|
165
|
-
}
|
|
166
|
-
|
|
123
|
+
}
|
|
124
|
+
setInputPartialSig(inputIndex, pubkey, signature) {
|
|
167
125
|
this.setInput(inputIndex, psbtIn.PARTIAL_SIG, pubkey, signature);
|
|
168
|
-
}
|
|
169
|
-
|
|
126
|
+
}
|
|
127
|
+
getInputPartialSig(inputIndex, pubkey) {
|
|
170
128
|
return this.getInputOptional(inputIndex, psbtIn.PARTIAL_SIG, pubkey);
|
|
171
|
-
}
|
|
172
|
-
|
|
129
|
+
}
|
|
130
|
+
setInputSighashType(inputIndex, sigHashtype) {
|
|
173
131
|
this.setInput(inputIndex, psbtIn.SIGHASH_TYPE, b(), uint32LE(sigHashtype));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
132
|
+
}
|
|
133
|
+
getInputSighashType(inputIndex) {
|
|
134
|
+
const result = this.getInputOptional(inputIndex, psbtIn.SIGHASH_TYPE, b());
|
|
177
135
|
if (!result)
|
|
178
136
|
return undefined;
|
|
179
137
|
return result.readUInt32LE(0);
|
|
180
|
-
}
|
|
181
|
-
|
|
138
|
+
}
|
|
139
|
+
setInputRedeemScript(inputIndex, redeemScript) {
|
|
182
140
|
this.setInput(inputIndex, psbtIn.REDEEM_SCRIPT, b(), redeemScript);
|
|
183
|
-
}
|
|
184
|
-
|
|
141
|
+
}
|
|
142
|
+
getInputRedeemScript(inputIndex) {
|
|
185
143
|
return this.getInputOptional(inputIndex, psbtIn.REDEEM_SCRIPT, b());
|
|
186
|
-
}
|
|
187
|
-
|
|
144
|
+
}
|
|
145
|
+
setInputBip32Derivation(inputIndex, pubkey, masterFingerprint, path) {
|
|
188
146
|
if (pubkey.length != 33)
|
|
189
147
|
throw new Error("Invalid pubkey length: " + pubkey.length);
|
|
190
148
|
this.setInput(inputIndex, psbtIn.BIP32_DERIVATION, pubkey, this.encodeBip32Derivation(masterFingerprint, path));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
149
|
+
}
|
|
150
|
+
getInputBip32Derivation(inputIndex, pubkey) {
|
|
151
|
+
const buf = this.getInputOptional(inputIndex, psbtIn.BIP32_DERIVATION, pubkey);
|
|
194
152
|
if (!buf)
|
|
195
153
|
return undefined;
|
|
196
154
|
return this.decodeBip32Derivation(buf);
|
|
197
|
-
}
|
|
198
|
-
|
|
155
|
+
}
|
|
156
|
+
setInputFinalScriptsig(inputIndex, scriptSig) {
|
|
199
157
|
this.setInput(inputIndex, psbtIn.FINAL_SCRIPTSIG, b(), scriptSig);
|
|
200
|
-
}
|
|
201
|
-
|
|
158
|
+
}
|
|
159
|
+
getInputFinalScriptsig(inputIndex) {
|
|
202
160
|
return this.getInputOptional(inputIndex, psbtIn.FINAL_SCRIPTSIG, b());
|
|
203
|
-
}
|
|
204
|
-
|
|
161
|
+
}
|
|
162
|
+
setInputFinalScriptwitness(inputIndex, scriptWitness) {
|
|
205
163
|
this.setInput(inputIndex, psbtIn.FINAL_SCRIPTWITNESS, b(), scriptWitness);
|
|
206
|
-
}
|
|
207
|
-
|
|
164
|
+
}
|
|
165
|
+
getInputFinalScriptwitness(inputIndex) {
|
|
208
166
|
return this.getInput(inputIndex, psbtIn.FINAL_SCRIPTWITNESS, b());
|
|
209
|
-
}
|
|
210
|
-
|
|
167
|
+
}
|
|
168
|
+
setInputPreviousTxId(inputIndex, txid) {
|
|
211
169
|
this.setInput(inputIndex, psbtIn.PREVIOUS_TXID, b(), txid);
|
|
212
|
-
}
|
|
213
|
-
|
|
170
|
+
}
|
|
171
|
+
getInputPreviousTxid(inputIndex) {
|
|
214
172
|
return this.getInput(inputIndex, psbtIn.PREVIOUS_TXID, b());
|
|
215
|
-
}
|
|
216
|
-
|
|
173
|
+
}
|
|
174
|
+
setInputOutputIndex(inputIndex, outputIndex) {
|
|
217
175
|
this.setInput(inputIndex, psbtIn.OUTPUT_INDEX, b(), uint32LE(outputIndex));
|
|
218
|
-
}
|
|
219
|
-
|
|
176
|
+
}
|
|
177
|
+
getInputOutputIndex(inputIndex) {
|
|
220
178
|
return this.getInput(inputIndex, psbtIn.OUTPUT_INDEX, b()).readUInt32LE(0);
|
|
221
|
-
}
|
|
222
|
-
|
|
179
|
+
}
|
|
180
|
+
setInputSequence(inputIndex, sequence) {
|
|
223
181
|
this.setInput(inputIndex, psbtIn.SEQUENCE, b(), uint32LE(sequence));
|
|
224
|
-
}
|
|
225
|
-
|
|
182
|
+
}
|
|
183
|
+
getInputSequence(inputIndex) {
|
|
226
184
|
var _a, _b;
|
|
227
185
|
return ((_b = (_a = this.getInputOptional(inputIndex, psbtIn.SEQUENCE, b())) === null || _a === void 0 ? void 0 : _a.readUInt32LE(0)) !== null && _b !== void 0 ? _b : 0xffffffff);
|
|
228
|
-
}
|
|
229
|
-
|
|
186
|
+
}
|
|
187
|
+
setInputTapKeySig(inputIndex, sig) {
|
|
230
188
|
this.setInput(inputIndex, psbtIn.TAP_KEY_SIG, b(), sig);
|
|
231
|
-
}
|
|
232
|
-
|
|
189
|
+
}
|
|
190
|
+
getInputTapKeySig(inputIndex) {
|
|
233
191
|
return this.getInputOptional(inputIndex, psbtIn.TAP_KEY_SIG, b());
|
|
234
|
-
}
|
|
235
|
-
|
|
192
|
+
}
|
|
193
|
+
setInputTapBip32Derivation(inputIndex, pubkey, hashes, masterFingerprint, path) {
|
|
236
194
|
if (pubkey.length != 32)
|
|
237
195
|
throw new Error("Invalid pubkey length: " + pubkey.length);
|
|
238
|
-
|
|
196
|
+
const buf = this.encodeTapBip32Derivation(hashes, masterFingerprint, path);
|
|
239
197
|
this.setInput(inputIndex, psbtIn.TAP_BIP32_DERIVATION, pubkey, buf);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
198
|
+
}
|
|
199
|
+
getInputTapBip32Derivation(inputIndex, pubkey) {
|
|
200
|
+
const buf = this.getInput(inputIndex, psbtIn.TAP_BIP32_DERIVATION, pubkey);
|
|
243
201
|
return this.decodeTapBip32Derivation(buf);
|
|
244
|
-
}
|
|
245
|
-
|
|
202
|
+
}
|
|
203
|
+
getInputKeyDatas(inputIndex, keyType) {
|
|
246
204
|
return this.getKeyDatas(this.inputMaps[inputIndex], keyType);
|
|
247
|
-
}
|
|
248
|
-
|
|
205
|
+
}
|
|
206
|
+
setOutputRedeemScript(outputIndex, redeemScript) {
|
|
249
207
|
this.setOutput(outputIndex, psbtOut.REDEEM_SCRIPT, b(), redeemScript);
|
|
250
|
-
}
|
|
251
|
-
|
|
208
|
+
}
|
|
209
|
+
getOutputRedeemScript(outputIndex) {
|
|
252
210
|
return this.getOutput(outputIndex, psbtOut.REDEEM_SCRIPT, b());
|
|
253
|
-
}
|
|
254
|
-
|
|
211
|
+
}
|
|
212
|
+
setOutputBip32Derivation(outputIndex, pubkey, masterFingerprint, path) {
|
|
255
213
|
this.setOutput(outputIndex, psbtOut.BIP_32_DERIVATION, pubkey, this.encodeBip32Derivation(masterFingerprint, path));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
214
|
+
}
|
|
215
|
+
getOutputBip32Derivation(outputIndex, pubkey) {
|
|
216
|
+
const buf = this.getOutput(outputIndex, psbtOut.BIP_32_DERIVATION, pubkey);
|
|
259
217
|
return this.decodeBip32Derivation(buf);
|
|
260
|
-
}
|
|
261
|
-
|
|
218
|
+
}
|
|
219
|
+
setOutputAmount(outputIndex, amount) {
|
|
262
220
|
this.setOutput(outputIndex, psbtOut.AMOUNT, b(), uint64LE(amount));
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
221
|
+
}
|
|
222
|
+
getOutputAmount(outputIndex) {
|
|
223
|
+
const buf = this.getOutput(outputIndex, psbtOut.AMOUNT, b());
|
|
266
224
|
return (0, buffertools_1.unsafeFrom64bitLE)(buf);
|
|
267
|
-
}
|
|
268
|
-
|
|
225
|
+
}
|
|
226
|
+
setOutputScript(outputIndex, scriptPubKey) {
|
|
269
227
|
this.setOutput(outputIndex, psbtOut.SCRIPT, b(), scriptPubKey);
|
|
270
|
-
}
|
|
271
|
-
|
|
228
|
+
}
|
|
229
|
+
getOutputScript(outputIndex) {
|
|
272
230
|
return this.getOutput(outputIndex, psbtOut.SCRIPT, b());
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
231
|
+
}
|
|
232
|
+
setOutputTapBip32Derivation(outputIndex, pubkey, hashes, fingerprint, path) {
|
|
233
|
+
const buf = this.encodeTapBip32Derivation(hashes, fingerprint, path);
|
|
276
234
|
this.setOutput(outputIndex, psbtOut.TAP_BIP32_DERIVATION, pubkey, buf);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
|
|
235
|
+
}
|
|
236
|
+
getOutputTapBip32Derivation(outputIndex, pubkey) {
|
|
237
|
+
const buf = this.getOutput(outputIndex, psbtOut.TAP_BIP32_DERIVATION, pubkey);
|
|
280
238
|
return this.decodeTapBip32Derivation(buf);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
m["delete"](k);
|
|
239
|
+
}
|
|
240
|
+
deleteInputEntries(inputIndex, keyTypes) {
|
|
241
|
+
const map = this.inputMaps[inputIndex];
|
|
242
|
+
map.forEach((_v, k, m) => {
|
|
243
|
+
if (this.isKeyType(k, keyTypes)) {
|
|
244
|
+
m.delete(k);
|
|
288
245
|
}
|
|
289
246
|
});
|
|
290
|
-
}
|
|
291
|
-
|
|
247
|
+
}
|
|
248
|
+
copy(to) {
|
|
292
249
|
this.copyMap(this.globalMap, to.globalMap);
|
|
293
250
|
this.copyMaps(this.inputMaps, to.inputMaps);
|
|
294
251
|
this.copyMaps(this.outputMaps, to.outputMaps);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
_this.copyMap(m, to_index);
|
|
252
|
+
}
|
|
253
|
+
copyMaps(from, to) {
|
|
254
|
+
from.forEach((m, index) => {
|
|
255
|
+
const to_index = new Map();
|
|
256
|
+
this.copyMap(m, to_index);
|
|
301
257
|
to[index] = to_index;
|
|
302
258
|
});
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
from.forEach(
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
259
|
+
}
|
|
260
|
+
copyMap(from, to) {
|
|
261
|
+
from.forEach((v, k) => to.set(k, Buffer.from(v)));
|
|
262
|
+
}
|
|
263
|
+
serialize() {
|
|
264
|
+
const buf = new buffertools_1.BufferWriter();
|
|
309
265
|
buf.writeSlice(Buffer.from([0x70, 0x73, 0x62, 0x74, 0xff]));
|
|
310
266
|
serializeMap(buf, this.globalMap);
|
|
311
|
-
this.inputMaps.forEach(
|
|
267
|
+
this.inputMaps.forEach((map) => {
|
|
312
268
|
serializeMap(buf, map);
|
|
313
269
|
});
|
|
314
|
-
this.outputMaps.forEach(
|
|
270
|
+
this.outputMaps.forEach((map) => {
|
|
315
271
|
serializeMap(buf, map);
|
|
316
272
|
});
|
|
317
273
|
return buf.buffer();
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
274
|
+
}
|
|
275
|
+
deserialize(psbt) {
|
|
276
|
+
const buf = new buffertools_1.BufferReader(psbt);
|
|
321
277
|
if (!buf.readSlice(5).equals(PSBT_MAGIC_BYTES)) {
|
|
322
278
|
throw new Error("Invalid magic bytes");
|
|
323
279
|
}
|
|
324
280
|
while (this.readKeyPair(this.globalMap, buf))
|
|
325
281
|
;
|
|
326
|
-
for (
|
|
282
|
+
for (let i = 0; i < this.getGlobalInputCount(); i++) {
|
|
327
283
|
this.inputMaps[i] = new Map();
|
|
328
284
|
while (this.readKeyPair(this.inputMaps[i], buf))
|
|
329
285
|
;
|
|
330
286
|
}
|
|
331
|
-
for (
|
|
287
|
+
for (let i = 0; i < this.getGlobalOutputCount(); i++) {
|
|
332
288
|
this.outputMaps[i] = new Map();
|
|
333
289
|
while (this.readKeyPair(this.outputMaps[i], buf))
|
|
334
290
|
;
|
|
335
291
|
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
292
|
+
}
|
|
293
|
+
readKeyPair(map, buf) {
|
|
294
|
+
const keyLen = buf.readVarInt();
|
|
339
295
|
if (keyLen == 0) {
|
|
340
296
|
return false;
|
|
341
297
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
298
|
+
const keyType = buf.readUInt8();
|
|
299
|
+
const keyData = buf.readSlice(keyLen - 1);
|
|
300
|
+
const value = buf.readVarSlice();
|
|
345
301
|
set(map, keyType, keyData, value);
|
|
346
302
|
return true;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (_this.isKeyType(k, [keyType])) {
|
|
303
|
+
}
|
|
304
|
+
getKeyDatas(map, keyType) {
|
|
305
|
+
const result = [];
|
|
306
|
+
map.forEach((_v, k) => {
|
|
307
|
+
if (this.isKeyType(k, [keyType])) {
|
|
353
308
|
result.push(Buffer.from(k.substring(2), "hex"));
|
|
354
309
|
}
|
|
355
310
|
});
|
|
356
311
|
return result;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
return keyTypes.some(
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
312
|
+
}
|
|
313
|
+
isKeyType(hexKey, keyTypes) {
|
|
314
|
+
const keyType = Buffer.from(hexKey.substring(0, 2), "hex").readUInt8(0);
|
|
315
|
+
return keyTypes.some((k) => k == keyType);
|
|
316
|
+
}
|
|
317
|
+
setGlobal(keyType, value) {
|
|
318
|
+
const key = new Key(keyType, Buffer.from([]));
|
|
364
319
|
this.globalMap.set(key.toString(), value);
|
|
365
|
-
}
|
|
366
|
-
|
|
320
|
+
}
|
|
321
|
+
getGlobal(keyType) {
|
|
367
322
|
return get(this.globalMap, keyType, b(), false);
|
|
368
|
-
}
|
|
369
|
-
|
|
323
|
+
}
|
|
324
|
+
getGlobalOptional(keyType) {
|
|
370
325
|
return get(this.globalMap, keyType, b(), true);
|
|
371
|
-
}
|
|
372
|
-
|
|
326
|
+
}
|
|
327
|
+
setInput(index, keyType, keyData, value) {
|
|
373
328
|
set(this.getMap(index, this.inputMaps), keyType, keyData, value);
|
|
374
|
-
}
|
|
375
|
-
|
|
329
|
+
}
|
|
330
|
+
getInput(index, keyType, keyData) {
|
|
376
331
|
return get(this.inputMaps[index], keyType, keyData, false);
|
|
377
|
-
}
|
|
378
|
-
|
|
332
|
+
}
|
|
333
|
+
getInputOptional(index, keyType, keyData) {
|
|
379
334
|
return get(this.inputMaps[index], keyType, keyData, true);
|
|
380
|
-
}
|
|
381
|
-
|
|
335
|
+
}
|
|
336
|
+
setOutput(index, keyType, keyData, value) {
|
|
382
337
|
set(this.getMap(index, this.outputMaps), keyType, keyData, value);
|
|
383
|
-
}
|
|
384
|
-
|
|
338
|
+
}
|
|
339
|
+
getOutput(index, keyType, keyData) {
|
|
385
340
|
return get(this.outputMaps[index], keyType, keyData, false);
|
|
386
|
-
}
|
|
387
|
-
|
|
341
|
+
}
|
|
342
|
+
getMap(index, maps) {
|
|
388
343
|
if (maps[index]) {
|
|
389
344
|
return maps[index];
|
|
390
345
|
}
|
|
391
346
|
return (maps[index] = new Map());
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
347
|
+
}
|
|
348
|
+
encodeBip32Derivation(masterFingerprint, path) {
|
|
349
|
+
const buf = new buffertools_1.BufferWriter();
|
|
395
350
|
this.writeBip32Derivation(buf, masterFingerprint, path);
|
|
396
351
|
return buf.buffer();
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
|
|
352
|
+
}
|
|
353
|
+
decodeBip32Derivation(buffer) {
|
|
354
|
+
const buf = new buffertools_1.BufferReader(buffer);
|
|
400
355
|
return this.readBip32Derivation(buf);
|
|
401
|
-
}
|
|
402
|
-
|
|
356
|
+
}
|
|
357
|
+
writeBip32Derivation(buf, masterFingerprint, path) {
|
|
403
358
|
buf.writeSlice(masterFingerprint);
|
|
404
|
-
path.forEach(
|
|
359
|
+
path.forEach((element) => {
|
|
405
360
|
buf.writeUInt32(element);
|
|
406
361
|
});
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
362
|
+
}
|
|
363
|
+
readBip32Derivation(buf) {
|
|
364
|
+
const masterFingerprint = buf.readSlice(4);
|
|
365
|
+
const path = [];
|
|
411
366
|
while (buf.offset < buf.buffer.length) {
|
|
412
367
|
path.push(buf.readUInt32());
|
|
413
368
|
}
|
|
414
|
-
return { masterFingerprint
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
|
|
369
|
+
return { masterFingerprint, path };
|
|
370
|
+
}
|
|
371
|
+
encodeTapBip32Derivation(hashes, masterFingerprint, path) {
|
|
372
|
+
const buf = new buffertools_1.BufferWriter();
|
|
418
373
|
buf.writeVarInt(hashes.length);
|
|
419
|
-
hashes.forEach(
|
|
374
|
+
hashes.forEach((h) => {
|
|
420
375
|
buf.writeSlice(h);
|
|
421
376
|
});
|
|
422
377
|
this.writeBip32Derivation(buf, masterFingerprint, path);
|
|
423
378
|
return buf.buffer();
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
for (
|
|
379
|
+
}
|
|
380
|
+
decodeTapBip32Derivation(buffer) {
|
|
381
|
+
const buf = new buffertools_1.BufferReader(buffer);
|
|
382
|
+
const hashCount = buf.readVarInt();
|
|
383
|
+
const hashes = [];
|
|
384
|
+
for (let i = 0; i < hashCount; i++) {
|
|
430
385
|
hashes.push(buf.readSlice(32));
|
|
431
386
|
}
|
|
432
|
-
|
|
433
|
-
return
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
}());
|
|
387
|
+
const deriv = this.readBip32Derivation(buf);
|
|
388
|
+
return Object.assign({ hashes }, deriv);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
437
391
|
exports.PsbtV2 = PsbtV2;
|
|
438
392
|
function get(map, keyType, keyData, acceptUndefined) {
|
|
439
393
|
if (!map)
|
|
440
394
|
throw Error("No such map");
|
|
441
|
-
|
|
442
|
-
|
|
395
|
+
const key = new Key(keyType, keyData);
|
|
396
|
+
const value = map.get(key.toString());
|
|
443
397
|
if (!value) {
|
|
444
398
|
if (acceptUndefined) {
|
|
445
399
|
return undefined;
|
|
@@ -449,56 +403,43 @@ function get(map, keyType, keyData, acceptUndefined) {
|
|
|
449
403
|
// Make sure to return a copy, to protect the underlying data.
|
|
450
404
|
return Buffer.from(value);
|
|
451
405
|
}
|
|
452
|
-
|
|
453
|
-
|
|
406
|
+
class Key {
|
|
407
|
+
constructor(keyType, keyData) {
|
|
454
408
|
this.keyType = keyType;
|
|
455
409
|
this.keyData = keyData;
|
|
456
410
|
}
|
|
457
|
-
|
|
458
|
-
|
|
411
|
+
toString() {
|
|
412
|
+
const buf = new buffertools_1.BufferWriter();
|
|
459
413
|
this.toBuffer(buf);
|
|
460
414
|
return buf.buffer().toString("hex");
|
|
461
|
-
}
|
|
462
|
-
|
|
415
|
+
}
|
|
416
|
+
serialize(buf) {
|
|
463
417
|
buf.writeVarInt(1 + this.keyData.length);
|
|
464
418
|
this.toBuffer(buf);
|
|
465
|
-
}
|
|
466
|
-
|
|
419
|
+
}
|
|
420
|
+
toBuffer(buf) {
|
|
467
421
|
buf.writeUInt8(this.keyType);
|
|
468
422
|
buf.writeSlice(this.keyData);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
function KeyPair(key, value) {
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
class KeyPair {
|
|
426
|
+
constructor(key, value) {
|
|
474
427
|
this.key = key;
|
|
475
428
|
this.value = value;
|
|
476
429
|
}
|
|
477
|
-
|
|
430
|
+
serialize(buf) {
|
|
478
431
|
this.key.serialize(buf);
|
|
479
432
|
buf.writeVarSlice(this.value);
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
}());
|
|
433
|
+
}
|
|
434
|
+
}
|
|
483
435
|
function createKey(buf) {
|
|
484
436
|
return new Key(buf.readUInt8(0), buf.slice(1));
|
|
485
437
|
}
|
|
486
438
|
function serializeMap(buf, map) {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
var value = map.get(k);
|
|
492
|
-
var keyPair = new KeyPair(createKey(Buffer.from(k, "hex")), value);
|
|
493
|
-
keyPair.serialize(buf);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
497
|
-
finally {
|
|
498
|
-
try {
|
|
499
|
-
if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
|
|
500
|
-
}
|
|
501
|
-
finally { if (e_1) throw e_1.error; }
|
|
439
|
+
for (const k of map.keys()) {
|
|
440
|
+
const value = map.get(k);
|
|
441
|
+
const keyPair = new KeyPair(createKey(Buffer.from(k, "hex")), value);
|
|
442
|
+
keyPair.serialize(buf);
|
|
502
443
|
}
|
|
503
444
|
buf.writeUInt8(0);
|
|
504
445
|
}
|
|
@@ -506,11 +447,11 @@ function b() {
|
|
|
506
447
|
return Buffer.from([]);
|
|
507
448
|
}
|
|
508
449
|
function set(map, keyType, keyData, value) {
|
|
509
|
-
|
|
450
|
+
const key = new Key(keyType, keyData);
|
|
510
451
|
map.set(key.toString(), value);
|
|
511
452
|
}
|
|
512
453
|
function uint32LE(n) {
|
|
513
|
-
|
|
454
|
+
const b = Buffer.alloc(4);
|
|
514
455
|
b.writeUInt32LE(n, 0);
|
|
515
456
|
return b;
|
|
516
457
|
}
|
|
@@ -518,7 +459,7 @@ function uint64LE(n) {
|
|
|
518
459
|
return (0, buffertools_1.unsafeTo64bitLE)(n);
|
|
519
460
|
}
|
|
520
461
|
function varint(n) {
|
|
521
|
-
|
|
462
|
+
const b = new buffertools_1.BufferWriter();
|
|
522
463
|
b.writeVarInt(n);
|
|
523
464
|
return b.buffer();
|
|
524
465
|
}
|