@dynamic-labs/ethereum 0.0.0-exp20240808.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.
- package/CHANGELOG.md +3746 -0
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/_virtual/_tslib.cjs +49 -0
- package/_virtual/_tslib.js +44 -0
- package/package.json +46 -0
- package/src/EthWalletConnector.cjs +234 -0
- package/src/EthWalletConnector.d.ts +3088 -0
- package/src/EthWalletConnector.js +230 -0
- package/src/coinbase/coinbase.cjs +98 -0
- package/src/coinbase/coinbase.d.ts +22 -0
- package/src/coinbase/coinbase.js +94 -0
- package/src/coinbase/helpers.cjs +38 -0
- package/src/coinbase/helpers.d.ts +3 -0
- package/src/coinbase/helpers.js +34 -0
- package/src/coinbase/index.d.ts +3 -0
- package/src/coinbase/types.d.ts +18 -0
- package/src/constants.cjs +8 -0
- package/src/constants.d.ts +1 -0
- package/src/constants.js +4 -0
- package/src/ethProviderHelper.cjs +163 -0
- package/src/ethProviderHelper.d.ts +43 -0
- package/src/ethProviderHelper.js +159 -0
- package/src/index.cjs +49 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +35 -0
- package/src/injected/ExodusEvm.cjs +20 -0
- package/src/injected/ExodusEvm.d.ts +8 -0
- package/src/injected/ExodusEvm.js +16 -0
- package/src/injected/FallbackEvmConnector.cjs +22 -0
- package/src/injected/FallbackEvmConnector.d.ts +9 -0
- package/src/injected/FallbackEvmConnector.js +18 -0
- package/src/injected/InjectedWalletBase.cjs +110 -0
- package/src/injected/InjectedWalletBase.d.ts +29 -0
- package/src/injected/InjectedWalletBase.js +108 -0
- package/src/injected/PhantomEvm.cjs +37 -0
- package/src/injected/PhantomEvm.d.ts +8 -0
- package/src/injected/PhantomEvm.js +33 -0
- package/src/injected/UnknownInjected.cjs +21 -0
- package/src/injected/UnknownInjected.d.ts +8 -0
- package/src/injected/UnknownInjected.js +17 -0
- package/src/injected/fetchInjectedWalletConnectors.cjs +67 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +6 -0
- package/src/injected/fetchInjectedWalletConnectors.js +62 -0
- package/src/injected/index.d.ts +5 -0
- package/src/polyfills.cjs +17 -0
- package/src/polyfills.d.ts +1 -0
- package/src/polyfills.js +15 -0
- package/src/types.d.ts +33 -0
- package/src/utils/findEvmNetwork.cjs +21 -0
- package/src/utils/findEvmNetwork.d.ts +6 -0
- package/src/utils/findEvmNetwork.js +17 -0
- package/src/utils/getNameservice.cjs +46 -0
- package/src/utils/getNameservice.d.ts +6 -0
- package/src/utils/getNameservice.js +42 -0
- package/src/utils/index.d.ts +2 -0
- package/src/utils/isEthWalletConnector/index.d.ts +1 -0
- package/src/utils/isEthWalletConnector/isEthWalletConnector.cjs +8 -0
- package/src/utils/isEthWalletConnector/isEthWalletConnector.d.ts +3 -0
- package/src/utils/isEthWalletConnector/isEthWalletConnector.js +4 -0
- package/src/utils/isString.d.ts +1 -0
- package/src/utils/isUnsupportedProviderError/index.d.ts +1 -0
- package/src/utils/isUnsupportedProviderError/isUnsupportedProviderError.d.ts +6 -0
- package/src/utils/last.d.ts +1 -0
- package/src/utils/parseIntSafe.cjs +23 -0
- package/src/utils/parseIntSafe.d.ts +1 -0
- package/src/utils/parseIntSafe.js +19 -0
- package/src/wallet/EthereumWallet.cjs +65 -0
- package/src/wallet/EthereumWallet.d.ts +17 -0
- package/src/wallet/EthereumWallet.js +61 -0
- package/src/wallet/index.d.ts +2 -0
- package/src/wallet/isEthereumWallet.cjs +8 -0
- package/src/wallet/isEthereumWallet.d.ts +3 -0
- package/src/wallet/isEthereumWallet.js +4 -0
- package/src/walletConnect/fetchWalletConnectWallets.cjs +30 -0
- package/src/walletConnect/fetchWalletConnectWallets.d.ts +6 -0
- package/src/walletConnect/fetchWalletConnectWallets.js +25 -0
- package/src/walletConnect/index.d.ts +2 -0
- package/src/walletConnect/walletConnect.cjs +531 -0
- package/src/walletConnect/walletConnect.d.ts +78 -0
- package/src/walletConnect/walletConnect.js +522 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
3
|
+
import { getAddress, formatEther } from 'viem';
|
|
4
|
+
import { toAccount } from 'viem/accounts';
|
|
5
|
+
import { parseEvmNetworks, retryableFn, FALLBACK_UNDEFINED, DynamicError } from '@dynamic-labs/utils';
|
|
6
|
+
import { chainsMap, getOrMapViemChain, createViemUiTransaction } from '@dynamic-labs/viem-utils';
|
|
7
|
+
import { WalletConnectorBase, logger } from '@dynamic-labs/wallet-connector-core';
|
|
8
|
+
import { findEvmNetwork } from './utils/findEvmNetwork.js';
|
|
9
|
+
import { getNameservice } from './utils/getNameservice.js';
|
|
10
|
+
import { EthereumWallet } from './wallet/EthereumWallet.js';
|
|
11
|
+
|
|
12
|
+
class EthWalletConnector extends WalletConnectorBase {
|
|
13
|
+
getPublicClient() {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
var _a, _b;
|
|
16
|
+
const networkId = (_a = (yield this.getNetwork())) !== null && _a !== void 0 ? _a : 1;
|
|
17
|
+
if (this.evmNetworks.length === 0) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const configurations = {
|
|
21
|
+
cosmos: [],
|
|
22
|
+
evm: this.evmNetworks,
|
|
23
|
+
solana: [],
|
|
24
|
+
starknet: undefined,
|
|
25
|
+
};
|
|
26
|
+
if (!this.chainRpcProviders)
|
|
27
|
+
return undefined;
|
|
28
|
+
const providers = this.chainRpcProviders.getProviders(configurations);
|
|
29
|
+
return (_b = this.chainRpcProviders.getEvmProviderByChainId(providers, networkId)) === null || _b === void 0 ? void 0 : _b.provider;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
constructor(props) {
|
|
33
|
+
var _a;
|
|
34
|
+
super(props);
|
|
35
|
+
this.ChainWallet = EthereumWallet;
|
|
36
|
+
this.evmNetworkRpcMap = () => this.evmNetworks.reduce((acc, network) => {
|
|
37
|
+
var _a;
|
|
38
|
+
[acc[network.chainId]] = ((_a = network === null || network === void 0 ? void 0 : network.privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a.length)
|
|
39
|
+
? network.privateCustomerRpcUrls
|
|
40
|
+
: network.rpcUrls;
|
|
41
|
+
return acc;
|
|
42
|
+
}, {});
|
|
43
|
+
this.evmNetworks = parseEvmNetworks(props.evmNetworks);
|
|
44
|
+
this.chainRpcProviders = props.chainRpcProviders;
|
|
45
|
+
(_a = this.chainRpcProviders) === null || _a === void 0 ? void 0 : _a.registerEvmProviders();
|
|
46
|
+
}
|
|
47
|
+
getNetwork() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const provider = this.getWalletClient();
|
|
50
|
+
if (!this.supportsNetworkSwitching || !provider) {
|
|
51
|
+
return Promise.resolve(undefined);
|
|
52
|
+
}
|
|
53
|
+
const chainId = yield retryableFn(provider.getChainId, {
|
|
54
|
+
fallbackValue: FALLBACK_UNDEFINED,
|
|
55
|
+
/**
|
|
56
|
+
* The timeout is set to 1 second because the getChainId method
|
|
57
|
+
* takes around 500ms to resolve on Brave. If the timeout is not set
|
|
58
|
+
* it will use 100ms by default and the method will fail.
|
|
59
|
+
* QNTM-815
|
|
60
|
+
*/
|
|
61
|
+
timeoutMs: 1000,
|
|
62
|
+
});
|
|
63
|
+
this.setActiveChain(chainsMap[chainId]);
|
|
64
|
+
return chainId;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
getNameService(address) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const rpcProvider = yield this.getPublicClient();
|
|
70
|
+
if (!rpcProvider) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
return getNameservice({ address, rpcProvider });
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
isTestnet() {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
var _a, _b, _c;
|
|
79
|
+
return (_c = (_b = (_a = (yield this.getPublicClient())) === null || _a === void 0 ? void 0 : _a.chain) === null || _b === void 0 ? void 0 : _b.testnet) !== null && _c !== void 0 ? _c : false;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
parseAddress(address) {
|
|
83
|
+
try {
|
|
84
|
+
// Ensures the address follows the EIP55 format.
|
|
85
|
+
// see: https://eips.ethereum.org/EIPS/eip-55
|
|
86
|
+
return getAddress(address);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
logger.debug(`Failed to parse EVM address into EIP55 format: error getting checksum, returning default ${address}`, err);
|
|
90
|
+
}
|
|
91
|
+
return address;
|
|
92
|
+
}
|
|
93
|
+
getSigner() {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
return this.getWalletClient();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
getBalance(address) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
const client = yield this.getPublicClient();
|
|
101
|
+
const result = yield (client === null || client === void 0 ? void 0 : client.getBalance({
|
|
102
|
+
address: address,
|
|
103
|
+
}));
|
|
104
|
+
if (!result && result !== BigInt(0))
|
|
105
|
+
return;
|
|
106
|
+
return formatEther(result);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
supportsNetworkSwitching() {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
switchNetwork(_a) {
|
|
113
|
+
return __awaiter(this, arguments, void 0, function* ({ networkName, networkChainId, }) {
|
|
114
|
+
const network = findEvmNetwork({
|
|
115
|
+
chainId: networkChainId,
|
|
116
|
+
name: networkName,
|
|
117
|
+
networks: this.evmNetworks,
|
|
118
|
+
});
|
|
119
|
+
if (!network) {
|
|
120
|
+
throw new DynamicError(`Could not find network mapping for chain ${networkName ? networkName : networkChainId}`);
|
|
121
|
+
}
|
|
122
|
+
if (!this.supportsNetworkSwitching()) {
|
|
123
|
+
throw new DynamicError('Network switching is not supported');
|
|
124
|
+
}
|
|
125
|
+
const provider = this.getWalletClient();
|
|
126
|
+
if (!provider) {
|
|
127
|
+
throw new DynamicError('Provider not found');
|
|
128
|
+
}
|
|
129
|
+
return this.providerSwitchNetwork({ network, provider });
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
getConnectedAccounts() {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const provider = this.getWalletClient();
|
|
135
|
+
if (!provider)
|
|
136
|
+
return [];
|
|
137
|
+
const addresses = yield retryableFn(provider.getAddresses, {
|
|
138
|
+
fallbackValue: [],
|
|
139
|
+
timeoutMs: 500,
|
|
140
|
+
});
|
|
141
|
+
if (addresses.length) {
|
|
142
|
+
this.setActiveAccount(addresses[0]);
|
|
143
|
+
}
|
|
144
|
+
return addresses;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
providerSwitchNetwork(_a) {
|
|
148
|
+
return __awaiter(this, arguments, void 0, function* ({ network, provider, }) {
|
|
149
|
+
var _b, _c, _d, _e;
|
|
150
|
+
const { chainId } = network;
|
|
151
|
+
const currentNetworkId = yield this.getNetwork();
|
|
152
|
+
if (currentNetworkId && currentNetworkId === chainId) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
if (!this.supportsNetworkSwitching()) {
|
|
157
|
+
throw new DynamicError('Network switching is not supported');
|
|
158
|
+
}
|
|
159
|
+
const viemChain = getOrMapViemChain(network);
|
|
160
|
+
yield provider.switchChain(viemChain);
|
|
161
|
+
if (this.key === 'magiceden') {
|
|
162
|
+
const newChainId = yield provider.getChainId();
|
|
163
|
+
this.emit('chainChange', { chain: newChainId.toString() });
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
this.setActiveChain(viemChain);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
// we need to check for unrecognized chain error first because it also contains 'rejected' in message
|
|
171
|
+
if (error.code === 4902 ||
|
|
172
|
+
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Unrecognized chain')) ||
|
|
173
|
+
// https://github.com/MetaMask/metamask-mobile/issues/3312#issuecomment-1065923294
|
|
174
|
+
((_d = (_c = error.data) === null || _c === void 0 ? void 0 : _c.orginalError) === null || _d === void 0 ? void 0 : _d.code) === 4902) {
|
|
175
|
+
// error code indicates the chain has not been added yet
|
|
176
|
+
// https://docs.metamask.io/guide/rpc-api.html#usage-with-wallet-switchethereumchain
|
|
177
|
+
return this.providerAddNetwork({ network, provider });
|
|
178
|
+
}
|
|
179
|
+
else if (((_e = error.message) === null || _e === void 0 ? void 0 : _e.includes('rejected')) ||
|
|
180
|
+
(typeof error === 'string' && error.includes('rejected'))) {
|
|
181
|
+
throw new DynamicError("User rejected the wallet's request to switch network");
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
providerAddNetwork(_a) {
|
|
190
|
+
return __awaiter(this, arguments, void 0, function* ({ network, provider, }) {
|
|
191
|
+
var _b;
|
|
192
|
+
try {
|
|
193
|
+
return yield provider.addChain({ chain: getOrMapViemChain(network) });
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('rejected')) ||
|
|
197
|
+
(typeof error === 'string' && error.includes('rejected'))) {
|
|
198
|
+
throw new DynamicError("User rejected the wallet's request to add network");
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
setActiveAccount(account) {
|
|
207
|
+
this.activeAccount = account ? toAccount(account) : undefined;
|
|
208
|
+
}
|
|
209
|
+
getActiveAccount() {
|
|
210
|
+
return this.activeAccount;
|
|
211
|
+
}
|
|
212
|
+
setActiveChain(chain) {
|
|
213
|
+
this.activeChain = chain;
|
|
214
|
+
}
|
|
215
|
+
getActiveChain() {
|
|
216
|
+
return this.activeChain;
|
|
217
|
+
}
|
|
218
|
+
createUiTransaction(from) {
|
|
219
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
220
|
+
const walletClient = yield this.getWalletClient();
|
|
221
|
+
const publicClient = yield this.getPublicClient();
|
|
222
|
+
if (!publicClient || !walletClient) {
|
|
223
|
+
throw new DynamicError('No public client available');
|
|
224
|
+
}
|
|
225
|
+
return createViemUiTransaction({ from, publicClient, walletClient });
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { EthWalletConnector };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var viem = require('viem');
|
|
8
|
+
var viemUtils = require('@dynamic-labs/viem-utils');
|
|
9
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
10
|
+
var EthWalletConnector = require('../EthWalletConnector.cjs');
|
|
11
|
+
var helpers = require('./helpers.cjs');
|
|
12
|
+
|
|
13
|
+
class Coinbase extends EthWalletConnector.EthWalletConnector {
|
|
14
|
+
constructor(_a) {
|
|
15
|
+
var { appName, appLogoUrl, evmNetworks, coinbaseWalletPreference } = _a, props = _tslib.__rest(_a, ["appName", "appLogoUrl", "evmNetworks", "coinbaseWalletPreference"]);
|
|
16
|
+
super(Object.assign({ evmNetworks }, props));
|
|
17
|
+
this.name = 'Coinbase';
|
|
18
|
+
this.overrideKey = 'coinbase';
|
|
19
|
+
this.supportedChains = ['EVM', 'ETH'];
|
|
20
|
+
this.connectedChain = 'EVM';
|
|
21
|
+
this.canConnectViaQrCode = false;
|
|
22
|
+
this.canConnectViaCustodialService = !this.isInstalledOnBrowser();
|
|
23
|
+
this.coinbaseProviderOpts = {
|
|
24
|
+
appLogoUrl: appLogoUrl,
|
|
25
|
+
appName: appName,
|
|
26
|
+
evmNetworks: evmNetworks,
|
|
27
|
+
walletPreference: coinbaseWalletPreference,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
get coinbaseProvider() {
|
|
31
|
+
return helpers.getCoinbaseProvider(this.coinbaseProviderOpts);
|
|
32
|
+
}
|
|
33
|
+
getConnectedAccounts() {
|
|
34
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
try {
|
|
36
|
+
const accounts = yield this.coinbaseProvider.request({
|
|
37
|
+
method: 'eth_accounts',
|
|
38
|
+
});
|
|
39
|
+
if (accounts[0]) {
|
|
40
|
+
this.setActiveAccount(accounts[0]);
|
|
41
|
+
}
|
|
42
|
+
return accounts;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
isInstalledOnBrowser() {
|
|
50
|
+
var _a;
|
|
51
|
+
return Boolean((_a = window === null || window === void 0 ? void 0 : window.coinbaseWalletExtension) === null || _a === void 0 ? void 0 : _a.isCoinbaseWallet);
|
|
52
|
+
}
|
|
53
|
+
getAddress() {
|
|
54
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const [address] = yield this.coinbaseProvider.request({
|
|
56
|
+
method: 'eth_requestAccounts',
|
|
57
|
+
});
|
|
58
|
+
this.setActiveAccount(address);
|
|
59
|
+
return address;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
signMessage(messageToSign) {
|
|
63
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const [address] = yield this.coinbaseProvider.request({
|
|
65
|
+
method: 'eth_requestAccounts',
|
|
66
|
+
});
|
|
67
|
+
try {
|
|
68
|
+
return yield this.coinbaseProvider.request({
|
|
69
|
+
method: 'personal_sign',
|
|
70
|
+
params: [viem.toHex(viem.toBytes(messageToSign)), address.toLowerCase()],
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
setupEventListeners() {
|
|
79
|
+
const { handleAccountChange, handleChainChange, handleDisconnect } = walletConnectorCore.eventListenerHandlers(this);
|
|
80
|
+
this.coinbaseProvider.on('accountsChanged', handleAccountChange);
|
|
81
|
+
this.coinbaseProvider.on('chainChanged', handleChainChange);
|
|
82
|
+
this.coinbaseProvider.on('disconnect', handleDisconnect);
|
|
83
|
+
this.teardownEventListeners = () => {
|
|
84
|
+
this.coinbaseProvider.removeListener('accountsChanged', handleAccountChange);
|
|
85
|
+
this.coinbaseProvider.removeListener('chainChanged', handleChainChange);
|
|
86
|
+
this.coinbaseProvider.removeListener('disconnect', handleDisconnect);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
getWalletClient(chainId) {
|
|
90
|
+
return viem.createWalletClient({
|
|
91
|
+
account: this.getActiveAccount(),
|
|
92
|
+
chain: chainId ? viemUtils.chainsMap[chainId] : undefined,
|
|
93
|
+
transport: viem.custom(this.coinbaseProvider),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
exports.Coinbase = Coinbase;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { WalletClient, Account, Transport, Chain as ViemChain } from 'viem';
|
|
2
|
+
import { ProviderInterface } from '@coinbase/wallet-sdk';
|
|
3
|
+
import { Chain } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
import { EthWalletConnector } from '../EthWalletConnector';
|
|
5
|
+
import { CoinbaseOpts, GetCoinbaseProviderOpts } from './types';
|
|
6
|
+
export declare class Coinbase extends EthWalletConnector {
|
|
7
|
+
name: string;
|
|
8
|
+
overrideKey: string;
|
|
9
|
+
supportedChains: Chain[];
|
|
10
|
+
connectedChain: Chain;
|
|
11
|
+
canConnectViaQrCode: boolean;
|
|
12
|
+
coinbaseProviderOpts: GetCoinbaseProviderOpts;
|
|
13
|
+
constructor({ appName, appLogoUrl, evmNetworks, coinbaseWalletPreference, ...props }: CoinbaseOpts);
|
|
14
|
+
get coinbaseProvider(): ProviderInterface;
|
|
15
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
16
|
+
isInstalledOnBrowser(): boolean;
|
|
17
|
+
canConnectViaCustodialService: boolean;
|
|
18
|
+
getAddress(): Promise<string | undefined>;
|
|
19
|
+
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
20
|
+
setupEventListeners(): void;
|
|
21
|
+
getWalletClient(chainId?: string): WalletClient<Transport, ViemChain, Account> | undefined;
|
|
22
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __rest, __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { toHex, toBytes, createWalletClient, custom } from 'viem';
|
|
4
|
+
import { chainsMap } from '@dynamic-labs/viem-utils';
|
|
5
|
+
import { eventListenerHandlers } from '@dynamic-labs/wallet-connector-core';
|
|
6
|
+
import { EthWalletConnector } from '../EthWalletConnector.js';
|
|
7
|
+
import { getCoinbaseProvider } from './helpers.js';
|
|
8
|
+
|
|
9
|
+
class Coinbase extends EthWalletConnector {
|
|
10
|
+
constructor(_a) {
|
|
11
|
+
var { appName, appLogoUrl, evmNetworks, coinbaseWalletPreference } = _a, props = __rest(_a, ["appName", "appLogoUrl", "evmNetworks", "coinbaseWalletPreference"]);
|
|
12
|
+
super(Object.assign({ evmNetworks }, props));
|
|
13
|
+
this.name = 'Coinbase';
|
|
14
|
+
this.overrideKey = 'coinbase';
|
|
15
|
+
this.supportedChains = ['EVM', 'ETH'];
|
|
16
|
+
this.connectedChain = 'EVM';
|
|
17
|
+
this.canConnectViaQrCode = false;
|
|
18
|
+
this.canConnectViaCustodialService = !this.isInstalledOnBrowser();
|
|
19
|
+
this.coinbaseProviderOpts = {
|
|
20
|
+
appLogoUrl: appLogoUrl,
|
|
21
|
+
appName: appName,
|
|
22
|
+
evmNetworks: evmNetworks,
|
|
23
|
+
walletPreference: coinbaseWalletPreference,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
get coinbaseProvider() {
|
|
27
|
+
return getCoinbaseProvider(this.coinbaseProviderOpts);
|
|
28
|
+
}
|
|
29
|
+
getConnectedAccounts() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
try {
|
|
32
|
+
const accounts = yield this.coinbaseProvider.request({
|
|
33
|
+
method: 'eth_accounts',
|
|
34
|
+
});
|
|
35
|
+
if (accounts[0]) {
|
|
36
|
+
this.setActiveAccount(accounts[0]);
|
|
37
|
+
}
|
|
38
|
+
return accounts;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
isInstalledOnBrowser() {
|
|
46
|
+
var _a;
|
|
47
|
+
return Boolean((_a = window === null || window === void 0 ? void 0 : window.coinbaseWalletExtension) === null || _a === void 0 ? void 0 : _a.isCoinbaseWallet);
|
|
48
|
+
}
|
|
49
|
+
getAddress() {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const [address] = yield this.coinbaseProvider.request({
|
|
52
|
+
method: 'eth_requestAccounts',
|
|
53
|
+
});
|
|
54
|
+
this.setActiveAccount(address);
|
|
55
|
+
return address;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
signMessage(messageToSign) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const [address] = yield this.coinbaseProvider.request({
|
|
61
|
+
method: 'eth_requestAccounts',
|
|
62
|
+
});
|
|
63
|
+
try {
|
|
64
|
+
return yield this.coinbaseProvider.request({
|
|
65
|
+
method: 'personal_sign',
|
|
66
|
+
params: [toHex(toBytes(messageToSign)), address.toLowerCase()],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
setupEventListeners() {
|
|
75
|
+
const { handleAccountChange, handleChainChange, handleDisconnect } = eventListenerHandlers(this);
|
|
76
|
+
this.coinbaseProvider.on('accountsChanged', handleAccountChange);
|
|
77
|
+
this.coinbaseProvider.on('chainChanged', handleChainChange);
|
|
78
|
+
this.coinbaseProvider.on('disconnect', handleDisconnect);
|
|
79
|
+
this.teardownEventListeners = () => {
|
|
80
|
+
this.coinbaseProvider.removeListener('accountsChanged', handleAccountChange);
|
|
81
|
+
this.coinbaseProvider.removeListener('chainChanged', handleChainChange);
|
|
82
|
+
this.coinbaseProvider.removeListener('disconnect', handleDisconnect);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
getWalletClient(chainId) {
|
|
86
|
+
return createWalletClient({
|
|
87
|
+
account: this.getActiveAccount(),
|
|
88
|
+
chain: chainId ? chainsMap[chainId] : undefined,
|
|
89
|
+
transport: custom(this.coinbaseProvider),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { Coinbase };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var walletSdk = require('@coinbase/wallet-sdk');
|
|
7
|
+
|
|
8
|
+
// storing a reference to the coinbase provider because the provider methods work better when
|
|
9
|
+
// they are called on the same instance
|
|
10
|
+
let coinbaseProvider;
|
|
11
|
+
const baseSepolia = 84532;
|
|
12
|
+
const baseMainnet = 8453;
|
|
13
|
+
const getCoinbaseProvider = ({ appLogoUrl, appName, evmNetworks, walletPreference, } = {}) => {
|
|
14
|
+
const appChainIds = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.map((network) => Number(network.chainId));
|
|
15
|
+
// just brings base sepolia to the front of the list, because that is treated as the default chain
|
|
16
|
+
const hasBaseSepolia = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.some((network) => network.chainId === baseSepolia);
|
|
17
|
+
if (hasBaseSepolia) {
|
|
18
|
+
appChainIds === null || appChainIds === void 0 ? void 0 : appChainIds.sort((a, b) => a === baseSepolia ? -1 : b === baseSepolia ? 1 : 0);
|
|
19
|
+
}
|
|
20
|
+
// just brings base mainnet to the front, because that that is treated as the default chain
|
|
21
|
+
const hasBaseMainnet = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.some((network) => network.chainId === baseMainnet);
|
|
22
|
+
if (hasBaseMainnet) {
|
|
23
|
+
appChainIds === null || appChainIds === void 0 ? void 0 : appChainIds.sort((a, b) => a === baseMainnet ? -1 : b === baseMainnet ? 1 : 0);
|
|
24
|
+
}
|
|
25
|
+
if (!coinbaseProvider) {
|
|
26
|
+
const coinbaseSdk = new walletSdk.CoinbaseWalletSDK({
|
|
27
|
+
appChainIds,
|
|
28
|
+
appLogoUrl,
|
|
29
|
+
appName,
|
|
30
|
+
});
|
|
31
|
+
coinbaseProvider = coinbaseSdk.makeWeb3Provider({
|
|
32
|
+
options: walletPreference !== null && walletPreference !== void 0 ? walletPreference : 'all',
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return coinbaseProvider;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.getCoinbaseProvider = getCoinbaseProvider;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { CoinbaseWalletSDK } from '@coinbase/wallet-sdk';
|
|
3
|
+
|
|
4
|
+
// storing a reference to the coinbase provider because the provider methods work better when
|
|
5
|
+
// they are called on the same instance
|
|
6
|
+
let coinbaseProvider;
|
|
7
|
+
const baseSepolia = 84532;
|
|
8
|
+
const baseMainnet = 8453;
|
|
9
|
+
const getCoinbaseProvider = ({ appLogoUrl, appName, evmNetworks, walletPreference, } = {}) => {
|
|
10
|
+
const appChainIds = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.map((network) => Number(network.chainId));
|
|
11
|
+
// just brings base sepolia to the front of the list, because that is treated as the default chain
|
|
12
|
+
const hasBaseSepolia = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.some((network) => network.chainId === baseSepolia);
|
|
13
|
+
if (hasBaseSepolia) {
|
|
14
|
+
appChainIds === null || appChainIds === void 0 ? void 0 : appChainIds.sort((a, b) => a === baseSepolia ? -1 : b === baseSepolia ? 1 : 0);
|
|
15
|
+
}
|
|
16
|
+
// just brings base mainnet to the front, because that that is treated as the default chain
|
|
17
|
+
const hasBaseMainnet = evmNetworks === null || evmNetworks === void 0 ? void 0 : evmNetworks.some((network) => network.chainId === baseMainnet);
|
|
18
|
+
if (hasBaseMainnet) {
|
|
19
|
+
appChainIds === null || appChainIds === void 0 ? void 0 : appChainIds.sort((a, b) => a === baseMainnet ? -1 : b === baseMainnet ? 1 : 0);
|
|
20
|
+
}
|
|
21
|
+
if (!coinbaseProvider) {
|
|
22
|
+
const coinbaseSdk = new CoinbaseWalletSDK({
|
|
23
|
+
appChainIds,
|
|
24
|
+
appLogoUrl,
|
|
25
|
+
appName,
|
|
26
|
+
});
|
|
27
|
+
coinbaseProvider = coinbaseSdk.makeWeb3Provider({
|
|
28
|
+
options: walletPreference !== null && walletPreference !== void 0 ? walletPreference : 'all',
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return coinbaseProvider;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { getCoinbaseProvider };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ProviderInterface } from '@coinbase/wallet-sdk';
|
|
2
|
+
import { type CoinbaseWalletPreference, type GenericNetwork } from '@dynamic-labs/types';
|
|
3
|
+
import { type EthWalletConnectorOpts } from '../EthWalletConnector';
|
|
4
|
+
export type GetCoinbaseProviderOpts = {
|
|
5
|
+
appLogoUrl?: string;
|
|
6
|
+
appName?: string;
|
|
7
|
+
evmNetworks?: GenericNetwork[];
|
|
8
|
+
walletPreference?: CoinbaseWalletPreference;
|
|
9
|
+
};
|
|
10
|
+
export type CoinbaseProviderHandlerOpts = {
|
|
11
|
+
onDisconnect(): Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
export type GetCoinbaseProvider = (opts?: GetCoinbaseProviderOpts) => ProviderInterface;
|
|
14
|
+
export type CoinbaseOpts = EthWalletConnectorOpts & {
|
|
15
|
+
appLogoUrl?: string;
|
|
16
|
+
appName?: string;
|
|
17
|
+
coinbaseWalletPreference?: CoinbaseWalletPreference;
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const INFURA_ID: "87939db78f824920ada5c872db3e56b8";
|
package/src/constants.js
ADDED