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