@dynamic-labs/starknet 3.0.0-alpha.28 → 3.0.0-alpha.29

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