@shapeshiftoss/hdwallet-keepkey 1.55.2-alpha.0 → 1.55.3

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/dist/bitcoin.js CHANGED
@@ -1,48 +1,10 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.btcIsSameAccount = exports.btcGetAccountPaths = exports.btcVerifyMessage = exports.btcSignMessage = exports.btcSupportsNativeShapeShift = exports.btcSupportsSecureTransfer = exports.btcSignTx = exports.btcGetAddress = exports.btcSupportsScriptType = exports.btcSupportsCoin = void 0;
39
- const Messages = __importStar(require("@keepkey/device-protocol/lib/messages_pb"));
40
- const Types = __importStar(require("@keepkey/device-protocol/lib/types_pb"));
41
- const bitcoinjs = __importStar(require("@shapeshiftoss/bitcoinjs-lib"));
42
- const core = __importStar(require("@shapeshiftoss/hdwallet-core"));
43
- const assert_1 = __importDefault(require("assert"));
44
- const icepick_1 = require("icepick");
45
- const utils_1 = require("./utils");
1
+ import * as Messages from "@keepkey/device-protocol/lib/messages_pb";
2
+ import * as Types from "@keepkey/device-protocol/lib/types_pb";
3
+ import * as bitcoinjs from "@shapeshiftoss/bitcoinjs-lib";
4
+ import * as core from "@shapeshiftoss/hdwallet-core";
5
+ import assert from "assert";
6
+ import { thaw } from "icepick";
7
+ import { toUTF8Array, translateInputScriptType, translateOutputScriptType } from "./utils";
46
8
  // FIXME: load this from the device's coin table, or from some static features
47
9
  // table... instead of, you know, adding another God-forsaken coin table.
48
10
  // :facepalm:
@@ -89,7 +51,7 @@ function prepareSignTx(coin, inputs, outputs) {
89
51
  utxo.setPrevIndex(input.vout);
90
52
  if (input.sequence !== undefined)
91
53
  utxo.setSequence(input.sequence);
92
- utxo.setScriptType((0, utils_1.translateInputScriptType)(input.scriptType));
54
+ utxo.setScriptType(translateInputScriptType(input.scriptType));
93
55
  utxo.setAddressNList(input.addressNList);
94
56
  utxo.setAmount(Number(input.amount));
95
57
  unsignedTx.addInputs(utxo, i);
@@ -106,7 +68,7 @@ function prepareSignTx(coin, inputs, outputs) {
106
68
  }
107
69
  if (output.isChange || output.addressType === core.BTCOutputAddressType.Transfer) {
108
70
  // BTCSignTxOutputTranfer || BTCSignTxOutputChange
109
- newOutput.setScriptType((0, utils_1.translateOutputScriptType)(output.scriptType));
71
+ newOutput.setScriptType(translateOutputScriptType(output.scriptType));
110
72
  newOutput.setAddressNList(output.addressNList);
111
73
  newOutput.setAddressType(output.isChange ? Types.OutputAddressType.CHANGE : Types.OutputAddressType.TRANSFER);
112
74
  }
@@ -119,7 +81,7 @@ function prepareSignTx(coin, inputs, outputs) {
119
81
  else {
120
82
  // BTCSignTxOutputSpend
121
83
  newOutput.setScriptType(Types.OutputScriptType.PAYTOADDRESS);
122
- (0, assert_1.default)(output.address !== undefined, "Output must have a valid BTC address.");
84
+ assert(output.address !== undefined, "Output must have a valid BTC address.");
123
85
  newOutput.setAddress(output.address);
124
86
  newOutput.setAddressType(Types.OutputAddressType.SPEND);
125
87
  }
@@ -206,13 +168,11 @@ function prepareSignTx(coin, inputs, outputs) {
206
168
  });
207
169
  return txmap;
208
170
  }
209
- function ensureCoinSupport(wallet, coin) {
210
- return __awaiter(this, void 0, void 0, function* () {
211
- if (!supportedCoins.includes(coin))
212
- throw new Error(`'${coin}' not yet supported in HDWalletKeepKey`);
213
- if (!wallet.btcSupportsCoin(coin))
214
- throw new Error(`'${coin} is not supported in this firmware version`);
215
- });
171
+ async function ensureCoinSupport(wallet, coin) {
172
+ if (!supportedCoins.includes(coin))
173
+ throw new Error(`'${coin}' not yet supported in HDWalletKeepKey`);
174
+ if (!wallet.btcSupportsCoin(coin))
175
+ throw new Error(`'${coin} is not supported in this firmware version`);
216
176
  }
217
177
  function validateVoutOrdering(msg) {
218
178
  // From THORChain specification:
@@ -231,7 +191,6 @@ function validateVoutOrdering(msg) {
231
191
  - count vouts > 4
232
192
  - count vouts with coins (value) > 2
233
193
  */
234
- var _a;
235
194
  // Check that vout:0 contains the vault address
236
195
  if (msg.outputs[0].address != msg.vaultAddress) {
237
196
  return false;
@@ -241,301 +200,277 @@ function validateVoutOrdering(msg) {
241
200
  return false;
242
201
  }
243
202
  // Check and make sure vout:2 has OP_RETURN data
244
- if (!(msg.outputs[2] && ((_a = msg.outputs[2]) === null || _a === void 0 ? void 0 : _a.opReturnData))) {
203
+ if (!(msg.outputs[2] && msg.outputs[2]?.opReturnData)) {
245
204
  return false;
246
205
  }
247
206
  return true;
248
207
  }
249
- function btcSupportsCoin(coin) {
250
- return __awaiter(this, void 0, void 0, function* () {
251
- // FIXME: inspect the CoinTable to determine which coins are actually supported by the device.
252
- return supportedCoins.includes(coin);
253
- });
208
+ export async function btcSupportsCoin(coin) {
209
+ // FIXME: inspect the CoinTable to determine which coins are actually supported by the device.
210
+ return supportedCoins.includes(coin);
254
211
  }
255
- exports.btcSupportsCoin = btcSupportsCoin;
256
- function btcSupportsScriptType(coin, scriptType) {
257
- return __awaiter(this, void 0, void 0, function* () {
258
- if (!supportedCoins.includes(coin))
259
- return false;
260
- if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendP2SHWitness)
261
- return false;
262
- if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendWitness)
263
- return false;
264
- return true;
265
- });
212
+ export async function btcSupportsScriptType(coin, scriptType) {
213
+ if (!supportedCoins.includes(coin))
214
+ return false;
215
+ if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendP2SHWitness)
216
+ return false;
217
+ if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendWitness)
218
+ return false;
219
+ return true;
266
220
  }
267
- exports.btcSupportsScriptType = btcSupportsScriptType;
268
- function btcGetAddress(wallet, transport, msg) {
269
- return __awaiter(this, void 0, void 0, function* () {
270
- yield ensureCoinSupport(wallet, msg.coin);
271
- const addr = new Messages.GetAddress();
272
- addr.setAddressNList(msg.addressNList);
273
- addr.setCoinName(msg.coin);
274
- addr.setShowDisplay(msg.showDisplay || false);
275
- addr.setScriptType((0, utils_1.translateInputScriptType)(msg.scriptType || core.BTCInputScriptType.SpendAddress));
276
- const response = yield transport.call(Messages.MessageType.MESSAGETYPE_GETADDRESS, addr, {
277
- msgTimeout: core.LONG_TIMEOUT,
278
- });
279
- if (response.message_type === core.Events.CANCEL)
280
- throw response;
281
- const btcAddress = response.proto;
282
- return core.mustBeDefined(btcAddress.getAddress());
221
+ export async function btcGetAddress(wallet, transport, msg) {
222
+ await ensureCoinSupport(wallet, msg.coin);
223
+ const addr = new Messages.GetAddress();
224
+ addr.setAddressNList(msg.addressNList);
225
+ addr.setCoinName(msg.coin);
226
+ addr.setShowDisplay(msg.showDisplay || false);
227
+ addr.setScriptType(translateInputScriptType(msg.scriptType || core.BTCInputScriptType.SpendAddress));
228
+ const response = await transport.call(Messages.MessageType.MESSAGETYPE_GETADDRESS, addr, {
229
+ msgTimeout: core.LONG_TIMEOUT,
283
230
  });
231
+ if (response.message_type === core.Events.CANCEL)
232
+ throw response;
233
+ const btcAddress = response.proto;
234
+ return core.mustBeDefined(btcAddress.getAddress());
284
235
  }
285
- exports.btcGetAddress = btcGetAddress;
286
- function btcSignTx(wallet, transport, msgIn) {
287
- return __awaiter(this, void 0, void 0, function* () {
288
- return transport.lockDuring(() => __awaiter(this, void 0, void 0, function* () {
289
- // Make a copy of the input parameter so as to not mutate the caller's data.
290
- // Unfreezing a recursively-frozen object is nontrivial, so we leverage an existing package
291
- const msg = (0, icepick_1.thaw)(msgIn);
292
- msg.outputs = (0, icepick_1.thaw)(msgIn.outputs);
293
- yield ensureCoinSupport(wallet, msg.coin);
294
- if (msg.opReturnData) {
295
- if (msg.opReturnData.length > 80) {
296
- throw new Error("OP_RETURN output character count is too damn high.");
297
- }
298
- msg.outputs.push({
299
- addressType: core.BTCOutputAddressType.Spend,
300
- opReturnData: Buffer.from(msg.opReturnData).toString("base64"),
301
- amount: "0",
302
- isChange: false,
303
- });
304
- }
305
- // If this is a THORChain transaction, validate the vout ordering
306
- if (msg.vaultAddress && !validateVoutOrdering(msg)) {
307
- throw new Error("Improper vout ordering for BTC Thorchain transaction");
236
+ export async function btcSignTx(wallet, transport, msgIn) {
237
+ return transport.lockDuring(async () => {
238
+ // Make a copy of the input parameter so as to not mutate the caller's data.
239
+ // Unfreezing a recursively-frozen object is nontrivial, so we leverage an existing package
240
+ const msg = thaw(msgIn);
241
+ msg.outputs = thaw(msgIn.outputs);
242
+ await ensureCoinSupport(wallet, msg.coin);
243
+ if (msg.opReturnData) {
244
+ if (msg.opReturnData.length > 80) {
245
+ throw new Error("OP_RETURN output character count is too damn high.");
308
246
  }
309
- const txmap = prepareSignTx(msg.coin, msg.inputs, msg.outputs);
310
- // Prepare and send initial message
311
- const tx = new Messages.SignTx();
312
- tx.setInputsCount(msg.inputs.length);
313
- tx.setOutputsCount(msg.outputs.length);
314
- tx.setCoinName(msg.coin);
315
- if (msg.version !== undefined)
316
- tx.setVersion(msg.version);
317
- tx.setLockTime(msg.locktime || 0);
318
- let responseType;
319
- let response;
320
- const { message_enum, proto } = yield transport.call(Messages.MessageType.MESSAGETYPE_SIGNTX, tx, {
321
- msgTimeout: core.LONG_TIMEOUT,
322
- omitLock: true,
323
- }); // 5 Minute timeout
324
- responseType = message_enum;
325
- response = proto;
326
- // Prepare structure for signatures
327
- const signatures = new Array(msg.inputs.length).fill(null);
328
- let serializedTx = "";
329
- try {
330
- // Begin callback loop
331
- // eslint-disable-next-line no-constant-condition
332
- while (true) {
333
- if (responseType !== Messages.MessageType.MESSAGETYPE_TXREQUEST) {
334
- throw new Error(`Unexpected message type: ${responseType}`);
335
- }
336
- const txRequest = response;
337
- // If there's some part of signed transaction, add it
247
+ msg.outputs.push({
248
+ addressType: core.BTCOutputAddressType.Spend,
249
+ opReturnData: Buffer.from(msg.opReturnData).toString("base64"),
250
+ amount: "0",
251
+ isChange: false,
252
+ });
253
+ }
254
+ // If this is a THORChain transaction, validate the vout ordering
255
+ if (msg.vaultAddress && !validateVoutOrdering(msg)) {
256
+ throw new Error("Improper vout ordering for BTC Thorchain transaction");
257
+ }
258
+ const txmap = prepareSignTx(msg.coin, msg.inputs, msg.outputs);
259
+ // Prepare and send initial message
260
+ const tx = new Messages.SignTx();
261
+ tx.setInputsCount(msg.inputs.length);
262
+ tx.setOutputsCount(msg.outputs.length);
263
+ tx.setCoinName(msg.coin);
264
+ if (msg.version !== undefined)
265
+ tx.setVersion(msg.version);
266
+ tx.setLockTime(msg.locktime || 0);
267
+ let responseType;
268
+ let response;
269
+ const { message_enum, proto } = await transport.call(Messages.MessageType.MESSAGETYPE_SIGNTX, tx, {
270
+ msgTimeout: core.LONG_TIMEOUT,
271
+ omitLock: true,
272
+ }); // 5 Minute timeout
273
+ responseType = message_enum;
274
+ response = proto;
275
+ // Prepare structure for signatures
276
+ const signatures = new Array(msg.inputs.length).fill(null);
277
+ let serializedTx = "";
278
+ try {
279
+ // Begin callback loop
280
+ // eslint-disable-next-line no-constant-condition
281
+ while (true) {
282
+ if (responseType !== Messages.MessageType.MESSAGETYPE_TXREQUEST) {
283
+ throw new Error(`Unexpected message type: ${responseType}`);
284
+ }
285
+ const txRequest = response;
286
+ // If there's some part of signed transaction, add it
287
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
288
+ if (txRequest.hasSerialized() && txRequest.getSerialized().hasSerializedTx()) {
338
289
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
339
- if (txRequest.hasSerialized() && txRequest.getSerialized().hasSerializedTx()) {
340
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
341
- serializedTx += core.toHexString(txRequest.getSerialized().getSerializedTx_asU8());
342
- }
290
+ serializedTx += core.toHexString(txRequest.getSerialized().getSerializedTx_asU8());
291
+ }
292
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
293
+ if (txRequest.hasSerialized() && txRequest.getSerialized().hasSignatureIndex()) {
343
294
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
344
- if (txRequest.hasSerialized() && txRequest.getSerialized().hasSignatureIndex()) {
345
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
346
- const sigIdx = txRequest.getSerialized().getSignatureIndex();
347
- if (signatures[sigIdx] !== null) {
348
- throw new Error(`Signature for index ${sigIdx} already filled`);
349
- }
350
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
351
- signatures[sigIdx] = core.toHexString(txRequest.getSerialized().getSignature_asU8());
295
+ const sigIdx = txRequest.getSerialized().getSignatureIndex();
296
+ if (signatures[sigIdx] !== null) {
297
+ throw new Error(`Signature for index ${sigIdx} already filled`);
352
298
  }
353
- if (txRequest.getRequestType() === Types.RequestType.TXFINISHED) {
354
- // Device didn't ask for more information, finish workflow
355
- break;
356
- }
357
- let currentTx;
358
- let currentMsg;
359
- let txAck;
360
- // Device asked for one more information, let's process it.
361
- if (!txRequest.hasDetails())
362
- throw new Error("expected details");
363
299
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
364
- const reqDetails = txRequest.getDetails();
300
+ signatures[sigIdx] = core.toHexString(txRequest.getSerialized().getSignature_asU8());
301
+ }
302
+ if (txRequest.getRequestType() === Types.RequestType.TXFINISHED) {
303
+ // Device didn't ask for more information, finish workflow
304
+ break;
305
+ }
306
+ let currentTx;
307
+ let currentMsg;
308
+ let txAck;
309
+ // Device asked for one more information, let's process it.
310
+ if (!txRequest.hasDetails())
311
+ throw new Error("expected details");
312
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
313
+ const reqDetails = txRequest.getDetails();
314
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
315
+ if (!reqDetails.hasTxHash()) {
316
+ currentTx = txmap["unsigned"];
317
+ }
318
+ else {
319
+ currentTx = txmap[core.toHexString(reqDetails.getTxHash_asU8())];
320
+ }
321
+ if (txRequest.getRequestType() === Types.RequestType.TXMETA) {
322
+ currentMsg = new Types.TransactionType();
323
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
324
+ if (currentTx.hasVersion())
325
+ currentMsg.setVersion(currentTx.getVersion());
365
326
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
366
- if (!reqDetails.hasTxHash()) {
367
- currentTx = txmap["unsigned"];
327
+ if (currentTx.hasLockTime())
328
+ currentMsg.setLockTime(currentTx.getLockTime());
329
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
330
+ if (currentTx.hasInputsCnt())
331
+ currentMsg.setInputsCnt(currentTx.getInputsCnt());
332
+ if (reqDetails.hasTxHash()) {
333
+ currentMsg.setOutputsCnt(currentTx.getBinOutputsList().length);
368
334
  }
369
335
  else {
370
- currentTx = txmap[core.toHexString(reqDetails.getTxHash_asU8())];
336
+ currentMsg.setOutputsCnt(currentTx.getOutputsList().length);
371
337
  }
372
- if (txRequest.getRequestType() === Types.RequestType.TXMETA) {
373
- currentMsg = new Types.TransactionType();
374
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
375
- if (currentTx.hasVersion())
376
- currentMsg.setVersion(currentTx.getVersion());
377
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
378
- if (currentTx.hasLockTime())
379
- currentMsg.setLockTime(currentTx.getLockTime());
380
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
381
- if (currentTx.hasInputsCnt())
382
- currentMsg.setInputsCnt(currentTx.getInputsCnt());
383
- if (reqDetails.hasTxHash()) {
384
- currentMsg.setOutputsCnt(currentTx.getBinOutputsList().length);
385
- }
386
- else {
387
- currentMsg.setOutputsCnt(currentTx.getOutputsList().length);
388
- }
389
- if (currentTx.hasExtraData()) {
390
- currentMsg.setExtraDataLen(currentTx.getExtraData_asU8().length);
391
- }
392
- else {
393
- currentMsg.setExtraDataLen(0);
394
- }
395
- txAck = new Messages.TxAck();
396
- txAck.setTx(currentMsg);
397
- const message = yield transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
398
- msgTimeout: core.LONG_TIMEOUT,
399
- omitLock: true,
400
- }); // 5 Minute timeout
401
- responseType = message.message_enum;
402
- response = message.proto;
403
- continue;
338
+ if (currentTx.hasExtraData()) {
339
+ currentMsg.setExtraDataLen(currentTx.getExtraData_asU8().length);
404
340
  }
405
- if (txRequest.getRequestType() === Types.RequestType.TXINPUT) {
406
- if (!reqDetails.hasRequestIndex())
407
- throw new Error("expected request index");
408
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
409
- const reqIndex = reqDetails.getRequestIndex();
410
- currentMsg = new Types.TransactionType();
411
- currentMsg.setInputsList([currentTx.getInputsList()[reqIndex]]);
412
- txAck = new Messages.TxAck();
413
- txAck.setTx(currentMsg);
414
- const message = yield transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
415
- msgTimeout: core.LONG_TIMEOUT,
416
- omitLock: true,
417
- }); // 5 Minute timeout
418
- responseType = message.message_enum;
419
- response = message.proto;
420
- continue;
341
+ else {
342
+ currentMsg.setExtraDataLen(0);
421
343
  }
422
- if (txRequest.getRequestType() === Types.RequestType.TXOUTPUT) {
423
- if (!reqDetails.hasRequestIndex())
424
- throw new Error("expected request index");
425
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
426
- const reqIndex = reqDetails.getRequestIndex();
427
- currentMsg = new Types.TransactionType();
428
- if (reqDetails.hasTxHash()) {
429
- currentMsg.setBinOutputsList([currentTx.getBinOutputsList()[reqIndex]]);
430
- }
431
- else {
432
- currentMsg.setOutputsList([currentTx.getOutputsList()[reqIndex]]);
433
- currentMsg.setOutputsCnt(1);
434
- }
435
- txAck = new Messages.TxAck();
436
- txAck.setTx(currentMsg);
437
- const message = yield transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
438
- msgTimeout: core.LONG_TIMEOUT,
439
- omitLock: true,
440
- }); // 5 Minute timeout
441
- responseType = message.message_enum;
442
- response = message.proto;
443
- continue;
344
+ txAck = new Messages.TxAck();
345
+ txAck.setTx(currentMsg);
346
+ const message = await transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
347
+ msgTimeout: core.LONG_TIMEOUT,
348
+ omitLock: true,
349
+ }); // 5 Minute timeout
350
+ responseType = message.message_enum;
351
+ response = message.proto;
352
+ continue;
353
+ }
354
+ if (txRequest.getRequestType() === Types.RequestType.TXINPUT) {
355
+ if (!reqDetails.hasRequestIndex())
356
+ throw new Error("expected request index");
357
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
358
+ const reqIndex = reqDetails.getRequestIndex();
359
+ currentMsg = new Types.TransactionType();
360
+ currentMsg.setInputsList([currentTx.getInputsList()[reqIndex]]);
361
+ txAck = new Messages.TxAck();
362
+ txAck.setTx(currentMsg);
363
+ const message = await transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
364
+ msgTimeout: core.LONG_TIMEOUT,
365
+ omitLock: true,
366
+ }); // 5 Minute timeout
367
+ responseType = message.message_enum;
368
+ response = message.proto;
369
+ continue;
370
+ }
371
+ if (txRequest.getRequestType() === Types.RequestType.TXOUTPUT) {
372
+ if (!reqDetails.hasRequestIndex())
373
+ throw new Error("expected request index");
374
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
375
+ const reqIndex = reqDetails.getRequestIndex();
376
+ currentMsg = new Types.TransactionType();
377
+ if (reqDetails.hasTxHash()) {
378
+ currentMsg.setBinOutputsList([currentTx.getBinOutputsList()[reqIndex]]);
444
379
  }
445
- if (txRequest.getRequestType() === Types.RequestType.TXEXTRADATA) {
446
- if (!reqDetails.hasExtraDataOffset() || !reqDetails.hasExtraDataLen())
447
- throw new Error("missing extra data offset and length");
448
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
449
- const offset = reqDetails.getExtraDataOffset();
450
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
451
- const length = reqDetails.getExtraDataLen();
452
- currentMsg = new Types.TransactionType();
453
- currentMsg.setExtraData(currentTx.getExtraData_asU8().slice(offset, offset + length));
454
- txAck = new Messages.TxAck();
455
- txAck.setTx(currentMsg);
456
- const message = yield transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
457
- msgTimeout: core.LONG_TIMEOUT,
458
- omitLock: true,
459
- }); // 5 Minute timeout
460
- responseType = message.message_enum;
461
- response = message.proto;
462
- continue;
380
+ else {
381
+ currentMsg.setOutputsList([currentTx.getOutputsList()[reqIndex]]);
382
+ currentMsg.setOutputsCnt(1);
463
383
  }
384
+ txAck = new Messages.TxAck();
385
+ txAck.setTx(currentMsg);
386
+ const message = await transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
387
+ msgTimeout: core.LONG_TIMEOUT,
388
+ omitLock: true,
389
+ }); // 5 Minute timeout
390
+ responseType = message.message_enum;
391
+ response = message.proto;
392
+ continue;
393
+ }
394
+ if (txRequest.getRequestType() === Types.RequestType.TXEXTRADATA) {
395
+ if (!reqDetails.hasExtraDataOffset() || !reqDetails.hasExtraDataLen())
396
+ throw new Error("missing extra data offset and length");
397
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
398
+ const offset = reqDetails.getExtraDataOffset();
399
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
400
+ const length = reqDetails.getExtraDataLen();
401
+ currentMsg = new Types.TransactionType();
402
+ currentMsg.setExtraData(currentTx.getExtraData_asU8().slice(offset, offset + length));
403
+ txAck = new Messages.TxAck();
404
+ txAck.setTx(currentMsg);
405
+ const message = await transport.call(Messages.MessageType.MESSAGETYPE_TXACK, txAck, {
406
+ msgTimeout: core.LONG_TIMEOUT,
407
+ omitLock: true,
408
+ }); // 5 Minute timeout
409
+ responseType = message.message_enum;
410
+ response = message.proto;
411
+ continue;
464
412
  }
465
413
  }
466
- catch (error) {
467
- console.error({ error });
468
- throw new Error("Failed to sign BTC transaction");
469
- }
470
- if (signatures.includes(null)) {
471
- throw new Error("Some signatures are missing!");
472
- }
473
- return {
474
- signatures: signatures,
475
- serializedTx: serializedTx,
476
- };
477
- }));
414
+ }
415
+ catch (error) {
416
+ console.error({ error });
417
+ throw new Error("Failed to sign BTC transaction");
418
+ }
419
+ if (signatures.includes(null)) {
420
+ throw new Error("Some signatures are missing!");
421
+ }
422
+ return {
423
+ signatures: signatures,
424
+ serializedTx: serializedTx,
425
+ };
478
426
  });
479
427
  }
480
- exports.btcSignTx = btcSignTx;
481
- function btcSupportsSecureTransfer() {
482
- return __awaiter(this, void 0, void 0, function* () {
483
- return true;
484
- });
428
+ export async function btcSupportsSecureTransfer() {
429
+ return true;
485
430
  }
486
- exports.btcSupportsSecureTransfer = btcSupportsSecureTransfer;
487
- function btcSupportsNativeShapeShift() {
431
+ export function btcSupportsNativeShapeShift() {
488
432
  return true;
489
433
  }
490
- exports.btcSupportsNativeShapeShift = btcSupportsNativeShapeShift;
491
- function btcSignMessage(wallet, transport, msg) {
492
- var _a;
493
- return __awaiter(this, void 0, void 0, function* () {
494
- yield ensureCoinSupport(wallet, msg.coin);
495
- const sign = new Messages.SignMessage();
496
- sign.setAddressNList(msg.addressNList);
497
- sign.setMessage((0, utils_1.toUTF8Array)(msg.message));
498
- sign.setCoinName(msg.coin || "Bitcoin");
499
- sign.setScriptType((0, utils_1.translateInputScriptType)((_a = msg.scriptType) !== null && _a !== void 0 ? _a : core.BTCInputScriptType.SpendAddress));
500
- const event = yield transport.call(Messages.MessageType.MESSAGETYPE_SIGNMESSAGE, sign, {
501
- msgTimeout: core.LONG_TIMEOUT,
502
- });
503
- const messageSignature = event.proto;
504
- const address = messageSignature.getAddress();
505
- if (!address)
506
- throw new Error("btcSignMessage failed");
507
- return {
508
- address,
509
- signature: core.toHexString(messageSignature.getSignature_asU8()),
510
- };
434
+ export async function btcSignMessage(wallet, transport, msg) {
435
+ await ensureCoinSupport(wallet, msg.coin);
436
+ const sign = new Messages.SignMessage();
437
+ sign.setAddressNList(msg.addressNList);
438
+ sign.setMessage(toUTF8Array(msg.message));
439
+ sign.setCoinName(msg.coin || "Bitcoin");
440
+ sign.setScriptType(translateInputScriptType(msg.scriptType ?? core.BTCInputScriptType.SpendAddress));
441
+ const event = await transport.call(Messages.MessageType.MESSAGETYPE_SIGNMESSAGE, sign, {
442
+ msgTimeout: core.LONG_TIMEOUT,
511
443
  });
444
+ const messageSignature = event.proto;
445
+ const address = messageSignature.getAddress();
446
+ if (!address)
447
+ throw new Error("btcSignMessage failed");
448
+ return {
449
+ address,
450
+ signature: core.toHexString(messageSignature.getSignature_asU8()),
451
+ };
512
452
  }
513
- exports.btcSignMessage = btcSignMessage;
514
- function btcVerifyMessage(wallet, transport, msg) {
515
- return __awaiter(this, void 0, void 0, function* () {
516
- yield ensureCoinSupport(wallet, msg.coin);
517
- const verify = new Messages.VerifyMessage();
518
- verify.setAddress(msg.address);
519
- verify.setSignature(core.arrayify("0x" + msg.signature));
520
- verify.setMessage((0, utils_1.toUTF8Array)(msg.message));
521
- verify.setCoinName(msg.coin);
522
- let event;
523
- try {
524
- event = yield transport.call(Messages.MessageType.MESSAGETYPE_VERIFYMESSAGE, verify);
525
- }
526
- catch (e) {
527
- if (core.isIndexable(e) && e.message_enum === Messages.MessageType.MESSAGETYPE_FAILURE) {
528
- return false;
529
- }
530
- throw e;
453
+ export async function btcVerifyMessage(wallet, transport, msg) {
454
+ await ensureCoinSupport(wallet, msg.coin);
455
+ const verify = new Messages.VerifyMessage();
456
+ verify.setAddress(msg.address);
457
+ verify.setSignature(core.arrayify("0x" + msg.signature));
458
+ verify.setMessage(toUTF8Array(msg.message));
459
+ verify.setCoinName(msg.coin);
460
+ let event;
461
+ try {
462
+ event = await transport.call(Messages.MessageType.MESSAGETYPE_VERIFYMESSAGE, verify);
463
+ }
464
+ catch (e) {
465
+ if (core.isIndexable(e) && e.message_enum === Messages.MessageType.MESSAGETYPE_FAILURE) {
466
+ return false;
531
467
  }
532
- const success = event.proto;
533
- return success.getMessage() === "Message verified";
534
- });
468
+ throw e;
469
+ }
470
+ const success = event.proto;
471
+ return success.getMessage() === "Message verified";
535
472
  }
536
- exports.btcVerifyMessage = btcVerifyMessage;
537
- function btcGetAccountPaths(msg) {
538
- var _a;
473
+ export function btcGetAccountPaths(msg) {
539
474
  const slip44 = core.slip44ByCoin(msg.coin);
540
475
  if (slip44 === undefined)
541
476
  return [];
@@ -548,7 +483,7 @@ function btcGetAccountPaths(msg) {
548
483
  const btcSegwitNative = segwitNativeAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx);
549
484
  // For BCH Forks
550
485
  const bchLegacy = legacyAccount(msg.coin, core.slip44ByCoin("BitcoinCash"), msg.accountIdx);
551
- let paths = (_a = {
486
+ let paths = {
552
487
  Bitcoin: [bip44, bip49, bip84],
553
488
  Litecoin: [bip44, bip49, bip84],
554
489
  Dash: [bip44],
@@ -558,15 +493,14 @@ function btcGetAccountPaths(msg) {
558
493
  BitcoinCash: [bip44, btcLegacy],
559
494
  BitcoinSV: [bip44, bchLegacy, btcLegacy],
560
495
  BitcoinGold: [bip44, bip49, bip84, btcLegacy, btcSegwit, btcSegwitNative],
561
- }[msg.coin]) !== null && _a !== void 0 ? _a : [];
496
+ }[msg.coin] ?? [];
562
497
  if (msg.scriptType !== undefined)
563
498
  paths = paths.filter((path) => {
564
499
  return path.scriptType === msg.scriptType;
565
500
  });
566
501
  return paths;
567
502
  }
568
- exports.btcGetAccountPaths = btcGetAccountPaths;
569
- function btcIsSameAccount(msg) {
503
+ export function btcIsSameAccount(msg) {
570
504
  if (msg.length < 1)
571
505
  return false;
572
506
  if (msg.length > 3)
@@ -609,5 +543,4 @@ function btcIsSameAccount(msg) {
609
543
  }
610
544
  return true;
611
545
  }
612
- exports.btcIsSameAccount = btcIsSameAccount;
613
546
  //# sourceMappingURL=bitcoin.js.map