@ledgerhq/live-common 21.35.0 → 21.36.2

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 (236) hide show
  1. package/lib/__tests__/families/bitcoin/wallet-btc/bitcoin.test.js +290 -225
  2. package/lib/__tests__/families/bitcoin/wallet-btc/bitcoin.test.js.map +1 -1
  3. package/lib/__tests__/families/bitcoin/wallet-btc/utils.test.js +2 -0
  4. package/lib/__tests__/families/bitcoin/wallet-btc/utils.test.js.map +1 -1
  5. package/lib/__tests__/families/bitcoin/wallet-btc/wallet.errorpropagation.test.d.ts +2 -0
  6. package/lib/__tests__/families/bitcoin/wallet-btc/wallet.errorpropagation.test.d.ts.map +1 -0
  7. package/lib/__tests__/families/bitcoin/wallet-btc/wallet.errorpropagation.test.js +86 -0
  8. package/lib/__tests__/families/bitcoin/wallet-btc/wallet.errorpropagation.test.js.map +1 -0
  9. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.js +38 -0
  10. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.js.map +1 -1
  11. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.txs.integration.test.js +4 -4
  12. package/lib/__tests__/families/bitcoin/wallet-btc/xpub.txs.integration.test.js.map +1 -1
  13. package/lib/account/support.js +1 -1
  14. package/lib/account/support.js.map +1 -1
  15. package/lib/apps/support.js +1 -1
  16. package/lib/bot/engine.d.ts.map +1 -1
  17. package/lib/bot/engine.js +77 -45
  18. package/lib/bot/engine.js.map +1 -1
  19. package/lib/bot/index.d.ts.map +1 -1
  20. package/lib/bot/index.js +44 -20
  21. package/lib/bot/index.js.map +1 -1
  22. package/lib/bot/types.d.ts +1 -0
  23. package/lib/bot/types.d.ts.map +1 -1
  24. package/lib/bridge/jsHelpers.d.ts +25 -2
  25. package/lib/bridge/jsHelpers.d.ts.map +1 -1
  26. package/lib/bridge/jsHelpers.js +94 -60
  27. package/lib/bridge/jsHelpers.js.map +1 -1
  28. package/lib/cross.d.ts.map +1 -1
  29. package/lib/cross.js +5 -3
  30. package/lib/cross.js.map +1 -1
  31. package/lib/exchange/swap/getCompleteSwapHistory.js +17 -14
  32. package/lib/exchange/swap/getCompleteSwapHistory.js.map +1 -1
  33. package/lib/families/algorand/js-synchronization.d.ts.map +1 -1
  34. package/lib/families/algorand/js-synchronization.js +2 -5
  35. package/lib/families/algorand/js-synchronization.js.map +1 -1
  36. package/lib/families/bitcoin/js-synchronisation.d.ts.map +1 -1
  37. package/lib/families/bitcoin/js-synchronisation.js +5 -2
  38. package/lib/families/bitcoin/js-synchronisation.js.map +1 -1
  39. package/lib/families/bitcoin/specs.d.ts.map +1 -1
  40. package/lib/families/bitcoin/specs.js +4 -2
  41. package/lib/families/bitcoin/specs.js.map +1 -1
  42. package/lib/families/bitcoin/wallet-btc/crypto/base.d.ts +5 -6
  43. package/lib/families/bitcoin/wallet-btc/crypto/base.d.ts.map +1 -1
  44. package/lib/families/bitcoin/wallet-btc/crypto/base.js +149 -59
  45. package/lib/families/bitcoin/wallet-btc/crypto/base.js.map +1 -1
  46. package/lib/families/bitcoin/wallet-btc/crypto/bip32.d.ts +1 -1
  47. package/lib/families/bitcoin/wallet-btc/crypto/bip32.d.ts.map +1 -1
  48. package/lib/families/bitcoin/wallet-btc/crypto/bip32.js +59 -9
  49. package/lib/families/bitcoin/wallet-btc/crypto/bip32.js.map +1 -1
  50. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.d.ts +1 -1
  51. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.d.ts.map +1 -1
  52. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js +76 -24
  53. package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js.map +1 -1
  54. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.d.ts +2 -2
  55. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.d.ts.map +1 -1
  56. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js +55 -2
  57. package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js.map +1 -1
  58. package/lib/families/bitcoin/wallet-btc/crypto/decred.d.ts +1 -1
  59. package/lib/families/bitcoin/wallet-btc/crypto/decred.d.ts.map +1 -1
  60. package/lib/families/bitcoin/wallet-btc/crypto/decred.js +61 -14
  61. package/lib/families/bitcoin/wallet-btc/crypto/decred.js.map +1 -1
  62. package/lib/families/bitcoin/wallet-btc/crypto/index.d.ts +1 -0
  63. package/lib/families/bitcoin/wallet-btc/crypto/index.d.ts.map +1 -1
  64. package/lib/families/bitcoin/wallet-btc/crypto/index.js +4 -1
  65. package/lib/families/bitcoin/wallet-btc/crypto/index.js.map +1 -1
  66. package/lib/families/bitcoin/wallet-btc/crypto/secp256k1.d.ts +16 -0
  67. package/lib/families/bitcoin/wallet-btc/crypto/secp256k1.d.ts.map +1 -0
  68. package/lib/families/bitcoin/wallet-btc/crypto/secp256k1.js +28 -0
  69. package/lib/families/bitcoin/wallet-btc/crypto/secp256k1.js.map +1 -0
  70. package/lib/families/bitcoin/wallet-btc/crypto/types.d.ts +2 -2
  71. package/lib/families/bitcoin/wallet-btc/crypto/types.d.ts.map +1 -1
  72. package/lib/families/bitcoin/wallet-btc/crypto/zec.d.ts +2 -2
  73. package/lib/families/bitcoin/wallet-btc/crypto/zec.d.ts.map +1 -1
  74. package/lib/families/bitcoin/wallet-btc/crypto/zec.js +72 -10
  75. package/lib/families/bitcoin/wallet-btc/crypto/zec.js.map +1 -1
  76. package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts +2 -2
  77. package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts.map +1 -1
  78. package/lib/families/bitcoin/wallet-btc/crypto/zen.js +60 -6
  79. package/lib/families/bitcoin/wallet-btc/crypto/zen.js.map +1 -1
  80. package/lib/families/bitcoin/wallet-btc/storage/index.d.ts +20 -4
  81. package/lib/families/bitcoin/wallet-btc/storage/index.d.ts.map +1 -1
  82. package/lib/families/bitcoin/wallet-btc/storage/index.js +45 -8
  83. package/lib/families/bitcoin/wallet-btc/storage/index.js.map +1 -1
  84. package/lib/families/bitcoin/wallet-btc/storage/types.d.ts +3 -3
  85. package/lib/families/bitcoin/wallet-btc/storage/types.d.ts.map +1 -1
  86. package/lib/families/bitcoin/wallet-btc/xpub.js +38 -31
  87. package/lib/families/bitcoin/wallet-btc/xpub.js.map +1 -1
  88. package/lib/families/celo/js-synchronisation.d.ts +2 -3
  89. package/lib/families/celo/js-synchronisation.d.ts.map +1 -1
  90. package/lib/families/celo/js-synchronisation.js +2 -5
  91. package/lib/families/celo/js-synchronisation.js.map +1 -1
  92. package/lib/families/cosmos/bridge/js.d.ts.map +1 -1
  93. package/lib/families/cosmos/bridge/js.js +1 -1
  94. package/lib/families/cosmos/bridge/js.js.map +1 -1
  95. package/lib/families/cosmos/js-synchronisation.d.ts +8 -2
  96. package/lib/families/cosmos/js-synchronisation.d.ts.map +1 -1
  97. package/lib/families/cosmos/js-synchronisation.js +3 -3
  98. package/lib/families/cosmos/js-synchronisation.js.map +1 -1
  99. package/lib/families/crypto_org/js-synchronisation.d.ts +2 -3
  100. package/lib/families/crypto_org/js-synchronisation.d.ts.map +1 -1
  101. package/lib/families/crypto_org/js-synchronisation.js +2 -3
  102. package/lib/families/crypto_org/js-synchronisation.js.map +1 -1
  103. package/lib/families/elrond/js-synchronisation.d.ts +2 -3
  104. package/lib/families/elrond/js-synchronisation.d.ts.map +1 -1
  105. package/lib/families/elrond/js-synchronisation.js +2 -3
  106. package/lib/families/elrond/js-synchronisation.js.map +1 -1
  107. package/lib/families/ethereum/bridge/js.js +2 -2
  108. package/lib/families/ethereum/bridge/js.js.map +1 -1
  109. package/lib/families/ethereum/synchronisation.d.ts.map +1 -1
  110. package/lib/families/ethereum/synchronisation.js +11 -10
  111. package/lib/families/ethereum/synchronisation.js.map +1 -1
  112. package/lib/families/filecoin/bridge/account.js +1 -1
  113. package/lib/families/filecoin/bridge/account.js.map +1 -1
  114. package/lib/families/filecoin/bridge/currency.js +1 -1
  115. package/lib/families/filecoin/bridge/currency.js.map +1 -1
  116. package/lib/families/filecoin/speculos-deviceActions.d.ts.map +1 -1
  117. package/lib/families/filecoin/speculos-deviceActions.js +14 -6
  118. package/lib/families/filecoin/speculos-deviceActions.js.map +1 -1
  119. package/lib/families/neo/bridge/js.js +2 -2
  120. package/lib/families/neo/bridge/js.js.map +1 -1
  121. package/lib/families/polkadot/js-synchronisation.d.ts.map +1 -1
  122. package/lib/families/polkadot/js-synchronisation.js +2 -5
  123. package/lib/families/polkadot/js-synchronisation.js.map +1 -1
  124. package/lib/families/solana/api/chain/index.d.ts +1 -1
  125. package/lib/families/solana/api/chain/index.d.ts.map +1 -1
  126. package/lib/families/solana/api/chain/index.js +13 -2
  127. package/lib/families/solana/api/chain/index.js.map +1 -1
  128. package/lib/families/solana/api/traced.d.ts +3 -0
  129. package/lib/families/solana/api/traced.d.ts.map +1 -0
  130. package/lib/families/solana/api/traced.js +82 -0
  131. package/lib/families/solana/api/traced.js.map +1 -0
  132. package/lib/families/solana/bridge/bridge.js +2 -2
  133. package/lib/families/solana/bridge/bridge.js.map +1 -1
  134. package/lib/families/solana/bridge/js.d.ts.map +1 -1
  135. package/lib/families/solana/bridge/js.js +12 -1
  136. package/lib/families/solana/bridge/js.js.map +1 -1
  137. package/lib/families/stellar/js-synchronization.d.ts +2 -3
  138. package/lib/families/stellar/js-synchronization.d.ts.map +1 -1
  139. package/lib/families/stellar/js-synchronization.js +2 -5
  140. package/lib/families/stellar/js-synchronization.js.map +1 -1
  141. package/lib/families/tezos/bridge/js.js +13 -12
  142. package/lib/families/tezos/bridge/js.js.map +1 -1
  143. package/lib/families/tezos/specs.d.ts.map +1 -1
  144. package/lib/families/tezos/specs.js +4 -3
  145. package/lib/families/tezos/specs.js.map +1 -1
  146. package/lib/families/tron/bridge/js.js +2 -2
  147. package/lib/families/tron/bridge/js.js.map +1 -1
  148. package/lib/featureFlags/FeatureToggle.d.ts +2 -2
  149. package/lib/featureFlags/FeatureToggle.d.ts.map +1 -1
  150. package/lib/featureFlags/FeatureToggle.js +1 -2
  151. package/lib/featureFlags/FeatureToggle.js.map +1 -1
  152. package/lib/featureFlags/defaultFeatures.d.ts.map +1 -1
  153. package/lib/featureFlags/defaultFeatures.js +3 -0
  154. package/lib/featureFlags/defaultFeatures.js.map +1 -1
  155. package/lib/featureFlags/index.d.ts +3 -2
  156. package/lib/featureFlags/index.d.ts.map +1 -1
  157. package/lib/featureFlags/index.js +8 -2
  158. package/lib/featureFlags/index.js.map +1 -1
  159. package/lib/featureFlags/types.d.ts +1 -1
  160. package/lib/featureFlags/types.d.ts.map +1 -1
  161. package/lib/featureFlags/useFeature.d.ts +2 -1
  162. package/lib/featureFlags/useFeature.d.ts.map +1 -1
  163. package/lib/featureFlags/useFeature.js +2 -2
  164. package/lib/featureFlags/useFeature.js.map +1 -1
  165. package/lib/hw/actions/app.d.ts.map +1 -1
  166. package/lib/hw/actions/app.js +2 -1
  167. package/lib/hw/actions/app.js.map +1 -1
  168. package/lib/market/MarketDataProvider.d.ts +4 -3
  169. package/lib/market/MarketDataProvider.d.ts.map +1 -1
  170. package/lib/market/MarketDataProvider.js +21 -60
  171. package/lib/market/MarketDataProvider.js.map +1 -1
  172. package/lib/notifications/AnnouncementProvider/machine.d.ts +21 -4
  173. package/lib/notifications/AnnouncementProvider/machine.d.ts.map +1 -1
  174. package/lib/notifications/ServiceStatusProvider/machine.d.ts +21 -4
  175. package/lib/notifications/ServiceStatusProvider/machine.d.ts.map +1 -1
  176. package/lib/platform/providers/GlobalCatalogProvider/api/mock.json +13 -0
  177. package/lib/platform/providers/RemoteLiveAppProvider/api/mock.json +256 -65
  178. package/package.json +7 -7
  179. package/src/__tests__/families/bitcoin/wallet-btc/bitcoin.test.ts +17 -16
  180. package/src/__tests__/families/bitcoin/wallet-btc/utils.test.ts +2 -0
  181. package/src/__tests__/families/bitcoin/wallet-btc/wallet.errorpropagation.test.ts +41 -0
  182. package/src/__tests__/families/bitcoin/wallet-btc/xpub.getAddress.ts +46 -0
  183. package/src/__tests__/families/bitcoin/wallet-btc/xpub.txs.integration.test.ts +4 -4
  184. package/src/account/support.ts +1 -1
  185. package/src/apps/support.ts +1 -1
  186. package/src/bot/engine.ts +21 -1
  187. package/src/bot/index.ts +45 -5
  188. package/src/bot/types.ts +2 -0
  189. package/src/bridge/jsHelpers.ts +126 -72
  190. package/src/cross.ts +4 -3
  191. package/src/exchange/swap/getCompleteSwapHistory.ts +1 -1
  192. package/src/families/algorand/js-synchronization.ts +2 -6
  193. package/src/families/bitcoin/js-synchronisation.ts +5 -2
  194. package/src/families/bitcoin/specs.ts +3 -2
  195. package/src/families/bitcoin/wallet-btc/crypto/base.ts +59 -44
  196. package/src/families/bitcoin/wallet-btc/crypto/bip32.ts +8 -3
  197. package/src/families/bitcoin/wallet-btc/crypto/bitcoin.ts +8 -8
  198. package/src/families/bitcoin/wallet-btc/crypto/bitcoincash.ts +8 -6
  199. package/src/families/bitcoin/wallet-btc/crypto/decred.ts +4 -3
  200. package/src/families/bitcoin/wallet-btc/crypto/index.ts +1 -0
  201. package/src/families/bitcoin/wallet-btc/crypto/secp256k1.ts +32 -0
  202. package/src/families/bitcoin/wallet-btc/crypto/types.ts +2 -2
  203. package/src/families/bitcoin/wallet-btc/crypto/zec.ts +20 -9
  204. package/src/families/bitcoin/wallet-btc/crypto/zen.ts +5 -5
  205. package/src/families/bitcoin/wallet-btc/storage/index.ts +66 -22
  206. package/src/families/bitcoin/wallet-btc/storage/types.ts +3 -3
  207. package/src/families/bitcoin/wallet-btc/xpub.ts +3 -3
  208. package/src/families/celo/js-synchronisation.ts +2 -7
  209. package/src/families/cosmos/bridge/js.ts +3 -6
  210. package/src/families/cosmos/js-synchronisation.ts +9 -5
  211. package/src/families/crypto_org/js-synchronisation.ts +2 -5
  212. package/src/families/elrond/js-synchronisation.ts +2 -5
  213. package/src/families/ethereum/bridge/js.ts +2 -2
  214. package/src/families/ethereum/synchronisation.ts +6 -3
  215. package/src/families/filecoin/bridge/account.ts +1 -1
  216. package/src/families/filecoin/bridge/currency.ts +1 -1
  217. package/src/families/filecoin/speculos-deviceActions.ts +11 -3
  218. package/src/families/neo/bridge/js.ts +2 -2
  219. package/src/families/polkadot/js-synchronisation.ts +2 -6
  220. package/src/families/solana/api/chain/index.ts +19 -2
  221. package/src/families/solana/api/traced.ts +99 -0
  222. package/src/families/solana/bridge/bridge.ts +2 -2
  223. package/src/families/solana/bridge/js.ts +12 -1
  224. package/src/families/stellar/js-synchronization.ts +2 -7
  225. package/src/families/tezos/bridge/js.ts +12 -12
  226. package/src/families/tezos/specs.ts +5 -3
  227. package/src/families/tron/bridge/js.ts +2 -2
  228. package/src/featureFlags/FeatureToggle.tsx +3 -1
  229. package/src/featureFlags/defaultFeatures.ts +3 -0
  230. package/src/featureFlags/index.ts +5 -2
  231. package/src/featureFlags/types.ts +2 -1
  232. package/src/featureFlags/useFeature.ts +4 -2
  233. package/src/hw/actions/app.ts +2 -1
  234. package/src/market/MarketDataProvider.tsx +73 -98
  235. package/src/platform/providers/GlobalCatalogProvider/api/mock.json +14 -1
  236. package/src/platform/providers/RemoteLiveAppProvider/api/mock.json +930 -739
@@ -113,7 +113,7 @@ const getCompleteSwapHistory = (
113
113
  day = startOfDay(swap.operation.date);
114
114
  data = [swap];
115
115
  continue;
116
- } else if (!skip) {
116
+ } else if (!skip && !data.find((d) => d.swapId === swap.swapId)) {
117
117
  data.push(swap);
118
118
  }
119
119
 
@@ -426,9 +426,5 @@ async function buildSubAccounts({
426
426
  return tokenAccounts;
427
427
  }
428
428
 
429
- const postSync = (_initial: Account, parent: Account) => {
430
- return parent;
431
- };
432
-
433
- export const scanAccounts = makeScanAccounts(getAccountShape);
434
- export const sync = makeSync(getAccountShape, postSync);
429
+ export const scanAccounts = makeScanAccounts({ getAccountShape });
430
+ export const sync = makeSync({ getAccountShape });
@@ -408,5 +408,8 @@ const getAddressFn = (transport) => {
408
408
  return (opts) => getAddressWithBtcInstance(transport, btc, opts);
409
409
  };
410
410
 
411
- export const scanAccounts = makeScanAccounts(getAccountShape, getAddressFn);
412
- export const sync = makeSync(getAccountShape, postSync);
411
+ export const scanAccounts = makeScanAccounts({
412
+ getAccountShape,
413
+ getAddressFn,
414
+ });
415
+ export const sync = makeSync({ getAccountShape, postSync });
@@ -96,7 +96,8 @@ const genericTest = ({
96
96
  // verify that no utxo that was supposed to be exploded were used
97
97
  expect(
98
98
  utxosPicked.filter(
99
- (u) => getUTXOStatus(u, transaction.utxoStrategy).excluded
99
+ (u: BitcoinOutput) =>
100
+ u.blockHeight && getUTXOStatus(u, transaction.utxoStrategy).excluded
100
101
  )
101
102
  ).toEqual([]);
102
103
  };
@@ -256,7 +257,7 @@ const bitcoinLikeMutations = ({
256
257
  test: ({ account }) => {
257
258
  expect(
258
259
  account.bitcoinResources?.utxos
259
- .filter((u) => u.blockHeight) // Exclude pending UTXOs
260
+ .filter((u) => u.blockHeight && u.blockHeight < account.blockHeight) // Exclude pending UTXOs and the Utxos just written into new block
260
261
  .reduce((p, c) => p.plus(c.value), new BigNumber(0))
261
262
  .toString()
262
263
  ).toBe("0");
@@ -29,9 +29,8 @@ export function fallbackValidateAddress(address: string): boolean {
29
29
  class Base implements ICrypto {
30
30
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
31
  network: any;
32
- protected static publickeyCache = {}; // xpub + account + index to publicKey
32
+ protected static bip32Cache = {}; // xpub + account + index to publicKey
33
33
  public static addressCache = {}; // derivationMode + xpub + account + index to address
34
- protected static bech32Cache = {}; // xpub to bech32 interface
35
34
 
36
35
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
36
  constructor({ network }: { network: any }) {
@@ -41,14 +40,24 @@ class Base implements ICrypto {
41
40
  this.network.usesTimestampedTransaction = false;
42
41
  }
43
42
 
44
- protected getPubkeyAt(xpub: string, account: number, index: number): Buffer {
45
- if (!Base.bech32Cache[`${this.network.name}-${xpub}`]) {
43
+ protected async getPubkeyAt(
44
+ xpub: string,
45
+ account: number,
46
+ index: number
47
+ ): Promise<Buffer> {
48
+ // a cache is stored in Base.bip32Cache to optimize the calculation
49
+ // at each step, we make sure the level has been calculated and calc if necessary
50
+
51
+ // 0: root level
52
+ const keyRoot = `${this.network.name}-${xpub}`;
53
+ let rootLevel = Base.bip32Cache[keyRoot]; // it's stored "in sync"
54
+ if (!rootLevel) {
46
55
  const buffer: Buffer = bs58.decode(xpub);
47
56
  const depth: number = buffer[4];
48
57
  const i: number = buffer.readUInt32BE(9);
49
58
  const chainCode: Buffer = buffer.slice(13, 45);
50
59
  const publicKey: Buffer = buffer.slice(45, 78);
51
- Base.bech32Cache[`${this.network.name}-${xpub}`] = new BIP32(
60
+ Base.bip32Cache[keyRoot] = rootLevel = new BIP32(
52
61
  publicKey,
53
62
  chainCode,
54
63
  this.network,
@@ -56,51 +65,53 @@ class Base implements ICrypto {
56
65
  i
57
66
  );
58
67
  }
59
- if (
60
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}-${index}`]
61
- ) {
62
- return Base.publickeyCache[
63
- `${this.network.name}-${xpub}-${account}-${index}`
64
- ];
68
+
69
+ // 1: account level
70
+ const keyAccount = `${keyRoot}-${account}`;
71
+ let accountLevelP = Base.bip32Cache[keyAccount]; // it's stored as promise
72
+ if (!accountLevelP) {
73
+ Base.bip32Cache[keyAccount] = accountLevelP = rootLevel.derive(account);
65
74
  }
66
- if (Base.publickeyCache[`${this.network.name}-${xpub}-${account}`]) {
67
- const publicKey =
68
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}`].derive(
69
- index
70
- ).publicKey;
71
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}-${index}`] =
72
- publicKey;
73
- return publicKey;
75
+
76
+ // 2: index level
77
+ const keyIndex = `${keyAccount}-${index}`;
78
+ let indexLevelP = Base.bip32Cache[keyIndex]; // it's stored as promise
79
+ if (!indexLevelP) {
80
+ Base.bip32Cache[keyIndex] = indexLevelP = accountLevelP.then((a) =>
81
+ a.derive(index)
82
+ );
74
83
  }
75
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}`] =
76
- Base.bech32Cache[`${this.network.name}-${xpub}`].derive(account);
77
- const publicKey =
78
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}`].derive(
79
- index
80
- ).publicKey;
81
- Base.publickeyCache[`${this.network.name}-${xpub}-${account}-${index}`] =
82
- publicKey;
83
- return publicKey;
84
+
85
+ // We can finally return the publicKey. in most case, indexLevelP will be "resolved"
86
+ return (await indexLevelP).publicKey;
84
87
  }
85
88
 
86
89
  // derive legacy address at account and index positions
87
- protected getLegacyAddress(
90
+ protected async getLegacyAddress(
88
91
  xpub: string,
89
92
  account: number,
90
93
  index: number
91
- ): string {
92
- const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
94
+ ): Promise<string> {
95
+ const publicKeyBuffer: Buffer = await this.getPubkeyAt(
96
+ xpub,
97
+ account,
98
+ index
99
+ );
93
100
  const publicKeyHash160: Buffer = bjs.crypto.hash160(publicKeyBuffer);
94
101
  return bjs.address.toBase58Check(publicKeyHash160, this.network.pubKeyHash);
95
102
  }
96
103
 
97
104
  // derive native SegWit at account and index positions
98
- private getNativeSegWitAddress(
105
+ private async getNativeSegWitAddress(
99
106
  xpub: string,
100
107
  account: number,
101
108
  index: number
102
- ): string {
103
- const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
109
+ ): Promise<string> {
110
+ const publicKeyBuffer: Buffer = await this.getPubkeyAt(
111
+ xpub,
112
+ account,
113
+ index
114
+ );
104
115
  const publicKeyHash160: Buffer = bjs.crypto.hash160(publicKeyBuffer);
105
116
  const words: number[] = bech32.toWords(publicKeyHash160);
106
117
  words.unshift(0x00);
@@ -108,12 +119,16 @@ class Base implements ICrypto {
108
119
  }
109
120
 
110
121
  // derive SegWit at account and index positions
111
- private getSegWitAddress(
122
+ private async getSegWitAddress(
112
123
  xpub: string,
113
124
  account: number,
114
125
  index: number
115
- ): string {
116
- const publicKeyBuffer: Buffer = this.getPubkeyAt(xpub, account, index);
126
+ ): Promise<string> {
127
+ const publicKeyBuffer: Buffer = await this.getPubkeyAt(
128
+ xpub,
129
+ account,
130
+ index
131
+ );
117
132
  const redeemOutput: Buffer = bjs.script.compile([
118
133
  0,
119
134
  bjs.crypto.hash160(publicKeyBuffer),
@@ -126,12 +141,12 @@ class Base implements ICrypto {
126
141
  }
127
142
 
128
143
  // get address given an address type
129
- getAddress(
144
+ async getAddress(
130
145
  derivationMode: string,
131
146
  xpub: string,
132
147
  account: number,
133
148
  index: number
134
- ): string {
149
+ ): Promise<string> {
135
150
  if (
136
151
  Base.addressCache[
137
152
  `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`
@@ -148,19 +163,19 @@ class Base implements ICrypto {
148
163
  return res;
149
164
  }
150
165
 
151
- customGetAddress(
166
+ async customGetAddress(
152
167
  derivationMode: string,
153
168
  xpub: string,
154
169
  account: number,
155
170
  index: number
156
- ): string {
171
+ ): Promise<string> {
157
172
  switch (derivationMode) {
158
173
  case DerivationModes.LEGACY:
159
- return this.getLegacyAddress(xpub, account, index);
174
+ return await this.getLegacyAddress(xpub, account, index);
160
175
  case DerivationModes.SEGWIT:
161
- return this.getSegWitAddress(xpub, account, index);
176
+ return await this.getSegWitAddress(xpub, account, index);
162
177
  case DerivationModes.NATIVE_SEGWIT:
163
- return this.getNativeSegWitAddress(xpub, account, index);
178
+ return await this.getNativeSegWitAddress(xpub, account, index);
164
179
  default:
165
180
  throw new Error(`Invalid derivation Mode: ${derivationMode}`);
166
181
  }
@@ -1,5 +1,6 @@
1
- import { publicKeyTweakAdd } from "secp256k1";
1
+ import invariant from "invariant";
2
2
  import createHmac from "create-hmac";
3
+ import { getSecp256k1Instance } from "./secp256k1";
3
4
 
4
5
  // the BIP32 class is inspired from https://github.com/bitcoinjs/bip32/blob/master/src/bip32.js
5
6
  class BIP32 {
@@ -15,20 +16,24 @@ class BIP32 {
15
16
  depth = 0,
16
17
  index = 0
17
18
  ) {
19
+ invariant(publicKey && publicKey.length > 0, "publicKey must not be empty");
20
+ invariant(chainCode && chainCode.length > 0, "chainCode must not be empty");
18
21
  this.publicKey = publicKey;
19
22
  this.chainCode = chainCode;
20
23
  this.network = network;
21
24
  this.depth = depth;
22
25
  this.index = index;
23
26
  }
24
- derive(index: number) {
27
+ async derive(index: number) {
25
28
  const data = Buffer.allocUnsafe(37);
26
29
  this.publicKey.copy(data, 0);
27
30
  data.writeUInt32BE(index, 33);
28
31
  const I = createHmac("sha512", this.chainCode).update(data).digest();
29
32
  const IL = I.slice(0, 32);
30
33
  const IR = I.slice(32);
31
- const Ki = Buffer.from(publicKeyTweakAdd(this.publicKey, IL));
34
+ const Ki = Buffer.from(
35
+ await getSecp256k1Instance().publicKeyTweakAdd(this.publicKey, IL)
36
+ );
32
37
  return new BIP32(Ki, IR, this.network, this.depth + 1, index);
33
38
  }
34
39
  }
@@ -3,7 +3,7 @@
3
3
  import * as bech32 from "bech32";
4
4
  import { bech32m } from "../../bech32m";
5
5
  import * as bjs from "bitcoinjs-lib";
6
- import { publicKeyTweakAdd } from "secp256k1";
6
+ import { getSecp256k1Instance } from "./secp256k1";
7
7
  import { InvalidAddress } from "@ledgerhq/errors";
8
8
  import { DerivationModes } from "../types";
9
9
  import Base from "./base";
@@ -119,15 +119,15 @@ class Bitcoin extends Base {
119
119
  }
120
120
 
121
121
  // get address given an address type
122
- customGetAddress(
122
+ async customGetAddress(
123
123
  derivationMode: string,
124
124
  xpub: string,
125
125
  account: number,
126
126
  index: number
127
- ): string {
127
+ ): Promise<string> {
128
128
  switch (derivationMode) {
129
129
  case DerivationModes.TAPROOT:
130
- return this.getTaprootAddress(xpub, account, index);
130
+ return await this.getTaprootAddress(xpub, account, index);
131
131
  default:
132
132
  return super.customGetAddress(derivationMode, xpub, account, index);
133
133
  }
@@ -176,12 +176,12 @@ class Bitcoin extends Base {
176
176
  return bjs.crypto.sha256(Buffer.concat([h, h, x]));
177
177
  }
178
178
 
179
- private getTaprootAddress(
179
+ private async getTaprootAddress(
180
180
  xpub: string,
181
181
  account: number,
182
182
  index: number
183
- ): string {
184
- const ecdsaPubkey = this.getPubkeyAt(xpub, account, index);
183
+ ): Promise<string> {
184
+ const ecdsaPubkey = await this.getPubkeyAt(xpub, account, index);
185
185
  // A BIP32 derived key can be converted to a schnorr pubkey by dropping
186
186
  // the first byte, which represent the oddness/evenness. In schnorr all
187
187
  // pubkeys are even.
@@ -196,7 +196,7 @@ class Bitcoin extends Base {
196
196
 
197
197
  // Q = P + int(hash_TapTweak(bytes(P)))G
198
198
  const outputEcdsaKey = Buffer.from(
199
- publicKeyTweakAdd(evenEcdsaPubkey, tweak)
199
+ await getSecp256k1Instance().publicKeyTweakAdd(evenEcdsaPubkey, tweak)
200
200
  );
201
201
  // Convert to schnorr.
202
202
  const outputSchnorrKey = outputEcdsaKey.slice(1);
@@ -21,22 +21,24 @@ class BitcoinCash extends Base {
21
21
  }
22
22
 
23
23
  // eslint-disable-next-line
24
- getLegacyBitcoinCashAddress(
24
+ async getLegacyBitcoinCashAddress(
25
25
  xpub: string,
26
26
  account: number,
27
27
  index: number
28
- ): string {
29
- return bchaddr.toCashAddress(super.getLegacyAddress(xpub, account, index));
28
+ ): Promise<string> {
29
+ return bchaddr.toCashAddress(
30
+ await super.getLegacyAddress(xpub, account, index)
31
+ );
30
32
  }
31
33
 
32
34
  // get address given an address type
33
- customGetAddress(
35
+ async customGetAddress(
34
36
  derivationMode: string,
35
37
  xpub: string,
36
38
  account: number,
37
39
  index: number
38
- ): string {
39
- return this.getLegacyBitcoinCashAddress(xpub, account, index);
40
+ ): Promise<string> {
41
+ return await this.getLegacyBitcoinCashAddress(xpub, account, index);
40
42
  }
41
43
 
42
44
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -68,12 +68,12 @@ class Decred extends Base {
68
68
  }
69
69
 
70
70
  // get address given an address type
71
- customGetAddress(
71
+ async customGetAddress(
72
72
  derivationMode: string,
73
73
  xpub: string,
74
74
  account: number,
75
75
  index: number
76
- ): string {
76
+ ): Promise<string> {
77
77
  let buffer: Buffer;
78
78
  try {
79
79
  buffer = Decred.bs58check.decode(xpub);
@@ -85,7 +85,8 @@ class Decred extends Base {
85
85
  const chainCode = buffer.slice(13, 45);
86
86
  const X = buffer.slice(45, 78);
87
87
  const hd = new BIP32(X, chainCode, this.network, depth, i);
88
- const publicKey = hd.derive(account).derive(index).publicKey;
88
+ const publicKey = (await (await hd.derive(account)).derive(index))
89
+ .publicKey;
89
90
  return Decred.getAddressFromPk(publicKey);
90
91
  }
91
92
  }
@@ -16,3 +16,4 @@ export { default as Qtum } from "./qtum";
16
16
  export { default as Vertcoin } from "./vtc";
17
17
  export { default as ViaCoin } from "./via";
18
18
  export { default as Decred } from "./decred";
19
+ export { setSecp256k1Instance, getSecp256k1Instance } from "./secp256k1";
@@ -0,0 +1,32 @@
1
+ import secp256k1 from "secp256k1";
2
+
3
+ /**
4
+ * Implement the subset of secp256k1 that wallet-btc needs to work.
5
+ * The interface is made asynchronous to be more efficient to run in a renderer thread and defer the cryptography
6
+ */
7
+ export type Secp256k1Instance = {
8
+ publicKeyTweakAdd(
9
+ publicKey: Uint8Array,
10
+ tweak: Uint8Array
11
+ ): Promise<Uint8Array>;
12
+ };
13
+
14
+ // default uses node.js's secp256k1
15
+ let impl: Secp256k1Instance = {
16
+ publicKeyTweakAdd: (publicKey, tweak) =>
17
+ Promise.resolve(secp256k1.publicKeyTweakAdd(publicKey, tweak)),
18
+ };
19
+
20
+ /**
21
+ * allows to override the default Secp256k1Instance
22
+ */
23
+ export function setSecp256k1Instance(override: Secp256k1Instance): void {
24
+ impl = override;
25
+ }
26
+
27
+ /**
28
+ * get the current Secp256k1Instance
29
+ */
30
+ export function getSecp256k1Instance(): Secp256k1Instance {
31
+ return impl;
32
+ }
@@ -6,13 +6,13 @@ export interface ICrypto {
6
6
  xpub: string,
7
7
  account: number,
8
8
  index: number
9
- ): string;
9
+ ): Promise<string>;
10
10
  customGetAddress(
11
11
  derivationMode: string,
12
12
  xpub: string,
13
13
  account: number,
14
14
  index: number
15
- ): string;
15
+ ): Promise<string>;
16
16
 
17
17
  getDerivationMode(address: string): string;
18
18
 
@@ -1,6 +1,3 @@
1
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
- // @ts-ignore
3
- import zec from "zcash-bitcore-lib";
4
1
  import bs58check from "bs58check";
5
2
  import * as bjs from "bitcoinjs-lib";
6
3
  import { InvalidAddress } from "@ledgerhq/errors";
@@ -30,21 +27,21 @@ class ZCash extends Base {
30
27
  }
31
28
 
32
29
  // eslint-disable-next-line
33
- getLegacyAddress(xpub: string, account: number, index: number): string {
34
- const pk = bjs.crypto.hash160(this.getPubkeyAt(xpub, account, index));
30
+ async getLegacyAddress(xpub: string, account: number, index: number): Promise<string> {
31
+ const pk = bjs.crypto.hash160(await this.getPubkeyAt(xpub, account, index));
35
32
  const payload = Buffer.allocUnsafe(22);
36
33
  payload.writeUInt16BE(this.network.pubKeyHash, 0);
37
34
  pk.copy(payload, 2);
38
35
  return bs58check.encode(payload);
39
36
  }
40
37
 
41
- customGetAddress(
38
+ async customGetAddress(
42
39
  derivationMode: string,
43
40
  xpub: string,
44
41
  account: number,
45
42
  index: number
46
- ): string {
47
- return this.getLegacyAddress(xpub, account, index);
43
+ ): Promise<string> {
44
+ return await this.getLegacyAddress(xpub, account, index);
48
45
  }
49
46
 
50
47
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -74,7 +71,21 @@ class ZCash extends Base {
74
71
 
75
72
  // eslint-disable-next-line class-methods-use-this
76
73
  validateAddress(address: string): boolean {
77
- return zec.Address.isValid(address, "livenet");
74
+ try {
75
+ const version = Number(
76
+ "0x" + bs58check.decode(address).slice(0, 2).toString("hex")
77
+ );
78
+ // refer to https://github.com/zcash-hackworks/bitcore-lib-zcash/blob/master/lib%2Faddress.js for the address validation
79
+ if (
80
+ version === this.network.pubKeyHash ||
81
+ version === this.network.scriptHash
82
+ ) {
83
+ return true;
84
+ }
85
+ } catch {
86
+ return false;
87
+ }
88
+ return false;
78
89
  }
79
90
  }
80
91
 
@@ -45,21 +45,21 @@ class Zen extends Base {
45
45
  }
46
46
 
47
47
  // eslint-disable-next-line
48
- getLegacyAddress(xpub: string, account: number, index: number): string {
49
- const pk = bjs.crypto.hash160(this.getPubkeyAt(xpub, account, index));
48
+ async getLegacyAddress(xpub: string, account: number, index: number): Promise<string> {
49
+ const pk = bjs.crypto.hash160(await this.getPubkeyAt(xpub, account, index));
50
50
  const payload = Buffer.allocUnsafe(22);
51
51
  payload.writeUInt16BE(this.network.pubKeyHash, 0);
52
52
  pk.copy(payload, 2);
53
53
  return bs58check.encode(payload);
54
54
  }
55
55
 
56
- customGetAddress(
56
+ async customGetAddress(
57
57
  derivationMode: string,
58
58
  xpub: string,
59
59
  account: number,
60
60
  index: number
61
- ): string {
62
- return this.getLegacyAddress(xpub, account, index);
61
+ ): Promise<string> {
62
+ return await this.getLegacyAddress(xpub, account, index);
63
63
  }
64
64
 
65
65
  // eslint-disable-next-line @typescript-eslint/no-unused-vars