@dynamic-labs/ethereum 2.1.0-alpha.9 → 2.1.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.
@@ -139,7 +139,11 @@ class EthWalletConnector extends WalletConnectorBase {
139
139
  if (!this.supportsNetworkSwitching()) {
140
140
  throw new DynamicError('Network switching is not supported');
141
141
  }
142
- return yield provider.switchChain(getOrMapViemChain(network));
142
+ yield provider.switchChain(getOrMapViemChain(network));
143
+ if (this.key === 'magiceden') {
144
+ const newChainId = yield provider.getChainId();
145
+ this.emit('chainChange', { chain: newChainId.toString() });
146
+ }
143
147
  }
144
148
  catch (error) {
145
149
  // we need to check for unrecognized chain error first because it also contains 'rejected' in message
@@ -5,103 +5,93 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
6
  var _tslib = require('../../_virtual/_tslib.cjs');
7
7
  var viem = require('viem');
8
- var walletBook = require('@dynamic-labs/wallet-book');
9
8
  var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
10
- var ethProviderHelper = require('../ethProviderHelper.cjs');
9
+ var viemUtils = require('@dynamic-labs/viem-utils');
11
10
  var EthWalletConnector = require('../EthWalletConnector.cjs');
12
- var client = require('./client/client.cjs');
11
+ var helpers = require('./helpers.cjs');
13
12
 
14
13
  class Coinbase extends EthWalletConnector.EthWalletConnector {
15
14
  constructor(_a) {
16
- var { appName, appLogoUrl, evmNetworks } = _a, props = _tslib.__rest(_a, ["appName", "appLogoUrl", "evmNetworks"]);
15
+ var { appName, appLogoUrl, evmNetworks, coinbaseWalletPreference } = _a, props = _tslib.__rest(_a, ["appName", "appLogoUrl", "evmNetworks", "coinbaseWalletPreference"]);
17
16
  super(Object.assign({ evmNetworks }, props));
18
17
  this.name = 'Coinbase';
19
18
  this.overrideKey = 'coinbase';
20
19
  this.supportedChains = ['EVM', 'ETH'];
21
20
  this.connectedChain = 'EVM';
22
- this.canConnectViaQrCode = true;
23
- this.coinbaseProviderOpts = {
24
- appLogoUrl: appLogoUrl,
25
- appName: appName,
26
- evmNetworks: evmNetworks,
27
- };
28
- const wallet = walletBook.findWalletBookWallet(this.walletBook, this.key);
29
- if (!wallet)
30
- return;
31
- this.ethProviderHelper = new ethProviderHelper.EthProviderHelper(wallet, this);
32
- }
33
- setupEventListeners() {
34
- var _a, _b, _c;
35
- if (!this.isInstalledOnBrowser()) {
36
- this.setupCoinbaseProviderEventListeners();
37
- return;
38
- }
39
- const provider = (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider();
40
- if (!provider) {
41
- return;
42
- }
43
- const setupResponse = (_c = (_b = this.ethProviderHelper) === null || _b === void 0 ? void 0 : _b._setupEventListeners) === null || _c === void 0 ? void 0 : _c.call(_b, this);
44
- if (!(setupResponse === null || setupResponse === void 0 ? void 0 : setupResponse.tearDownEventListeners))
45
- return;
46
- this.teardownEventListeners = setupResponse.tearDownEventListeners;
21
+ this.canConnectViaQrCode = false;
22
+ this.canConnectViaCustodialService = true;
23
+ this.coinbaseProvider = helpers.getCoinbaseProvider({
24
+ opts: {
25
+ appLogoUrl: appLogoUrl,
26
+ appName: appName,
27
+ evmNetworks: evmNetworks,
28
+ walletPreference: coinbaseWalletPreference,
29
+ },
30
+ });
47
31
  }
48
- getWalletClient(chainId) {
49
- var _a;
50
- if (this.isInstalledOnBrowser()) {
51
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findWalletClient(chainId);
52
- }
53
- return viem.createWalletClient({
54
- transport: viem.custom(client.getCoinbaseClient({
55
- opts: this.coinbaseProviderOpts,
56
- }).provider),
32
+ getConnectedAccounts() {
33
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
34
+ try {
35
+ const accounts = yield this.coinbaseProvider.request({
36
+ method: 'eth_accounts',
37
+ });
38
+ if (accounts[0]) {
39
+ this.setActiveAccount(accounts[0]);
40
+ }
41
+ return accounts;
42
+ }
43
+ catch (error) {
44
+ return [];
45
+ }
57
46
  });
58
47
  }
59
48
  isInstalledOnBrowser() {
60
49
  var _a;
61
- return ((_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.findProvider()) !== undefined;
50
+ return Boolean((_a = window === null || window === void 0 ? void 0 : window.coinbaseWalletExtension) === null || _a === void 0 ? void 0 : _a.isCoinbaseWallet);
62
51
  }
63
- getAddress(opts) {
52
+ getAddress() {
64
53
  return _tslib.__awaiter(this, void 0, void 0, function* () {
65
- var _a;
66
- if (this.isInstalledOnBrowser()) {
67
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.getAddress();
68
- }
69
- return client.getAddress(this.coinbaseProviderOpts, opts);
54
+ const [address] = yield this.coinbaseProvider.request({
55
+ method: 'eth_requestAccounts',
56
+ });
57
+ this.setActiveAccount(address);
58
+ return address;
70
59
  });
71
60
  }
72
61
  signMessage(messageToSign) {
73
62
  return _tslib.__awaiter(this, void 0, void 0, function* () {
74
- var _a;
75
- if (this.isInstalledOnBrowser()) {
76
- return (_a = this.ethProviderHelper) === null || _a === void 0 ? void 0 : _a.signMessage(messageToSign);
63
+ const [address] = yield this.coinbaseProvider.request({
64
+ method: 'eth_requestAccounts',
65
+ });
66
+ try {
67
+ return yield this.coinbaseProvider.request({
68
+ method: 'personal_sign',
69
+ params: [viem.toHex(viem.toBytes(messageToSign)), address.toLowerCase()],
70
+ });
71
+ }
72
+ catch (err) {
73
+ return undefined;
77
74
  }
78
- return client.signMessage(this.coinbaseProviderOpts, messageToSign);
79
- });
80
- }
81
- endSession() {
82
- return _tslib.__awaiter(this, void 0, void 0, function* () {
83
- if (this.isInstalledOnBrowser())
84
- return;
85
- client.killCoinbaseSession();
86
75
  });
87
76
  }
88
- setupCoinbaseProviderEventListeners() {
89
- const { provider } = client.getCoinbaseClient({
90
- opts: this.coinbaseProviderOpts,
91
- });
92
- if (!provider) {
93
- return;
94
- }
77
+ setupEventListeners() {
95
78
  const { handleAccountChange, handleChainChange, handleDisconnect } = walletConnectorCore.eventListenerHandlers(this);
96
- provider.on('accountsChanged', handleAccountChange);
97
- provider.on('chainChanged', handleChainChange);
98
- provider.on('disconnect', handleDisconnect);
79
+ this.coinbaseProvider.on('accountsChanged', handleAccountChange);
80
+ this.coinbaseProvider.on('chainChanged', handleChainChange);
81
+ this.coinbaseProvider.on('disconnect', handleDisconnect);
99
82
  this.teardownEventListeners = () => {
100
- provider.removeListener('accountsChanged', handleAccountChange);
101
- provider.removeListener('chainChanged', handleChainChange);
102
- provider.removeListener('disconnect', handleDisconnect);
83
+ this.coinbaseProvider.removeListener('accountsChanged', handleAccountChange);
84
+ this.coinbaseProvider.removeListener('chainChanged', handleChainChange);
85
+ this.coinbaseProvider.removeListener('disconnect', handleDisconnect);
103
86
  };
104
87
  }
88
+ getWalletClient(chainId) {
89
+ return viem.createWalletClient({
90
+ account: this.getActiveAccount(),
91
+ chain: chainId ? viemUtils.chainsMap[chainId] : undefined,
92
+ transport: viem.custom(this.coinbaseProvider),
93
+ });
94
+ }
105
95
  }
106
96
 
107
97
  exports.Coinbase = Coinbase;