@txnlab/use-wallet 1.3.1 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +490 -188
  2. package/dist/cjs/index.js +231 -129
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/jest.config.d.ts +7 -0
  5. package/dist/cjs/src/clients/algosigner/client.d.ts +3 -3
  6. package/dist/cjs/src/clients/algosigner/types.d.ts +2 -8
  7. package/dist/cjs/src/clients/base/base.d.ts +3 -2
  8. package/dist/cjs/src/clients/base/index.d.ts +2 -2
  9. package/dist/cjs/src/clients/daffi/client.d.ts +5 -4
  10. package/dist/cjs/src/clients/daffi/types.d.ts +5 -7
  11. package/dist/cjs/src/clients/defly/client.d.ts +6 -5
  12. package/dist/cjs/src/clients/defly/types.d.ts +5 -7
  13. package/dist/cjs/src/clients/exodus/client.d.ts +6 -5
  14. package/dist/cjs/src/clients/exodus/types.d.ts +5 -10
  15. package/dist/cjs/src/clients/index.d.ts +1 -1
  16. package/dist/cjs/src/clients/kmd/client.d.ts +4 -5
  17. package/dist/cjs/src/clients/kmd/types.d.ts +4 -9
  18. package/dist/cjs/src/clients/mnemonic/client.d.ts +4 -5
  19. package/dist/cjs/src/clients/mnemonic/types.d.ts +2 -9
  20. package/dist/cjs/src/clients/myalgo/client.d.ts +6 -5
  21. package/dist/cjs/src/clients/myalgo/types.d.ts +10 -12
  22. package/dist/cjs/src/clients/pera/client.d.ts +6 -5
  23. package/dist/cjs/src/clients/pera/types.d.ts +5 -7
  24. package/dist/cjs/src/clients/{walletconnect → walletconnect2}/client.d.ts +8 -7
  25. package/dist/cjs/src/clients/walletconnect2/constants.d.ts +6 -0
  26. package/dist/cjs/src/clients/walletconnect2/index.d.ts +2 -0
  27. package/dist/cjs/src/clients/walletconnect2/types.d.ts +38 -0
  28. package/dist/cjs/src/clients/walletconnect2/utils.d.ts +2 -0
  29. package/dist/cjs/src/hooks/index.d.ts +1 -0
  30. package/dist/cjs/src/hooks/useInitializeProviders.d.ts +9 -0
  31. package/dist/cjs/src/hooks/useInitializeProviders.test.d.ts +4 -0
  32. package/dist/cjs/src/hooks/useWallet.d.ts +4 -13
  33. package/dist/cjs/src/hooks/useWallet.test.d.ts +4 -0
  34. package/dist/cjs/src/index.d.ts +1 -1
  35. package/dist/cjs/src/store/state/clientStore.d.ts +2 -23
  36. package/dist/cjs/src/testUtils/createWrapper.d.ts +6 -0
  37. package/dist/cjs/src/testUtils/mockAccounts.d.ts +3 -0
  38. package/dist/cjs/src/testUtils/mockClients.d.ts +33 -0
  39. package/dist/cjs/src/types/index.d.ts +1 -0
  40. package/dist/cjs/src/types/node.d.ts +2 -1
  41. package/dist/cjs/src/types/providers.d.ts +71 -0
  42. package/dist/cjs/src/types/wallet.d.ts +5 -3
  43. package/dist/cjs/src/utils/clearAccounts.d.ts +1 -1
  44. package/dist/cjs/src/utils/encodeNFDTransactionArray.spec.d.ts +1 -0
  45. package/dist/cjs/src/utils/initializeProviders.d.ts +2 -12
  46. package/dist/cjs/src/utils/initializeProviders.test.d.ts +4 -0
  47. package/dist/cjs/src/utils/providers.d.ts +1 -1
  48. package/dist/cjs/src/utils/providers.spec.d.ts +1 -0
  49. package/dist/cjs/src/utils/reconnectProviders.d.ts +1 -5
  50. package/dist/cjs/src/utils/reconnectProviders.test.d.ts +1 -0
  51. package/dist/cjs/src/utils/types.d.ts +2 -0
  52. package/dist/esm/index.js +231 -130
  53. package/dist/esm/jest.config.d.ts +7 -0
  54. package/dist/esm/src/clients/algosigner/client.d.ts +3 -3
  55. package/dist/esm/src/clients/algosigner/types.d.ts +2 -8
  56. package/dist/esm/src/clients/base/base.d.ts +3 -2
  57. package/dist/esm/src/clients/base/index.d.ts +2 -2
  58. package/dist/esm/src/clients/daffi/client.d.ts +5 -4
  59. package/dist/esm/src/clients/daffi/types.d.ts +5 -7
  60. package/dist/esm/src/clients/defly/client.d.ts +6 -5
  61. package/dist/esm/src/clients/defly/types.d.ts +5 -7
  62. package/dist/esm/src/clients/exodus/client.d.ts +6 -5
  63. package/dist/esm/src/clients/exodus/types.d.ts +5 -10
  64. package/dist/esm/src/clients/index.d.ts +1 -1
  65. package/dist/esm/src/clients/kmd/client.d.ts +4 -5
  66. package/dist/esm/src/clients/kmd/types.d.ts +4 -9
  67. package/dist/esm/src/clients/mnemonic/client.d.ts +4 -5
  68. package/dist/esm/src/clients/mnemonic/types.d.ts +2 -9
  69. package/dist/esm/src/clients/myalgo/client.d.ts +6 -5
  70. package/dist/esm/src/clients/myalgo/types.d.ts +10 -12
  71. package/dist/esm/src/clients/pera/client.d.ts +6 -5
  72. package/dist/esm/src/clients/pera/types.d.ts +5 -7
  73. package/dist/esm/src/clients/{walletconnect → walletconnect2}/client.d.ts +8 -7
  74. package/dist/esm/src/clients/walletconnect2/constants.d.ts +6 -0
  75. package/dist/esm/src/clients/walletconnect2/index.d.ts +2 -0
  76. package/dist/esm/src/clients/walletconnect2/types.d.ts +38 -0
  77. package/dist/esm/src/clients/walletconnect2/utils.d.ts +2 -0
  78. package/dist/esm/src/hooks/index.d.ts +1 -0
  79. package/dist/esm/src/hooks/useInitializeProviders.d.ts +9 -0
  80. package/dist/esm/src/hooks/useInitializeProviders.test.d.ts +4 -0
  81. package/dist/esm/src/hooks/useWallet.d.ts +4 -13
  82. package/dist/esm/src/hooks/useWallet.test.d.ts +4 -0
  83. package/dist/esm/src/index.d.ts +1 -1
  84. package/dist/esm/src/store/state/clientStore.d.ts +2 -23
  85. package/dist/esm/src/testUtils/createWrapper.d.ts +6 -0
  86. package/dist/esm/src/testUtils/mockAccounts.d.ts +3 -0
  87. package/dist/esm/src/testUtils/mockClients.d.ts +33 -0
  88. package/dist/esm/src/types/index.d.ts +1 -0
  89. package/dist/esm/src/types/node.d.ts +2 -1
  90. package/dist/esm/src/types/providers.d.ts +71 -0
  91. package/dist/esm/src/types/wallet.d.ts +5 -3
  92. package/dist/esm/src/utils/clearAccounts.d.ts +1 -1
  93. package/dist/esm/src/utils/encodeNFDTransactionArray.spec.d.ts +1 -0
  94. package/dist/esm/src/utils/initializeProviders.d.ts +2 -12
  95. package/dist/esm/src/utils/initializeProviders.test.d.ts +4 -0
  96. package/dist/esm/src/utils/providers.d.ts +1 -1
  97. package/dist/esm/src/utils/providers.spec.d.ts +1 -0
  98. package/dist/esm/src/utils/reconnectProviders.d.ts +1 -5
  99. package/dist/esm/src/utils/reconnectProviders.test.d.ts +1 -0
  100. package/dist/esm/src/utils/types.d.ts +2 -0
  101. package/dist/index.d.ts +240 -254
  102. package/package.json +17 -18
  103. package/dist/cjs/src/clients/walletconnect/constants.d.ts +0 -2
  104. package/dist/cjs/src/clients/walletconnect/index.d.ts +0 -2
  105. package/dist/cjs/src/clients/walletconnect/types.d.ts +0 -55
  106. package/dist/esm/src/clients/walletconnect/constants.d.ts +0 -2
  107. package/dist/esm/src/clients/walletconnect/index.d.ts +0 -2
  108. package/dist/esm/src/clients/walletconnect/types.d.ts +0 -55
package/dist/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import require$$0, { useDebugValue, useState, useEffect, createContext, useContext, useMemo } from 'react';
2
+ import { getSdkError } from '@walletconnect/utils';
2
3
  import algosdk from 'algosdk';
3
4
 
4
5
  var PROVIDER_ID;
@@ -965,12 +966,14 @@ const isIOS = getIsIOS();
965
966
  class BaseClient {
966
967
  algosdk;
967
968
  algodClient;
969
+ clientOptions;
968
970
  keepWCAlive;
969
971
  metadata;
970
972
  static metadata;
971
- constructor(metadata, algosdk, algodClient) {
973
+ constructor(metadata, algosdk, algodClient, clientOptions) {
972
974
  this.algosdk = algosdk;
973
975
  this.algodClient = algodClient;
976
+ this.clientOptions = clientOptions;
974
977
  this.keepWCAlive = new Audio();
975
978
  this.metadata = metadata;
976
979
  }
@@ -1083,10 +1086,12 @@ const ICON$8 = 'data:image/svg+xml;base64,' +
1083
1086
 
1084
1087
  class PeraWalletClient extends BaseClient {
1085
1088
  #client;
1089
+ clientOptions;
1086
1090
  network;
1087
- constructor({ metadata, client, algosdk, algodClient, network }) {
1091
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1088
1092
  super(metadata, algosdk, algodClient);
1089
1093
  this.#client = client;
1094
+ this.clientOptions = clientOptions;
1090
1095
  this.network = network;
1091
1096
  this.metadata = PeraWalletClient.metadata;
1092
1097
  }
@@ -1102,11 +1107,12 @@ class PeraWalletClient extends BaseClient {
1102
1107
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1103
1108
  const algodClient = getAlgodClient(algosdk, algodOptions);
1104
1109
  const peraWallet = new PeraWalletConnect({
1105
- ...(clientOptions ? clientOptions : { shouldShowSignTxnToast: false })
1110
+ ...(clientOptions && clientOptions)
1106
1111
  });
1107
1112
  return new PeraWalletClient({
1108
1113
  metadata: PeraWalletClient.metadata,
1109
1114
  client: peraWallet,
1115
+ clientOptions,
1110
1116
  algosdk,
1111
1117
  algodClient,
1112
1118
  network
@@ -1227,10 +1233,12 @@ const ICON$7 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eN
1227
1233
 
1228
1234
  class DaffiWalletClient extends BaseClient {
1229
1235
  #client;
1236
+ clientOptions;
1230
1237
  network;
1231
- constructor({ metadata, client, algosdk, algodClient, network }) {
1238
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1232
1239
  super(metadata, algosdk, algodClient);
1233
1240
  this.#client = client;
1241
+ this.clientOptions = clientOptions;
1234
1242
  this.network = network;
1235
1243
  this.metadata = DaffiWalletClient.metadata;
1236
1244
  }
@@ -1251,6 +1259,7 @@ class DaffiWalletClient extends BaseClient {
1251
1259
  return new DaffiWalletClient({
1252
1260
  metadata: DaffiWalletClient.metadata,
1253
1261
  client: daffiWallet,
1262
+ clientOptions,
1254
1263
  algosdk,
1255
1264
  algodClient,
1256
1265
  network
@@ -1376,10 +1385,12 @@ const ICON$6 = 'data:image/svg+xml;base64,' +
1376
1385
  */
1377
1386
  class MyAlgoWalletClient extends BaseClient {
1378
1387
  #client;
1388
+ clientOptions;
1379
1389
  network;
1380
- constructor({ metadata, client, algosdk, algodClient, network }) {
1390
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1381
1391
  super(metadata, algosdk, algodClient);
1382
1392
  this.#client = client;
1393
+ this.clientOptions = clientOptions;
1383
1394
  this.network = network;
1384
1395
  this.metadata = MyAlgoWalletClient.metadata;
1385
1396
  }
@@ -1400,6 +1411,7 @@ class MyAlgoWalletClient extends BaseClient {
1400
1411
  return new MyAlgoWalletClient({
1401
1412
  metadata: MyAlgoWalletClient.metadata,
1402
1413
  client: myAlgo,
1414
+ clientOptions,
1403
1415
  algosdk: algosdk,
1404
1416
  algodClient: algodClient,
1405
1417
  network
@@ -1475,10 +1487,12 @@ const ICON$5 = 'data:image/svg+xml;base64,' +
1475
1487
 
1476
1488
  class DeflyWalletClient extends BaseClient {
1477
1489
  #client;
1490
+ clientOptions;
1478
1491
  network;
1479
- constructor({ metadata, client, algosdk, algodClient, network }) {
1492
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1480
1493
  super(metadata, algosdk, algodClient);
1481
1494
  this.#client = client;
1495
+ this.clientOptions = clientOptions;
1482
1496
  this.network = network;
1483
1497
  this.metadata = DeflyWalletClient.metadata;
1484
1498
  }
@@ -1494,7 +1508,7 @@ class DeflyWalletClient extends BaseClient {
1494
1508
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1495
1509
  const algodClient = getAlgodClient(algosdk, algodOptions);
1496
1510
  const deflyWallet = new DeflyWalletConnect({
1497
- ...(clientOptions ? clientOptions : { shouldShowSignTxnToast: false })
1511
+ ...(clientOptions && clientOptions)
1498
1512
  });
1499
1513
  return new DeflyWalletClient({
1500
1514
  metadata: DeflyWalletClient.metadata,
@@ -1619,12 +1633,12 @@ const ICON$4 = 'data:image/svg+xml;base64,' +
1619
1633
 
1620
1634
  class ExodusClient extends BaseClient {
1621
1635
  #client;
1622
- #onlyIfTrusted;
1636
+ clientOptions;
1623
1637
  network;
1624
- constructor({ metadata, client, algosdk, algodClient, onlyIfTrusted, network }) {
1638
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1625
1639
  super(metadata, algosdk, algodClient);
1626
1640
  this.#client = client;
1627
- this.#onlyIfTrusted = onlyIfTrusted;
1641
+ this.clientOptions = clientOptions;
1628
1642
  this.network = network;
1629
1643
  this.metadata = ExodusClient.metadata;
1630
1644
  }
@@ -1644,11 +1658,10 @@ class ExodusClient extends BaseClient {
1644
1658
  const exodus = window.exodus.algorand;
1645
1659
  return new ExodusClient({
1646
1660
  metadata: ExodusClient.metadata,
1647
- id: PROVIDER_ID.EXODUS,
1648
1661
  client: exodus,
1649
1662
  algosdk: algosdk,
1650
1663
  algodClient: algodClient,
1651
- onlyIfTrusted: clientOptions?.onlyIfTrusted || false,
1664
+ clientOptions: clientOptions || { onlyIfTrusted: false },
1652
1665
  network
1653
1666
  });
1654
1667
  }
@@ -1660,7 +1673,7 @@ class ExodusClient extends BaseClient {
1660
1673
  }
1661
1674
  async connect() {
1662
1675
  const { address } = await this.#client.connect({
1663
- onlyIfTrusted: this.#onlyIfTrusted
1676
+ onlyIfTrusted: this.clientOptions.onlyIfTrusted
1664
1677
  });
1665
1678
  if (!address) {
1666
1679
  throw new Error(`No accounts found for ${ExodusClient.metadata.id}`);
@@ -1763,7 +1776,6 @@ class AlgoSignerClient extends BaseClient {
1763
1776
  const algosigner = window.algorand;
1764
1777
  return new AlgoSignerClient({
1765
1778
  metadata: AlgoSignerClient.metadata,
1766
- id: PROVIDER_ID.ALGOSIGNER,
1767
1779
  client: algosigner,
1768
1780
  algosdk: algosdk,
1769
1781
  algodClient: algodClient,
@@ -1880,17 +1892,47 @@ class AlgoSignerClient extends BaseClient {
1880
1892
  }
1881
1893
  }
1882
1894
 
1895
+ const getPayloadId = () => {
1896
+ const date = Date.now() * Math.pow(10, 3);
1897
+ const extra = Math.floor(Math.random() * Math.pow(10, 3));
1898
+ return date + extra;
1899
+ };
1900
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1901
+ const formatJsonRpcRequest = (method, params) => {
1902
+ return {
1903
+ id: getPayloadId(),
1904
+ jsonrpc: '2.0',
1905
+ method,
1906
+ params
1907
+ };
1908
+ };
1909
+
1910
+ const isPublicNetwork = (network) => {
1911
+ return network === 'betanet' || network === 'testnet' || network === 'mainnet';
1912
+ };
1913
+
1883
1914
  const ICON$2 = 'data:image/svg+xml;base64,' +
1884
1915
  'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgNDgwIDQ4MCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDgwIDQ4MDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsOiMzMzk2RkY7fQo8L3N0eWxlPgo8cGF0aCBjbGFzcz0ic3QwIiBkPSJNMTI2LjYsMTY4YzYyLjYtNjEuMywxNjQuMi02MS4zLDIyNi44LDBsNy41LDcuNGMzLjEsMy4xLDMuMSw4LDAsMTEuMWwtMjUuOCwyNS4yYy0xLjYsMS41LTQuMSwxLjUtNS43LDAKCWwtMTAuNC0xMC4yYy00My43LTQyLjgtMTE0LjUtNDIuOC0xNTguMiwwbC0xMS4xLDEwLjljLTEuNiwxLjUtNC4xLDEuNS01LjcsMGwtMjUuOC0yNS4yYy0zLjEtMy4xLTMuMS04LDAtMTEuMUwxMjYuNiwxNjh6CgkgTTQwNi43LDIyMC4ybDIyLjksMjIuNWMzLjEsMy4xLDMuMSw4LDAsMTEuMUwzMjYuMiwzNTUuMWMtMy4xLDMuMS04LjIsMy4xLTExLjMsMGwtNzMuNC03MS45Yy0wLjgtMC44LTIuMS0wLjgtMi44LDBsLTczLjQsNzEuOQoJYy0zLjEsMy4xLTguMiwzLjEtMTEuMywwTDUwLjMsMjUzLjhjLTMuMS0zLjEtMy4xLTgsMC0xMS4xbDIyLjktMjIuNWMzLjEtMy4xLDguMi0zLjEsMTEuMywwbDczLjQsNzEuOWMwLjgsMC44LDIuMSwwLjgsMi44LDAKCWw3My40LTcxLjljMy4xLTMuMSw4LjItMy4xLDExLjMsMGw3My40LDcxLjljMC44LDAuOCwyLjEsMC44LDIuOCwwbDczLjQtNzEuOUMzOTguNSwyMTcuMSw0MDMuNiwyMTcuMSw0MDYuNywyMjAuMkw0MDYuNywyMjAuMnoiLz4KPC9zdmc+Cg==';
1885
- const DEFAULT_NETWORK = 'mainnet';
1916
+ const DEFAULT_NETWORK = 'mainnet';
1917
+ const ALGORAND_CHAINS = {
1918
+ mainnet: 'algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k',
1919
+ testnet: 'algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDe',
1920
+ betanet: 'algorand:mFgazF-2uRS1tMiL9dsj01hJGySEmPN2'
1921
+ };
1886
1922
 
1887
1923
  class WalletConnectClient extends BaseClient {
1888
1924
  #client;
1925
+ clientOptions;
1926
+ #modal;
1889
1927
  network;
1890
- constructor({ metadata, client, algosdk, algodClient, network }) {
1928
+ chain;
1929
+ constructor({ metadata, client, clientOptions, modal, algosdk, algodClient, network, chain }) {
1891
1930
  super(metadata, algosdk, algodClient);
1892
1931
  this.#client = client;
1932
+ this.clientOptions = clientOptions;
1933
+ this.#modal = modal;
1893
1934
  this.network = network;
1935
+ this.chain = chain;
1894
1936
  this.metadata = WalletConnectClient.metadata;
1895
1937
  }
1896
1938
  static metadata = {
@@ -1899,98 +1941,120 @@ class WalletConnectClient extends BaseClient {
1899
1941
  icon: ICON$2,
1900
1942
  isWalletConnect: true
1901
1943
  };
1902
- static async init({ clientOptions, algodOptions, clientStatic, modalStatic, algosdkStatic, network = DEFAULT_NETWORK }) {
1944
+ static async init({ clientOptions, algodOptions, clientStatic, modalStatic, modalOptions, algosdkStatic, network = DEFAULT_NETWORK }) {
1903
1945
  try {
1904
- const WalletConnect = clientStatic || (await import('@walletconnect/client')).default;
1905
- const QRCodeModal = modalStatic || (await import('algorand-walletconnect-qrcode-modal')).default;
1906
- const walletConnect = new WalletConnect({
1907
- bridge: 'https://bridge.walletconnect.org',
1908
- qrcodeModal: QRCodeModal,
1909
- storageId: 'walletconnect-generic',
1910
- ...(clientOptions || {})
1946
+ if (!isPublicNetwork(network)) {
1947
+ throw new Error(`WalletConnect only supports Algorand mainnet, testnet, and betanet. "${network}" is not supported.`);
1948
+ }
1949
+ const chain = ALGORAND_CHAINS[network];
1950
+ // Initialize sign client
1951
+ const Client = clientStatic || (await import('@walletconnect/sign-client')).default;
1952
+ const client = await Client.init(clientOptions);
1953
+ // Initialize web3modal
1954
+ const modalModule = modalStatic
1955
+ ? { WalletConnectModal: modalStatic }
1956
+ : await import('@walletconnect/modal');
1957
+ const Web3Modal = modalModule.WalletConnectModal;
1958
+ const modal = new Web3Modal({
1959
+ explorerExcludedWalletIds: 'ALL',
1960
+ ...modalOptions,
1961
+ projectId: clientOptions?.projectId || '',
1962
+ walletConnectVersion: 2
1911
1963
  });
1964
+ // Initialize algod client
1912
1965
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1913
1966
  const algodClient = getAlgodClient(algosdk, algodOptions);
1914
- const initWallet = {
1967
+ // Initialize wallet client
1968
+ const walletClient = new WalletConnectClient({
1915
1969
  metadata: WalletConnectClient.metadata,
1916
- client: walletConnect,
1917
- algosdk: algosdk,
1918
- algodClient: algodClient,
1919
- network
1920
- };
1921
- return new WalletConnectClient(initWallet);
1970
+ client,
1971
+ clientOptions,
1972
+ modal,
1973
+ algosdk,
1974
+ algodClient,
1975
+ network,
1976
+ chain
1977
+ });
1978
+ walletClient.#subscribeToEvents();
1979
+ return walletClient;
1922
1980
  }
1923
- catch (e) {
1924
- console.error('Error initializing...', e);
1981
+ catch (error) {
1982
+ console.error('Error initializing', error);
1925
1983
  return null;
1926
1984
  }
1927
1985
  }
1928
1986
  async connect() {
1929
- let chainId = 416001;
1930
- if (this.network === 'betanet') {
1931
- chainId = 416003;
1932
- }
1933
- else if (this.network === 'testnet') {
1934
- chainId = 416002;
1935
- }
1936
- if (!this.#client.connected) {
1937
- await this.#client.createSession({ chainId });
1987
+ const requiredNamespaces = {
1988
+ algorand: {
1989
+ chains: [this.chain],
1990
+ methods: ['algo_signTxn'],
1991
+ events: []
1992
+ }
1993
+ };
1994
+ const { uri, approval } = await this.#client.connect({ requiredNamespaces });
1995
+ if (uri) {
1996
+ await this.#modal.openModal({ uri, standaloneChains: [this.chain] });
1938
1997
  }
1939
1998
  return new Promise((resolve, reject) => {
1940
- this.#client.on('connect', (error, payload) => {
1941
- if (error) {
1942
- reject(error);
1999
+ const unsubscribeModal = this.#modal.subscribeModal((state) => {
2000
+ if (!state.open) {
2001
+ unsubscribeModal();
2002
+ reject(new Error('Modal closed'));
1943
2003
  }
1944
- const { accounts } = payload.params[0];
1945
- resolve({
1946
- ...WalletConnectClient.metadata,
1947
- accounts: accounts.map((address, index) => ({
1948
- name: `Wallet Connect ${index + 1}`,
1949
- address,
1950
- providerId: WalletConnectClient.metadata.id
1951
- }))
1952
- });
1953
2004
  });
1954
- this.#client.on('session_update', (error, payload) => {
1955
- if (error) {
1956
- reject(error);
1957
- }
1958
- const { accounts } = payload.params[0];
2005
+ approval()
2006
+ .then((session) => {
2007
+ const { accounts } = session.namespaces.algorand;
1959
2008
  resolve({
1960
2009
  ...WalletConnectClient.metadata,
1961
- accounts: accounts.map((address, index) => ({
1962
- name: `Wallet Connect ${index + 1}`,
1963
- address,
2010
+ accounts: accounts.map((accountStr, index) => ({
2011
+ name: `WalletConnect ${index + 1}`,
2012
+ address: accountStr.split(':').pop(),
1964
2013
  providerId: WalletConnectClient.metadata.id
1965
2014
  }))
1966
2015
  });
2016
+ })
2017
+ .catch((error) => {
2018
+ reject(error);
2019
+ })
2020
+ .finally(() => {
2021
+ unsubscribeModal();
2022
+ this.#modal.closeModal();
1967
2023
  });
1968
2024
  });
1969
2025
  }
1970
2026
  // eslint-disable-next-line @typescript-eslint/require-await
1971
2027
  async reconnect() {
1972
- const accounts = this.#client.accounts;
1973
- if (!accounts) {
2028
+ const session = this.#getSession();
2029
+ if (typeof session === 'undefined') {
1974
2030
  return null;
1975
2031
  }
2032
+ const { accounts } = session.namespaces.algorand;
1976
2033
  return {
1977
2034
  ...WalletConnectClient.metadata,
1978
- accounts: accounts.map((address, index) => ({
1979
- name: `Wallet Connect ${index + 1}`,
1980
- address,
2035
+ accounts: accounts.map((accountStr, index) => ({
2036
+ name: `WalletConnect ${index + 1}`,
2037
+ address: accountStr.split(':').pop(),
1981
2038
  providerId: WalletConnectClient.metadata.id
1982
2039
  }))
1983
2040
  };
1984
2041
  }
1985
- check() {
1986
- return this.#client.connected;
1987
- }
1988
2042
  async disconnect() {
1989
2043
  try {
1990
- await this.#client.killSession();
2044
+ if (typeof this.#client === 'undefined') {
2045
+ throw new Error('WalletConnect is not initialized');
2046
+ }
2047
+ const session = this.#getSession();
2048
+ if (typeof session === 'undefined') {
2049
+ throw new Error('Session is not connected');
2050
+ }
2051
+ await this.#client.disconnect({
2052
+ topic: session.topic,
2053
+ reason: getSdkError('USER_DISCONNECTED')
2054
+ });
1991
2055
  }
1992
- catch (e) {
1993
- console.error('Error disconnecting', e);
2056
+ catch (error) {
2057
+ console.error('Error disconnecting', error);
1994
2058
  }
1995
2059
  }
1996
2060
  async signTransactions(connectedAccounts, txnGroups, indexesToSign, returnGroup = true) {
@@ -2029,21 +2093,26 @@ class WalletConnectClient extends BaseClient {
2029
2093
  }
2030
2094
  return acc;
2031
2095
  }, []);
2032
- const { formatJsonRpcRequest } = await import('@json-rpc-tools/utils');
2033
- const requestParams = [txnsToSign];
2034
- const request = formatJsonRpcRequest('algo_signTxn', requestParams);
2096
+ const session = this.#getSession();
2097
+ if (typeof session === 'undefined') {
2098
+ throw new Error('Session is not connected');
2099
+ }
2100
+ const request = formatJsonRpcRequest('algo_signTxn', [txnsToSign]);
2035
2101
  // Play an audio file to keep Wallet Connect's web socket open on iOS
2036
2102
  // when the user goes into background mode.
2037
- await this.keepWCAliveStart();
2038
- // Sign them with the client.
2039
- const result = await this.#client.sendCustomRequest(request);
2040
- this.keepWCAliveStop();
2103
+ // await this.keepWCAliveStart()
2104
+ const response = await this.#client.request({
2105
+ chainId: this.chain,
2106
+ topic: session.topic,
2107
+ request
2108
+ });
2109
+ // this.keepWCAliveStop()
2041
2110
  // Check if the result is the same length as the transactions
2042
- const lengthsMatch = result.length === transactions.length;
2111
+ const lengthsMatch = response.length === transactions.length;
2043
2112
  // Join the newly signed transactions with the original group of transactions.
2044
2113
  const signedTxns = transactions.reduce((acc, txn, i) => {
2045
2114
  if (signedIndexes.includes(i)) {
2046
- const signedByUser = lengthsMatch ? result[i] : result.shift();
2115
+ const signedByUser = lengthsMatch ? response[i] : response.shift();
2047
2116
  signedByUser && acc.push(new Uint8Array(Buffer.from(signedByUser, 'base64')));
2048
2117
  }
2049
2118
  else if (returnGroup) {
@@ -2053,6 +2122,23 @@ class WalletConnectClient extends BaseClient {
2053
2122
  }, []);
2054
2123
  return signedTxns;
2055
2124
  }
2125
+ #subscribeToEvents() {
2126
+ if (typeof this.#client === 'undefined') {
2127
+ throw new Error('WalletConnect is not initialized');
2128
+ }
2129
+ this.#client.on('session_event', (args) => {
2130
+ console.log('EVENT', 'session_event', args);
2131
+ });
2132
+ this.#client.on('session_update', ({ topic, params }) => {
2133
+ console.log('EVENT', 'session_update', { topic, params });
2134
+ });
2135
+ this.#client.on('session_delete', () => {
2136
+ console.log('EVENT', 'session_delete');
2137
+ });
2138
+ }
2139
+ #getSession() {
2140
+ return this.#client.session.getAll().at(-1);
2141
+ }
2056
2142
  }
2057
2143
 
2058
2144
  const ICON$1 = 'data:image/svg+xml;base64,' +
@@ -2063,14 +2149,12 @@ class KMDWalletClient extends BaseClient {
2063
2149
  #wallet;
2064
2150
  #password;
2065
2151
  walletId;
2066
- id;
2067
2152
  network;
2068
- constructor({ metadata, client, id, wallet, password, algosdk, algodClient, network }) {
2153
+ constructor({ metadata, client, wallet, password, algosdk, algodClient, network }) {
2069
2154
  super(metadata, algosdk, algodClient);
2070
2155
  this.#client = client;
2071
2156
  this.#wallet = wallet;
2072
2157
  this.#password = password;
2073
- this.id = id;
2074
2158
  this.walletId = '';
2075
2159
  this.network = network;
2076
2160
  this.metadata = KMDWalletClient.metadata;
@@ -2089,7 +2173,6 @@ class KMDWalletClient extends BaseClient {
2089
2173
  const kmdClient = new algosdk.Kmd(token, host, port);
2090
2174
  return new KMDWalletClient({
2091
2175
  metadata: KMDWalletClient.metadata,
2092
- id: PROVIDER_ID.KMD,
2093
2176
  password,
2094
2177
  wallet,
2095
2178
  client: kmdClient,
@@ -2227,11 +2310,9 @@ const ICON = "data:image/svg+xml,%3c%3fxml version='1.0' encoding='UTF-8'%3f%3e
2227
2310
 
2228
2311
  class MnemonicWalletClient extends BaseClient {
2229
2312
  #client;
2230
- id;
2231
2313
  network;
2232
- constructor({ metadata, id, algosdk, algodClient, network }) {
2314
+ constructor({ metadata, algosdk, algodClient, network }) {
2233
2315
  super(metadata, algosdk, algodClient);
2234
- this.id = id;
2235
2316
  this.network = network;
2236
2317
  this.metadata = MnemonicWalletClient.metadata;
2237
2318
  }
@@ -2245,10 +2326,8 @@ class MnemonicWalletClient extends BaseClient {
2245
2326
  try {
2246
2327
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
2247
2328
  const algodClient = getAlgodClient(algosdk, algodOptions);
2248
- console.log(network, algodClient);
2249
2329
  return new MnemonicWalletClient({
2250
2330
  metadata: MnemonicWalletClient.metadata,
2251
- id: PROVIDER_ID.MNEMONIC,
2252
2331
  algosdk: algosdk,
2253
2332
  algodClient: algodClient,
2254
2333
  network
@@ -2362,9 +2441,8 @@ var allClients = {
2362
2441
  [MnemonicWalletClient.metadata.id]: MnemonicWalletClient
2363
2442
  };
2364
2443
 
2365
- const clearActiveAccount = useWalletStore.getState().clearActiveAccount;
2366
- const removeAccounts = useWalletStore.getState().removeAccounts;
2367
2444
  const clearAccounts = (id) => {
2445
+ const { clearActiveAccount, removeAccounts } = useWalletStore.getState();
2368
2446
  clearActiveAccount(id);
2369
2447
  removeAccounts(id);
2370
2448
  };
@@ -2417,13 +2495,13 @@ function useWallet() {
2417
2495
  }));
2418
2496
  // eslint-disable-next-line react-hooks/exhaustive-deps
2419
2497
  }, [clients, connectedAccounts, connectedActiveAccounts, activeAccount]);
2420
- const getClient = async (id) => {
2498
+ const getClient = (id) => {
2421
2499
  if (!id)
2422
2500
  throw new Error('Provier ID is missing.');
2423
- const client = await clients?.[id];
2424
- if (!client)
2501
+ const walletClient = clients?.[id];
2502
+ if (!walletClient)
2425
2503
  throw new Error('Client not found for ID');
2426
- return client;
2504
+ return walletClient;
2427
2505
  };
2428
2506
  const status = useMemo(() => {
2429
2507
  if (activeAccount === undefined) {
@@ -2460,7 +2538,7 @@ function useWallet() {
2460
2538
  };
2461
2539
  const connect = async (id) => {
2462
2540
  try {
2463
- const walletClient = await getClient(id);
2541
+ const walletClient = getClient(id);
2464
2542
  const walletInfo = await walletClient?.connect(() => clearAccounts(id));
2465
2543
  if (!walletInfo || !walletInfo.accounts.length) {
2466
2544
  throw new Error('Failed to connect ' + id);
@@ -2474,7 +2552,7 @@ function useWallet() {
2474
2552
  };
2475
2553
  const reconnect = async (id) => {
2476
2554
  try {
2477
- const walletClient = await getClient(id);
2555
+ const walletClient = getClient(id);
2478
2556
  const walletInfo = await walletClient?.reconnect(() => clearAccounts(id));
2479
2557
  if (walletInfo && walletInfo.accounts.length) {
2480
2558
  addAccounts(walletInfo.accounts);
@@ -2487,7 +2565,7 @@ function useWallet() {
2487
2565
  };
2488
2566
  const disconnect = async (id) => {
2489
2567
  try {
2490
- const walletClient = await getClient(id);
2568
+ const walletClient = getClient(id);
2491
2569
  await walletClient?.disconnect();
2492
2570
  }
2493
2571
  catch (e) {
@@ -2507,7 +2585,7 @@ function useWallet() {
2507
2585
  }
2508
2586
  };
2509
2587
  const signTransactions = async (transactions, indexesToSign, returnGroup = true) => {
2510
- const walletClient = await getClient(activeAccount?.providerId);
2588
+ const walletClient = getClient(activeAccount?.providerId);
2511
2589
  if (!walletClient || !activeAccount?.address) {
2512
2590
  throw new Error('No wallet found.');
2513
2591
  }
@@ -2515,7 +2593,7 @@ function useWallet() {
2515
2593
  return signedTransactions;
2516
2594
  };
2517
2595
  const sendTransactions = async (transactions, waitRoundsToConfirm) => {
2518
- const walletClient = await getClient(activeAccount?.providerId);
2596
+ const walletClient = getClient(activeAccount?.providerId);
2519
2597
  const result = await walletClient?.sendRawTransactions(transactions, waitRoundsToConfirm);
2520
2598
  return result;
2521
2599
  };
@@ -2527,7 +2605,7 @@ function useWallet() {
2527
2605
  const getAccountInfo = async () => {
2528
2606
  if (!activeAccount)
2529
2607
  throw new Error('No selected account.');
2530
- const walletClient = await getClient(activeAccount.providerId);
2608
+ const walletClient = getClient(activeAccount.providerId);
2531
2609
  const accountInfo = await walletClient?.getAccountInfo(activeAccount.address);
2532
2610
  return accountInfo;
2533
2611
  };
@@ -2537,11 +2615,11 @@ function useWallet() {
2537
2615
  const getAssets = async () => {
2538
2616
  if (!activeAccount)
2539
2617
  throw new Error('No selected account.');
2540
- const walletClient = await getClient(activeAccount.providerId);
2618
+ const walletClient = getClient(activeAccount.providerId);
2541
2619
  return await walletClient?.getAssets(activeAccount.address);
2542
2620
  };
2543
- const groupTransactionsBySender = async (transactions) => {
2544
- const walletClient = await getClient(activeAccount?.providerId);
2621
+ const groupTransactionsBySender = (transactions) => {
2622
+ const walletClient = getClient(activeAccount?.providerId);
2545
2623
  return walletClient?.groupTransactionsBySender(transactions);
2546
2624
  };
2547
2625
  return {
@@ -2564,32 +2642,35 @@ function useWallet() {
2564
2642
  };
2565
2643
  }
2566
2644
 
2567
- const initializeProviders = (providers, nodeConfig, algosdkStatic) => {
2645
+ const initializeProviders = async (providers, nodeConfig, algosdkStatic) => {
2568
2646
  const initializedProviders = {};
2569
2647
  if (typeof window === 'undefined') {
2648
+ // @todo add `debug: Boolean` option to enable/disable console logs
2570
2649
  console.warn('Window object is not available, skipping initialization.');
2571
2650
  return initializedProviders;
2572
2651
  }
2573
2652
  const { network = DEFAULT_NETWORK$1, nodeServer = DEFAULT_NODE_BASEURL, nodePort = DEFAULT_NODE_PORT, nodeToken = DEFAULT_NODE_TOKEN } = nodeConfig || {};
2574
- if (!providers || providers.length === 0)
2575
- for (const [id, client] of Object.entries(allClients)) {
2576
- if (id === 'kmd' || id === 'mnemonic') {
2577
- continue;
2578
- }
2579
- initializedProviders[id] = client.init({
2580
- network,
2581
- algodOptions: [nodeToken, nodeServer, nodePort],
2582
- algosdkStatic: algosdkStatic
2583
- });
2584
- }
2585
- if (providers) {
2586
- for (const id of providers) {
2587
- initializedProviders[id] = allClients[id].init({
2588
- network,
2589
- algodOptions: [nodeToken, nodeServer, nodePort],
2590
- algosdkStatic: algosdkStatic
2591
- });
2592
- }
2653
+ const initClient = async (provider) => {
2654
+ const { id, ...providerConfig } = typeof provider === 'string' ? { id: provider } : provider;
2655
+ const initParams = {
2656
+ network,
2657
+ algodOptions: [nodeToken, nodeServer, nodePort],
2658
+ algosdkStatic,
2659
+ ...providerConfig
2660
+ };
2661
+ const client = await allClients[id].init(initParams);
2662
+ initializedProviders[id] = client;
2663
+ };
2664
+ // Initialize default providers if `providers` is undefined or empty
2665
+ if (!providers || providers.length === 0) {
2666
+ const initPromises = Object.keys(allClients)
2667
+ .filter((id) => id !== PROVIDER_ID.KMD && id !== PROVIDER_ID.MNEMONIC && id !== PROVIDER_ID.WALLETCONNECT)
2668
+ .map((id) => initClient(id));
2669
+ await Promise.all(initPromises);
2670
+ }
2671
+ else {
2672
+ const initPromises = providers.map((provider) => initClient(provider));
2673
+ await Promise.all(initPromises);
2593
2674
  }
2594
2675
  return initializedProviders;
2595
2676
  };
@@ -2607,11 +2688,10 @@ const reconnectProviders = async (providers) => {
2607
2688
  try {
2608
2689
  const clients = Object.values(providers);
2609
2690
  for (const client of clients) {
2610
- const c = await client;
2611
- const id = c?.metadata.id;
2691
+ const id = client?.metadata.id;
2612
2692
  // Only reconnect to active providers
2613
2693
  if (id && isActiveProvider(id)) {
2614
- await c.reconnect(() => clearAccounts(id));
2694
+ await client.reconnect(() => clearAccounts(id));
2615
2695
  }
2616
2696
  }
2617
2697
  }
@@ -2626,4 +2706,25 @@ function encodeNFDTransactionsArray(transactionsArray) {
2626
2706
  });
2627
2707
  }
2628
2708
 
2629
- export { DEFAULT_NETWORK$1 as DEFAULT_NETWORK, DEFAULT_NODE_BASEURL, DEFAULT_NODE_PORT, DEFAULT_NODE_TOKEN, PROVIDER_ID, clientStore as WalletProvider, AlgoSignerClient as algosigner, DeflyWalletClient as defly, encodeNFDTransactionsArray, ExodusClient as exodus, initializeProviders, KMDWalletClient as kmd, MnemonicWalletClient as mnemonic, MyAlgoWalletClient as myalgo, PeraWalletClient as pera, reconnectProviders, useWallet, WalletConnectClient as walletconnect };
2709
+ function useInitializeProviders({ providers, nodeConfig, algosdkStatic } = {}) {
2710
+ const [walletProviders, setWalletProviders] = useState(null);
2711
+ useEffect(() => {
2712
+ async function initializeAndConnect() {
2713
+ try {
2714
+ // Initialize with provided or default configuration
2715
+ const initializedProviders = await initializeProviders(providers, nodeConfig, algosdkStatic);
2716
+ setWalletProviders(initializedProviders);
2717
+ // Reconnect the session when the user returns to the app
2718
+ await reconnectProviders(initializedProviders);
2719
+ }
2720
+ catch (error) {
2721
+ console.error('Error initializing wallet providers:', error);
2722
+ }
2723
+ }
2724
+ void initializeAndConnect();
2725
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2726
+ }, []);
2727
+ return walletProviders;
2728
+ }
2729
+
2730
+ export { DEFAULT_NETWORK$1 as DEFAULT_NETWORK, DEFAULT_NODE_BASEURL, DEFAULT_NODE_PORT, DEFAULT_NODE_TOKEN, PROVIDER_ID, clientStore as WalletProvider, AlgoSignerClient as algosigner, DeflyWalletClient as defly, encodeNFDTransactionsArray, ExodusClient as exodus, initializeProviders, KMDWalletClient as kmd, MnemonicWalletClient as mnemonic, MyAlgoWalletClient as myalgo, PeraWalletClient as pera, reconnectProviders, useInitializeProviders, useWallet, WalletConnectClient as walletconnect };