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