@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.
Files changed (165) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/Btc.d.ts.map +1 -1
  3. package/lib/Btc.js +72 -97
  4. package/lib/Btc.js.map +1 -1
  5. package/lib/BtcNew.js +217 -313
  6. package/lib/BtcNew.js.map +1 -1
  7. package/lib/BtcOld.js +46 -106
  8. package/lib/BtcOld.js.map +1 -1
  9. package/lib/bip32.js +12 -12
  10. package/lib/bip32.js.map +1 -1
  11. package/lib/buffertools.js +66 -69
  12. package/lib/buffertools.js.map +1 -1
  13. package/lib/compressPublicKey.js +3 -3
  14. package/lib/compressPublicKey.js.map +1 -1
  15. package/lib/constants.js +1 -1
  16. package/lib/createTransaction.d.ts +1 -1
  17. package/lib/createTransaction.d.ts.map +1 -1
  18. package/lib/createTransaction.js +285 -398
  19. package/lib/createTransaction.js.map +1 -1
  20. package/lib/debug.js +11 -13
  21. package/lib/debug.js.map +1 -1
  22. package/lib/finalizeInput.js +23 -62
  23. package/lib/finalizeInput.js.map +1 -1
  24. package/lib/getAppAndVersion.d.ts +1 -1
  25. package/lib/getAppAndVersion.d.ts.map +1 -1
  26. package/lib/getAppAndVersion.js +29 -72
  27. package/lib/getAppAndVersion.js.map +1 -1
  28. package/lib/getTrustedInput.js +108 -251
  29. package/lib/getTrustedInput.js.map +1 -1
  30. package/lib/getTrustedInputBIP143.js +9 -10
  31. package/lib/getTrustedInputBIP143.js.map +1 -1
  32. package/lib/getWalletPublicKey.d.ts +1 -1
  33. package/lib/getWalletPublicKey.d.ts.map +1 -1
  34. package/lib/getWalletPublicKey.js +27 -73
  35. package/lib/getWalletPublicKey.js.map +1 -1
  36. package/lib/hashPublicKey.js +4 -4
  37. package/lib/hashPublicKey.js.map +1 -1
  38. package/lib/index.js +3 -3
  39. package/lib/index.js.map +1 -1
  40. package/lib/newops/accounttype.d.ts +2 -2
  41. package/lib/newops/accounttype.d.ts.map +1 -1
  42. package/lib/newops/accounttype.js +85 -125
  43. package/lib/newops/accounttype.js.map +1 -1
  44. package/lib/newops/appClient.js +98 -205
  45. package/lib/newops/appClient.js.map +1 -1
  46. package/lib/newops/clientCommands.js +122 -213
  47. package/lib/newops/clientCommands.js.map +1 -1
  48. package/lib/newops/merkelizedPsbt.js +28 -75
  49. package/lib/newops/merkelizedPsbt.js.map +1 -1
  50. package/lib/newops/merkle.js +38 -67
  51. package/lib/newops/merkle.js.map +1 -1
  52. package/lib/newops/merkleMap.js +11 -12
  53. package/lib/newops/merkleMap.js.map +1 -1
  54. package/lib/newops/policy.d.ts +1 -1
  55. package/lib/newops/policy.d.ts.map +1 -1
  56. package/lib/newops/policy.js +17 -18
  57. package/lib/newops/policy.js.map +1 -1
  58. package/lib/newops/psbtExtractor.js +9 -9
  59. package/lib/newops/psbtExtractor.js.map +1 -1
  60. package/lib/newops/psbtFinalizer.js +22 -22
  61. package/lib/newops/psbtFinalizer.js.map +1 -1
  62. package/lib/newops/psbtv2.d.ts +1 -1
  63. package/lib/newops/psbtv2.d.ts.map +1 -1
  64. package/lib/newops/psbtv2.js +227 -286
  65. package/lib/newops/psbtv2.js.map +1 -1
  66. package/lib/serializeTransaction.js +13 -15
  67. package/lib/serializeTransaction.js.map +1 -1
  68. package/lib/shouldUseTrustedInputForSegwit.js +4 -5
  69. package/lib/shouldUseTrustedInputForSegwit.js.map +1 -1
  70. package/lib/signMessage.js +47 -99
  71. package/lib/signMessage.js.map +1 -1
  72. package/lib/signP2SHTransaction.d.ts +1 -1
  73. package/lib/signP2SHTransaction.d.ts.map +1 -1
  74. package/lib/signP2SHTransaction.js +91 -187
  75. package/lib/signP2SHTransaction.js.map +1 -1
  76. package/lib/signTransaction.js +8 -9
  77. package/lib/signTransaction.js.map +1 -1
  78. package/lib/splitTransaction.js +50 -54
  79. package/lib/splitTransaction.js.map +1 -1
  80. package/lib/startUntrustedHashTransactionInput.js +65 -167
  81. package/lib/startUntrustedHashTransactionInput.js.map +1 -1
  82. package/lib/types.js +1 -1
  83. package/lib/varint.js +10 -10
  84. package/lib/varint.js.map +1 -1
  85. package/lib-es/Btc.d.ts.map +1 -1
  86. package/lib-es/Btc.js +58 -84
  87. package/lib-es/Btc.js.map +1 -1
  88. package/lib-es/BtcNew.js +205 -302
  89. package/lib-es/BtcNew.js.map +1 -1
  90. package/lib-es/BtcOld.js +35 -96
  91. package/lib-es/BtcOld.js.map +1 -1
  92. package/lib-es/bip32.js +7 -7
  93. package/lib-es/bip32.js.map +1 -1
  94. package/lib-es/buffertools.js +62 -67
  95. package/lib-es/buffertools.js.map +1 -1
  96. package/lib-es/compressPublicKey.js +2 -2
  97. package/lib-es/compressPublicKey.js.map +1 -1
  98. package/lib-es/constants.js +12 -12
  99. package/lib-es/constants.js.map +1 -1
  100. package/lib-es/createTransaction.d.ts +1 -1
  101. package/lib-es/createTransaction.d.ts.map +1 -1
  102. package/lib-es/createTransaction.js +271 -384
  103. package/lib-es/createTransaction.js.map +1 -1
  104. package/lib-es/debug.js +10 -12
  105. package/lib-es/debug.js.map +1 -1
  106. package/lib-es/finalizeInput.js +20 -59
  107. package/lib-es/finalizeInput.js.map +1 -1
  108. package/lib-es/getAppAndVersion.d.ts +1 -1
  109. package/lib-es/getAppAndVersion.d.ts.map +1 -1
  110. package/lib-es/getAppAndVersion.js +27 -70
  111. package/lib-es/getAppAndVersion.js.map +1 -1
  112. package/lib-es/getTrustedInput.js +104 -247
  113. package/lib-es/getTrustedInput.js.map +1 -1
  114. package/lib-es/getTrustedInputBIP143.js +5 -6
  115. package/lib-es/getTrustedInputBIP143.js.map +1 -1
  116. package/lib-es/getWalletPublicKey.d.ts +1 -1
  117. package/lib-es/getWalletPublicKey.d.ts.map +1 -1
  118. package/lib-es/getWalletPublicKey.js +25 -71
  119. package/lib-es/getWalletPublicKey.js.map +1 -1
  120. package/lib-es/newops/accounttype.d.ts +2 -2
  121. package/lib-es/newops/accounttype.d.ts.map +1 -1
  122. package/lib-es/newops/accounttype.js +79 -123
  123. package/lib-es/newops/accounttype.js.map +1 -1
  124. package/lib-es/newops/appClient.js +92 -200
  125. package/lib-es/newops/appClient.js.map +1 -1
  126. package/lib-es/newops/clientCommands.js +117 -214
  127. package/lib-es/newops/clientCommands.js.map +1 -1
  128. package/lib-es/newops/merkelizedPsbt.js +25 -73
  129. package/lib-es/newops/merkelizedPsbt.js.map +1 -1
  130. package/lib-es/newops/merkle.js +36 -66
  131. package/lib-es/newops/merkle.js.map +1 -1
  132. package/lib-es/newops/merkleMap.js +8 -10
  133. package/lib-es/newops/merkleMap.js.map +1 -1
  134. package/lib-es/newops/policy.d.ts +1 -1
  135. package/lib-es/newops/policy.d.ts.map +1 -1
  136. package/lib-es/newops/policy.js +12 -14
  137. package/lib-es/newops/policy.js.map +1 -1
  138. package/lib-es/newops/psbtExtractor.js +7 -7
  139. package/lib-es/newops/psbtExtractor.js.map +1 -1
  140. package/lib-es/newops/psbtFinalizer.js +19 -19
  141. package/lib-es/newops/psbtFinalizer.js.map +1 -1
  142. package/lib-es/newops/psbtv2.d.ts +1 -1
  143. package/lib-es/newops/psbtv2.d.ts.map +1 -1
  144. package/lib-es/newops/psbtv2.js +225 -286
  145. package/lib-es/newops/psbtv2.js.map +1 -1
  146. package/lib-es/serializeTransaction.js +11 -13
  147. package/lib-es/serializeTransaction.js.map +1 -1
  148. package/lib-es/shouldUseTrustedInputForSegwit.js +1 -2
  149. package/lib-es/shouldUseTrustedInputForSegwit.js.map +1 -1
  150. package/lib-es/signMessage.js +44 -96
  151. package/lib-es/signMessage.js.map +1 -1
  152. package/lib-es/signP2SHTransaction.d.ts +1 -1
  153. package/lib-es/signP2SHTransaction.d.ts.map +1 -1
  154. package/lib-es/signP2SHTransaction.js +84 -180
  155. package/lib-es/signP2SHTransaction.js.map +1 -1
  156. package/lib-es/signTransaction.js +6 -7
  157. package/lib-es/signTransaction.js.map +1 -1
  158. package/lib-es/splitTransaction.js +46 -50
  159. package/lib-es/splitTransaction.js.map +1 -1
  160. package/lib-es/startUntrustedHashTransactionInput.js +62 -164
  161. package/lib-es/startUntrustedHashTransactionInput.js.map +1 -1
  162. package/lib-es/varint.js +9 -9
  163. package/lib-es/varint.js.map +1 -1
  164. package/package.json +5 -6
  165. 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
- var defaultsSignTransaction = {
23
+ const defaultsSignTransaction = {
73
24
  lockTime: DEFAULT_LOCKTIME,
74
25
  sigHashType: SIGHASH_ALL,
75
26
  segwit: false,
76
27
  additionals: [],
77
- onDeviceStreaming: function (_e) { },
78
- onDeviceSignatureGranted: function () { },
79
- onDeviceSignatureRequested: function () { }
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
- var signTx, inputs, associatedKeysets, changePath, outputScriptHex, lockTime, sigHashType, segwit, initialTimestamp, additionals, expiryHeight, onDeviceStreaming, onDeviceSignatureGranted, onDeviceSignatureRequested, useTrustedInputForSegwit, a, e_1, notify, isDecred, isZcash, isXST, startTime, sapling, bech32, useBip143, lockTimeBuffer, nullScript, nullPrevout, defaultVersion, trustedInputs, regularOutputs, signatures, publicKeys, firstRun, resuming, targetTransaction, getTrustedInputCall, outputScript, inputs_1, inputs_1_1, input, trustedInput, sequence, outputs, index, e_2_1, result_1, i, r, i, i, input, script, pseudoTX, pseudoTrustedInputs, signature, i, signatureSize, keySize, offset, result, witness, i, tmpScriptData, decredWitness_1;
84
- var e_2, _a;
85
- return __generator(this, function (_b) {
86
- switch (_b.label) {
87
- case 0:
88
- signTx = __assign(__assign({}, defaultsSignTransaction), arg);
89
- inputs = signTx.inputs, associatedKeysets = signTx.associatedKeysets, changePath = signTx.changePath, outputScriptHex = signTx.outputScriptHex, lockTime = signTx.lockTime, sigHashType = signTx.sigHashType, segwit = signTx.segwit, initialTimestamp = signTx.initialTimestamp, additionals = signTx.additionals, expiryHeight = signTx.expiryHeight, onDeviceStreaming = signTx.onDeviceStreaming, onDeviceSignatureGranted = signTx.onDeviceSignatureGranted, onDeviceSignatureRequested = signTx.onDeviceSignatureRequested;
90
- useTrustedInputForSegwit = signTx.useTrustedInputForSegwit;
91
- if (!(useTrustedInputForSegwit === undefined)) return [3 /*break*/, 4];
92
- _b.label = 1;
93
- case 1:
94
- _b.trys.push([1, 3, , 4]);
95
- return [4 /*yield*/, getAppAndVersion(transport)];
96
- case 2:
97
- a = _b.sent();
98
- useTrustedInputForSegwit = shouldUseTrustedInputForSegwit(a);
99
- return [3 /*break*/, 4];
100
- case 3:
101
- e_1 = _b.sent();
102
- if (e_1.statusCode === 0x6d00) {
103
- useTrustedInputForSegwit = false;
104
- }
105
- else {
106
- throw e_1;
107
- }
108
- return [3 /*break*/, 4];
109
- case 4:
110
- notify = function (loop, i) {
111
- var length = inputs.length;
112
- if (length < 3)
113
- return; // there is not enough significant event to worth notifying (aka just use a spinner)
114
- var index = length * loop + i;
115
- var total = 2 * length;
116
- var progress = index / total;
117
- onDeviceStreaming({
118
- progress: progress,
119
- total: total,
120
- index: index
121
- });
122
- };
123
- isDecred = additionals.includes("decred");
124
- isZcash = additionals.includes("zcash");
125
- isXST = additionals.includes("stealthcoin");
126
- startTime = Date.now();
127
- sapling = additionals.includes("sapling");
128
- bech32 = segwit && additionals.includes("bech32");
129
- useBip143 = segwit ||
130
- (!!additionals &&
131
- (additionals.includes("abc") ||
132
- additionals.includes("gold") ||
133
- additionals.includes("bip143"))) ||
134
- (!!expiryHeight && !isDecred);
135
- lockTimeBuffer = Buffer.alloc(4);
136
- lockTimeBuffer.writeUInt32LE(lockTime, 0);
137
- nullScript = Buffer.alloc(0);
138
- nullPrevout = Buffer.alloc(0);
139
- defaultVersion = Buffer.alloc(4);
140
- !!expiryHeight && !isDecred
141
- ? defaultVersion.writeUInt32LE(isZcash ? 0x80000005 : sapling ? 0x80000004 : 0x80000003, 0) // v5 format for zcash refer to https://zips.z.cash/zip-0225
142
- : isXST
143
- ? defaultVersion.writeUInt32LE(2, 0)
144
- : defaultVersion.writeUInt32LE(1, 0);
145
- trustedInputs = [];
146
- regularOutputs = [];
147
- signatures = [];
148
- publicKeys = [];
149
- firstRun = true;
150
- resuming = false;
151
- targetTransaction = {
152
- inputs: [],
153
- version: defaultVersion,
154
- timestamp: Buffer.alloc(0)
155
- };
156
- getTrustedInputCall = useBip143 && !useTrustedInputForSegwit
157
- ? getTrustedInputBIP143
158
- : getTrustedInput;
159
- outputScript = Buffer.from(outputScriptHex, "hex");
160
- notify(0, 0);
161
- _b.label = 5;
162
- case 5:
163
- _b.trys.push([5, 11, 12, 13]);
164
- inputs_1 = __values(inputs), inputs_1_1 = inputs_1.next();
165
- _b.label = 6;
166
- case 6:
167
- if (!!inputs_1_1.done) return [3 /*break*/, 10];
168
- input = inputs_1_1.value;
169
- if (!!resuming) return [3 /*break*/, 8];
170
- return [4 /*yield*/, getTrustedInputCall(transport, input[1], input[0], additionals)];
171
- case 7:
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