@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/cjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var require$$0 = require('react');
4
+ var utils = require('@walletconnect/utils');
4
5
  var algosdk = require('algosdk');
5
6
 
6
7
  exports.PROVIDER_ID = void 0;
@@ -967,12 +968,14 @@ const isIOS = getIsIOS();
967
968
  class BaseClient {
968
969
  algosdk;
969
970
  algodClient;
971
+ clientOptions;
970
972
  keepWCAlive;
971
973
  metadata;
972
974
  static metadata;
973
- constructor(metadata, algosdk, algodClient) {
975
+ constructor(metadata, algosdk, algodClient, clientOptions) {
974
976
  this.algosdk = algosdk;
975
977
  this.algodClient = algodClient;
978
+ this.clientOptions = clientOptions;
976
979
  this.keepWCAlive = new Audio();
977
980
  this.metadata = metadata;
978
981
  }
@@ -1085,10 +1088,12 @@ const ICON$8 = 'data:image/svg+xml;base64,' +
1085
1088
 
1086
1089
  class PeraWalletClient extends BaseClient {
1087
1090
  #client;
1091
+ clientOptions;
1088
1092
  network;
1089
- constructor({ metadata, client, algosdk, algodClient, network }) {
1093
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1090
1094
  super(metadata, algosdk, algodClient);
1091
1095
  this.#client = client;
1096
+ this.clientOptions = clientOptions;
1092
1097
  this.network = network;
1093
1098
  this.metadata = PeraWalletClient.metadata;
1094
1099
  }
@@ -1104,11 +1109,12 @@ class PeraWalletClient extends BaseClient {
1104
1109
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1105
1110
  const algodClient = getAlgodClient(algosdk, algodOptions);
1106
1111
  const peraWallet = new PeraWalletConnect({
1107
- ...(clientOptions ? clientOptions : { shouldShowSignTxnToast: false })
1112
+ ...(clientOptions && clientOptions)
1108
1113
  });
1109
1114
  return new PeraWalletClient({
1110
1115
  metadata: PeraWalletClient.metadata,
1111
1116
  client: peraWallet,
1117
+ clientOptions,
1112
1118
  algosdk,
1113
1119
  algodClient,
1114
1120
  network
@@ -1229,10 +1235,12 @@ const ICON$7 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eN
1229
1235
 
1230
1236
  class DaffiWalletClient extends BaseClient {
1231
1237
  #client;
1238
+ clientOptions;
1232
1239
  network;
1233
- constructor({ metadata, client, algosdk, algodClient, network }) {
1240
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1234
1241
  super(metadata, algosdk, algodClient);
1235
1242
  this.#client = client;
1243
+ this.clientOptions = clientOptions;
1236
1244
  this.network = network;
1237
1245
  this.metadata = DaffiWalletClient.metadata;
1238
1246
  }
@@ -1253,6 +1261,7 @@ class DaffiWalletClient extends BaseClient {
1253
1261
  return new DaffiWalletClient({
1254
1262
  metadata: DaffiWalletClient.metadata,
1255
1263
  client: daffiWallet,
1264
+ clientOptions,
1256
1265
  algosdk,
1257
1266
  algodClient,
1258
1267
  network
@@ -1378,10 +1387,12 @@ const ICON$6 = 'data:image/svg+xml;base64,' +
1378
1387
  */
1379
1388
  class MyAlgoWalletClient extends BaseClient {
1380
1389
  #client;
1390
+ clientOptions;
1381
1391
  network;
1382
- constructor({ metadata, client, algosdk, algodClient, network }) {
1392
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1383
1393
  super(metadata, algosdk, algodClient);
1384
1394
  this.#client = client;
1395
+ this.clientOptions = clientOptions;
1385
1396
  this.network = network;
1386
1397
  this.metadata = MyAlgoWalletClient.metadata;
1387
1398
  }
@@ -1402,6 +1413,7 @@ class MyAlgoWalletClient extends BaseClient {
1402
1413
  return new MyAlgoWalletClient({
1403
1414
  metadata: MyAlgoWalletClient.metadata,
1404
1415
  client: myAlgo,
1416
+ clientOptions,
1405
1417
  algosdk: algosdk,
1406
1418
  algodClient: algodClient,
1407
1419
  network
@@ -1477,10 +1489,12 @@ const ICON$5 = 'data:image/svg+xml;base64,' +
1477
1489
 
1478
1490
  class DeflyWalletClient extends BaseClient {
1479
1491
  #client;
1492
+ clientOptions;
1480
1493
  network;
1481
- constructor({ metadata, client, algosdk, algodClient, network }) {
1494
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1482
1495
  super(metadata, algosdk, algodClient);
1483
1496
  this.#client = client;
1497
+ this.clientOptions = clientOptions;
1484
1498
  this.network = network;
1485
1499
  this.metadata = DeflyWalletClient.metadata;
1486
1500
  }
@@ -1496,7 +1510,7 @@ class DeflyWalletClient extends BaseClient {
1496
1510
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1497
1511
  const algodClient = getAlgodClient(algosdk, algodOptions);
1498
1512
  const deflyWallet = new DeflyWalletConnect({
1499
- ...(clientOptions ? clientOptions : { shouldShowSignTxnToast: false })
1513
+ ...(clientOptions && clientOptions)
1500
1514
  });
1501
1515
  return new DeflyWalletClient({
1502
1516
  metadata: DeflyWalletClient.metadata,
@@ -1621,12 +1635,12 @@ const ICON$4 = 'data:image/svg+xml;base64,' +
1621
1635
 
1622
1636
  class ExodusClient extends BaseClient {
1623
1637
  #client;
1624
- #onlyIfTrusted;
1638
+ clientOptions;
1625
1639
  network;
1626
- constructor({ metadata, client, algosdk, algodClient, onlyIfTrusted, network }) {
1640
+ constructor({ metadata, client, clientOptions, algosdk, algodClient, network }) {
1627
1641
  super(metadata, algosdk, algodClient);
1628
1642
  this.#client = client;
1629
- this.#onlyIfTrusted = onlyIfTrusted;
1643
+ this.clientOptions = clientOptions;
1630
1644
  this.network = network;
1631
1645
  this.metadata = ExodusClient.metadata;
1632
1646
  }
@@ -1646,11 +1660,10 @@ class ExodusClient extends BaseClient {
1646
1660
  const exodus = window.exodus.algorand;
1647
1661
  return new ExodusClient({
1648
1662
  metadata: ExodusClient.metadata,
1649
- id: exports.PROVIDER_ID.EXODUS,
1650
1663
  client: exodus,
1651
1664
  algosdk: algosdk,
1652
1665
  algodClient: algodClient,
1653
- onlyIfTrusted: clientOptions?.onlyIfTrusted || false,
1666
+ clientOptions: clientOptions || { onlyIfTrusted: false },
1654
1667
  network
1655
1668
  });
1656
1669
  }
@@ -1662,7 +1675,7 @@ class ExodusClient extends BaseClient {
1662
1675
  }
1663
1676
  async connect() {
1664
1677
  const { address } = await this.#client.connect({
1665
- onlyIfTrusted: this.#onlyIfTrusted
1678
+ onlyIfTrusted: this.clientOptions.onlyIfTrusted
1666
1679
  });
1667
1680
  if (!address) {
1668
1681
  throw new Error(`No accounts found for ${ExodusClient.metadata.id}`);
@@ -1765,7 +1778,6 @@ class AlgoSignerClient extends BaseClient {
1765
1778
  const algosigner = window.algorand;
1766
1779
  return new AlgoSignerClient({
1767
1780
  metadata: AlgoSignerClient.metadata,
1768
- id: exports.PROVIDER_ID.ALGOSIGNER,
1769
1781
  client: algosigner,
1770
1782
  algosdk: algosdk,
1771
1783
  algodClient: algodClient,
@@ -1882,17 +1894,47 @@ class AlgoSignerClient extends BaseClient {
1882
1894
  }
1883
1895
  }
1884
1896
 
1897
+ const getPayloadId = () => {
1898
+ const date = Date.now() * Math.pow(10, 3);
1899
+ const extra = Math.floor(Math.random() * Math.pow(10, 3));
1900
+ return date + extra;
1901
+ };
1902
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1903
+ const formatJsonRpcRequest = (method, params) => {
1904
+ return {
1905
+ id: getPayloadId(),
1906
+ jsonrpc: '2.0',
1907
+ method,
1908
+ params
1909
+ };
1910
+ };
1911
+
1912
+ const isPublicNetwork = (network) => {
1913
+ return network === 'betanet' || network === 'testnet' || network === 'mainnet';
1914
+ };
1915
+
1885
1916
  const ICON$2 = 'data:image/svg+xml;base64,' +
1886
1917
  'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgNDgwIDQ4MCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDgwIDQ4MDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsOiMzMzk2RkY7fQo8L3N0eWxlPgo8cGF0aCBjbGFzcz0ic3QwIiBkPSJNMTI2LjYsMTY4YzYyLjYtNjEuMywxNjQuMi02MS4zLDIyNi44LDBsNy41LDcuNGMzLjEsMy4xLDMuMSw4LDAsMTEuMWwtMjUuOCwyNS4yYy0xLjYsMS41LTQuMSwxLjUtNS43LDAKCWwtMTAuNC0xMC4yYy00My43LTQyLjgtMTE0LjUtNDIuOC0xNTguMiwwbC0xMS4xLDEwLjljLTEuNiwxLjUtNC4xLDEuNS01LjcsMGwtMjUuOC0yNS4yYy0zLjEtMy4xLTMuMS04LDAtMTEuMUwxMjYuNiwxNjh6CgkgTTQwNi43LDIyMC4ybDIyLjksMjIuNWMzLjEsMy4xLDMuMSw4LDAsMTEuMUwzMjYuMiwzNTUuMWMtMy4xLDMuMS04LjIsMy4xLTExLjMsMGwtNzMuNC03MS45Yy0wLjgtMC44LTIuMS0wLjgtMi44LDBsLTczLjQsNzEuOQoJYy0zLjEsMy4xLTguMiwzLjEtMTEuMywwTDUwLjMsMjUzLjhjLTMuMS0zLjEtMy4xLTgsMC0xMS4xbDIyLjktMjIuNWMzLjEtMy4xLDguMi0zLjEsMTEuMywwbDczLjQsNzEuOWMwLjgsMC44LDIuMSwwLjgsMi44LDAKCWw3My40LTcxLjljMy4xLTMuMSw4LjItMy4xLDExLjMsMGw3My40LDcxLjljMC44LDAuOCwyLjEsMC44LDIuOCwwbDczLjQtNzEuOUMzOTguNSwyMTcuMSw0MDMuNiwyMTcuMSw0MDYuNywyMjAuMkw0MDYuNywyMjAuMnoiLz4KPC9zdmc+Cg==';
1887
- const DEFAULT_NETWORK = 'mainnet';
1918
+ const DEFAULT_NETWORK = 'mainnet';
1919
+ const ALGORAND_CHAINS = {
1920
+ mainnet: 'algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k',
1921
+ testnet: 'algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDe',
1922
+ betanet: 'algorand:mFgazF-2uRS1tMiL9dsj01hJGySEmPN2'
1923
+ };
1888
1924
 
1889
1925
  class WalletConnectClient extends BaseClient {
1890
1926
  #client;
1927
+ clientOptions;
1928
+ #modal;
1891
1929
  network;
1892
- constructor({ metadata, client, algosdk, algodClient, network }) {
1930
+ chain;
1931
+ constructor({ metadata, client, clientOptions, modal, algosdk, algodClient, network, chain }) {
1893
1932
  super(metadata, algosdk, algodClient);
1894
1933
  this.#client = client;
1934
+ this.clientOptions = clientOptions;
1935
+ this.#modal = modal;
1895
1936
  this.network = network;
1937
+ this.chain = chain;
1896
1938
  this.metadata = WalletConnectClient.metadata;
1897
1939
  }
1898
1940
  static metadata = {
@@ -1901,98 +1943,120 @@ class WalletConnectClient extends BaseClient {
1901
1943
  icon: ICON$2,
1902
1944
  isWalletConnect: true
1903
1945
  };
1904
- static async init({ clientOptions, algodOptions, clientStatic, modalStatic, algosdkStatic, network = DEFAULT_NETWORK }) {
1946
+ static async init({ clientOptions, algodOptions, clientStatic, modalStatic, modalOptions, algosdkStatic, network = DEFAULT_NETWORK }) {
1905
1947
  try {
1906
- const WalletConnect = clientStatic || (await import('@walletconnect/client')).default;
1907
- const QRCodeModal = modalStatic || (await import('algorand-walletconnect-qrcode-modal')).default;
1908
- const walletConnect = new WalletConnect({
1909
- bridge: 'https://bridge.walletconnect.org',
1910
- qrcodeModal: QRCodeModal,
1911
- storageId: 'walletconnect-generic',
1912
- ...(clientOptions || {})
1948
+ if (!isPublicNetwork(network)) {
1949
+ throw new Error(`WalletConnect only supports Algorand mainnet, testnet, and betanet. "${network}" is not supported.`);
1950
+ }
1951
+ const chain = ALGORAND_CHAINS[network];
1952
+ // Initialize sign client
1953
+ const Client = clientStatic || (await import('@walletconnect/sign-client')).default;
1954
+ const client = await Client.init(clientOptions);
1955
+ // Initialize web3modal
1956
+ const modalModule = modalStatic
1957
+ ? { WalletConnectModal: modalStatic }
1958
+ : await import('@walletconnect/modal');
1959
+ const Web3Modal = modalModule.WalletConnectModal;
1960
+ const modal = new Web3Modal({
1961
+ explorerExcludedWalletIds: 'ALL',
1962
+ ...modalOptions,
1963
+ projectId: clientOptions?.projectId || '',
1964
+ walletConnectVersion: 2
1913
1965
  });
1966
+ // Initialize algod client
1914
1967
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1915
1968
  const algodClient = getAlgodClient(algosdk, algodOptions);
1916
- const initWallet = {
1969
+ // Initialize wallet client
1970
+ const walletClient = new WalletConnectClient({
1917
1971
  metadata: WalletConnectClient.metadata,
1918
- client: walletConnect,
1919
- algosdk: algosdk,
1920
- algodClient: algodClient,
1921
- network
1922
- };
1923
- return new WalletConnectClient(initWallet);
1972
+ client,
1973
+ clientOptions,
1974
+ modal,
1975
+ algosdk,
1976
+ algodClient,
1977
+ network,
1978
+ chain
1979
+ });
1980
+ walletClient.#subscribeToEvents();
1981
+ return walletClient;
1924
1982
  }
1925
- catch (e) {
1926
- console.error('Error initializing...', e);
1983
+ catch (error) {
1984
+ console.error('Error initializing', error);
1927
1985
  return null;
1928
1986
  }
1929
1987
  }
1930
1988
  async connect() {
1931
- let chainId = 416001;
1932
- if (this.network === 'betanet') {
1933
- chainId = 416003;
1934
- }
1935
- else if (this.network === 'testnet') {
1936
- chainId = 416002;
1937
- }
1938
- if (!this.#client.connected) {
1939
- await this.#client.createSession({ chainId });
1989
+ const requiredNamespaces = {
1990
+ algorand: {
1991
+ chains: [this.chain],
1992
+ methods: ['algo_signTxn'],
1993
+ events: []
1994
+ }
1995
+ };
1996
+ const { uri, approval } = await this.#client.connect({ requiredNamespaces });
1997
+ if (uri) {
1998
+ await this.#modal.openModal({ uri, standaloneChains: [this.chain] });
1940
1999
  }
1941
2000
  return new Promise((resolve, reject) => {
1942
- this.#client.on('connect', (error, payload) => {
1943
- if (error) {
1944
- reject(error);
2001
+ const unsubscribeModal = this.#modal.subscribeModal((state) => {
2002
+ if (!state.open) {
2003
+ unsubscribeModal();
2004
+ reject(new Error('Modal closed'));
1945
2005
  }
1946
- const { accounts } = payload.params[0];
1947
- resolve({
1948
- ...WalletConnectClient.metadata,
1949
- accounts: accounts.map((address, index) => ({
1950
- name: `Wallet Connect ${index + 1}`,
1951
- address,
1952
- providerId: WalletConnectClient.metadata.id
1953
- }))
1954
- });
1955
2006
  });
1956
- this.#client.on('session_update', (error, payload) => {
1957
- if (error) {
1958
- reject(error);
1959
- }
1960
- const { accounts } = payload.params[0];
2007
+ approval()
2008
+ .then((session) => {
2009
+ const { accounts } = session.namespaces.algorand;
1961
2010
  resolve({
1962
2011
  ...WalletConnectClient.metadata,
1963
- accounts: accounts.map((address, index) => ({
1964
- name: `Wallet Connect ${index + 1}`,
1965
- address,
2012
+ accounts: accounts.map((accountStr, index) => ({
2013
+ name: `WalletConnect ${index + 1}`,
2014
+ address: accountStr.split(':').pop(),
1966
2015
  providerId: WalletConnectClient.metadata.id
1967
2016
  }))
1968
2017
  });
2018
+ })
2019
+ .catch((error) => {
2020
+ reject(error);
2021
+ })
2022
+ .finally(() => {
2023
+ unsubscribeModal();
2024
+ this.#modal.closeModal();
1969
2025
  });
1970
2026
  });
1971
2027
  }
1972
2028
  // eslint-disable-next-line @typescript-eslint/require-await
1973
2029
  async reconnect() {
1974
- const accounts = this.#client.accounts;
1975
- if (!accounts) {
2030
+ const session = this.#getSession();
2031
+ if (typeof session === 'undefined') {
1976
2032
  return null;
1977
2033
  }
2034
+ const { accounts } = session.namespaces.algorand;
1978
2035
  return {
1979
2036
  ...WalletConnectClient.metadata,
1980
- accounts: accounts.map((address, index) => ({
1981
- name: `Wallet Connect ${index + 1}`,
1982
- address,
2037
+ accounts: accounts.map((accountStr, index) => ({
2038
+ name: `WalletConnect ${index + 1}`,
2039
+ address: accountStr.split(':').pop(),
1983
2040
  providerId: WalletConnectClient.metadata.id
1984
2041
  }))
1985
2042
  };
1986
2043
  }
1987
- check() {
1988
- return this.#client.connected;
1989
- }
1990
2044
  async disconnect() {
1991
2045
  try {
1992
- await this.#client.killSession();
2046
+ if (typeof this.#client === 'undefined') {
2047
+ throw new Error('WalletConnect is not initialized');
2048
+ }
2049
+ const session = this.#getSession();
2050
+ if (typeof session === 'undefined') {
2051
+ throw new Error('Session is not connected');
2052
+ }
2053
+ await this.#client.disconnect({
2054
+ topic: session.topic,
2055
+ reason: utils.getSdkError('USER_DISCONNECTED')
2056
+ });
1993
2057
  }
1994
- catch (e) {
1995
- console.error('Error disconnecting', e);
2058
+ catch (error) {
2059
+ console.error('Error disconnecting', error);
1996
2060
  }
1997
2061
  }
1998
2062
  async signTransactions(connectedAccounts, txnGroups, indexesToSign, returnGroup = true) {
@@ -2031,21 +2095,26 @@ class WalletConnectClient extends BaseClient {
2031
2095
  }
2032
2096
  return acc;
2033
2097
  }, []);
2034
- const { formatJsonRpcRequest } = await import('@json-rpc-tools/utils');
2035
- const requestParams = [txnsToSign];
2036
- const request = formatJsonRpcRequest('algo_signTxn', requestParams);
2098
+ const session = this.#getSession();
2099
+ if (typeof session === 'undefined') {
2100
+ throw new Error('Session is not connected');
2101
+ }
2102
+ const request = formatJsonRpcRequest('algo_signTxn', [txnsToSign]);
2037
2103
  // Play an audio file to keep Wallet Connect's web socket open on iOS
2038
2104
  // when the user goes into background mode.
2039
- await this.keepWCAliveStart();
2040
- // Sign them with the client.
2041
- const result = await this.#client.sendCustomRequest(request);
2042
- this.keepWCAliveStop();
2105
+ // await this.keepWCAliveStart()
2106
+ const response = await this.#client.request({
2107
+ chainId: this.chain,
2108
+ topic: session.topic,
2109
+ request
2110
+ });
2111
+ // this.keepWCAliveStop()
2043
2112
  // Check if the result is the same length as the transactions
2044
- const lengthsMatch = result.length === transactions.length;
2113
+ const lengthsMatch = response.length === transactions.length;
2045
2114
  // Join the newly signed transactions with the original group of transactions.
2046
2115
  const signedTxns = transactions.reduce((acc, txn, i) => {
2047
2116
  if (signedIndexes.includes(i)) {
2048
- const signedByUser = lengthsMatch ? result[i] : result.shift();
2117
+ const signedByUser = lengthsMatch ? response[i] : response.shift();
2049
2118
  signedByUser && acc.push(new Uint8Array(Buffer.from(signedByUser, 'base64')));
2050
2119
  }
2051
2120
  else if (returnGroup) {
@@ -2055,6 +2124,23 @@ class WalletConnectClient extends BaseClient {
2055
2124
  }, []);
2056
2125
  return signedTxns;
2057
2126
  }
2127
+ #subscribeToEvents() {
2128
+ if (typeof this.#client === 'undefined') {
2129
+ throw new Error('WalletConnect is not initialized');
2130
+ }
2131
+ this.#client.on('session_event', (args) => {
2132
+ console.log('EVENT', 'session_event', args);
2133
+ });
2134
+ this.#client.on('session_update', ({ topic, params }) => {
2135
+ console.log('EVENT', 'session_update', { topic, params });
2136
+ });
2137
+ this.#client.on('session_delete', () => {
2138
+ console.log('EVENT', 'session_delete');
2139
+ });
2140
+ }
2141
+ #getSession() {
2142
+ return this.#client.session.getAll().at(-1);
2143
+ }
2058
2144
  }
2059
2145
 
2060
2146
  const ICON$1 = 'data:image/svg+xml;base64,' +
@@ -2065,14 +2151,12 @@ class KMDWalletClient extends BaseClient {
2065
2151
  #wallet;
2066
2152
  #password;
2067
2153
  walletId;
2068
- id;
2069
2154
  network;
2070
- constructor({ metadata, client, id, wallet, password, algosdk, algodClient, network }) {
2155
+ constructor({ metadata, client, wallet, password, algosdk, algodClient, network }) {
2071
2156
  super(metadata, algosdk, algodClient);
2072
2157
  this.#client = client;
2073
2158
  this.#wallet = wallet;
2074
2159
  this.#password = password;
2075
- this.id = id;
2076
2160
  this.walletId = '';
2077
2161
  this.network = network;
2078
2162
  this.metadata = KMDWalletClient.metadata;
@@ -2091,7 +2175,6 @@ class KMDWalletClient extends BaseClient {
2091
2175
  const kmdClient = new algosdk.Kmd(token, host, port);
2092
2176
  return new KMDWalletClient({
2093
2177
  metadata: KMDWalletClient.metadata,
2094
- id: exports.PROVIDER_ID.KMD,
2095
2178
  password,
2096
2179
  wallet,
2097
2180
  client: kmdClient,
@@ -2229,11 +2312,9 @@ const ICON = "data:image/svg+xml,%3c%3fxml version='1.0' encoding='UTF-8'%3f%3e
2229
2312
 
2230
2313
  class MnemonicWalletClient extends BaseClient {
2231
2314
  #client;
2232
- id;
2233
2315
  network;
2234
- constructor({ metadata, id, algosdk, algodClient, network }) {
2316
+ constructor({ metadata, algosdk, algodClient, network }) {
2235
2317
  super(metadata, algosdk, algodClient);
2236
- this.id = id;
2237
2318
  this.network = network;
2238
2319
  this.metadata = MnemonicWalletClient.metadata;
2239
2320
  }
@@ -2247,10 +2328,8 @@ class MnemonicWalletClient extends BaseClient {
2247
2328
  try {
2248
2329
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
2249
2330
  const algodClient = getAlgodClient(algosdk, algodOptions);
2250
- console.log(network, algodClient);
2251
2331
  return new MnemonicWalletClient({
2252
2332
  metadata: MnemonicWalletClient.metadata,
2253
- id: exports.PROVIDER_ID.MNEMONIC,
2254
2333
  algosdk: algosdk,
2255
2334
  algodClient: algodClient,
2256
2335
  network
@@ -2364,9 +2443,8 @@ var allClients = {
2364
2443
  [MnemonicWalletClient.metadata.id]: MnemonicWalletClient
2365
2444
  };
2366
2445
 
2367
- const clearActiveAccount = useWalletStore.getState().clearActiveAccount;
2368
- const removeAccounts = useWalletStore.getState().removeAccounts;
2369
2446
  const clearAccounts = (id) => {
2447
+ const { clearActiveAccount, removeAccounts } = useWalletStore.getState();
2370
2448
  clearActiveAccount(id);
2371
2449
  removeAccounts(id);
2372
2450
  };
@@ -2419,13 +2497,13 @@ function useWallet() {
2419
2497
  }));
2420
2498
  // eslint-disable-next-line react-hooks/exhaustive-deps
2421
2499
  }, [clients, connectedAccounts, connectedActiveAccounts, activeAccount]);
2422
- const getClient = async (id) => {
2500
+ const getClient = (id) => {
2423
2501
  if (!id)
2424
2502
  throw new Error('Provier ID is missing.');
2425
- const client = await clients?.[id];
2426
- if (!client)
2503
+ const walletClient = clients?.[id];
2504
+ if (!walletClient)
2427
2505
  throw new Error('Client not found for ID');
2428
- return client;
2506
+ return walletClient;
2429
2507
  };
2430
2508
  const status = require$$0.useMemo(() => {
2431
2509
  if (activeAccount === undefined) {
@@ -2462,7 +2540,7 @@ function useWallet() {
2462
2540
  };
2463
2541
  const connect = async (id) => {
2464
2542
  try {
2465
- const walletClient = await getClient(id);
2543
+ const walletClient = getClient(id);
2466
2544
  const walletInfo = await walletClient?.connect(() => clearAccounts(id));
2467
2545
  if (!walletInfo || !walletInfo.accounts.length) {
2468
2546
  throw new Error('Failed to connect ' + id);
@@ -2476,7 +2554,7 @@ function useWallet() {
2476
2554
  };
2477
2555
  const reconnect = async (id) => {
2478
2556
  try {
2479
- const walletClient = await getClient(id);
2557
+ const walletClient = getClient(id);
2480
2558
  const walletInfo = await walletClient?.reconnect(() => clearAccounts(id));
2481
2559
  if (walletInfo && walletInfo.accounts.length) {
2482
2560
  addAccounts(walletInfo.accounts);
@@ -2489,7 +2567,7 @@ function useWallet() {
2489
2567
  };
2490
2568
  const disconnect = async (id) => {
2491
2569
  try {
2492
- const walletClient = await getClient(id);
2570
+ const walletClient = getClient(id);
2493
2571
  await walletClient?.disconnect();
2494
2572
  }
2495
2573
  catch (e) {
@@ -2509,7 +2587,7 @@ function useWallet() {
2509
2587
  }
2510
2588
  };
2511
2589
  const signTransactions = async (transactions, indexesToSign, returnGroup = true) => {
2512
- const walletClient = await getClient(activeAccount?.providerId);
2590
+ const walletClient = getClient(activeAccount?.providerId);
2513
2591
  if (!walletClient || !activeAccount?.address) {
2514
2592
  throw new Error('No wallet found.');
2515
2593
  }
@@ -2517,7 +2595,7 @@ function useWallet() {
2517
2595
  return signedTransactions;
2518
2596
  };
2519
2597
  const sendTransactions = async (transactions, waitRoundsToConfirm) => {
2520
- const walletClient = await getClient(activeAccount?.providerId);
2598
+ const walletClient = getClient(activeAccount?.providerId);
2521
2599
  const result = await walletClient?.sendRawTransactions(transactions, waitRoundsToConfirm);
2522
2600
  return result;
2523
2601
  };
@@ -2529,7 +2607,7 @@ function useWallet() {
2529
2607
  const getAccountInfo = async () => {
2530
2608
  if (!activeAccount)
2531
2609
  throw new Error('No selected account.');
2532
- const walletClient = await getClient(activeAccount.providerId);
2610
+ const walletClient = getClient(activeAccount.providerId);
2533
2611
  const accountInfo = await walletClient?.getAccountInfo(activeAccount.address);
2534
2612
  return accountInfo;
2535
2613
  };
@@ -2539,11 +2617,11 @@ function useWallet() {
2539
2617
  const getAssets = async () => {
2540
2618
  if (!activeAccount)
2541
2619
  throw new Error('No selected account.');
2542
- const walletClient = await getClient(activeAccount.providerId);
2620
+ const walletClient = getClient(activeAccount.providerId);
2543
2621
  return await walletClient?.getAssets(activeAccount.address);
2544
2622
  };
2545
- const groupTransactionsBySender = async (transactions) => {
2546
- const walletClient = await getClient(activeAccount?.providerId);
2623
+ const groupTransactionsBySender = (transactions) => {
2624
+ const walletClient = getClient(activeAccount?.providerId);
2547
2625
  return walletClient?.groupTransactionsBySender(transactions);
2548
2626
  };
2549
2627
  return {
@@ -2566,32 +2644,35 @@ function useWallet() {
2566
2644
  };
2567
2645
  }
2568
2646
 
2569
- const initializeProviders = (providers, nodeConfig, algosdkStatic) => {
2647
+ const initializeProviders = async (providers, nodeConfig, algosdkStatic) => {
2570
2648
  const initializedProviders = {};
2571
2649
  if (typeof window === 'undefined') {
2650
+ // @todo add `debug: Boolean` option to enable/disable console logs
2572
2651
  console.warn('Window object is not available, skipping initialization.');
2573
2652
  return initializedProviders;
2574
2653
  }
2575
2654
  const { network = DEFAULT_NETWORK$1, nodeServer = DEFAULT_NODE_BASEURL, nodePort = DEFAULT_NODE_PORT, nodeToken = DEFAULT_NODE_TOKEN } = nodeConfig || {};
2576
- if (!providers || providers.length === 0)
2577
- for (const [id, client] of Object.entries(allClients)) {
2578
- if (id === 'kmd' || id === 'mnemonic') {
2579
- continue;
2580
- }
2581
- initializedProviders[id] = client.init({
2582
- network,
2583
- algodOptions: [nodeToken, nodeServer, nodePort],
2584
- algosdkStatic: algosdkStatic
2585
- });
2586
- }
2587
- if (providers) {
2588
- for (const id of providers) {
2589
- initializedProviders[id] = allClients[id].init({
2590
- network,
2591
- algodOptions: [nodeToken, nodeServer, nodePort],
2592
- algosdkStatic: algosdkStatic
2593
- });
2594
- }
2655
+ const initClient = async (provider) => {
2656
+ const { id, ...providerConfig } = typeof provider === 'string' ? { id: provider } : provider;
2657
+ const initParams = {
2658
+ network,
2659
+ algodOptions: [nodeToken, nodeServer, nodePort],
2660
+ algosdkStatic,
2661
+ ...providerConfig
2662
+ };
2663
+ const client = await allClients[id].init(initParams);
2664
+ initializedProviders[id] = client;
2665
+ };
2666
+ // Initialize default providers if `providers` is undefined or empty
2667
+ if (!providers || providers.length === 0) {
2668
+ const initPromises = Object.keys(allClients)
2669
+ .filter((id) => id !== exports.PROVIDER_ID.KMD && id !== exports.PROVIDER_ID.MNEMONIC && id !== exports.PROVIDER_ID.WALLETCONNECT)
2670
+ .map((id) => initClient(id));
2671
+ await Promise.all(initPromises);
2672
+ }
2673
+ else {
2674
+ const initPromises = providers.map((provider) => initClient(provider));
2675
+ await Promise.all(initPromises);
2595
2676
  }
2596
2677
  return initializedProviders;
2597
2678
  };
@@ -2609,11 +2690,10 @@ const reconnectProviders = async (providers) => {
2609
2690
  try {
2610
2691
  const clients = Object.values(providers);
2611
2692
  for (const client of clients) {
2612
- const c = await client;
2613
- const id = c?.metadata.id;
2693
+ const id = client?.metadata.id;
2614
2694
  // Only reconnect to active providers
2615
2695
  if (id && isActiveProvider(id)) {
2616
- await c.reconnect(() => clearAccounts(id));
2696
+ await client.reconnect(() => clearAccounts(id));
2617
2697
  }
2618
2698
  }
2619
2699
  }
@@ -2628,6 +2708,27 @@ function encodeNFDTransactionsArray(transactionsArray) {
2628
2708
  });
2629
2709
  }
2630
2710
 
2711
+ function useInitializeProviders({ providers, nodeConfig, algosdkStatic } = {}) {
2712
+ const [walletProviders, setWalletProviders] = require$$0.useState(null);
2713
+ require$$0.useEffect(() => {
2714
+ async function initializeAndConnect() {
2715
+ try {
2716
+ // Initialize with provided or default configuration
2717
+ const initializedProviders = await initializeProviders(providers, nodeConfig, algosdkStatic);
2718
+ setWalletProviders(initializedProviders);
2719
+ // Reconnect the session when the user returns to the app
2720
+ await reconnectProviders(initializedProviders);
2721
+ }
2722
+ catch (error) {
2723
+ console.error('Error initializing wallet providers:', error);
2724
+ }
2725
+ }
2726
+ void initializeAndConnect();
2727
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2728
+ }, []);
2729
+ return walletProviders;
2730
+ }
2731
+
2631
2732
  exports.DEFAULT_NETWORK = DEFAULT_NETWORK$1;
2632
2733
  exports.DEFAULT_NODE_BASEURL = DEFAULT_NODE_BASEURL;
2633
2734
  exports.DEFAULT_NODE_PORT = DEFAULT_NODE_PORT;
@@ -2643,6 +2744,7 @@ exports.mnemonic = MnemonicWalletClient;
2643
2744
  exports.myalgo = MyAlgoWalletClient;
2644
2745
  exports.pera = PeraWalletClient;
2645
2746
  exports.reconnectProviders = reconnectProviders;
2747
+ exports.useInitializeProviders = useInitializeProviders;
2646
2748
  exports.useWallet = useWallet;
2647
2749
  exports.walletconnect = WalletConnectClient;
2648
2750
  //# sourceMappingURL=index.js.map