@hashgraph/hedera-wallet-connect 2.0.4-canary.8800524.0 → 2.0.4-canary.9ae9a05.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.
@@ -5,7 +5,7 @@ import { ProviderUtil } from '@reown/appkit/store';
5
5
  import { LedgerId } from '@hashgraph/sdk';
6
6
  import { BrowserProvider, Contract, formatUnits, JsonRpcSigner, parseUnits } from 'ethers';
7
7
  import { HederaConnector } from './connectors';
8
- import { hederaNamespace, getAccountBalance } from './utils';
8
+ import { hederaNamespace, getAccountBalance, HederaChainDefinition } from './utils';
9
9
  import { createLogger } from '../lib/shared/logger';
10
10
  export class HederaAdapter extends AdapterBlueprint {
11
11
  constructor(params) {
@@ -25,6 +25,27 @@ export class HederaAdapter extends AdapterBlueprint {
25
25
  }
26
26
  super(Object.assign({}, params));
27
27
  this.logger = createLogger('HederaAdapter');
28
+ // Override getCaipNetworks to return appropriate networks based on namespace
29
+ this.getCaipNetworks = (namespace) => {
30
+ const targetNamespace = namespace || this.namespace;
31
+ if (targetNamespace === 'eip155') {
32
+ // Return EIP155 Hedera networks
33
+ return [HederaChainDefinition.EVM.Mainnet, HederaChainDefinition.EVM.Testnet];
34
+ }
35
+ else if (targetNamespace === hederaNamespace) {
36
+ // Return native Hedera networks
37
+ return [HederaChainDefinition.Native.Mainnet, HederaChainDefinition.Native.Testnet];
38
+ }
39
+ else {
40
+ // Return all Hedera networks if no specific namespace is requested
41
+ return [
42
+ HederaChainDefinition.EVM.Mainnet,
43
+ HederaChainDefinition.EVM.Testnet,
44
+ HederaChainDefinition.Native.Mainnet,
45
+ HederaChainDefinition.Native.Testnet,
46
+ ];
47
+ }
48
+ };
28
49
  }
29
50
  async setUniversalProvider(universalProvider) {
30
51
  this.addConnector(new HederaConnector({
@@ -10,7 +10,6 @@ export declare class HederaConnector implements ChainAdapterConnector {
10
10
  readonly chain: ChainNamespace;
11
11
  provider: UniversalProvider;
12
12
  protected caipNetworks: CaipNetwork[];
13
- private logger;
14
13
  constructor({ provider, caipNetworks, namespace }: HederaConnector.Options);
15
14
  get chains(): CaipNetwork[];
16
15
  connectWalletConnect(): Promise<{
@@ -1,14 +1,12 @@
1
1
  import { ConstantsUtil } from '@reown/appkit-common';
2
2
  import { PresetsUtil } from '@reown/appkit-utils';
3
3
  import { createNamespaces } from '../utils';
4
- import { createLogger } from '../../lib/shared/logger';
5
4
  export class HederaConnector {
6
5
  constructor({ provider, caipNetworks, namespace }) {
7
6
  this.id = ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT;
8
7
  this.name = PresetsUtil.ConnectorNamesMap[ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT];
9
8
  this.type = 'WALLET_CONNECT';
10
9
  this.imageId = PresetsUtil.ConnectorImageIds[ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT];
11
- this.logger = createLogger('HederaConnector');
12
10
  this.caipNetworks = caipNetworks;
13
11
  this.provider = provider;
14
12
  this.chain = namespace;
@@ -17,54 +15,9 @@ export class HederaConnector {
17
15
  return this.caipNetworks;
18
16
  }
19
17
  async connectWalletConnect() {
20
- var _a, _b, _c;
21
- this.logger.debug('connectWalletConnect called for', this.chain);
22
- this.logger.debug('Provider type:', (_b = (_a = this.provider) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name);
23
- this.logger.debug('Provider session exists:', !!((_c = this.provider) === null || _c === void 0 ? void 0 : _c.session));
24
- const isAuthenticated = await this.authenticate();
25
- this.logger.debug('Is authenticated:', isAuthenticated);
26
- if (!isAuthenticated) {
27
- // Check for stored connection params from the dApp
28
- let connectParams = undefined;
29
- this.logger.debug('Checking for stored params');
30
- if (typeof window !== 'undefined' && window.sessionStorage) {
31
- const storedParams = sessionStorage.getItem('hwcV2ConnectionParams');
32
- this.logger.debug('Stored params in sessionStorage:', storedParams);
33
- if (storedParams) {
34
- try {
35
- connectParams = JSON.parse(storedParams);
36
- this.logger.info('Using stored connection params from dApp in connector:', connectParams);
37
- // Don't clear here - let the provider handle it
38
- }
39
- catch (e) {
40
- this.logger.warn('Failed to parse stored connection params in connector:', e);
41
- }
42
- }
43
- }
44
- // If no stored params, create default namespaces
45
- if (!connectParams) {
46
- const namespaces = createNamespaces(this.caipNetworks);
47
- connectParams = { optionalNamespaces: namespaces };
48
- this.logger.debug('No stored params, using default namespaces:', connectParams);
49
- }
50
- this.logger.debug('Connecting with params:', {
51
- namespace: this.chain,
52
- caipNetworks: this.caipNetworks.map((n) => ({
53
- id: n.id,
54
- chainNamespace: n.chainNamespace,
55
- caipNetworkId: n.caipNetworkId,
56
- name: n.name,
57
- })),
58
- connectParams,
59
- });
60
- this.logger.debug('Final params before provider.connect:', connectParams);
61
- this.logger.debug('Calling provider.connect with params');
62
- await this.provider.connect(connectParams);
63
- this.logger.info('Provider.connect completed successfully');
64
- }
65
- else {
66
- this.logger.info('Already authenticated, skipping namespace setup');
67
- }
18
+ const namespaces = createNamespaces(this.caipNetworks);
19
+ const connectParams = { optionalNamespaces: namespaces };
20
+ await this.provider.connect(connectParams);
68
21
  return {
69
22
  clientId: await this.provider.client.core.crypto.getClientId(),
70
23
  session: this.provider.session,
@@ -53,9 +53,7 @@ class EIP155Provider {
53
53
  return this.chainId.toString();
54
54
  if (this.namespace.defaultChain)
55
55
  return this.namespace.defaultChain;
56
- const chainId = this.namespace.chains[0];
57
- if (!chainId)
58
- throw new Error(`ChainId not found`);
56
+ const chainId = this.namespace.chains[0] || 'eip155:295'; // default to mainnet
59
57
  return chainId.split(':')[1];
60
58
  }
61
59
  // ---------- Private ----------------------------------------------- //
@@ -42,9 +42,7 @@ class HIP820Provider {
42
42
  return this.chainId;
43
43
  if (this.namespace.defaultChain)
44
44
  return this.namespace.defaultChain;
45
- const chainId = this.namespace.chains[0];
46
- if (!chainId)
47
- throw new Error(`ChainId not found`);
45
+ const chainId = this.namespace.chains[0] || 'hedera:mainnet'; // default to mainnet
48
46
  return chainId.split(':')[1];
49
47
  }
50
48
  // create signer on demand
@@ -13,19 +13,11 @@ export class HederaProvider extends UniversalProvider {
13
13
  this.hederaLogger = createLogger('HederaProvider');
14
14
  }
15
15
  static async init(opts) {
16
- var _a, _b, _c, _d, _e, _f, _g, _h;
16
+ var _a, _b;
17
17
  const provider = new HederaProvider(opts);
18
- //@ts-expect-error - private base method
18
+ //@ts-expect-error
19
19
  await provider.initialize();
20
- provider.namespaces = Object.assign(Object.assign({}, (((_a = provider.namespaces) === null || _a === void 0 ? void 0 : _a.eip155)
21
- ? {
22
- eip155: Object.assign(Object.assign({}, (_b = provider.namespaces) === null || _b === void 0 ? void 0 : _b.eip155), { rpcMap: ((_d = (_c = provider.optionalNamespaces) === null || _c === void 0 ? void 0 : _c.eip155) === null || _d === void 0 ? void 0 : _d.rpcMap) || {} }),
23
- }
24
- : {})), (((_e = provider.namespaces) === null || _e === void 0 ? void 0 : _e.hedera)
25
- ? {
26
- hedera: Object.assign(Object.assign({}, (_f = provider.namespaces) === null || _f === void 0 ? void 0 : _f.hedera), { rpcMap: ((_h = (_g = provider.optionalNamespaces) === null || _g === void 0 ? void 0 : _g.hedera) === null || _h === void 0 ? void 0 : _h.rpcMap) || {} }),
27
- }
28
- : {}));
20
+ provider.namespaces = Object.assign(Object.assign({}, (((_a = provider.providerOpts) === null || _a === void 0 ? void 0 : _a.optionalNamespaces) || {})), (((_b = provider.providerOpts) === null || _b === void 0 ? void 0 : _b.requiredNamespaces) || {}));
29
21
  if (provider.session)
30
22
  provider.initProviders();
31
23
  return provider;
@@ -34,13 +26,13 @@ export class HederaProvider extends UniversalProvider {
34
26
  this.events.emit(event, data);
35
27
  }
36
28
  getAccountAddresses() {
37
- if (!this.session || !this.namespaces) {
29
+ if (!this.session) {
38
30
  throw new Error('Not initialized. Please call connect()');
39
31
  }
40
32
  return Object.values(this.session.namespaces).flatMap((namespace) => { var _a; return (_a = namespace.accounts.map((account) => account.split(':')[2])) !== null && _a !== void 0 ? _a : []; });
41
33
  }
42
34
  async request(args, chain, expiry) {
43
- var _a, _b, _c, _d;
35
+ var _a, _b, _c, _d, _e;
44
36
  if (!this.session || !this.namespaces) {
45
37
  throw new Error('Please call connect() before request()');
46
38
  }
@@ -61,8 +53,8 @@ export class HederaProvider extends UniversalProvider {
61
53
  if (!this.eip155Provider) {
62
54
  throw new Error('eip155Provider not initialized');
63
55
  }
64
- chainId = chainId !== null && chainId !== void 0 ? chainId : (_c = this.namespaces.eip155) === null || _c === void 0 ? void 0 : _c.chains[0];
65
- return (_d = this.eip155Provider) === null || _d === void 0 ? void 0 : _d.request({
56
+ chainId = chainId !== null && chainId !== void 0 ? chainId : (_d = (_c = this.namespaces) === null || _c === void 0 ? void 0 : _c.eip155) === null || _d === void 0 ? void 0 : _d.chains[0];
57
+ return (_e = this.eip155Provider) === null || _e === void 0 ? void 0 : _e.request({
66
58
  request: Object.assign({}, args),
67
59
  chainId: chainId,
68
60
  topic: this.session.topic,
@@ -415,54 +407,6 @@ export class HederaProvider extends UniversalProvider {
415
407
  }
416
408
  async connect(params) {
417
409
  this.hederaLogger.debug('connect called with params:', params);
418
- // Check for stored connection params from the dApp
419
- if (!params || (!params.requiredNamespaces && !params.optionalNamespaces)) {
420
- // Try to get params from sessionStorage (set by the dApp)
421
- if (typeof window !== 'undefined' && window.sessionStorage) {
422
- const storedParams = sessionStorage.getItem('hwcV2ConnectionParams');
423
- this.hederaLogger.debug('Stored params in sessionStorage:', storedParams);
424
- if (storedParams) {
425
- try {
426
- params = JSON.parse(storedParams);
427
- this.hederaLogger.info('Using stored connection params from dApp:', params);
428
- // Clear the stored params after using them
429
- sessionStorage.removeItem('hwcV2ConnectionParams');
430
- }
431
- catch (e) {
432
- this.hederaLogger.warn('Failed to parse stored connection params:', e);
433
- }
434
- }
435
- }
436
- }
437
- // If still no params provided or empty namespaces, create default namespaces
438
- if (!params || (!params.requiredNamespaces && !params.optionalNamespaces)) {
439
- this.hederaLogger.info('No namespaces provided, creating default namespaces');
440
- // Create default namespaces based on initialized state
441
- const defaultNamespaces = {
442
- hedera: {
443
- methods: Object.values(HederaJsonRpcMethod),
444
- chains: ['hedera:testnet', 'hedera:mainnet'],
445
- events: ['accountsChanged', 'chainChanged'],
446
- },
447
- eip155: {
448
- methods: [
449
- 'eth_sendTransaction',
450
- 'eth_signTransaction',
451
- 'eth_sign',
452
- 'personal_sign',
453
- 'eth_signTypedData',
454
- 'eth_signTypedData_v4',
455
- 'eth_accounts',
456
- 'eth_chainId',
457
- ],
458
- chains: ['eip155:296', 'eip155:295'],
459
- events: ['accountsChanged', 'chainChanged'],
460
- },
461
- };
462
- params = Object.assign({ requiredNamespaces: defaultNamespaces }, params);
463
- this.hederaLogger.debug('Using default namespaces:', params.requiredNamespaces);
464
- }
465
- this.hederaLogger.debug('Final params before super.connect:', params);
466
410
  // Update the internal namespace properties before connecting
467
411
  if (params) {
468
412
  if (params.requiredNamespaces) {
@@ -471,10 +415,15 @@ export class HederaProvider extends UniversalProvider {
471
415
  this.requiredNamespaces = params.requiredNamespaces;
472
416
  }
473
417
  if (params.optionalNamespaces) {
474
- this.hederaLogger.debug('Setting optionalNamespaces:', params.optionalNamespaces);
418
+ this.hederaLogger.debug('Setting optionalNamespaces:', params.requiredNamespaces);
475
419
  // @ts-ignore - accessing private property
476
420
  this.optionalNamespaces = params.optionalNamespaces;
477
421
  }
422
+ if (params.namespaces) {
423
+ this.hederaLogger.debug('Setting namespaces:', params.namespaces);
424
+ // @ts-ignore - accessing private property
425
+ this.namespaces = params.namespaces;
426
+ }
478
427
  }
479
428
  this.hederaLogger.debug('Calling super.connect with params');
480
429
  // Try to directly pass the namespaces to the parent connect
@@ -492,6 +441,9 @@ export class HederaProvider extends UniversalProvider {
492
441
  return result;
493
442
  }
494
443
  async pair(pairingTopic) {
444
+ console.log(pairingTopic);
445
+ //@ts-expect-error
446
+ console.log(this.requiredNamespaces);
495
447
  const session = await super.pair(pairingTopic);
496
448
  this.initProviders();
497
449
  return session;
@@ -506,10 +458,13 @@ export class HederaProvider extends UniversalProvider {
506
458
  const namespaces = Object.keys(this.namespaces);
507
459
  const providers = {};
508
460
  namespaces.forEach((namespace) => {
509
- const accounts = this.session.namespaces[namespace].accounts;
461
+ var _a, _b, _c, _d;
462
+ const accounts = ((_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.namespaces[namespace]) === null || _b === void 0 ? void 0 : _b.accounts) || [];
510
463
  const approvedChains = getChainsFromApprovedSession(accounts);
511
464
  const mergedNamespaces = mergeRequiredOptionalNamespaces(this.namespaces, this.optionalNamespaces);
512
- const combinedNamespace = Object.assign(Object.assign({}, mergedNamespaces[namespace]), { accounts, chains: approvedChains });
465
+ const combinedNamespace = Object.assign(Object.assign(Object.assign({}, mergedNamespaces[namespace]), { accounts, chains: approvedChains }), (((_d = (_c = this.optionalNamespaces) === null || _c === void 0 ? void 0 : _c[namespace]) === null || _d === void 0 ? void 0 : _d.rpcMap) && {
466
+ rpcMap: this.optionalNamespaces[namespace].rpcMap,
467
+ }));
513
468
  switch (namespace) {
514
469
  case 'hedera': {
515
470
  const provider = new HIP820Provider({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hashgraph/hedera-wallet-connect",
3
- "version": "2.0.4-canary.8800524.0",
3
+ "version": "2.0.4-canary.9ae9a05.0",
4
4
  "description": "A library to facilitate integrating Hedera with WalletConnect",
5
5
  "repository": {
6
6
  "type": "git",
@@ -38,6 +38,7 @@
38
38
  "@swc/jest": "^0.2.37",
39
39
  "@types/jest": "^30.0.0",
40
40
  "jest": "^30.0.3",
41
+ "nodemon": "^3.1.10",
41
42
  "prettier": "^3.5.3",
42
43
  "ts-node": "^10.9.2",
43
44
  "typescript": "^5.8.2"