@dynamic-labs/starknet 1.4.15 → 1.4.16

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/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ ### [1.4.16](https://github.com/dynamic-labs/DynamicAuth/compare/v1.4.15...v1.4.16) (2024-07-25)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * improve network switch ux for metamask starknet snap ([#6376](https://github.com/dynamic-labs/DynamicAuth/issues/6376)) ([#6419](https://github.com/dynamic-labs/DynamicAuth/issues/6419)) ([4909806](https://github.com/dynamic-labs/DynamicAuth/commit/4909806ee5fa8a6a90d85f7e9c24b2bde6de5e75))
8
+
2
9
  ### [1.4.15](https://github.com/dynamic-labs/DynamicAuth/compare/v1.4.14...v1.4.15) (2024-07-17)
3
10
 
4
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/starknet",
3
- "version": "1.4.15",
3
+ "version": "1.4.16",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -32,11 +32,12 @@
32
32
  "starknetkit": "1.1.3",
33
33
  "@dynamic-labs/sdk-api": "0.0.387",
34
34
  "@module-federation/runtime": "0.1.19",
35
- "@dynamic-labs/rpc-providers": "1.4.15",
36
- "@dynamic-labs/types": "1.4.15",
37
- "@dynamic-labs/utils": "1.4.15",
38
- "@dynamic-labs/wallet-book": "1.4.15",
39
- "@dynamic-labs/wallet-connector-core": "1.4.15"
35
+ "@dynamic-labs/logger": "1.4.16",
36
+ "@dynamic-labs/rpc-providers": "1.4.16",
37
+ "@dynamic-labs/types": "1.4.16",
38
+ "@dynamic-labs/utils": "1.4.16",
39
+ "@dynamic-labs/wallet-book": "1.4.16",
40
+ "@dynamic-labs/wallet-connector-core": "1.4.16"
40
41
  },
41
42
  "peerDependencies": {}
42
43
  }
@@ -4,22 +4,93 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../../_virtual/_tslib.cjs');
6
6
  var utils = require('@dynamic-labs/utils');
7
+ var logger$1 = require('@dynamic-labs/logger');
7
8
  var starknetWalletConnector = require('../starknetWalletConnector.cjs');
8
9
  var starknetSnap = require('../utils/starknetSnap.cjs');
9
10
 
11
+ const logger = new logger$1.Logger('MetaMask Starknet Snap', logger$1.LogLevel.INFO);
10
12
  class MetaMask extends starknetWalletConnector["default"] {
11
13
  constructor(opts) {
12
14
  super('MetaMask Starknet', 'metamask_snap', opts);
13
15
  this.overrideKey = 'metamaskstarknet';
14
- this.canSetEventListeners = false;
15
- if (!window.starknet_metamask_snap) {
16
- const { providers } = utils.Eip6963ProviderSingleton.get();
17
- const metamaskProvider = providers.find((p) => ['io.metamask', 'io.metamask.flask'].includes(p.info.rdns));
18
- if (metamaskProvider) {
19
- window.starknet_metamask_snap = starknetSnap.createMetaMaskProviderWrapper(metamaskProvider.provider);
20
- }
16
+ const { providers } = utils.Eip6963ProviderSingleton.get();
17
+ const metamaskProvider = providers.find((p) => ['io.metamask', 'io.metamask.flask'].includes(p.info.rdns));
18
+ if (metamaskProvider) {
19
+ this.provider = metamaskProvider.provider;
20
+ }
21
+ if (!window.starknet_metamask_snap && metamaskProvider) {
22
+ window.starknet_metamask_snap = starknetSnap.createMetaMaskProviderWrapper(metamaskProvider.provider);
21
23
  }
22
24
  }
25
+ getNetwork() {
26
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
27
+ if (!this.provider) {
28
+ logger.error('[getNetwork] - No provider found, returning undefined');
29
+ return undefined;
30
+ }
31
+ try {
32
+ logger.info('[getNetwork] - trying to fetch network using provider');
33
+ // we are using this method to get the network so that we always "see" the absolute
34
+ // active network in the companion site. when using the snap wrapper to get the network,
35
+ // we don't "see" the actual active network in the companion site – instead we see the
36
+ // network that was active at the time of the snap initialization
37
+ const result = yield this.provider.request({
38
+ method: 'wallet_invokeSnap',
39
+ params: {
40
+ request: {
41
+ method: 'starkNet_getCurrentNetwork',
42
+ params: {},
43
+ },
44
+ snapId: 'npm:@consensys/starknet-snap',
45
+ },
46
+ });
47
+ if (!('chainId' in result) || typeof result.chainId !== 'string') {
48
+ logger.error(`[getNetwork] - result.chainId should be a string, but got: ${
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ result.chainId}`);
51
+ return undefined;
52
+ }
53
+ if (result.chainId !== this.currentChainId) {
54
+ const resultChainName = this.mapChainIdToNetworkName(result.chainId);
55
+ const currentChainName = this.currentChainId
56
+ ? this.mapChainIdToNetworkName(this.currentChainId)
57
+ : undefined;
58
+ logger.info(`[getNetwork] - emitting chainChange event. got chainId: ${result.chainId} (${resultChainName}). current chainId: ${this.currentChainId} (${currentChainName})`);
59
+ this.emit('chainChange', { chain: result.chainId });
60
+ }
61
+ this.currentChainId = result.chainId;
62
+ return this.currentChainId;
63
+ }
64
+ catch (e) {
65
+ logger.error('[getNetwork] - network fetch request failed, returning undefined', e);
66
+ return undefined;
67
+ }
68
+ });
69
+ }
70
+ setupEventListeners() {
71
+ this.intervalId = setInterval(() => {
72
+ this.getNetwork().then((chainId) => {
73
+ if (!chainId) {
74
+ return;
75
+ }
76
+ const resultChainName = this.mapChainIdToNetworkName(chainId);
77
+ const currentChainName = this.currentChainId
78
+ ? this.mapChainIdToNetworkName(this.currentChainId)
79
+ : undefined;
80
+ logger.info(`[setupEventListeners] - got network: ${chainId} (${resultChainName}). current network: ${this.currentChainId} (${currentChainName})`);
81
+ if (chainId !== this.currentChainId) {
82
+ logger.info(`[setupEventListeners] - emitting chainChange event: ${chainId}`);
83
+ this.emit('chainChange', { chain: chainId });
84
+ this.currentChainId = chainId;
85
+ }
86
+ });
87
+ }, 5000);
88
+ }
89
+ teardownEventListeners() {
90
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
91
+ clearInterval(this.intervalId);
92
+ });
93
+ }
23
94
  getConnectedAccounts() {
24
95
  return _tslib.__awaiter(this, void 0, void 0, function* () {
25
96
  const wallet = this.getWallet();
@@ -2,7 +2,12 @@ import { type WalletConnector } from '@dynamic-labs/wallet-connector-core';
2
2
  import StarknetProvider from '../starknetWalletConnector';
3
3
  export declare class MetaMask extends StarknetProvider implements WalletConnector {
4
4
  overrideKey: string;
5
- canSetEventListeners: boolean;
5
+ private currentChainId;
6
+ private intervalId;
7
+ private provider;
6
8
  constructor(opts: any);
9
+ getNetwork(): Promise<string | undefined>;
10
+ setupEventListeners(): void;
11
+ teardownEventListeners(): Promise<void>;
7
12
  getConnectedAccounts(): Promise<string[]>;
8
13
  }
@@ -1,21 +1,92 @@
1
1
  import { __awaiter } from '../../_virtual/_tslib.js';
2
2
  import { Eip6963ProviderSingleton } from '@dynamic-labs/utils';
3
+ import { Logger, LogLevel } from '@dynamic-labs/logger';
3
4
  import StarknetWalletConnector from '../starknetWalletConnector.js';
4
5
  import { createMetaMaskProviderWrapper } from '../utils/starknetSnap.js';
5
6
 
7
+ const logger = new Logger('MetaMask Starknet Snap', LogLevel.INFO);
6
8
  class MetaMask extends StarknetWalletConnector {
7
9
  constructor(opts) {
8
10
  super('MetaMask Starknet', 'metamask_snap', opts);
9
11
  this.overrideKey = 'metamaskstarknet';
10
- this.canSetEventListeners = false;
11
- if (!window.starknet_metamask_snap) {
12
- const { providers } = Eip6963ProviderSingleton.get();
13
- const metamaskProvider = providers.find((p) => ['io.metamask', 'io.metamask.flask'].includes(p.info.rdns));
14
- if (metamaskProvider) {
15
- window.starknet_metamask_snap = createMetaMaskProviderWrapper(metamaskProvider.provider);
16
- }
12
+ const { providers } = Eip6963ProviderSingleton.get();
13
+ const metamaskProvider = providers.find((p) => ['io.metamask', 'io.metamask.flask'].includes(p.info.rdns));
14
+ if (metamaskProvider) {
15
+ this.provider = metamaskProvider.provider;
16
+ }
17
+ if (!window.starknet_metamask_snap && metamaskProvider) {
18
+ window.starknet_metamask_snap = createMetaMaskProviderWrapper(metamaskProvider.provider);
17
19
  }
18
20
  }
21
+ getNetwork() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ if (!this.provider) {
24
+ logger.error('[getNetwork] - No provider found, returning undefined');
25
+ return undefined;
26
+ }
27
+ try {
28
+ logger.info('[getNetwork] - trying to fetch network using provider');
29
+ // we are using this method to get the network so that we always "see" the absolute
30
+ // active network in the companion site. when using the snap wrapper to get the network,
31
+ // we don't "see" the actual active network in the companion site – instead we see the
32
+ // network that was active at the time of the snap initialization
33
+ const result = yield this.provider.request({
34
+ method: 'wallet_invokeSnap',
35
+ params: {
36
+ request: {
37
+ method: 'starkNet_getCurrentNetwork',
38
+ params: {},
39
+ },
40
+ snapId: 'npm:@consensys/starknet-snap',
41
+ },
42
+ });
43
+ if (!('chainId' in result) || typeof result.chainId !== 'string') {
44
+ logger.error(`[getNetwork] - result.chainId should be a string, but got: ${
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ result.chainId}`);
47
+ return undefined;
48
+ }
49
+ if (result.chainId !== this.currentChainId) {
50
+ const resultChainName = this.mapChainIdToNetworkName(result.chainId);
51
+ const currentChainName = this.currentChainId
52
+ ? this.mapChainIdToNetworkName(this.currentChainId)
53
+ : undefined;
54
+ logger.info(`[getNetwork] - emitting chainChange event. got chainId: ${result.chainId} (${resultChainName}). current chainId: ${this.currentChainId} (${currentChainName})`);
55
+ this.emit('chainChange', { chain: result.chainId });
56
+ }
57
+ this.currentChainId = result.chainId;
58
+ return this.currentChainId;
59
+ }
60
+ catch (e) {
61
+ logger.error('[getNetwork] - network fetch request failed, returning undefined', e);
62
+ return undefined;
63
+ }
64
+ });
65
+ }
66
+ setupEventListeners() {
67
+ this.intervalId = setInterval(() => {
68
+ this.getNetwork().then((chainId) => {
69
+ if (!chainId) {
70
+ return;
71
+ }
72
+ const resultChainName = this.mapChainIdToNetworkName(chainId);
73
+ const currentChainName = this.currentChainId
74
+ ? this.mapChainIdToNetworkName(this.currentChainId)
75
+ : undefined;
76
+ logger.info(`[setupEventListeners] - got network: ${chainId} (${resultChainName}). current network: ${this.currentChainId} (${currentChainName})`);
77
+ if (chainId !== this.currentChainId) {
78
+ logger.info(`[setupEventListeners] - emitting chainChange event: ${chainId}`);
79
+ this.emit('chainChange', { chain: chainId });
80
+ this.currentChainId = chainId;
81
+ }
82
+ });
83
+ }, 5000);
84
+ }
85
+ teardownEventListeners() {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ clearInterval(this.intervalId);
88
+ });
89
+ }
19
90
  getConnectedAccounts() {
20
91
  return __awaiter(this, void 0, void 0, function* () {
21
92
  const wallet = this.getWallet();