@ledgerhq/live-common 21.31.0 → 21.32.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.
Files changed (143) hide show
  1. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.d.ts +2 -0
  2. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.d.ts.map +1 -0
  3. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.js +100 -0
  4. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.js.map +1 -0
  5. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.pickingStrategies.integration.test.js +0 -10
  6. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.pickingStrategies.integration.test.js.map +1 -1
  7. package/lib/api/Ethereum.d.ts.map +1 -1
  8. package/lib/api/Ethereum.js +1 -0
  9. package/lib/api/Ethereum.js.map +1 -1
  10. package/lib/apps/logic.d.ts.map +1 -1
  11. package/lib/apps/logic.js +7 -5
  12. package/lib/apps/logic.js.map +1 -1
  13. package/lib/apps/polyfill.d.ts.map +1 -1
  14. package/lib/apps/polyfill.js +1 -0
  15. package/lib/apps/polyfill.js.map +1 -1
  16. package/lib/bot/engine.d.ts.map +1 -1
  17. package/lib/bot/engine.js +3 -0
  18. package/lib/bot/engine.js.map +1 -1
  19. package/lib/errors.d.ts +3 -0
  20. package/lib/errors.d.ts.map +1 -1
  21. package/lib/errors.js +3 -2
  22. package/lib/errors.js.map +1 -1
  23. package/lib/exchange/platform/transfer/index.d.ts.map +1 -1
  24. package/lib/exchange/platform/transfer/index.js +15 -1
  25. package/lib/exchange/platform/transfer/index.js.map +1 -1
  26. package/lib/families/algorand/buildTransaction.d.ts +2 -2
  27. package/lib/families/algorand/buildTransaction.d.ts.map +1 -1
  28. package/lib/families/algorand/buildTransaction.js +13 -19
  29. package/lib/families/algorand/buildTransaction.js.map +1 -1
  30. package/lib/families/algorand/js-getFeesForTransaction.js +4 -6
  31. package/lib/families/algorand/js-getFeesForTransaction.js.map +1 -1
  32. package/lib/families/algorand/js-signOperation.js +3 -7
  33. package/lib/families/algorand/js-signOperation.js.map +1 -1
  34. package/lib/families/algorand/js-synchronization.d.ts.map +1 -1
  35. package/lib/families/algorand/js-synchronization.js +1 -0
  36. package/lib/families/algorand/js-synchronization.js.map +1 -1
  37. package/lib/families/bitcoin/js-signOperation.d.ts.map +1 -1
  38. package/lib/families/bitcoin/js-signOperation.js +3 -5
  39. package/lib/families/bitcoin/js-signOperation.js.map +1 -1
  40. package/lib/families/bitcoin/js-synchronisation.js +4 -4
  41. package/lib/families/bitcoin/js-synchronisation.js.map +1 -1
  42. package/lib/families/bitcoin/wallet-btc/crypto/base.d.ts +4 -3
  43. package/lib/families/bitcoin/wallet-btc/crypto/base.d.ts.map +1 -1
  44. package/lib/families/bitcoin/wallet-btc/crypto/base.js +8 -7
  45. package/lib/families/bitcoin/wallet-btc/crypto/base.js.map +1 -1
  46. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.d.ts +1 -1
  47. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.d.ts.map +1 -1
  48. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js +3 -7
  49. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js.map +1 -1
  50. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.d.ts +3 -4
  51. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.d.ts.map +1 -1
  52. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js +27 -53
  53. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js.map +1 -1
  54. package/lib/families/bitcoin/wallet-btc/crypto/decred.d.ts +1 -1
  55. package/lib/families/bitcoin/wallet-btc/crypto/decred.d.ts.map +1 -1
  56. package/lib/families/bitcoin/wallet-btc/crypto/decred.js +2 -7
  57. package/lib/families/bitcoin/wallet-btc/crypto/decred.js.map +1 -1
  58. package/lib/families/bitcoin/wallet-btc/crypto/litecoin.d.ts +1 -6
  59. package/lib/families/bitcoin/wallet-btc/crypto/litecoin.d.ts.map +1 -1
  60. package/lib/families/bitcoin/wallet-btc/crypto/litecoin.js +0 -51
  61. package/lib/families/bitcoin/wallet-btc/crypto/litecoin.js.map +1 -1
  62. package/lib/families/bitcoin/wallet-btc/crypto/types.d.ts +1 -0
  63. package/lib/families/bitcoin/wallet-btc/crypto/types.d.ts.map +1 -1
  64. package/lib/families/bitcoin/wallet-btc/crypto/zec.d.ts +3 -4
  65. package/lib/families/bitcoin/wallet-btc/crypto/zec.d.ts.map +1 -1
  66. package/lib/families/bitcoin/wallet-btc/crypto/zec.js +26 -17
  67. package/lib/families/bitcoin/wallet-btc/crypto/zec.js.map +1 -1
  68. package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts +3 -4
  69. package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts.map +1 -1
  70. package/lib/families/bitcoin/wallet-btc/crypto/zen.js +33 -24
  71. package/lib/families/bitcoin/wallet-btc/crypto/zen.js.map +1 -1
  72. package/lib/families/bitcoin/wallet-btc/explorer/index.d.ts.map +1 -1
  73. package/lib/families/bitcoin/wallet-btc/explorer/index.js +20 -0
  74. package/lib/families/bitcoin/wallet-btc/explorer/index.js.map +1 -1
  75. package/lib/families/bitcoin/wallet-btc/storage/types.d.ts +0 -1
  76. package/lib/families/bitcoin/wallet-btc/storage/types.d.ts.map +1 -1
  77. package/lib/families/celo/speculos-deviceActions.d.ts.map +1 -1
  78. package/lib/families/celo/speculos-deviceActions.js +1 -2
  79. package/lib/families/celo/speculos-deviceActions.js.map +1 -1
  80. package/lib/families/ethereum/signOperation.d.ts.map +1 -1
  81. package/lib/families/ethereum/signOperation.js +26 -15
  82. package/lib/families/ethereum/signOperation.js.map +1 -1
  83. package/lib/families/ethereum/specs.d.ts.map +1 -1
  84. package/lib/families/ethereum/specs.js +2 -4
  85. package/lib/families/ethereum/specs.js.map +1 -1
  86. package/lib/families/ethereum/transaction.d.ts +4 -1
  87. package/lib/families/ethereum/transaction.d.ts.map +1 -1
  88. package/lib/families/ethereum/transaction.js +15 -27
  89. package/lib/families/ethereum/transaction.js.map +1 -1
  90. package/lib/families/solana/api/chain/instruction/token/types.d.ts +1 -1
  91. package/lib/families/solana/specs.js +1 -1
  92. package/lib/families/solana/specs.js.map +1 -1
  93. package/lib/families/tezos/bakers.whitelist-default.d.ts.map +1 -1
  94. package/lib/families/tezos/bakers.whitelist-default.js +1 -1
  95. package/lib/families/tezos/bakers.whitelist-default.js.map +1 -1
  96. package/lib/families/tezos/bridge/js.d.ts.map +1 -1
  97. package/lib/families/tezos/bridge/js.js +2 -1
  98. package/lib/families/tezos/bridge/js.js.map +1 -1
  99. package/lib/families/tezos/test-specifics.d.ts.map +1 -1
  100. package/lib/families/tezos/test-specifics.js +1 -0
  101. package/lib/families/tezos/test-specifics.js.map +1 -1
  102. package/lib/families/tron/synchronisation.test.js +1 -0
  103. package/lib/families/tron/synchronisation.test.js.map +1 -1
  104. package/lib/load/speculos.d.ts +7 -0
  105. package/lib/load/speculos.d.ts.map +1 -1
  106. package/lib/load/speculos.js +27 -11
  107. package/lib/load/speculos.js.map +1 -1
  108. package/package.json +21 -21
  109. package/src/__tests__/__snapshots__/all.libcore.ts.snap +0 -23
  110. package/src/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.ts +47 -0
  111. package/src/__tests__/families/bitcoin/wallet-btc/xpub.pickingStrategies.integration.test.ts +0 -11
  112. package/src/api/Ethereum.ts +1 -0
  113. package/src/apps/logic.ts +6 -3
  114. package/src/apps/polyfill.ts +1 -0
  115. package/src/bot/engine.ts +3 -0
  116. package/src/errors.ts +3 -0
  117. package/src/exchange/platform/transfer/index.ts +21 -1
  118. package/src/families/algorand/buildTransaction.ts +4 -8
  119. package/src/families/algorand/js-getFeesForTransaction.ts +1 -1
  120. package/src/families/algorand/js-signOperation.ts +2 -2
  121. package/src/families/algorand/js-synchronization.ts +1 -0
  122. package/src/families/bitcoin/js-signOperation.ts +3 -5
  123. package/src/families/bitcoin/js-synchronisation.ts +4 -4
  124. package/src/families/bitcoin/wallet-btc/crypto/base.ts +31 -18
  125. package/src/families/bitcoin/wallet-btc/crypto/bitcoin.ts +3 -17
  126. package/src/families/bitcoin/wallet-btc/crypto/bitcoincash.ts +5 -39
  127. package/src/families/bitcoin/wallet-btc/crypto/decred.ts +2 -15
  128. package/src/families/bitcoin/wallet-btc/crypto/litecoin.ts +1 -71
  129. package/src/families/bitcoin/wallet-btc/crypto/types.ts +7 -0
  130. package/src/families/bitcoin/wallet-btc/crypto/zec.ts +4 -22
  131. package/src/families/bitcoin/wallet-btc/crypto/zen.ts +4 -22
  132. package/src/families/bitcoin/wallet-btc/explorer/index.ts +20 -0
  133. package/src/families/bitcoin/wallet-btc/storage/types.ts +0 -1
  134. package/src/families/celo/speculos-deviceActions.ts +0 -1
  135. package/src/families/ethereum/signOperation.ts +21 -20
  136. package/src/families/ethereum/specs.ts +0 -2
  137. package/src/families/ethereum/transaction.ts +10 -24
  138. package/src/families/solana/specs.ts +1 -1
  139. package/src/families/tezos/bakers.whitelist-default.ts +0 -2
  140. package/src/families/tezos/bridge/js.ts +6 -1
  141. package/src/families/tezos/test-specifics.ts +1 -0
  142. package/src/families/tron/synchronisation.test.ts +2 -0
  143. package/src/load/speculos.ts +23 -10
@@ -138,6 +138,7 @@ export const apiForCurrency = (currency: CryptoCurrency): API => {
138
138
  noinput: true,
139
139
  no_input: true,
140
140
  no_token: true,
141
+ filtering: true,
141
142
  block_hash,
142
143
  },
143
144
  }),
package/src/apps/logic.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { $Shape } from "utility-types";
2
2
  import { Subject } from "rxjs";
3
3
  import flatMap from "lodash/flatMap";
4
- import invariant from "invariant";
5
4
  import semver from "semver";
6
5
  import { getDeviceModel } from "@ledgerhq/devices";
7
6
  import type { App } from "../types/manager";
@@ -17,7 +16,7 @@ import {
17
16
  findCryptoCurrencyById,
18
17
  isCurrencySupported,
19
18
  } from "../currencies";
20
- import { LatestFirmwareVersionRequired } from "../errors";
19
+ import { LatestFirmwareVersionRequired, NoSuchAppOnProvider } from "../errors";
21
20
 
22
21
  export const initState = (
23
22
  {
@@ -292,6 +291,7 @@ export const reducer = (state: State, action: Action): State => {
292
291
  // No app found but fw update is available
293
292
  if (
294
293
  !depApp &&
294
+ state.firmware?.updateAvailable?.final?.version &&
295
295
  semver.lt(
296
296
  state.deviceInfo.version,
297
297
  state.firmware?.updateAvailable?.final?.version
@@ -306,7 +306,10 @@ export const reducer = (state: State, action: Action): State => {
306
306
  );
307
307
  }
308
308
 
309
- invariant(depApp, "no such app '%s'", name);
309
+ if (!depApp) {
310
+ throw new NoSuchAppOnProvider("", { appName: name });
311
+ }
312
+
310
313
  const deps = depApp.dependencies;
311
314
  const dependentsOfDep = flatMap(deps, (dep) =>
312
315
  findDependents(state.appByName, dep)
@@ -53,6 +53,7 @@ export const whitelistDependencies = ["Decred", "Decred Testnet"];
53
53
  ["POAP", "Ethereum"],
54
54
  ["OlympusDAO", "Ethereum"],
55
55
  ["Rarible", "Ethereum"],
56
+ ["Ricochet", "Ethereum"],
56
57
  ].forEach(([name, dep]) => declareDep(name, dep));
57
58
  export const getDependencies = (appName: string): string[] =>
58
59
  directDep[appName] || [];
package/src/bot/engine.ts CHANGED
@@ -81,6 +81,9 @@ export async function runWithAppSpec<T extends Transaction>(
81
81
  const mutationReports: MutationReport<T>[] = [];
82
82
  const { appQuery, currency, dependency } = spec;
83
83
  const appCandidate = findAppCandidate(appCandidates, appQuery);
84
+ if (!appCandidate) {
85
+ console.warn("no app found for " + spec.name, { appQuery, appCandidates });
86
+ }
84
87
  invariant(
85
88
  appCandidate,
86
89
  "%s: no app found. Are you sure your COINAPPS is up to date?",
package/src/errors.ts CHANGED
@@ -141,6 +141,9 @@ export const SatStackDescriptorNotImported = createCustomErrorClass(
141
141
  export const SwapNoAvailableProviders = createCustomErrorClass(
142
142
  "SwapNoAvailableProviders"
143
143
  );
144
+ export const NoSuchAppOnProvider = createCustomErrorClass(
145
+ "NoSuchAppOnProvider"
146
+ );
144
147
  export const SwapExchangeRateAmountTooLow = createCustomErrorClass(
145
148
  "SwapExchangeRateAmountTooLow"
146
149
  );
@@ -32,7 +32,27 @@ const fundProviders: Record<
32
32
  curve: string;
33
33
  }
34
34
  > = {
35
- // FIXME: no prod provider for now
35
+ baanx: {
36
+ /**
37
+ * nameAndPubkey is the concatenation of:
38
+ * - an empty buffer of the size of the partner name
39
+ * - a buffer created from the partner name string in ascii encoding
40
+ * - a buffer created from the hexadecimal version of the partner public key
41
+ */
42
+ nameAndPubkey: Buffer.concat([
43
+ Buffer.from([5]),
44
+ Buffer.from("Baanx", "ascii"),
45
+ Buffer.from(
46
+ "04551878b446b6a711949fa51cc5a8685602f8ffb1dfd08f6ab869019d7c125d7737a79e8b5022d860ec7dfbe062d510fec3b5fe0f6ebb1f5e55a074bb7e5dbc4e",
47
+ "hex"
48
+ ),
49
+ ]),
50
+ signature: Buffer.from(
51
+ "304402200345c39e93a22c5ac3f1e70f8b9938b3a60d3a4906067443cf11095af0e685a502201ee5d88dd5539ce36341e49e2505c2a1659e26d8ff08801ed33c50a9126aedd1",
52
+ "hex"
53
+ ),
54
+ curve: "secp256r1",
55
+ },
36
56
  };
37
57
 
38
58
  const getProvider = (
@@ -1,6 +1,5 @@
1
1
  import { encode as msgpackEncode } from "algo-msgpack-with-bigint";
2
2
  import type {
3
- SuggestedParams as AlgoSuggestedParams,
4
3
  Transaction as AlgoTransaction,
5
4
  EncodedTransaction as AlgoTransactionPayload,
6
5
  EncodedSignedTransaction as AlgoSignedTransactionPayload,
@@ -28,8 +27,7 @@ export const buildTransactionPayload = async (
28
27
 
29
28
  const note = memo ? new TextEncoder().encode(memo) : undefined;
30
29
 
31
- // Minor hack: cast the result as a algosdk.SuggestedParams, while it's actually a TransactionParametersResponse
32
- const params = (await getTransactionParams()) as AlgoSuggestedParams;
30
+ const params = await getTransactionParams();
33
31
 
34
32
  let algoTxn: AlgoTransaction;
35
33
  if (subAccount || (assetId && mode === "optIn")) {
@@ -78,18 +76,16 @@ export const buildTransactionPayload = async (
78
76
  return sorted;
79
77
  };
80
78
 
81
- export const encodeToSign = async (
82
- payload: AlgoTransactionPayload
83
- ): Promise<string> => {
79
+ export const encodeToSign = (payload: AlgoTransactionPayload): string => {
84
80
  const msgPackEncoded = msgpackEncode(payload);
85
81
 
86
82
  return Buffer.from(msgPackEncoded).toString("hex");
87
83
  };
88
84
 
89
- export const encodeToBroadcast = async (
85
+ export const encodeToBroadcast = (
90
86
  payload: AlgoTransactionPayload,
91
87
  signature: Buffer
92
- ): Promise<Buffer> => {
88
+ ): Buffer => {
93
89
  const signedPayload: AlgoSignedTransactionPayload = {
94
90
  sig: signature,
95
91
  txn: payload,
@@ -17,7 +17,7 @@ export const getEstimatedFees = async (
17
17
  let suggestedFees = 0;
18
18
  if (params.fee) {
19
19
  const payload = await buildTransactionPayload(account, transaction);
20
- const encoded = await encodeToSign(payload);
20
+ const encoded = encodeToSign(payload);
21
21
  suggestedFees =
22
22
  params.fee * (Buffer.from(encoded, "hex").length + SINGLE_SIGNATURE_SIZE);
23
23
  }
@@ -36,7 +36,7 @@ export const signOperation = ({
36
36
 
37
37
  const algoTx = await buildTransactionPayload(account, transaction);
38
38
 
39
- const toSign = await encodeToSign(algoTx);
39
+ const toSign = encodeToSign(algoTx);
40
40
 
41
41
  const hwApp = new Algorand(transport);
42
42
  const { freshAddressPath } = account;
@@ -53,7 +53,7 @@ export const signOperation = ({
53
53
  throw new Error("No signature");
54
54
  }
55
55
 
56
- const toBroadcast = await encodeToBroadcast(algoTx, signature);
56
+ const toBroadcast = encodeToBroadcast(algoTx, signature);
57
57
 
58
58
  const operation = buildOptimisticOperation(account, transaction);
59
59
 
@@ -291,6 +291,7 @@ const getAccountShape: GetAccountShape = async (
291
291
 
292
292
  const shape = {
293
293
  id: accountId,
294
+ xpub: address,
294
295
  blockHeight: round,
295
296
  balance,
296
297
  spendableBalance,
@@ -32,7 +32,7 @@ const signOperation = ({
32
32
  try {
33
33
  log("hw", `signTransaction ${currency.id} for account ${account.id}`);
34
34
  const txInfo = await buildTransaction(account, transaction);
35
- let senders: string[] = [];
35
+ let senders = new Set<string>();
36
36
  let recipients: string[] = [];
37
37
  let fee = new BigNumber(0);
38
38
  // Maybe better not re-calculate these fields here, instead include them
@@ -41,9 +41,7 @@ const signOperation = ({
41
41
  account,
42
42
  transaction,
43
43
  }).then((res) => {
44
- senders = res.txInputs
45
- .map((i) => i.address)
46
- .filter(Boolean) as string[];
44
+ senders = new Set(res.txInputs.map((i) => i.address).filter(Boolean));
47
45
  recipients = res.txOutputs
48
46
  .filter((o) => o.address && !o.isChange)
49
47
  .map((o) => o.address) as string[];
@@ -140,7 +138,7 @@ const signOperation = ({
140
138
  fee,
141
139
  blockHash: null,
142
140
  blockHeight: null,
143
- senders,
141
+ senders: Array.from(senders),
144
142
  recipients,
145
143
  accountId: account.id,
146
144
  date: new Date(),
@@ -100,7 +100,7 @@ const mapTxToOperations = (
100
100
  const blockHeight = tx.block?.height;
101
101
  const blockHash = tx.block?.hash;
102
102
  const date = new Date(tx.block?.time || tx.received_at);
103
- const senders: string[] = [];
103
+ const senders = new Set<string>();
104
104
  const recipients: string[] = [];
105
105
  let type: OperationType = "OUT";
106
106
  let value = new BigNumber(0);
@@ -111,7 +111,7 @@ const mapTxToOperations = (
111
111
 
112
112
  for (const input of tx.inputs) {
113
113
  if (input.address) {
114
- senders.push(
114
+ senders.add(
115
115
  syncReplaceAddress ? syncReplaceAddress(input.address) : input.address
116
116
  );
117
117
 
@@ -195,7 +195,7 @@ const mapTxToOperations = (
195
195
  type,
196
196
  value,
197
197
  fee,
198
- senders,
198
+ senders: Array.from(senders),
199
199
  recipients,
200
200
  blockHeight,
201
201
  blockHash,
@@ -229,7 +229,7 @@ const mapTxToOperations = (
229
229
  type,
230
230
  value,
231
231
  fee,
232
- senders,
232
+ senders: Array.from(senders),
233
233
  recipients,
234
234
  blockHeight,
235
235
  blockHash,
@@ -84,14 +84,22 @@ class Base implements ICrypto {
84
84
  }
85
85
 
86
86
  // derive legacy address at account and index positions
87
- getLegacyAddress(xpub: string, account: number, index: number): string {
87
+ protected getLegacyAddress(
88
+ xpub: string,
89
+ account: number,
90
+ index: number
91
+ ): string {
88
92
  const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
89
93
  const publicKeyHash160: Buffer = bjs.crypto.hash160(publicKeyBuffer);
90
94
  return bjs.address.toBase58Check(publicKeyHash160, this.network.pubKeyHash);
91
95
  }
92
96
 
93
97
  // derive native SegWit at account and index positions
94
- getNativeSegWitAddress(xpub: string, account: number, index: number): string {
98
+ private getNativeSegWitAddress(
99
+ xpub: string,
100
+ account: number,
101
+ index: number
102
+ ): string {
95
103
  const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
96
104
  const publicKeyHash160: Buffer = bjs.crypto.hash160(publicKeyBuffer);
97
105
  const words: number[] = bech32.toWords(publicKeyHash160);
@@ -100,7 +108,11 @@ class Base implements ICrypto {
100
108
  }
101
109
 
102
110
  // derive SegWit at account and index positions
103
- getSegWitAddress(xpub: string, account: number, index: number): string {
111
+ private getSegWitAddress(
112
+ xpub: string,
113
+ account: number,
114
+ index: number
115
+ ): string {
104
116
  const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
105
117
  const redeemOutput: Buffer = bjs.script.compile([
106
118
  0,
@@ -129,28 +141,29 @@ class Base implements ICrypto {
129
141
  `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
130
142
  ];
131
143
  }
144
+ const res = this.customGetAddress(derivationMode, xpub, account, index);
145
+ Base.addressCache[
146
+ `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
147
+ ] = res;
148
+ return res;
149
+ }
150
+
151
+ customGetAddress(
152
+ derivationMode: string,
153
+ xpub: string,
154
+ account: number,
155
+ index: number
156
+ ): string {
132
157
  switch (derivationMode) {
133
158
  case DerivationModes.LEGACY:
134
- Base.addressCache[
135
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
136
- ] = this.getLegacyAddress(xpub, account, index);
137
- break;
159
+ return this.getLegacyAddress(xpub, account, index);
138
160
  case DerivationModes.SEGWIT:
139
- Base.addressCache[
140
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
141
- ] = this.getSegWitAddress(xpub, account, index);
142
- break;
161
+ return this.getSegWitAddress(xpub, account, index);
143
162
  case DerivationModes.NATIVE_SEGWIT:
144
- Base.addressCache[
145
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
146
- ] = this.getNativeSegWitAddress(xpub, account, index);
147
- break;
163
+ return this.getNativeSegWitAddress(xpub, account, index);
148
164
  default:
149
165
  throw new Error(`Invalid derivation Mode: ${derivationMode}`);
150
166
  }
151
- return Base.addressCache[
152
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
153
- ];
154
167
  }
155
168
 
156
169
  // infer address type from its syntax
@@ -119,31 +119,17 @@ class Bitcoin extends Base {
119
119
  }
120
120
 
121
121
  // get address given an address type
122
- getAddress(
122
+ customGetAddress(
123
123
  derivationMode: string,
124
124
  xpub: string,
125
125
  account: number,
126
126
  index: number
127
127
  ): string {
128
- if (
129
- Base.addressCache[
130
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
131
- ]
132
- ) {
133
- return Base.addressCache[
134
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
135
- ];
136
- }
137
128
  switch (derivationMode) {
138
129
  case DerivationModes.TAPROOT:
139
- Base.addressCache[
140
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
141
- ] = this.getTaprootAddress(xpub, account, index);
142
- return Base.addressCache[
143
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
144
- ];
130
+ return this.getTaprootAddress(xpub, account, index);
145
131
  default:
146
- return super.getAddress(derivationMode, xpub, account, index);
132
+ return super.customGetAddress(derivationMode, xpub, account, index);
147
133
  }
148
134
  }
149
135
 
@@ -1,73 +1,44 @@
1
- // Fix the "More than one instance of bitcore-lib-cash" issue. Refer to https://github.com/bitpay/bitcore/issues/1457
2
- delete global._bitcoreCash;
3
- import * as bch from "bitcore-lib-cash";
4
1
  import bchaddr from "bchaddrjs";
5
2
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
3
  // @ts-ignore
7
4
  import { toOutputScript } from "bitcoinjs-lib/src/address";
8
5
  import { InvalidAddress } from "@ledgerhq/errors";
9
6
  import { DerivationModes } from "../types";
10
- import { ICrypto } from "./types";
11
7
  import Base from "./base";
12
8
 
13
9
  // a mock explorer class that just use js objects
14
- class BitcoinCash implements ICrypto {
10
+ class BitcoinCash extends Base {
15
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
12
  network: any;
17
13
 
18
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
15
  constructor({ network }: { network: any }) {
16
+ super({ network });
20
17
  this.network = network;
21
18
  this.network.dustThreshold = 5430;
22
19
  this.network.dustPolicy = "FIXED";
23
20
  this.network.usesTimestampedTransaction = false;
24
21
  }
25
22
 
26
- // Based on https://github.com/go-faast/bitcoin-cash-payments/blob/54397eb97c7a9bf08b32e10bef23d5f27aa5ab01/index.js#L63-L73
27
23
  // eslint-disable-next-line
28
24
  getLegacyBitcoinCashAddress(
29
25
  xpub: string,
30
26
  account: number,
31
27
  index: number
32
28
  ): string {
33
- const node = new bch.HDPublicKey(xpub);
34
- const child = node.derive(account).derive(index);
35
- const address = new bch.Address(child.publicKey, bch.Networks.livenet);
36
- const addrstr = address.toString().split(":");
37
- if (addrstr.length === 2) {
38
- return bchaddr.toCashAddress(bchaddr.toLegacyAddress(addrstr[1]));
39
- }
40
- throw new Error(`Unable to derive cash address for ${address}`);
29
+ return bchaddr.toCashAddress(super.getLegacyAddress(xpub, account, index));
41
30
  }
42
31
 
43
32
  // get address given an address type
44
- getAddress(
33
+ customGetAddress(
45
34
  derivationMode: string,
46
35
  xpub: string,
47
36
  account: number,
48
37
  index: number
49
38
  ): string {
50
- if (
51
- Base.addressCache[
52
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
53
- ]
54
- ) {
55
- return Base.addressCache[
56
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
57
- ];
58
- }
59
- const address = this.getLegacyBitcoinCashAddress(xpub, account, index);
60
- Base.addressCache[
61
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
62
- ] = address;
63
- return address;
39
+ return this.getLegacyBitcoinCashAddress(xpub, account, index);
64
40
  }
65
41
 
66
- // infer address type from its syntax
67
- //
68
- // TODO: improve the prefix matching: make the expected prefix
69
- // correspond to the actual type (currently, a `ltc1` prefix
70
- // could match a native Bitcoin address type for instance)
71
42
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
72
43
  getDerivationMode(address: string) {
73
44
  return DerivationModes.LEGACY;
@@ -85,11 +56,6 @@ class BitcoinCash implements ICrypto {
85
56
  validateAddress(address: string): boolean {
86
57
  return bchaddr.isValidAddress(address);
87
58
  }
88
-
89
- // eslint-disable-next-line
90
- isTaprootAddress(address: string): boolean {
91
- return false;
92
- }
93
59
  }
94
60
 
95
61
  export default BitcoinCash;
@@ -68,21 +68,12 @@ class Decred extends Base {
68
68
  }
69
69
 
70
70
  // get address given an address type
71
- getAddress(
71
+ customGetAddress(
72
72
  derivationMode: string,
73
73
  xpub: string,
74
74
  account: number,
75
75
  index: number
76
76
  ): string {
77
- if (
78
- Base.addressCache[
79
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
80
- ]
81
- ) {
82
- return Base.addressCache[
83
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
84
- ];
85
- }
86
77
  let buffer: Buffer;
87
78
  try {
88
79
  buffer = Decred.bs58check.decode(xpub);
@@ -95,11 +86,7 @@ class Decred extends Base {
95
86
  const X = buffer.slice(45, 78);
96
87
  const hd = new BIP32(X, chainCode, this.network, depth, i);
97
88
  const publicKey = hd.derive(account).derive(index).publicKey;
98
- const address = Decred.getAddressFromPk(publicKey);
99
- Base.addressCache[
100
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
101
- ] = address;
102
- return address;
89
+ return Decred.getAddressFromPk(publicKey);
103
90
  }
104
91
  }
105
92
 
@@ -1,15 +1,12 @@
1
- import * as bjs from "bitcoinjs-lib";
2
- import * as bip32 from "bip32";
3
1
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
2
  // @ts-ignore
5
3
  import { toOutputScript } from "bitcoinjs-lib/src/address";
6
4
  import * as bech32 from "bech32";
7
5
  import { DerivationModes } from "../types";
8
- import { ICrypto } from "./types";
9
6
  import Base from "./base";
10
7
 
11
8
  // Todo copy paste from bitcoin.ts. we can merge them later
12
- class Litecoin extends Base implements ICrypto {
9
+ class Litecoin extends Base {
13
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
11
  network: any;
15
12
 
@@ -21,73 +18,6 @@ class Litecoin extends Base implements ICrypto {
21
18
  this.network.usesTimestampedTransaction = false;
22
19
  }
23
20
 
24
- getLegacyAddress(xpub: string, account: number, index: number): string {
25
- const { address } = bjs.payments.p2pkh({
26
- pubkey: bip32.fromBase58(xpub, this.network).derive(account).derive(index)
27
- .publicKey,
28
- network: this.network,
29
- });
30
- return String(address);
31
- }
32
-
33
- getSegWitAddress(xpub: string, account: number, index: number): string {
34
- const { address } = bjs.payments.p2sh({
35
- redeem: bjs.payments.p2wpkh({
36
- pubkey: bip32
37
- .fromBase58(xpub, this.network)
38
- .derive(account)
39
- .derive(index).publicKey,
40
- network: this.network,
41
- }),
42
- });
43
- return String(address);
44
- }
45
-
46
- getNativeSegWitAddress(xpub: string, account: number, index: number): string {
47
- const { address } = bjs.payments.p2wpkh({
48
- pubkey: bip32.fromBase58(xpub, this.network).derive(account).derive(index)
49
- .publicKey,
50
- network: this.network,
51
- });
52
-
53
- return String(address);
54
- }
55
-
56
- getAddress(
57
- derivationMode: DerivationModes,
58
- xpub: string,
59
- account: number,
60
- index: number
61
- ): string {
62
- if (
63
- Base.addressCache[
64
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
65
- ]
66
- ) {
67
- return Base.addressCache[
68
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
69
- ];
70
- }
71
- let address: string;
72
- switch (derivationMode) {
73
- case DerivationModes.LEGACY:
74
- address = this.getLegacyAddress(xpub, account, index);
75
- break;
76
- case DerivationModes.SEGWIT:
77
- address = this.getSegWitAddress(xpub, account, index);
78
- break;
79
- case DerivationModes.NATIVE_SEGWIT:
80
- address = this.getNativeSegWitAddress(xpub, account, index);
81
- break;
82
- default:
83
- throw new Error("Should not be reachable");
84
- }
85
- Base.addressCache[
86
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
87
- ] = address;
88
- return address;
89
- }
90
-
91
21
  // infer address type from its syntax
92
22
  getDerivationMode(address: string) {
93
23
  if (address.match("^(ltc1).*")) {
@@ -7,6 +7,13 @@ export interface ICrypto {
7
7
  account: number,
8
8
  index: number
9
9
  ): string;
10
+ customGetAddress(
11
+ derivationMode: string,
12
+ xpub: string,
13
+ account: number,
14
+ index: number
15
+ ): string;
16
+
10
17
  getDerivationMode(address: string): string;
11
18
 
12
19
  toOutputScript(address: string): Buffer;
@@ -8,15 +8,15 @@ import bs58check from "bs58check";
8
8
  import coininfo from "coininfo";
9
9
  import { InvalidAddress } from "@ledgerhq/errors";
10
10
  import { DerivationModes } from "../types";
11
- import { ICrypto } from "./types";
12
11
  import Base from "./base";
13
12
 
14
- class ZCash implements ICrypto {
13
+ class ZCash extends Base {
15
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
15
  network: any;
17
16
 
18
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
18
  constructor({ network }: { network: any }) {
19
+ super({ network });
20
20
  this.network = network;
21
21
  this.network.dustThreshold = 10000;
22
22
  this.network.dustPolicy = "FIXED";
@@ -47,26 +47,13 @@ class ZCash implements ICrypto {
47
47
  return address.toString();
48
48
  }
49
49
 
50
- getAddress(
50
+ customGetAddress(
51
51
  derivationMode: string,
52
52
  xpub: string,
53
53
  account: number,
54
54
  index: number
55
55
  ): string {
56
- if (
57
- Base.addressCache[
58
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
59
- ]
60
- ) {
61
- return Base.addressCache[
62
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
63
- ];
64
- }
65
- const address = this.getLegacyAddress(xpub, account, index);
66
- Base.addressCache[
67
- `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
68
- ] = address;
69
- return address;
56
+ return this.getLegacyAddress(xpub, account, index);
70
57
  }
71
58
 
72
59
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -89,11 +76,6 @@ class ZCash implements ICrypto {
89
76
  validateAddress(address: string): boolean {
90
77
  return zec.Address.isValid(address, "livenet");
91
78
  }
92
-
93
- // eslint-disable-next-line
94
- isTaprootAddress(address: string): boolean {
95
- return false;
96
- }
97
79
  }
98
80
 
99
81
  export default ZCash;