@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
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -18,44 +7,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
18
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
8
|
});
|
|
20
9
|
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (_) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var __values = (this && this.__values) || function(o) {
|
|
49
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
50
|
-
if (m) return m.call(o);
|
|
51
|
-
if (o && typeof o.length === "number") return {
|
|
52
|
-
next: function () {
|
|
53
|
-
if (o && i >= o.length) o = void 0;
|
|
54
|
-
return { value: o && o[i++], done: !o };
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
58
|
-
};
|
|
59
10
|
import { log } from "@ledgerhq/logs";
|
|
60
11
|
import { hashPublicKey } from "./hashPublicKey";
|
|
61
12
|
import { getWalletPublicKey } from "./getWalletPublicKey";
|
|
@@ -69,349 +20,285 @@ import { hashOutputFull, provideOutputFullChangePath } from "./finalizeInput";
|
|
|
69
20
|
import { getAppAndVersion } from "./getAppAndVersion";
|
|
70
21
|
import { DEFAULT_LOCKTIME, DEFAULT_SEQUENCE, SIGHASH_ALL, OP_DUP, OP_HASH160, HASH_SIZE, OP_EQUALVERIFY, OP_CHECKSIG, } from "./constants";
|
|
71
22
|
import { shouldUseTrustedInputForSegwit } from "./shouldUseTrustedInputForSegwit";
|
|
72
|
-
|
|
23
|
+
const defaultsSignTransaction = {
|
|
73
24
|
lockTime: DEFAULT_LOCKTIME,
|
|
74
25
|
sigHashType: SIGHASH_ALL,
|
|
75
26
|
segwit: false,
|
|
76
27
|
additionals: [],
|
|
77
|
-
onDeviceStreaming:
|
|
78
|
-
onDeviceSignatureGranted:
|
|
79
|
-
onDeviceSignatureRequested:
|
|
28
|
+
onDeviceStreaming: (_e) => { },
|
|
29
|
+
onDeviceSignatureGranted: () => { },
|
|
30
|
+
onDeviceSignatureRequested: () => { },
|
|
80
31
|
};
|
|
81
32
|
export function createTransaction(transport, arg) {
|
|
82
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
trustedInput = _b.sent();
|
|
173
|
-
log("hw", "got trustedInput=" + trustedInput);
|
|
174
|
-
sequence = Buffer.alloc(4);
|
|
175
|
-
sequence.writeUInt32LE(input.length >= 4 && typeof input[3] === "number"
|
|
176
|
-
? input[3]
|
|
177
|
-
: DEFAULT_SEQUENCE, 0);
|
|
178
|
-
trustedInputs.push({
|
|
179
|
-
trustedInput: true,
|
|
180
|
-
value: Buffer.from(trustedInput, "hex"),
|
|
181
|
-
sequence: sequence
|
|
182
|
-
});
|
|
183
|
-
_b.label = 8;
|
|
184
|
-
case 8:
|
|
185
|
-
outputs = input[0].outputs;
|
|
186
|
-
index = input[1];
|
|
187
|
-
if (outputs && index <= outputs.length - 1) {
|
|
188
|
-
regularOutputs.push(outputs[index]);
|
|
189
|
-
}
|
|
190
|
-
if (expiryHeight && !isDecred) {
|
|
191
|
-
targetTransaction.nVersionGroupId = Buffer.from(
|
|
192
|
-
// nVersionGroupId is 0x26A7270A for zcash from https://z.cash/upgrade/nu5/
|
|
193
|
-
isZcash
|
|
194
|
-
? [0x0a, 0x27, 0xa7, 0x26]
|
|
195
|
-
: sapling
|
|
196
|
-
? [0x85, 0x20, 0x2f, 0x89]
|
|
197
|
-
: [0x70, 0x82, 0xc4, 0x03]);
|
|
198
|
-
targetTransaction.nExpiryHeight = expiryHeight;
|
|
199
|
-
// For sapling : valueBalance (8), nShieldedSpend (1), nShieldedOutput (1), nJoinSplit (1)
|
|
200
|
-
// Overwinter : use nJoinSplit (1)
|
|
201
|
-
targetTransaction.extraData = Buffer.from(sapling
|
|
202
|
-
? [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
|
|
203
|
-
: [0x00]);
|
|
204
|
-
}
|
|
205
|
-
else if (isDecred) {
|
|
206
|
-
targetTransaction.nExpiryHeight = expiryHeight;
|
|
207
|
-
}
|
|
208
|
-
_b.label = 9;
|
|
209
|
-
case 9:
|
|
210
|
-
inputs_1_1 = inputs_1.next();
|
|
211
|
-
return [3 /*break*/, 6];
|
|
212
|
-
case 10: return [3 /*break*/, 13];
|
|
213
|
-
case 11:
|
|
214
|
-
e_2_1 = _b.sent();
|
|
215
|
-
e_2 = { error: e_2_1 };
|
|
216
|
-
return [3 /*break*/, 13];
|
|
217
|
-
case 12:
|
|
218
|
-
try {
|
|
219
|
-
if (inputs_1_1 && !inputs_1_1.done && (_a = inputs_1["return"])) _a.call(inputs_1);
|
|
220
|
-
}
|
|
221
|
-
finally { if (e_2) throw e_2.error; }
|
|
222
|
-
return [7 /*endfinally*/];
|
|
223
|
-
case 13:
|
|
224
|
-
targetTransaction.inputs = inputs.map(function (input, idx) {
|
|
225
|
-
var sequence = Buffer.alloc(4);
|
|
226
|
-
sequence.writeUInt32LE(input.length >= 4 && typeof input[3] === "number"
|
|
227
|
-
? input[3]
|
|
228
|
-
: DEFAULT_SEQUENCE, 0);
|
|
229
|
-
return {
|
|
230
|
-
script: isZcash ? regularOutputs[idx].script : nullScript,
|
|
231
|
-
prevout: nullPrevout,
|
|
232
|
-
sequence: sequence
|
|
233
|
-
};
|
|
234
|
-
});
|
|
235
|
-
if (!!resuming) return [3 /*break*/, 18];
|
|
236
|
-
result_1 = [];
|
|
237
|
-
i = 0;
|
|
238
|
-
_b.label = 14;
|
|
239
|
-
case 14:
|
|
240
|
-
if (!(i < inputs.length)) return [3 /*break*/, 17];
|
|
241
|
-
return [4 /*yield*/, getWalletPublicKey(transport, {
|
|
242
|
-
path: associatedKeysets[i]
|
|
243
|
-
})];
|
|
244
|
-
case 15:
|
|
245
|
-
r = _b.sent();
|
|
246
|
-
notify(0, i + 1);
|
|
247
|
-
result_1.push(r);
|
|
248
|
-
_b.label = 16;
|
|
249
|
-
case 16:
|
|
250
|
-
i++;
|
|
251
|
-
return [3 /*break*/, 14];
|
|
252
|
-
case 17:
|
|
253
|
-
for (i = 0; i < result_1.length; i++) {
|
|
254
|
-
publicKeys.push(compressPublicKey(Buffer.from(result_1[i].publicKey, "hex")));
|
|
255
|
-
}
|
|
256
|
-
_b.label = 18;
|
|
257
|
-
case 18:
|
|
258
|
-
if (initialTimestamp !== undefined) {
|
|
259
|
-
targetTransaction.timestamp = Buffer.alloc(4);
|
|
260
|
-
targetTransaction.timestamp.writeUInt32LE(Math.floor(initialTimestamp + (Date.now() - startTime) / 1000), 0);
|
|
261
|
-
}
|
|
262
|
-
onDeviceSignatureRequested();
|
|
263
|
-
if (!useBip143) return [3 /*break*/, 23];
|
|
264
|
-
// Do the first run with all inputs
|
|
265
|
-
return [4 /*yield*/, startUntrustedHashTransactionInput(transport, true, targetTransaction, trustedInputs, true, !!expiryHeight, additionals, useTrustedInputForSegwit)];
|
|
266
|
-
case 19:
|
|
267
|
-
// Do the first run with all inputs
|
|
268
|
-
_b.sent();
|
|
269
|
-
if (!(!resuming && changePath)) return [3 /*break*/, 21];
|
|
270
|
-
return [4 /*yield*/, provideOutputFullChangePath(transport, changePath)];
|
|
271
|
-
case 20:
|
|
272
|
-
_b.sent();
|
|
273
|
-
_b.label = 21;
|
|
274
|
-
case 21: return [4 /*yield*/, hashOutputFull(transport, outputScript)];
|
|
275
|
-
case 22:
|
|
276
|
-
_b.sent();
|
|
277
|
-
_b.label = 23;
|
|
278
|
-
case 23:
|
|
279
|
-
if (!(!!expiryHeight && !isDecred)) return [3 /*break*/, 25];
|
|
280
|
-
return [4 /*yield*/, signTransaction(transport, "", lockTime, SIGHASH_ALL, expiryHeight)];
|
|
281
|
-
case 24:
|
|
282
|
-
_b.sent();
|
|
283
|
-
_b.label = 25;
|
|
284
|
-
case 25:
|
|
285
|
-
i = 0;
|
|
286
|
-
_b.label = 26;
|
|
287
|
-
case 26:
|
|
288
|
-
if (!(i < inputs.length)) return [3 /*break*/, 34];
|
|
289
|
-
input = inputs[i];
|
|
290
|
-
script = inputs[i].length >= 3 && typeof input[2] === "string"
|
|
291
|
-
? Buffer.from(input[2], "hex")
|
|
292
|
-
: !segwit
|
|
293
|
-
? regularOutputs[i].script
|
|
294
|
-
: Buffer.concat([
|
|
295
|
-
Buffer.from([OP_DUP, OP_HASH160, HASH_SIZE]),
|
|
296
|
-
hashPublicKey(publicKeys[i]),
|
|
297
|
-
Buffer.from([OP_EQUALVERIFY, OP_CHECKSIG]),
|
|
298
|
-
]);
|
|
299
|
-
pseudoTX = Object.assign({}, targetTransaction);
|
|
300
|
-
pseudoTrustedInputs = useBip143 ? [trustedInputs[i]] : trustedInputs;
|
|
301
|
-
if (useBip143) {
|
|
302
|
-
pseudoTX.inputs = [__assign(__assign({}, pseudoTX.inputs[i]), { script: script })];
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
pseudoTX.inputs[i].script = script;
|
|
306
|
-
}
|
|
307
|
-
return [4 /*yield*/, startUntrustedHashTransactionInput(transport, !useBip143 && firstRun, pseudoTX, pseudoTrustedInputs, useBip143, !!expiryHeight && !isDecred, additionals, useTrustedInputForSegwit)];
|
|
308
|
-
case 27:
|
|
309
|
-
_b.sent();
|
|
310
|
-
if (!!useBip143) return [3 /*break*/, 31];
|
|
311
|
-
if (!(!resuming && changePath)) return [3 /*break*/, 29];
|
|
312
|
-
return [4 /*yield*/, provideOutputFullChangePath(transport, changePath)];
|
|
313
|
-
case 28:
|
|
314
|
-
_b.sent();
|
|
315
|
-
_b.label = 29;
|
|
316
|
-
case 29: return [4 /*yield*/, hashOutputFull(transport, outputScript, additionals)];
|
|
317
|
-
case 30:
|
|
318
|
-
_b.sent();
|
|
319
|
-
_b.label = 31;
|
|
320
|
-
case 31:
|
|
321
|
-
if (firstRun) {
|
|
322
|
-
onDeviceSignatureGranted();
|
|
323
|
-
notify(1, 0);
|
|
324
|
-
}
|
|
325
|
-
return [4 /*yield*/, signTransaction(transport, associatedKeysets[i], lockTime, sigHashType, expiryHeight, additionals)];
|
|
326
|
-
case 32:
|
|
327
|
-
signature = _b.sent();
|
|
328
|
-
notify(1, i + 1);
|
|
329
|
-
signatures.push(signature);
|
|
330
|
-
targetTransaction.inputs[i].script = nullScript;
|
|
331
|
-
if (firstRun) {
|
|
332
|
-
firstRun = false;
|
|
333
|
-
}
|
|
334
|
-
_b.label = 33;
|
|
335
|
-
case 33:
|
|
336
|
-
i++;
|
|
337
|
-
return [3 /*break*/, 26];
|
|
338
|
-
case 34:
|
|
339
|
-
// Populate the final input scripts
|
|
340
|
-
for (i = 0; i < inputs.length; i++) {
|
|
341
|
-
if (segwit) {
|
|
342
|
-
targetTransaction.witness = Buffer.alloc(0);
|
|
343
|
-
if (!bech32) {
|
|
344
|
-
targetTransaction.inputs[i].script = Buffer.concat([
|
|
345
|
-
Buffer.from("160014", "hex"),
|
|
346
|
-
hashPublicKey(publicKeys[i]),
|
|
347
|
-
]);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
else {
|
|
351
|
-
signatureSize = Buffer.alloc(1);
|
|
352
|
-
keySize = Buffer.alloc(1);
|
|
353
|
-
signatureSize[0] = signatures[i].length;
|
|
354
|
-
keySize[0] = publicKeys[i].length;
|
|
355
|
-
targetTransaction.inputs[i].script = Buffer.concat([
|
|
356
|
-
signatureSize,
|
|
357
|
-
signatures[i],
|
|
358
|
-
keySize,
|
|
359
|
-
publicKeys[i],
|
|
360
|
-
]);
|
|
361
|
-
}
|
|
362
|
-
offset = useBip143 && !useTrustedInputForSegwit ? 0 : 4;
|
|
363
|
-
targetTransaction.inputs[i].prevout = trustedInputs[i].value.slice(offset, offset + 0x24);
|
|
364
|
-
}
|
|
365
|
-
targetTransaction.locktime = lockTimeBuffer;
|
|
366
|
-
result = Buffer.concat([
|
|
367
|
-
serializeTransaction(targetTransaction, false, targetTransaction.timestamp, additionals),
|
|
368
|
-
outputScript,
|
|
369
|
-
]);
|
|
370
|
-
if (segwit && !isDecred) {
|
|
371
|
-
witness = Buffer.alloc(0);
|
|
372
|
-
for (i = 0; i < inputs.length; i++) {
|
|
373
|
-
tmpScriptData = Buffer.concat([
|
|
374
|
-
Buffer.from("02", "hex"),
|
|
375
|
-
Buffer.from([signatures[i].length]),
|
|
376
|
-
signatures[i],
|
|
377
|
-
Buffer.from([publicKeys[i].length]),
|
|
378
|
-
publicKeys[i],
|
|
379
|
-
]);
|
|
380
|
-
witness = Buffer.concat([witness, tmpScriptData]);
|
|
381
|
-
}
|
|
382
|
-
result = Buffer.concat([result, witness]);
|
|
383
|
-
}
|
|
384
|
-
// from to https://zips.z.cash/zip-0225, zcash is different with other coins, the lock_time and nExpiryHeight fields are before the inputs and outputs
|
|
385
|
-
if (!isZcash) {
|
|
386
|
-
result = Buffer.concat([result, lockTimeBuffer]);
|
|
387
|
-
if (expiryHeight) {
|
|
388
|
-
result = Buffer.concat([
|
|
389
|
-
result,
|
|
390
|
-
targetTransaction.nExpiryHeight || Buffer.alloc(0),
|
|
391
|
-
targetTransaction.extraData || Buffer.alloc(0),
|
|
392
|
-
]);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
if (isDecred) {
|
|
396
|
-
decredWitness_1 = Buffer.from([targetTransaction.inputs.length]);
|
|
397
|
-
inputs.forEach(function (input, inputIndex) {
|
|
398
|
-
decredWitness_1 = Buffer.concat([
|
|
399
|
-
decredWitness_1,
|
|
400
|
-
Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
|
401
|
-
Buffer.from([0x00, 0x00, 0x00, 0x00]),
|
|
402
|
-
Buffer.from([0xff, 0xff, 0xff, 0xff]),
|
|
403
|
-
Buffer.from([targetTransaction.inputs[inputIndex].script.length]),
|
|
404
|
-
targetTransaction.inputs[inputIndex].script,
|
|
405
|
-
]);
|
|
406
|
-
});
|
|
407
|
-
result = Buffer.concat([result, decredWitness_1]);
|
|
408
|
-
}
|
|
409
|
-
if (isZcash) {
|
|
410
|
-
result = Buffer.concat([result, Buffer.from([0x00, 0x00, 0x00])]);
|
|
411
|
-
}
|
|
412
|
-
return [2 /*return*/, result.toString("hex")];
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const signTx = Object.assign(Object.assign({}, defaultsSignTransaction), arg);
|
|
35
|
+
const { inputs, associatedKeysets, changePath, outputScriptHex, lockTime, sigHashType, segwit, initialTimestamp, additionals, expiryHeight, onDeviceStreaming, onDeviceSignatureGranted, onDeviceSignatureRequested, } = signTx;
|
|
36
|
+
let useTrustedInputForSegwit = signTx.useTrustedInputForSegwit;
|
|
37
|
+
if (useTrustedInputForSegwit === undefined) {
|
|
38
|
+
try {
|
|
39
|
+
const a = yield getAppAndVersion(transport);
|
|
40
|
+
useTrustedInputForSegwit = shouldUseTrustedInputForSegwit(a);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (e.statusCode === 0x6d00) {
|
|
44
|
+
useTrustedInputForSegwit = false;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw e;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// loop: 0 or 1 (before and after)
|
|
52
|
+
// i: index of the input being streamed
|
|
53
|
+
// i goes on 0...n, inluding n. in order for the progress value to go to 1
|
|
54
|
+
// we normalize the 2 loops to make a global percentage
|
|
55
|
+
const notify = (loop, i) => {
|
|
56
|
+
const { length } = inputs;
|
|
57
|
+
if (length < 3)
|
|
58
|
+
return; // there is not enough significant event to worth notifying (aka just use a spinner)
|
|
59
|
+
const index = length * loop + i;
|
|
60
|
+
const total = 2 * length;
|
|
61
|
+
const progress = index / total;
|
|
62
|
+
onDeviceStreaming({
|
|
63
|
+
progress,
|
|
64
|
+
total,
|
|
65
|
+
index,
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
const isDecred = additionals.includes("decred");
|
|
69
|
+
const isZcash = additionals.includes("zcash");
|
|
70
|
+
const isXST = additionals.includes("stealthcoin");
|
|
71
|
+
const startTime = Date.now();
|
|
72
|
+
const sapling = additionals.includes("sapling");
|
|
73
|
+
const bech32 = segwit && additionals.includes("bech32");
|
|
74
|
+
const useBip143 = segwit ||
|
|
75
|
+
(!!additionals &&
|
|
76
|
+
(additionals.includes("abc") ||
|
|
77
|
+
additionals.includes("gold") ||
|
|
78
|
+
additionals.includes("bip143"))) ||
|
|
79
|
+
(!!expiryHeight && !isDecred);
|
|
80
|
+
// Inputs are provided as arrays of [transaction, output_index, optional redeem script, optional sequence]
|
|
81
|
+
// associatedKeysets are provided as arrays of [path]
|
|
82
|
+
const lockTimeBuffer = Buffer.alloc(4);
|
|
83
|
+
lockTimeBuffer.writeUInt32LE(lockTime, 0);
|
|
84
|
+
const nullScript = Buffer.alloc(0);
|
|
85
|
+
const nullPrevout = Buffer.alloc(0);
|
|
86
|
+
const defaultVersion = Buffer.alloc(4);
|
|
87
|
+
!!expiryHeight && !isDecred
|
|
88
|
+
? defaultVersion.writeUInt32LE(isZcash ? 0x80000005 : sapling ? 0x80000004 : 0x80000003, 0) // v5 format for zcash refer to https://zips.z.cash/zip-0225
|
|
89
|
+
: isXST
|
|
90
|
+
? defaultVersion.writeUInt32LE(2, 0)
|
|
91
|
+
: defaultVersion.writeUInt32LE(1, 0);
|
|
92
|
+
// Default version to 2 for XST not to have timestamp
|
|
93
|
+
const trustedInputs = [];
|
|
94
|
+
const regularOutputs = [];
|
|
95
|
+
const signatures = [];
|
|
96
|
+
const publicKeys = [];
|
|
97
|
+
let firstRun = true;
|
|
98
|
+
const resuming = false;
|
|
99
|
+
const targetTransaction = {
|
|
100
|
+
inputs: [],
|
|
101
|
+
version: defaultVersion,
|
|
102
|
+
timestamp: Buffer.alloc(0),
|
|
103
|
+
};
|
|
104
|
+
const getTrustedInputCall = useBip143 && !useTrustedInputForSegwit
|
|
105
|
+
? getTrustedInputBIP143
|
|
106
|
+
: getTrustedInput;
|
|
107
|
+
const outputScript = Buffer.from(outputScriptHex, "hex");
|
|
108
|
+
notify(0, 0);
|
|
109
|
+
// first pass on inputs to get trusted inputs
|
|
110
|
+
for (const input of inputs) {
|
|
111
|
+
if (!resuming) {
|
|
112
|
+
const trustedInput = yield getTrustedInputCall(transport, input[1], input[0], additionals);
|
|
113
|
+
log("hw", "got trustedInput=" + trustedInput);
|
|
114
|
+
const sequence = Buffer.alloc(4);
|
|
115
|
+
sequence.writeUInt32LE(input.length >= 4 && typeof input[3] === "number"
|
|
116
|
+
? input[3]
|
|
117
|
+
: DEFAULT_SEQUENCE, 0);
|
|
118
|
+
trustedInputs.push({
|
|
119
|
+
trustedInput: true,
|
|
120
|
+
value: Buffer.from(trustedInput, "hex"),
|
|
121
|
+
sequence,
|
|
122
|
+
});
|
|
413
123
|
}
|
|
124
|
+
const { outputs } = input[0];
|
|
125
|
+
const index = input[1];
|
|
126
|
+
if (outputs && index <= outputs.length - 1) {
|
|
127
|
+
regularOutputs.push(outputs[index]);
|
|
128
|
+
}
|
|
129
|
+
if (expiryHeight && !isDecred) {
|
|
130
|
+
targetTransaction.nVersionGroupId = Buffer.from(
|
|
131
|
+
// nVersionGroupId is 0x26A7270A for zcash from https://z.cash/upgrade/nu5/
|
|
132
|
+
isZcash
|
|
133
|
+
? [0x0a, 0x27, 0xa7, 0x26]
|
|
134
|
+
: sapling
|
|
135
|
+
? [0x85, 0x20, 0x2f, 0x89]
|
|
136
|
+
: [0x70, 0x82, 0xc4, 0x03]);
|
|
137
|
+
targetTransaction.nExpiryHeight = expiryHeight;
|
|
138
|
+
// For sapling : valueBalance (8), nShieldedSpend (1), nShieldedOutput (1), nJoinSplit (1)
|
|
139
|
+
// Overwinter : use nJoinSplit (1)
|
|
140
|
+
targetTransaction.extraData = Buffer.from(sapling
|
|
141
|
+
? [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
|
|
142
|
+
: [0x00]);
|
|
143
|
+
}
|
|
144
|
+
else if (isDecred) {
|
|
145
|
+
targetTransaction.nExpiryHeight = expiryHeight;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
targetTransaction.inputs = inputs.map((input, idx) => {
|
|
149
|
+
const sequence = Buffer.alloc(4);
|
|
150
|
+
sequence.writeUInt32LE(input.length >= 4 && typeof input[3] === "number"
|
|
151
|
+
? input[3]
|
|
152
|
+
: DEFAULT_SEQUENCE, 0);
|
|
153
|
+
return {
|
|
154
|
+
script: isZcash ? regularOutputs[idx].script : nullScript,
|
|
155
|
+
prevout: nullPrevout,
|
|
156
|
+
sequence,
|
|
157
|
+
};
|
|
414
158
|
});
|
|
159
|
+
if (!resuming) {
|
|
160
|
+
// Collect public keys
|
|
161
|
+
const result = [];
|
|
162
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
163
|
+
const r = yield getWalletPublicKey(transport, {
|
|
164
|
+
path: associatedKeysets[i],
|
|
165
|
+
});
|
|
166
|
+
notify(0, i + 1);
|
|
167
|
+
result.push(r);
|
|
168
|
+
}
|
|
169
|
+
for (let i = 0; i < result.length; i++) {
|
|
170
|
+
publicKeys.push(compressPublicKey(Buffer.from(result[i].publicKey, "hex")));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (initialTimestamp !== undefined) {
|
|
174
|
+
targetTransaction.timestamp = Buffer.alloc(4);
|
|
175
|
+
targetTransaction.timestamp.writeUInt32LE(Math.floor(initialTimestamp + (Date.now() - startTime) / 1000), 0);
|
|
176
|
+
}
|
|
177
|
+
onDeviceSignatureRequested();
|
|
178
|
+
if (useBip143) {
|
|
179
|
+
// Do the first run with all inputs
|
|
180
|
+
yield startUntrustedHashTransactionInput(transport, true, targetTransaction, trustedInputs, true, !!expiryHeight, additionals, useTrustedInputForSegwit);
|
|
181
|
+
if (!resuming && changePath) {
|
|
182
|
+
yield provideOutputFullChangePath(transport, changePath);
|
|
183
|
+
}
|
|
184
|
+
yield hashOutputFull(transport, outputScript);
|
|
185
|
+
}
|
|
186
|
+
if (!!expiryHeight && !isDecred) {
|
|
187
|
+
yield signTransaction(transport, "", lockTime, SIGHASH_ALL, expiryHeight);
|
|
188
|
+
}
|
|
189
|
+
// Do the second run with the individual transaction
|
|
190
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
191
|
+
const input = inputs[i];
|
|
192
|
+
const script = inputs[i].length >= 3 && typeof input[2] === "string"
|
|
193
|
+
? Buffer.from(input[2], "hex")
|
|
194
|
+
: !segwit
|
|
195
|
+
? regularOutputs[i].script
|
|
196
|
+
: Buffer.concat([
|
|
197
|
+
Buffer.from([OP_DUP, OP_HASH160, HASH_SIZE]),
|
|
198
|
+
hashPublicKey(publicKeys[i]),
|
|
199
|
+
Buffer.from([OP_EQUALVERIFY, OP_CHECKSIG]),
|
|
200
|
+
]);
|
|
201
|
+
const pseudoTX = Object.assign({}, targetTransaction);
|
|
202
|
+
const pseudoTrustedInputs = useBip143 ? [trustedInputs[i]] : trustedInputs;
|
|
203
|
+
if (useBip143) {
|
|
204
|
+
pseudoTX.inputs = [Object.assign(Object.assign({}, pseudoTX.inputs[i]), { script })];
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
pseudoTX.inputs[i].script = script;
|
|
208
|
+
}
|
|
209
|
+
yield startUntrustedHashTransactionInput(transport, !useBip143 && firstRun, pseudoTX, pseudoTrustedInputs, useBip143, !!expiryHeight && !isDecred, additionals, useTrustedInputForSegwit);
|
|
210
|
+
if (!useBip143) {
|
|
211
|
+
if (!resuming && changePath) {
|
|
212
|
+
yield provideOutputFullChangePath(transport, changePath);
|
|
213
|
+
}
|
|
214
|
+
yield hashOutputFull(transport, outputScript, additionals);
|
|
215
|
+
}
|
|
216
|
+
if (firstRun) {
|
|
217
|
+
onDeviceSignatureGranted();
|
|
218
|
+
notify(1, 0);
|
|
219
|
+
}
|
|
220
|
+
const signature = yield signTransaction(transport, associatedKeysets[i], lockTime, sigHashType, expiryHeight, additionals);
|
|
221
|
+
notify(1, i + 1);
|
|
222
|
+
signatures.push(signature);
|
|
223
|
+
targetTransaction.inputs[i].script = nullScript;
|
|
224
|
+
if (firstRun) {
|
|
225
|
+
firstRun = false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Populate the final input scripts
|
|
229
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
230
|
+
if (segwit) {
|
|
231
|
+
targetTransaction.witness = Buffer.alloc(0);
|
|
232
|
+
if (!bech32) {
|
|
233
|
+
targetTransaction.inputs[i].script = Buffer.concat([
|
|
234
|
+
Buffer.from("160014", "hex"),
|
|
235
|
+
hashPublicKey(publicKeys[i]),
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const signatureSize = Buffer.alloc(1);
|
|
241
|
+
const keySize = Buffer.alloc(1);
|
|
242
|
+
signatureSize[0] = signatures[i].length;
|
|
243
|
+
keySize[0] = publicKeys[i].length;
|
|
244
|
+
targetTransaction.inputs[i].script = Buffer.concat([
|
|
245
|
+
signatureSize,
|
|
246
|
+
signatures[i],
|
|
247
|
+
keySize,
|
|
248
|
+
publicKeys[i],
|
|
249
|
+
]);
|
|
250
|
+
}
|
|
251
|
+
const offset = useBip143 && !useTrustedInputForSegwit ? 0 : 4;
|
|
252
|
+
targetTransaction.inputs[i].prevout = trustedInputs[i].value.slice(offset, offset + 0x24);
|
|
253
|
+
}
|
|
254
|
+
targetTransaction.locktime = lockTimeBuffer;
|
|
255
|
+
let result = Buffer.concat([
|
|
256
|
+
serializeTransaction(targetTransaction, false, targetTransaction.timestamp, additionals),
|
|
257
|
+
outputScript,
|
|
258
|
+
]);
|
|
259
|
+
if (segwit && !isDecred) {
|
|
260
|
+
let witness = Buffer.alloc(0);
|
|
261
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
262
|
+
const tmpScriptData = Buffer.concat([
|
|
263
|
+
Buffer.from("02", "hex"),
|
|
264
|
+
Buffer.from([signatures[i].length]),
|
|
265
|
+
signatures[i],
|
|
266
|
+
Buffer.from([publicKeys[i].length]),
|
|
267
|
+
publicKeys[i],
|
|
268
|
+
]);
|
|
269
|
+
witness = Buffer.concat([witness, tmpScriptData]);
|
|
270
|
+
}
|
|
271
|
+
result = Buffer.concat([result, witness]);
|
|
272
|
+
}
|
|
273
|
+
// from to https://zips.z.cash/zip-0225, zcash is different with other coins, the lock_time and nExpiryHeight fields are before the inputs and outputs
|
|
274
|
+
if (!isZcash) {
|
|
275
|
+
result = Buffer.concat([result, lockTimeBuffer]);
|
|
276
|
+
if (expiryHeight) {
|
|
277
|
+
result = Buffer.concat([
|
|
278
|
+
result,
|
|
279
|
+
targetTransaction.nExpiryHeight || Buffer.alloc(0),
|
|
280
|
+
targetTransaction.extraData || Buffer.alloc(0),
|
|
281
|
+
]);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (isDecred) {
|
|
285
|
+
let decredWitness = Buffer.from([targetTransaction.inputs.length]);
|
|
286
|
+
inputs.forEach((input, inputIndex) => {
|
|
287
|
+
decredWitness = Buffer.concat([
|
|
288
|
+
decredWitness,
|
|
289
|
+
Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
|
290
|
+
Buffer.from([0x00, 0x00, 0x00, 0x00]),
|
|
291
|
+
Buffer.from([0xff, 0xff, 0xff, 0xff]),
|
|
292
|
+
Buffer.from([targetTransaction.inputs[inputIndex].script.length]),
|
|
293
|
+
targetTransaction.inputs[inputIndex].script,
|
|
294
|
+
]);
|
|
295
|
+
});
|
|
296
|
+
result = Buffer.concat([result, decredWitness]);
|
|
297
|
+
}
|
|
298
|
+
if (isZcash) {
|
|
299
|
+
result = Buffer.concat([result, Buffer.from([0x00, 0x00, 0x00])]);
|
|
300
|
+
}
|
|
301
|
+
return result.toString("hex");
|
|
415
302
|
});
|
|
416
303
|
}
|
|
417
304
|
//# sourceMappingURL=createTransaction.js.map
|