@txnlab/use-wallet 2.0.0-alpha.3 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -955,6 +955,11 @@ const useHydratedWalletStore = ((selector, compare) => {
955
955
  return hydrated ? store : selector(emptyState);
956
956
  });
957
957
 
958
+ const useDebugStore = create((set) => ({
959
+ debug: false,
960
+ setDebug: (debug) => set({ debug })
961
+ }));
962
+
958
963
  const ClientContext = require$$0.createContext(null);
959
964
  var clientStore = ClientContext.Provider;
960
965
 
@@ -1091,6 +1096,25 @@ class BaseClient {
1091
1096
  const ICON$8 = 'data:image/svg+xml;base64,' +
1092
1097
  'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNzcgMTg3Ij48cmVjdCB4PSItMTEuMzgiIHk9Ii0yNS45NyIgd2lkdGg9IjIwMC4wMiIgaGVpZ2h0PSIyMzEuNTMiIHN0eWxlPSJmaWxsOiNmZTU7Ii8+PHBhdGggZD0iTTk0LjA1LDU5LjYxYzIuMDUsOC40OCwxLjM2LDE1Ljk0LTEuNTUsMTYuNjYtMi45LC43Mi02LjkxLTUuNTctOC45Ni0xNC4wNS0yLjA1LTguNDgtMS4zNi0xNS45NCwxLjU1LTE2LjY2LDIuOS0uNzIsNi45MSw1LjU3LDguOTYsMTQuMDVaIiBzdHlsZT0iZmlsbDojMWMxYzFjOyIvPjxwYXRoIGQ9Ik0xMjcuODUsNjYuOWMtNC41My00LjgxLTEzLjU1LTMuNS0yMC4xNSwyLjkxLTYuNTksNi40MS04LjI2LDE1LjUtMy43MywyMC4zMSw0LjUzLDQuOCwxMy41NSwzLjUsMjAuMTUtMi45MXM4LjI2LTE1LjUsMy43My0yMC4zMVoiIHN0eWxlPSJmaWxsOiMxYzFjMWM7Ii8+PHBhdGggZD0iTTkxLjc5LDE0MC40N2MyLjktLjcyLDMuNDktOC42LDEuMzItMTcuNjEtMi4xNy05LTYuMjktMTUuNzEtOS4xOS0xNC45OS0yLjksLjcyLTMuNDksOC42LTEuMzIsMTcuNjEsMi4xNyw5LDYuMjksMTUuNzEsOS4xOSwxNC45OVoiIHN0eWxlPSJmaWxsOiMxYzFjMWM7Ii8+PHBhdGggZD0iTTYyLjIyLDcxLjNjOC4zNywyLjQ3LDE0LjQ4LDYuOCwxMy42Niw5LjY3LS44MywyLjg3LTguMjgsMy4yLTE2LjY1LC43My04LjM3LTIuNDctMTQuNDgtNi44LTEzLjY2LTkuNjcsLjgzLTIuODcsOC4yOC0zLjIsMTYuNjUtLjczWiIgc3R5bGU9ImZpbGw6IzFjMWMxYzsiLz48cGF0aCBkPSJNMTE2LjU0LDEwMy43NGM4Ljg4LDIuNjIsMTUuNDEsNy4wNywxNC41OSw5Ljk0LS44MywyLjg3LTguNywzLjA4LTE3LjU4LC40Ni04Ljg4LTIuNjItMTUuNDEtNy4wNy0xNC41OS05Ljk0LC44My0yLjg3LDguNy0zLjA4LDE3LjU4LS40NloiIHN0eWxlPSJmaWxsOiMxYzFjMWM7Ii8+PHBhdGggZD0iTTcxLjY0LDk3LjcxYy0yLjA4LTIuMTUtOC44OCwuOTgtMTUuMiw2Ljk5LTYuMzIsNi4wMS05Ljc2LDEyLjYzLTcuNjksMTQuNzgsMi4wOCwyLjE1LDguODgtLjk4LDE1LjItNi45OSw2LjMyLTYuMDEsOS43Ni0xMi42Myw3LjY5LTE0Ljc4WiIgc3R5bGU9ImZpbGw6IzFjMWMxYzsiLz48L3N2Zz4=';
1093
1098
 
1099
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1100
+ const debugLog = (...args) => {
1101
+ const { debug } = useDebugStore.getState();
1102
+ if (debug) {
1103
+ console.log('%c[USE-WALLET]', 'color: #FF602E; font-weight: bold;', ...args);
1104
+ }
1105
+ };
1106
+ const getProviderList = (providers) => {
1107
+ return providers
1108
+ .map((provider) => {
1109
+ if (typeof provider === 'string') {
1110
+ return provider;
1111
+ }
1112
+ return provider.id;
1113
+ })
1114
+ .map((id) => id.toUpperCase())
1115
+ .join(', ');
1116
+ };
1117
+
1094
1118
  class PeraWalletClient extends BaseClient {
1095
1119
  #client;
1096
1120
  clientOptions;
@@ -1110,13 +1134,14 @@ class PeraWalletClient extends BaseClient {
1110
1134
  };
1111
1135
  static async init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1112
1136
  try {
1137
+ debugLog(`${exports.PROVIDER_ID.PERA.toUpperCase()} initializing...`);
1113
1138
  const PeraWalletConnect = clientStatic || (await import('@perawallet/connect')).PeraWalletConnect;
1114
1139
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1115
1140
  const algodClient = getAlgodClient(algosdk, algodOptions);
1116
1141
  const peraWallet = new PeraWalletConnect({
1117
1142
  ...(clientOptions && clientOptions)
1118
1143
  });
1119
- return new PeraWalletClient({
1144
+ const provider = new PeraWalletClient({
1120
1145
  metadata: PeraWalletClient.metadata,
1121
1146
  client: peraWallet,
1122
1147
  clientOptions,
@@ -1124,6 +1149,8 @@ class PeraWalletClient extends BaseClient {
1124
1149
  algodClient,
1125
1150
  network
1126
1151
  });
1152
+ debugLog(`${exports.PROVIDER_ID.PERA.toUpperCase()} initialized`, '✅');
1153
+ return provider;
1127
1154
  }
1128
1155
  catch (e) {
1129
1156
  console.error('Error initializing...', e);
@@ -1257,13 +1284,14 @@ class DaffiWalletClient extends BaseClient {
1257
1284
  };
1258
1285
  static async init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1259
1286
  try {
1287
+ debugLog(`${exports.PROVIDER_ID.DAFFI.toUpperCase()} initializing...`);
1260
1288
  const DaffiWalletConnect = clientStatic || (await import('@daffiwallet/connect')).DaffiWalletConnect;
1261
1289
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1262
1290
  const algodClient = getAlgodClient(algosdk, algodOptions);
1263
1291
  const daffiWallet = new DaffiWalletConnect({
1264
1292
  ...(clientOptions ? clientOptions : { shouldShowSignTxnToast: false })
1265
1293
  });
1266
- return new DaffiWalletClient({
1294
+ const provider = new DaffiWalletClient({
1267
1295
  metadata: DaffiWalletClient.metadata,
1268
1296
  client: daffiWallet,
1269
1297
  clientOptions,
@@ -1271,6 +1299,8 @@ class DaffiWalletClient extends BaseClient {
1271
1299
  algodClient,
1272
1300
  network
1273
1301
  });
1302
+ debugLog(`${exports.PROVIDER_ID.DAFFI.toUpperCase()} initialized`, '✅');
1303
+ return provider;
1274
1304
  }
1275
1305
  catch (e) {
1276
1306
  console.error('Error initializing...', e);
@@ -1409,13 +1439,14 @@ class MyAlgoWalletClient extends BaseClient {
1409
1439
  };
1410
1440
  static async init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1411
1441
  try {
1442
+ debugLog(`${exports.PROVIDER_ID.MYALGO.toUpperCase()} initializing...`);
1412
1443
  const MyAlgoConnect = clientStatic || (await import('@randlabs/myalgo-connect')).default;
1413
1444
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1414
1445
  const algodClient = getAlgodClient(algosdk, algodOptions);
1415
1446
  const myAlgo = new MyAlgoConnect({
1416
1447
  ...(clientOptions ? clientOptions : { disableLedgerNano: false })
1417
1448
  });
1418
- return new MyAlgoWalletClient({
1449
+ const provider = new MyAlgoWalletClient({
1419
1450
  metadata: MyAlgoWalletClient.metadata,
1420
1451
  client: myAlgo,
1421
1452
  clientOptions,
@@ -1423,6 +1454,8 @@ class MyAlgoWalletClient extends BaseClient {
1423
1454
  algodClient: algodClient,
1424
1455
  network
1425
1456
  });
1457
+ debugLog(`${exports.PROVIDER_ID.MYALGO.toUpperCase()} initialized`, '✅');
1458
+ return provider;
1426
1459
  }
1427
1460
  catch (e) {
1428
1461
  console.error('Error initializing...', e);
@@ -1511,19 +1544,22 @@ class DeflyWalletClient extends BaseClient {
1511
1544
  };
1512
1545
  static async init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1513
1546
  try {
1547
+ debugLog(`${exports.PROVIDER_ID.DEFLY.toUpperCase()} initializing...`);
1514
1548
  const DeflyWalletConnect = clientStatic || (await import('@blockshake/defly-connect')).DeflyWalletConnect;
1515
1549
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1516
1550
  const algodClient = getAlgodClient(algosdk, algodOptions);
1517
1551
  const deflyWallet = new DeflyWalletConnect({
1518
1552
  ...(clientOptions && clientOptions)
1519
1553
  });
1520
- return new DeflyWalletClient({
1554
+ const provider = new DeflyWalletClient({
1521
1555
  metadata: DeflyWalletClient.metadata,
1522
1556
  client: deflyWallet,
1523
1557
  algosdk,
1524
1558
  algodClient,
1525
1559
  network
1526
1560
  });
1561
+ debugLog(`${exports.PROVIDER_ID.DEFLY.toUpperCase()} initialized`, '✅');
1562
+ return provider;
1527
1563
  }
1528
1564
  catch (e) {
1529
1565
  console.error('Error initializing...', e);
@@ -1657,13 +1693,14 @@ class ExodusClient extends BaseClient {
1657
1693
  };
1658
1694
  static async init({ clientOptions, algodOptions, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1659
1695
  try {
1696
+ debugLog(`${exports.PROVIDER_ID.EXODUS.toUpperCase()} initializing...`);
1660
1697
  if (typeof window == 'undefined' || window.exodus === undefined) {
1661
1698
  throw new Error('Exodus is not available.');
1662
1699
  }
1663
1700
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1664
1701
  const algodClient = getAlgodClient(algosdk, algodOptions);
1665
1702
  const exodus = window.exodus.algorand;
1666
- return new ExodusClient({
1703
+ const provider = new ExodusClient({
1667
1704
  metadata: ExodusClient.metadata,
1668
1705
  client: exodus,
1669
1706
  algosdk: algosdk,
@@ -1671,6 +1708,8 @@ class ExodusClient extends BaseClient {
1671
1708
  clientOptions: clientOptions || { onlyIfTrusted: false },
1672
1709
  network
1673
1710
  });
1711
+ debugLog(`${exports.PROVIDER_ID.EXODUS.toUpperCase()} initialized`, '✅');
1712
+ return provider;
1674
1713
  }
1675
1714
  catch (e) {
1676
1715
  console.warn(e);
@@ -1775,19 +1814,22 @@ class AlgoSignerClient extends BaseClient {
1775
1814
  };
1776
1815
  static async init({ algodOptions, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
1777
1816
  try {
1817
+ debugLog(`${exports.PROVIDER_ID.ALGOSIGNER.toUpperCase()} initializing...`);
1778
1818
  if (typeof window == 'undefined' || window.algorand === undefined) {
1779
1819
  throw new Error('AlgoSigner is not available.');
1780
1820
  }
1781
1821
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1782
1822
  const algodClient = getAlgodClient(algosdk, algodOptions);
1783
1823
  const algosigner = window.algorand;
1784
- return new AlgoSignerClient({
1824
+ const provider = new AlgoSignerClient({
1785
1825
  metadata: AlgoSignerClient.metadata,
1786
1826
  client: algosigner,
1787
1827
  algosdk: algosdk,
1788
1828
  algodClient: algodClient,
1789
1829
  network
1790
1830
  });
1831
+ debugLog(`${exports.PROVIDER_ID.ALGOSIGNER.toUpperCase()} initialized`, '✅');
1832
+ return provider;
1791
1833
  }
1792
1834
  catch (e) {
1793
1835
  console.warn(e);
@@ -1948,6 +1990,7 @@ class WalletConnectClient extends BaseClient {
1948
1990
  };
1949
1991
  static async init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network = DEFAULT_NETWORK }) {
1950
1992
  try {
1993
+ debugLog(`${exports.PROVIDER_ID.WALLETCONNECT.toUpperCase()} initializing...`);
1951
1994
  if (!isPublicNetwork(network)) {
1952
1995
  throw new Error(`WalletConnect only supports Algorand mainnet, testnet, and betanet. "${network}" is not supported.`);
1953
1996
  }
@@ -1970,8 +2013,8 @@ class WalletConnectClient extends BaseClient {
1970
2013
  // Initialize algod client
1971
2014
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
1972
2015
  const algodClient = getAlgodClient(algosdk, algodOptions);
1973
- // Initialize wallet client
1974
- return new WalletConnectClient({
2016
+ // Initialize provider client
2017
+ const provider = new WalletConnectClient({
1975
2018
  metadata: WalletConnectClient.metadata,
1976
2019
  client,
1977
2020
  clientOptions,
@@ -1980,6 +2023,8 @@ class WalletConnectClient extends BaseClient {
1980
2023
  network,
1981
2024
  chain
1982
2025
  });
2026
+ debugLog(`${exports.PROVIDER_ID.WALLETCONNECT.toUpperCase()} initialized`, '✅');
2027
+ return provider;
1983
2028
  }
1984
2029
  catch (error) {
1985
2030
  console.error('Error initializing WalletConnect client', error);
@@ -2138,11 +2183,12 @@ class KMDWalletClient extends BaseClient {
2138
2183
  };
2139
2184
  static async init({ clientOptions, algodOptions, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
2140
2185
  try {
2186
+ debugLog(`${exports.PROVIDER_ID.KMD.toUpperCase()} initializing...`);
2141
2187
  const { token = 'a'.repeat(64), host = 'http://localhost', port = '4002', wallet = 'unencrypted-default-wallet', password = '' } = clientOptions || {};
2142
2188
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
2143
2189
  const algodClient = getAlgodClient(algosdk, algodOptions);
2144
2190
  const kmdClient = new algosdk.Kmd(token, host, port);
2145
- return new KMDWalletClient({
2191
+ const provider = new KMDWalletClient({
2146
2192
  metadata: KMDWalletClient.metadata,
2147
2193
  password,
2148
2194
  wallet,
@@ -2151,6 +2197,8 @@ class KMDWalletClient extends BaseClient {
2151
2197
  algodClient: algodClient,
2152
2198
  network
2153
2199
  });
2200
+ debugLog(`${exports.PROVIDER_ID.KMD.toUpperCase()} initialized`, '✅');
2201
+ return provider;
2154
2202
  }
2155
2203
  catch (e) {
2156
2204
  console.error('Error initializing...', e);
@@ -2294,15 +2342,18 @@ class MnemonicWalletClient extends BaseClient {
2294
2342
  isWalletConnect: false
2295
2343
  };
2296
2344
  static async init({ algodOptions, algosdkStatic, network = DEFAULT_NETWORK$1 }) {
2345
+ debugLog(`${exports.PROVIDER_ID.MNEMONIC.toUpperCase()} initializing...`);
2297
2346
  try {
2298
2347
  const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk;
2299
2348
  const algodClient = getAlgodClient(algosdk, algodOptions);
2300
- return new MnemonicWalletClient({
2349
+ const provider = new MnemonicWalletClient({
2301
2350
  metadata: MnemonicWalletClient.metadata,
2302
2351
  algosdk: algosdk,
2303
2352
  algodClient: algodClient,
2304
2353
  network
2305
2354
  });
2355
+ debugLog(`${exports.PROVIDER_ID.MNEMONIC.toUpperCase()} initialized`);
2356
+ return provider;
2306
2357
  }
2307
2358
  catch (e) {
2308
2359
  console.error('Error initializing...', e);
@@ -2616,8 +2667,7 @@ function useWallet() {
2616
2667
  const initializeProviders = async (providers, nodeConfig, algosdkStatic) => {
2617
2668
  const initializedProviders = {};
2618
2669
  if (typeof window === 'undefined') {
2619
- // @todo add `debug: Boolean` option to enable/disable console logs
2620
- console.warn('Window object is not available, skipping initialization.');
2670
+ debugLog('Window object is not available, skipping initialization');
2621
2671
  return initializedProviders;
2622
2672
  }
2623
2673
  const { network = DEFAULT_NETWORK$1, nodeServer = DEFAULT_NODE_BASEURL, nodePort = DEFAULT_NODE_PORT, nodeToken = DEFAULT_NODE_TOKEN } = nodeConfig || {};
@@ -2632,14 +2682,15 @@ const initializeProviders = async (providers, nodeConfig, algosdkStatic) => {
2632
2682
  const client = await allClients[id].init(initParams);
2633
2683
  initializedProviders[id] = client;
2634
2684
  };
2635
- // Initialize default providers if `providers` is undefined or empty
2636
2685
  if (!providers || providers.length === 0) {
2686
+ debugLog('Initializing default providers:', getProviderList(DEFAULT_PROVIDERS));
2637
2687
  const initPromises = Object.keys(allClients)
2638
2688
  .filter((id) => DEFAULT_PROVIDERS.includes(id))
2639
2689
  .map((id) => initClient(id));
2640
2690
  await Promise.all(initPromises);
2641
2691
  }
2642
2692
  else {
2693
+ debugLog('Initializing custom providers:', getProviderList(providers));
2643
2694
  const initPromises = providers.map((provider) => initClient(provider));
2644
2695
  await Promise.all(initPromises);
2645
2696
  }
@@ -2677,7 +2728,10 @@ function encodeNFDTransactionsArray(transactionsArray) {
2677
2728
  });
2678
2729
  }
2679
2730
 
2680
- function useInitializeProviders({ providers = [], nodeConfig, algosdkStatic } = {}) {
2731
+ function useInitializeProviders({ providers = [], nodeConfig, algosdkStatic, debug = false } = {}) {
2732
+ // Enable debug mode
2733
+ const { setDebug } = useDebugStore();
2734
+ require$$0.useEffect(() => setDebug(debug), [debug, setDebug]);
2681
2735
  const [walletProviders, setWalletProviders] = require$$0.useState(null);
2682
2736
  require$$0.useEffect(() => {
2683
2737
  async function initializeAndConnect() {