@dynamic-labs/ethereum 2.0.0-alpha.9 → 2.0.1

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 (43) hide show
  1. package/CHANGELOG.md +328 -0
  2. package/package.json +11 -9
  3. package/src/EthWalletConnector.cjs +27 -27
  4. package/src/EthWalletConnector.d.ts +13 -10
  5. package/src/EthWalletConnector.js +27 -27
  6. package/src/coinbase/client/client.cjs +14 -14
  7. package/src/coinbase/client/client.d.ts +5 -4
  8. package/src/coinbase/client/client.js +13 -13
  9. package/src/coinbase/client/types.d.ts +6 -3
  10. package/src/coinbase/coinbase.cjs +32 -15
  11. package/src/coinbase/coinbase.d.ts +5 -4
  12. package/src/coinbase/coinbase.js +33 -16
  13. package/src/ethProviderHelper.cjs +11 -6
  14. package/src/ethProviderHelper.d.ts +6 -4
  15. package/src/ethProviderHelper.js +11 -6
  16. package/src/index.cjs +5 -1
  17. package/src/index.d.ts +1 -0
  18. package/src/index.js +4 -2
  19. package/src/injected/InjectedWalletBase.cjs +32 -15
  20. package/src/injected/InjectedWalletBase.d.ts +2 -2
  21. package/src/injected/InjectedWalletBase.js +32 -15
  22. package/src/injected/PhantomEvm.cjs +3 -3
  23. package/src/injected/PhantomEvm.d.ts +1 -1
  24. package/src/injected/PhantomEvm.js +3 -3
  25. package/src/injected/Zerion.cjs +38 -0
  26. package/src/injected/Zerion.d.ts +10 -0
  27. package/src/injected/Zerion.js +34 -0
  28. package/src/injected/fetchInjectedWalletConnectors.cjs +25 -0
  29. package/src/injected/fetchInjectedWalletConnectors.js +25 -0
  30. package/src/types.d.ts +1 -0
  31. package/src/utils/findEvmNetwork.d.ts +2 -2
  32. package/src/utils/getNameservice.cjs +45 -0
  33. package/src/utils/getNameservice.d.ts +6 -0
  34. package/src/utils/getNameservice.js +41 -0
  35. package/src/utils/index.d.ts +2 -0
  36. package/src/utils/isEthWalletConnector/index.d.ts +1 -0
  37. package/src/utils/isEthWalletConnector/isEthWalletConnector.cjs +7 -0
  38. package/src/utils/isEthWalletConnector/isEthWalletConnector.d.ts +3 -0
  39. package/src/utils/isEthWalletConnector/isEthWalletConnector.js +3 -0
  40. package/src/walletConnect/index.d.ts +1 -1
  41. package/src/walletConnect/walletConnect.cjs +118 -79
  42. package/src/walletConnect/walletConnect.d.ts +19 -18
  43. package/src/walletConnect/walletConnect.js +117 -78
@@ -1,13 +1,15 @@
1
1
  import { __awaiter } from '../_virtual/_tslib.js';
2
2
  import { formatEther } from 'viem';
3
+ import { toAccount } from 'viem/accounts';
3
4
  import { WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
4
5
  import { parseEvmNetworks, retryableFn, FALLBACK_UNDEFINED, DynamicError, getOrMapViemChain } from '@dynamic-labs/utils';
5
6
  import { findEvmNetwork } from './utils/findEvmNetwork.js';
7
+ import { getNameservice } from './utils/getNameservice.js';
6
8
 
7
9
  class EthWalletConnector extends WalletConnectorBase {
8
10
  getPublicClient() {
9
- var _a, _b, _c;
10
11
  return __awaiter(this, void 0, void 0, function* () {
12
+ var _a, _b, _c;
11
13
  const networkId = (_a = (yield this.getNetwork())) !== null && _a !== void 0 ? _a : 1;
12
14
  if (this.evmNetworks.length === 0) {
13
15
  return undefined;
@@ -62,19 +64,7 @@ class EthWalletConnector extends WalletConnectorBase {
62
64
  if (!address || !rpcProvider) {
63
65
  return;
64
66
  }
65
- const ensName = yield rpcProvider.getEnsName({
66
- address: address,
67
- });
68
- const ensAvatar = ensName
69
- ? yield rpcProvider.getEnsAvatar({
70
- name: ensName,
71
- })
72
- : undefined;
73
- const ensData = {
74
- avatar: ensAvatar !== null && ensAvatar !== void 0 ? ensAvatar : undefined,
75
- name: ensName !== null && ensName !== void 0 ? ensName : undefined,
76
- };
77
- return ensData;
67
+ return getNameservice({ address, rpcProvider });
78
68
  });
79
69
  }
80
70
  getSigner() {
@@ -101,8 +91,8 @@ class EthWalletConnector extends WalletConnectorBase {
101
91
  supportsNetworkSwitching() {
102
92
  return true;
103
93
  }
104
- switchNetwork({ networkName, networkChainId, }) {
105
- return __awaiter(this, void 0, void 0, function* () {
94
+ switchNetwork(_a) {
95
+ return __awaiter(this, arguments, void 0, function* ({ networkName, networkChainId, }) {
106
96
  const network = findEvmNetwork({
107
97
  chainId: networkChainId,
108
98
  name: networkName,
@@ -126,15 +116,19 @@ class EthWalletConnector extends WalletConnectorBase {
126
116
  const provider = this.getWalletClient();
127
117
  if (!provider)
128
118
  return [];
129
- return retryableFn(provider.getAddresses, {
119
+ const addresses = yield retryableFn(provider.getAddresses, {
130
120
  fallbackValue: [],
131
121
  timeoutMs: 300,
132
122
  });
123
+ if (addresses.length) {
124
+ this.setActiveAccount(addresses[0]);
125
+ }
126
+ return addresses;
133
127
  });
134
128
  }
135
- providerSwitchNetwork({ network, provider, }) {
136
- var _a, _b, _c, _d;
137
- return __awaiter(this, void 0, void 0, function* () {
129
+ providerSwitchNetwork(_a) {
130
+ return __awaiter(this, arguments, void 0, function* ({ network, provider, }) {
131
+ var _b, _c, _d, _e;
138
132
  const { chainId } = network;
139
133
  const currentNetworkId = yield this.getNetwork();
140
134
  if (currentNetworkId && currentNetworkId === chainId) {
@@ -149,14 +143,14 @@ class EthWalletConnector extends WalletConnectorBase {
149
143
  catch (error) {
150
144
  // we need to check for unrecognized chain error first because it also contains 'rejected' in message
151
145
  if (error.code === 4902 ||
152
- ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Unrecognized chain')) ||
146
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Unrecognized chain')) ||
153
147
  // https://github.com/MetaMask/metamask-mobile/issues/3312#issuecomment-1065923294
154
- ((_c = (_b = error.data) === null || _b === void 0 ? void 0 : _b.orginalError) === null || _c === void 0 ? void 0 : _c.code) === 4902) {
148
+ ((_d = (_c = error.data) === null || _c === void 0 ? void 0 : _c.orginalError) === null || _d === void 0 ? void 0 : _d.code) === 4902) {
155
149
  // error code indicates the chain has not been added yet
156
150
  // https://docs.metamask.io/guide/rpc-api.html#usage-with-wallet-switchethereumchain
157
151
  return this.providerAddNetwork({ network, provider });
158
152
  }
159
- else if (((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('rejected')) ||
153
+ else if (((_e = error.message) === null || _e === void 0 ? void 0 : _e.includes('rejected')) ||
160
154
  (typeof error === 'string' && error.includes('rejected'))) {
161
155
  throw new DynamicError("User rejected the wallet's request to switch network");
162
156
  }
@@ -166,14 +160,14 @@ class EthWalletConnector extends WalletConnectorBase {
166
160
  }
167
161
  });
168
162
  }
169
- providerAddNetwork({ network, provider, }) {
170
- var _a;
171
- return __awaiter(this, void 0, void 0, function* () {
163
+ providerAddNetwork(_a) {
164
+ return __awaiter(this, arguments, void 0, function* ({ network, provider, }) {
165
+ var _b;
172
166
  try {
173
167
  return yield provider.addChain({ chain: getOrMapViemChain(network) });
174
168
  }
175
169
  catch (error) {
176
- if (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('rejected')) ||
170
+ if (((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('rejected')) ||
177
171
  (typeof error === 'string' && error.includes('rejected'))) {
178
172
  throw new DynamicError("User rejected the wallet's request to add network");
179
173
  }
@@ -183,6 +177,12 @@ class EthWalletConnector extends WalletConnectorBase {
183
177
  }
184
178
  });
185
179
  }
180
+ setActiveAccount(account) {
181
+ this.activeAccount = account ? toAccount(account) : undefined;
182
+ }
183
+ getActiveAccount() {
184
+ return this.activeAccount;
185
+ }
186
186
  }
187
187
 
188
188
  export { EthWalletConnector };
@@ -10,11 +10,15 @@ var constants = require('../../constants.cjs');
10
10
 
11
11
  const jsonRpcUrl = `https://mainnet.infura.io/v3/${constants.INFURA_ID}`;
12
12
  const chainId = 1;
13
+ // storing a reference to the coinbase sdk so that we can call getQrUrl on it when we need a fresh qr code
14
+ let coinbaseSdk;
15
+ // storing a reference to the coinbase provider because the provider methods work better when
16
+ // they are called on the same instance
13
17
  let coinbaseProvider;
14
- const getCoinbaseProvider = ({ opts: { appLogoUrl, appName = '', evmNetworks = [] } = {}, }) => {
18
+ const getCoinbaseClient = ({ opts: { appLogoUrl, appName = '', evmNetworks = [] } = {}, }) => {
15
19
  var _a, _b;
16
20
  if (!coinbaseProvider) {
17
- const coinbaseWalletSDK = new walletSdk.CoinbaseWalletSDK({
21
+ coinbaseSdk = new walletSdk.CoinbaseWalletSDK({
18
22
  appLogoUrl,
19
23
  appName,
20
24
  enableMobileWalletLink: true,
@@ -25,21 +29,17 @@ const getCoinbaseProvider = ({ opts: { appLogoUrl, appName = '', evmNetworks = [
25
29
  const rpcUrl = ((_a = network === null || network === void 0 ? void 0 : network.privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a[0]) ||
26
30
  ((_b = network === null || network === void 0 ? void 0 : network.rpcUrls) === null || _b === void 0 ? void 0 : _b[0]) ||
27
31
  jsonRpcUrl;
28
- coinbaseProvider = coinbaseWalletSDK.makeWeb3Provider(rpcUrl, chainId);
32
+ coinbaseProvider = coinbaseSdk.makeWeb3Provider(rpcUrl, chainId);
29
33
  }
30
- return coinbaseProvider;
34
+ return { provider: coinbaseProvider, sdk: coinbaseSdk };
31
35
  };
32
36
  const killCoinbaseSession = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
33
37
  yield (coinbaseProvider === null || coinbaseProvider === void 0 ? void 0 : coinbaseProvider.close());
34
- coinbaseProvider === null || coinbaseProvider === void 0 ? void 0 : coinbaseProvider.disconnect();
35
- // We needed to remove the provider, because after a disconnect the initial settings that
36
- // were set in the SDK reset, and they need to be initialized again.
37
- coinbaseProvider = undefined;
38
38
  });
39
- const fetchPublicAddress = (coinbaseProviderOpts, opts) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
39
+ const getAddress = (coinbaseProviderOpts, opts) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
40
40
  var _a;
41
- const provider = getCoinbaseProvider({ opts: coinbaseProviderOpts });
42
- const { qrUrl } = provider;
41
+ const { provider, sdk } = getCoinbaseClient({ opts: coinbaseProviderOpts });
42
+ const qrUrl = sdk.getQrUrl();
43
43
  if (!qrUrl) {
44
44
  throw new utils.DynamicError('no qr url available');
45
45
  }
@@ -50,7 +50,7 @@ const fetchPublicAddress = (coinbaseProviderOpts, opts) => _tslib.__awaiter(void
50
50
  return address;
51
51
  });
52
52
  const signMessage = (coinbaseProviderOpts, messageToSign) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
53
- const provider = getCoinbaseProvider({ opts: coinbaseProviderOpts });
53
+ const { provider } = getCoinbaseClient({ opts: coinbaseProviderOpts });
54
54
  const [address] = yield provider.request({
55
55
  method: 'eth_requestAccounts',
56
56
  });
@@ -66,7 +66,7 @@ const signMessage = (coinbaseProviderOpts, messageToSign) => _tslib.__awaiter(vo
66
66
  }
67
67
  });
68
68
 
69
- exports.fetchPublicAddress = fetchPublicAddress;
70
- exports.getCoinbaseProvider = getCoinbaseProvider;
69
+ exports.getAddress = getAddress;
70
+ exports.getCoinbaseClient = getCoinbaseClient;
71
71
  exports.killCoinbaseSession = killCoinbaseSession;
72
72
  exports.signMessage = signMessage;
@@ -1,6 +1,7 @@
1
- import { FetchPublicAddressOpts } from '@dynamic-labs/wallet-connector-core';
2
- import { GetCoinbaseProvider, GetCoinbaseProviderOpts } from './types';
3
- export declare const getCoinbaseProvider: GetCoinbaseProvider;
1
+ import { GetAddressOpts } from '@dynamic-labs/wallet-connector-core';
2
+ import { GetCoinbaseClient, GetCoinbaseProviderOpts } from './types';
3
+ export declare const clearClientVariables: () => void;
4
+ export declare const getCoinbaseClient: GetCoinbaseClient;
4
5
  export declare const killCoinbaseSession: () => Promise<void>;
5
- export declare const fetchPublicAddress: (coinbaseProviderOpts: GetCoinbaseProviderOpts, opts?: FetchPublicAddressOpts) => Promise<string | undefined>;
6
+ export declare const getAddress: (coinbaseProviderOpts: GetCoinbaseProviderOpts, opts?: GetAddressOpts) => Promise<string | undefined>;
6
7
  export declare const signMessage: (coinbaseProviderOpts: GetCoinbaseProviderOpts, messageToSign: string) => Promise<string | undefined>;
@@ -6,11 +6,15 @@ import { INFURA_ID } from '../../constants.js';
6
6
 
7
7
  const jsonRpcUrl = `https://mainnet.infura.io/v3/${INFURA_ID}`;
8
8
  const chainId = 1;
9
+ // storing a reference to the coinbase sdk so that we can call getQrUrl on it when we need a fresh qr code
10
+ let coinbaseSdk;
11
+ // storing a reference to the coinbase provider because the provider methods work better when
12
+ // they are called on the same instance
9
13
  let coinbaseProvider;
10
- const getCoinbaseProvider = ({ opts: { appLogoUrl, appName = '', evmNetworks = [] } = {}, }) => {
14
+ const getCoinbaseClient = ({ opts: { appLogoUrl, appName = '', evmNetworks = [] } = {}, }) => {
11
15
  var _a, _b;
12
16
  if (!coinbaseProvider) {
13
- const coinbaseWalletSDK = new CoinbaseWalletSDK({
17
+ coinbaseSdk = new CoinbaseWalletSDK({
14
18
  appLogoUrl,
15
19
  appName,
16
20
  enableMobileWalletLink: true,
@@ -21,21 +25,17 @@ const getCoinbaseProvider = ({ opts: { appLogoUrl, appName = '', evmNetworks = [
21
25
  const rpcUrl = ((_a = network === null || network === void 0 ? void 0 : network.privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a[0]) ||
22
26
  ((_b = network === null || network === void 0 ? void 0 : network.rpcUrls) === null || _b === void 0 ? void 0 : _b[0]) ||
23
27
  jsonRpcUrl;
24
- coinbaseProvider = coinbaseWalletSDK.makeWeb3Provider(rpcUrl, chainId);
28
+ coinbaseProvider = coinbaseSdk.makeWeb3Provider(rpcUrl, chainId);
25
29
  }
26
- return coinbaseProvider;
30
+ return { provider: coinbaseProvider, sdk: coinbaseSdk };
27
31
  };
28
32
  const killCoinbaseSession = () => __awaiter(void 0, void 0, void 0, function* () {
29
33
  yield (coinbaseProvider === null || coinbaseProvider === void 0 ? void 0 : coinbaseProvider.close());
30
- coinbaseProvider === null || coinbaseProvider === void 0 ? void 0 : coinbaseProvider.disconnect();
31
- // We needed to remove the provider, because after a disconnect the initial settings that
32
- // were set in the SDK reset, and they need to be initialized again.
33
- coinbaseProvider = undefined;
34
34
  });
35
- const fetchPublicAddress = (coinbaseProviderOpts, opts) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const getAddress = (coinbaseProviderOpts, opts) => __awaiter(void 0, void 0, void 0, function* () {
36
36
  var _a;
37
- const provider = getCoinbaseProvider({ opts: coinbaseProviderOpts });
38
- const { qrUrl } = provider;
37
+ const { provider, sdk } = getCoinbaseClient({ opts: coinbaseProviderOpts });
38
+ const qrUrl = sdk.getQrUrl();
39
39
  if (!qrUrl) {
40
40
  throw new DynamicError('no qr url available');
41
41
  }
@@ -46,7 +46,7 @@ const fetchPublicAddress = (coinbaseProviderOpts, opts) => __awaiter(void 0, voi
46
46
  return address;
47
47
  });
48
48
  const signMessage = (coinbaseProviderOpts, messageToSign) => __awaiter(void 0, void 0, void 0, function* () {
49
- const provider = getCoinbaseProvider({ opts: coinbaseProviderOpts });
49
+ const { provider } = getCoinbaseClient({ opts: coinbaseProviderOpts });
50
50
  const [address] = yield provider.request({
51
51
  method: 'eth_requestAccounts',
52
52
  });
@@ -62,4 +62,4 @@ const signMessage = (coinbaseProviderOpts, messageToSign) => __awaiter(void 0, v
62
62
  }
63
63
  });
64
64
 
65
- export { fetchPublicAddress, getCoinbaseProvider, killCoinbaseSession, signMessage };
65
+ export { getAddress, getCoinbaseClient, killCoinbaseSession, signMessage };
@@ -1,4 +1,4 @@
1
- import { CoinbaseWalletProvider } from '@coinbase/wallet-sdk';
1
+ import CoinbaseWalletSDK, { CoinbaseWalletProvider } from '@coinbase/wallet-sdk';
2
2
  import { GenericNetwork } from '@dynamic-labs/types';
3
3
  export type GetCoinbaseProviderOpts = {
4
4
  appLogoUrl?: string;
@@ -8,7 +8,10 @@ export type GetCoinbaseProviderOpts = {
8
8
  export type CoinbaseProviderHandlerOpts = {
9
9
  onDisconnect(): Promise<void>;
10
10
  };
11
- export type GetCoinbaseProvider = (args: {
11
+ export type GetCoinbaseClient = (args: {
12
12
  handlers?: CoinbaseProviderHandlerOpts;
13
13
  opts?: GetCoinbaseProviderOpts;
14
- }) => CoinbaseWalletProvider;
14
+ }) => {
15
+ provider: CoinbaseWalletProvider;
16
+ sdk: CoinbaseWalletSDK;
17
+ };
@@ -5,14 +5,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _tslib = require('../../_virtual/_tslib.cjs');
6
6
  var viem = require('viem');
7
7
  var walletBook = require('@dynamic-labs/wallet-book');
8
- require('../polyfills.cjs');
9
- require('@dynamic-labs/turnkey');
10
- require('@dynamic-labs/utils');
8
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
11
9
  var ethProviderHelper = require('../ethProviderHelper.cjs');
12
10
  var EthWalletConnector = require('../EthWalletConnector.cjs');
13
- require('../walletConnect/walletConnect.cjs');
14
11
  var client = require('./client/client.cjs');
15
- require('@dynamic-labs/rpc-provider-ethereum');
16
12
 
17
13
  class Coinbase extends EthWalletConnector.EthWalletConnector {
18
14
  constructor(_a) {
@@ -31,10 +27,14 @@ class Coinbase extends EthWalletConnector.EthWalletConnector {
31
27
  const wallet = walletBook.findWalletBookWallet(this.walletBook, this.key);
32
28
  if (!wallet)
33
29
  return;
34
- this.ethProviderHelper = new ethProviderHelper.EthProviderHelper(wallet);
30
+ this.ethProviderHelper = new ethProviderHelper.EthProviderHelper(wallet, this);
35
31
  }
36
32
  setupEventListeners() {
37
33
  var _a, _b, _c;
34
+ if (!this.isInstalledOnBrowser()) {
35
+ this.setupCoinbaseProviderEventListeners();
36
+ return;
37
+ }
38
38
  const provider = (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider();
39
39
  if (!provider) {
40
40
  return;
@@ -44,33 +44,33 @@ class Coinbase extends EthWalletConnector.EthWalletConnector {
44
44
  return;
45
45
  this.teardownEventListeners = setupResponse.tearDownEventListeners;
46
46
  }
47
- getWalletClient() {
47
+ getWalletClient(chainId) {
48
48
  var _a;
49
49
  if (this.isInstalledOnBrowser()) {
50
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findWalletClient();
50
+ return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findWalletClient(chainId);
51
51
  }
52
52
  return viem.createWalletClient({
53
- transport: viem.custom(client.getCoinbaseProvider({
53
+ transport: viem.custom(client.getCoinbaseClient({
54
54
  opts: this.coinbaseProviderOpts,
55
- })),
55
+ }).provider),
56
56
  });
57
57
  }
58
58
  isInstalledOnBrowser() {
59
59
  var _a;
60
60
  return ((_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider()) !== undefined;
61
61
  }
62
- fetchPublicAddress(opts) {
63
- var _a;
62
+ getAddress(opts) {
64
63
  return _tslib.__awaiter(this, void 0, void 0, function* () {
64
+ var _a;
65
65
  if (this.isInstalledOnBrowser()) {
66
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.fetchPublicAddress();
66
+ return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.getAddress();
67
67
  }
68
- return client.fetchPublicAddress(this.coinbaseProviderOpts, opts);
68
+ return client.getAddress(this.coinbaseProviderOpts, opts);
69
69
  });
70
70
  }
71
71
  signMessage(messageToSign) {
72
- var _a;
73
72
  return _tslib.__awaiter(this, void 0, void 0, function* () {
73
+ var _a;
74
74
  if (this.isInstalledOnBrowser()) {
75
75
  return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.signMessage(messageToSign);
76
76
  }
@@ -84,6 +84,23 @@ class Coinbase extends EthWalletConnector.EthWalletConnector {
84
84
  client.killCoinbaseSession();
85
85
  });
86
86
  }
87
+ setupCoinbaseProviderEventListeners() {
88
+ const { provider } = client.getCoinbaseClient({
89
+ opts: this.coinbaseProviderOpts,
90
+ });
91
+ if (!provider) {
92
+ return;
93
+ }
94
+ const { handleAccountChange, handleChainChange, handleDisconnect } = walletConnectorCore.eventListenerHandlers(this);
95
+ provider.on('accountsChanged', handleAccountChange);
96
+ provider.on('chainChanged', handleChainChange);
97
+ provider.on('disconnect', handleDisconnect);
98
+ this.teardownEventListeners = () => {
99
+ provider.removeListener('accountsChanged', handleAccountChange);
100
+ provider.removeListener('chainChanged', handleChainChange);
101
+ provider.removeListener('disconnect', handleDisconnect);
102
+ };
103
+ }
87
104
  }
88
105
 
89
106
  exports.Coinbase = Coinbase;
@@ -1,5 +1,5 @@
1
- import { Chain, FetchPublicAddressOpts } from '@dynamic-labs/wallet-connector-core';
2
- import { EthProviderHelper } from '..';
1
+ import { Chain, GetAddressOpts } from '@dynamic-labs/wallet-connector-core';
2
+ import { EthProviderHelper } from '../ethProviderHelper';
3
3
  import { EthWalletConnector, EthWalletConnectorOpts } from '../EthWalletConnector';
4
4
  type CoinbaseOpts = EthWalletConnectorOpts & {
5
5
  appLogoUrl?: string;
@@ -15,7 +15,7 @@ export declare class Coinbase extends EthWalletConnector {
15
15
  ethProviderHelper: EthProviderHelper | undefined;
16
16
  constructor({ appName, appLogoUrl, evmNetworks, ...props }: CoinbaseOpts);
17
17
  setupEventListeners(): void;
18
- getWalletClient(): {
18
+ getWalletClient(chainId?: string): {
19
19
  account: import("viem").Account | undefined;
20
20
  batch?: {
21
21
  multicall?: boolean | {
@@ -531,8 +531,9 @@ export declare class Coinbase extends EthWalletConnector {
531
531
  } & Partial<Pick<import("viem").PublicActions<import("viem").CustomTransport, undefined, undefined>, "getChainId" | "prepareTransactionRequest" | "sendRawTransaction" | "call" | "createContractEventFilter" | "createEventFilter" | "estimateContractGas" | "estimateGas" | "getBlock" | "getBlockNumber" | "getContractEvents" | "getEnsText" | "getFilterChanges" | "getGasPrice" | "getLogs" | "getTransaction" | "getTransactionCount" | "getTransactionReceipt" | "readContract" | "simulateContract" | "uninstallFilter" | "watchBlockNumber" | "watchContractEvent"> & Pick<import("viem").WalletActions<undefined, undefined>, "sendTransaction" | "writeContract">>>(fn: (client: import("viem").Client<import("viem").CustomTransport, undefined, undefined, import("viem").WalletRpcSchema, import("viem").WalletActions<undefined, undefined>>) => client_1) => import("viem").Client<import("viem").CustomTransport, undefined, undefined, import("viem").WalletRpcSchema, { [K_1 in keyof client_1]: client_1[K_1]; } & import("viem").WalletActions<undefined, undefined>>;
532
532
  } | undefined;
533
533
  isInstalledOnBrowser(): boolean;
534
- fetchPublicAddress(opts?: FetchPublicAddressOpts): Promise<string | undefined>;
534
+ getAddress(opts?: GetAddressOpts): Promise<string | undefined>;
535
535
  signMessage(messageToSign: string): Promise<string | undefined>;
536
536
  endSession(): Promise<void>;
537
+ private setupCoinbaseProviderEventListeners;
537
538
  }
538
539
  export {};
@@ -1,14 +1,10 @@
1
1
  import { __rest, __awaiter } from '../../_virtual/_tslib.js';
2
2
  import { createWalletClient, custom } from 'viem';
3
3
  import { findWalletBookWallet } from '@dynamic-labs/wallet-book';
4
- import '../polyfills.js';
5
- import '@dynamic-labs/turnkey';
6
- import '@dynamic-labs/utils';
4
+ import { eventListenerHandlers } from '@dynamic-labs/wallet-connector-core';
7
5
  import { EthProviderHelper } from '../ethProviderHelper.js';
8
6
  import { EthWalletConnector } from '../EthWalletConnector.js';
9
- import '../walletConnect/walletConnect.js';
10
- import { getCoinbaseProvider, fetchPublicAddress, signMessage, killCoinbaseSession } from './client/client.js';
11
- import '@dynamic-labs/rpc-provider-ethereum';
7
+ import { getCoinbaseClient, getAddress, signMessage, killCoinbaseSession } from './client/client.js';
12
8
 
13
9
  class Coinbase extends EthWalletConnector {
14
10
  constructor(_a) {
@@ -27,10 +23,14 @@ class Coinbase extends EthWalletConnector {
27
23
  const wallet = findWalletBookWallet(this.walletBook, this.key);
28
24
  if (!wallet)
29
25
  return;
30
- this.ethProviderHelper = new EthProviderHelper(wallet);
26
+ this.ethProviderHelper = new EthProviderHelper(wallet, this);
31
27
  }
32
28
  setupEventListeners() {
33
29
  var _a, _b, _c;
30
+ if (!this.isInstalledOnBrowser()) {
31
+ this.setupCoinbaseProviderEventListeners();
32
+ return;
33
+ }
34
34
  const provider = (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider();
35
35
  if (!provider) {
36
36
  return;
@@ -40,33 +40,33 @@ class Coinbase extends EthWalletConnector {
40
40
  return;
41
41
  this.teardownEventListeners = setupResponse.tearDownEventListeners;
42
42
  }
43
- getWalletClient() {
43
+ getWalletClient(chainId) {
44
44
  var _a;
45
45
  if (this.isInstalledOnBrowser()) {
46
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findWalletClient();
46
+ return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findWalletClient(chainId);
47
47
  }
48
48
  return createWalletClient({
49
- transport: custom(getCoinbaseProvider({
49
+ transport: custom(getCoinbaseClient({
50
50
  opts: this.coinbaseProviderOpts,
51
- })),
51
+ }).provider),
52
52
  });
53
53
  }
54
54
  isInstalledOnBrowser() {
55
55
  var _a;
56
56
  return ((_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider()) !== undefined;
57
57
  }
58
- fetchPublicAddress(opts) {
59
- var _a;
58
+ getAddress(opts) {
60
59
  return __awaiter(this, void 0, void 0, function* () {
60
+ var _a;
61
61
  if (this.isInstalledOnBrowser()) {
62
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.fetchPublicAddress();
62
+ return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.getAddress();
63
63
  }
64
- return fetchPublicAddress(this.coinbaseProviderOpts, opts);
64
+ return getAddress(this.coinbaseProviderOpts, opts);
65
65
  });
66
66
  }
67
67
  signMessage(messageToSign) {
68
- var _a;
69
68
  return __awaiter(this, void 0, void 0, function* () {
69
+ var _a;
70
70
  if (this.isInstalledOnBrowser()) {
71
71
  return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.signMessage(messageToSign);
72
72
  }
@@ -80,6 +80,23 @@ class Coinbase extends EthWalletConnector {
80
80
  killCoinbaseSession();
81
81
  });
82
82
  }
83
+ setupCoinbaseProviderEventListeners() {
84
+ const { provider } = getCoinbaseClient({
85
+ opts: this.coinbaseProviderOpts,
86
+ });
87
+ if (!provider) {
88
+ return;
89
+ }
90
+ const { handleAccountChange, handleChainChange, handleDisconnect } = eventListenerHandlers(this);
91
+ provider.on('accountsChanged', handleAccountChange);
92
+ provider.on('chainChanged', handleChainChange);
93
+ provider.on('disconnect', handleDisconnect);
94
+ this.teardownEventListeners = () => {
95
+ provider.removeListener('accountsChanged', handleAccountChange);
96
+ provider.removeListener('chainChanged', handleChainChange);
97
+ provider.removeListener('disconnect', handleDisconnect);
98
+ };
99
+ }
83
100
  }
84
101
 
85
102
  export { Coinbase };
@@ -6,11 +6,13 @@ var _tslib = require('../_virtual/_tslib.cjs');
6
6
  var viem = require('viem');
7
7
  var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
8
8
  var utils = require('@dynamic-labs/utils');
9
+ var viemUtils = require('@dynamic-labs/viem-utils');
9
10
  var eip6963Provider = require('./eip6963Provider.cjs');
10
11
 
11
12
  class EthProviderHelper {
12
- constructor(wallet) {
13
+ constructor(wallet, connector) {
13
14
  this.wallet = wallet;
15
+ this.connector = connector;
14
16
  }
15
17
  getInstalledProvider() {
16
18
  const eip6963Config = this.getEip6963Config();
@@ -75,29 +77,32 @@ class EthProviderHelper {
75
77
  findProvider() {
76
78
  return this.getInstalledProvider();
77
79
  }
78
- findWalletClient() {
80
+ findWalletClient(chainId) {
79
81
  const provider = this.findProvider();
80
82
  if (!provider) {
81
83
  return undefined;
82
84
  }
83
85
  return viem.createWalletClient({
86
+ account: this.connector.getActiveAccount(),
87
+ chain: chainId ? viemUtils.chainsMap[chainId] : undefined,
84
88
  transport: viem.custom(provider),
85
89
  });
86
90
  }
87
- fetchPublicAddress() {
91
+ getAddress() {
88
92
  return _tslib.__awaiter(this, void 0, void 0, function* () {
89
93
  const client = this.findWalletClient();
90
94
  if (!client) {
91
95
  return Promise.resolve(undefined);
92
96
  }
93
- return this.fetchPublicAddressWithProvider(client);
97
+ return this.getAddressWithProvider(client);
94
98
  });
95
99
  }
96
- fetchPublicAddressWithProvider(client) {
100
+ getAddressWithProvider(client) {
97
101
  return _tslib.__awaiter(this, void 0, void 0, function* () {
98
102
  try {
99
103
  const [lowercaseAddress] = yield client.requestAddresses();
100
104
  const publicAddress = viem.getAddress(lowercaseAddress);
105
+ this.connector.setActiveAccount(publicAddress);
101
106
  return publicAddress;
102
107
  }
103
108
  catch (err) {
@@ -108,7 +113,7 @@ class EthProviderHelper {
108
113
  }
109
114
  signMessage(messageToSign) {
110
115
  return _tslib.__awaiter(this, void 0, void 0, function* () {
111
- const walletAddress = yield this.fetchPublicAddress();
116
+ const walletAddress = yield this.getAddress();
112
117
  if (!walletAddress) {
113
118
  return Promise.resolve(undefined);
114
119
  }
@@ -2,9 +2,11 @@ import { Hex, WalletClient } from 'viem';
2
2
  import { ProviderCondition, WalletConnector } from '@dynamic-labs/wallet-connector-core';
3
3
  import { WalletSchema } from '@dynamic-labs/wallet-book';
4
4
  import { IEthereum, ProviderFlag } from './types';
5
+ import { EthWalletConnector } from '.';
5
6
  export declare class EthProviderHelper {
6
7
  private wallet;
7
- constructor(wallet: WalletSchema);
8
+ private connector;
9
+ constructor(wallet: WalletSchema, connector: EthWalletConnector);
8
10
  getInstalledProvider(): IEthereum | undefined;
9
11
  getEip6963Config(): {
10
12
  rdns: string;
@@ -27,9 +29,9 @@ export declare class EthProviderHelper {
27
29
  eip6963ProviderLookup(rdns: string): IEthereum | undefined;
28
30
  isInstalledHelper(): boolean;
29
31
  findProvider(): IEthereum | undefined;
30
- findWalletClient(): WalletClient | undefined;
31
- fetchPublicAddress(): Promise<Hex | undefined>;
32
- fetchPublicAddressWithProvider(client: WalletClient): Promise<Hex | undefined>;
32
+ findWalletClient(chainId?: string): WalletClient | undefined;
33
+ getAddress(): Promise<Hex | undefined>;
34
+ getAddressWithProvider(client: WalletClient): Promise<Hex | undefined>;
33
35
  signMessage(messageToSign: string): Promise<string | undefined>;
34
36
  _setupEventListeners(walletConnector: WalletConnector): {
35
37
  tearDownEventListeners: () => void;
@@ -2,11 +2,13 @@ import { __awaiter } from '../_virtual/_tslib.js';
2
2
  import { createWalletClient, custom, getAddress } from 'viem';
3
3
  import { ProviderLookup, eventListenerHandlers, logger } from '@dynamic-labs/wallet-connector-core';
4
4
  import { getProvidersFromWindow } from '@dynamic-labs/utils';
5
+ import { chainsMap } from '@dynamic-labs/viem-utils';
5
6
  import { Eip6963ProviderSingleton } from './eip6963Provider.js';
6
7
 
7
8
  class EthProviderHelper {
8
- constructor(wallet) {
9
+ constructor(wallet, connector) {
9
10
  this.wallet = wallet;
11
+ this.connector = connector;
10
12
  }
11
13
  getInstalledProvider() {
12
14
  const eip6963Config = this.getEip6963Config();
@@ -71,29 +73,32 @@ class EthProviderHelper {
71
73
  findProvider() {
72
74
  return this.getInstalledProvider();
73
75
  }
74
- findWalletClient() {
76
+ findWalletClient(chainId) {
75
77
  const provider = this.findProvider();
76
78
  if (!provider) {
77
79
  return undefined;
78
80
  }
79
81
  return createWalletClient({
82
+ account: this.connector.getActiveAccount(),
83
+ chain: chainId ? chainsMap[chainId] : undefined,
80
84
  transport: custom(provider),
81
85
  });
82
86
  }
83
- fetchPublicAddress() {
87
+ getAddress() {
84
88
  return __awaiter(this, void 0, void 0, function* () {
85
89
  const client = this.findWalletClient();
86
90
  if (!client) {
87
91
  return Promise.resolve(undefined);
88
92
  }
89
- return this.fetchPublicAddressWithProvider(client);
93
+ return this.getAddressWithProvider(client);
90
94
  });
91
95
  }
92
- fetchPublicAddressWithProvider(client) {
96
+ getAddressWithProvider(client) {
93
97
  return __awaiter(this, void 0, void 0, function* () {
94
98
  try {
95
99
  const [lowercaseAddress] = yield client.requestAddresses();
96
100
  const publicAddress = getAddress(lowercaseAddress);
101
+ this.connector.setActiveAccount(publicAddress);
97
102
  return publicAddress;
98
103
  }
99
104
  catch (err) {
@@ -104,7 +109,7 @@ class EthProviderHelper {
104
109
  }
105
110
  signMessage(messageToSign) {
106
111
  return __awaiter(this, void 0, void 0, function* () {
107
- const walletAddress = yield this.fetchPublicAddress();
112
+ const walletAddress = yield this.getAddress();
108
113
  if (!walletAddress) {
109
114
  return Promise.resolve(undefined);
110
115
  }