-
Notifications
You must be signed in to change notification settings - Fork 1
/
web3.connector.ts
395 lines (373 loc) · 11.3 KB
/
web3.connector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { inject, injectable } from 'inversify';
import { BrowserProvider, ethers } from 'ethers';
import {
EthProviders,
EthProvidersSchema,
PROVIDER_ERROR_CODES,
TYPES,
WEB3_EVENTS,
} from '@akashaorg/typings/lib/sdk';
import Logging from '../logging';
import EventBus from './event-bus';
import pino from 'pino';
import { createFormattedValue } from '../helpers/observable';
import { validate } from './validator';
import { z } from 'zod';
import { createWeb3Modal, defaultConfig } from '@web3modal/ethers';
import type { Web3Modal } from '@web3modal/ethers/dist/types/src/client';
import AWF_Config from './config';
@injectable()
class Web3Connector {
#logFactory: Logging;
#log: pino.Logger;
#web3Instance: ethers.BrowserProvider | undefined | null;
#globalChannel: EventBus;
#wallet: ethers.Wallet | null;
#w3modal: Web3Modal;
#currentProviderType: string | undefined | null;
readonly network = 'sepolia';
#networkId = '0xaa36a7';
// mapping for network name and ids
readonly networkId = Object.freeze({
mainnet: 1,
ropsten: 3,
rinkeby: 4,
goerli: 5,
kovan: 42,
sepolia: 11155111,
});
private _config: AWF_Config;
/*
* Web3Connector constructor
*
* @param logFactory - Logging factory to create logger
* @param globalChannel - EventBus for emitting events
*
* Initializes:
* - Logger
* - EventBus
* - Web3Modal configuration
* - Project ID (required)
* - Chain configuration
* - Theming
* - Privacy and terms URLs
*
* Creates Web3Modal instance
* Registers wallet change event listeners
*
* Throws error if WALLETCONNECT_PROJECT_ID env var not set
*/
constructor(
@inject(TYPES.Log) logFactory: Logging,
@inject(TYPES.EventBus) globalChannel: EventBus,
@inject(TYPES.Config) config: AWF_Config,
) {
this.#logFactory = logFactory;
this.#log = this.#logFactory.create('Web3Connector');
this.#globalChannel = globalChannel;
this.#web3Instance = null;
this.#wallet = null;
this._config = config;
const projectId = this._config.getOption('wallet_connect_project_id');
if (!projectId) {
throw new Error('WALLETCONNECT_PROJECT_ID is not set');
}
const chains = [
{
chainId: this.networkId.sepolia,
name: 'Ethereum',
currency: 'ETH',
explorerUrl: 'https://sepolia.etherscan.io/',
rpcUrl: 'https://rpc2.sepolia.org',
},
];
const ethersConfig = defaultConfig({
metadata: {
name: 'AKASHA World',
description: 'AKASHA Web3Modal',
url: 'https://akasha.world',
icons: ['https://avatars.githubusercontent.com/u/9638191'],
},
defaultChainId: this.networkId.sepolia,
rpcUrl: 'https://rpc2.sepolia.org',
enableCoinbase: true,
});
this.#w3modal = createWeb3Modal({
ethersConfig,
projectId,
chains,
themeMode: 'light',
themeVariables: {
'--w3m-font-family': 'Inter, Content-font, Roboto, sans-serif',
'--w3m-color-mix': '#7222d2',
'--w3m-accent': '#4e71ff',
},
privacyPolicyUrl: 'https://akasha.org/privacy-policy/',
termsConditionsUrl: 'https://akasha.org/legal/',
});
this.#_registerWalletChangeEvents();
}
/*
* Connect to a web3 wallet provider
*
* @returns {Promise<{connected: boolean, unsubscribe?: () => void}>}
* - Returns a promise that resolves to an object indicating if connected
* - The object contains:
* - connected: boolean - Whether successfully connected or not
* - unsubscribe: () => void - Optional function to unsubscribe from provider changes
*
* The method first checks if already connected.
* If not connected:
* - Opens the Web3Modal connect modal view
* - Subscribes to provider changes
* - When provider, isConnected and address is set, resolves promise
*
* If already connected, resolves returning current connected state.
*/
async connect(): Promise<{ connected: boolean; unsubscribe?: () => void }> {
if (!this.#w3modal.getIsConnected()) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const unsubscribe = this.#w3modal.subscribeProvider(state => {
if (state.provider && state.isConnected && state.address) {
resolve({ connected: true, unsubscribe });
}
});
await this.#w3modal.open({ view: 'Connect' });
});
}
return { connected: this.#w3modal.getIsConnected() };
}
/*
* Get the current Web3Modal theme
*/
getCurrentTheme() {
return this.#w3modal.getThemeMode();
}
/*
* Toggle the Web3Modal dark theme
*
* @param {boolean} enable - Optional flag to force enable dark theme
*
* If enable passed:
* - Enables dark theme
*
* If no enable passed:
* - Toggles dark on if currently light
* - Toggles dark off if currently dark
*/
toggleDarkTheme(enable?: boolean) {
const themeMode = this.#w3modal.getThemeMode();
if (enable || themeMode !== 'dark') {
this.#w3modal.setThemeMode('dark');
} else {
this.#w3modal.setThemeMode('light');
}
}
/*
* Register event listeners for wallet provider changes
*
* Subscribes to provider changes from Web3Modal:
* - On provider connected:
* - Set web3 instance from Web3Modal provider
* - Set current provider type
* - Emit CONNECTED event with provider type
* - Register web3 provider change event listeners
*
* - On provider disconnected:
* - Remove web3 provider change event listeners
*
* This allows reacting to changes in wallet connection state.
*/
#_registerWalletChangeEvents() {
this.#w3modal.subscribeProvider(event => {
if (event.isConnected) {
const provider = this.#w3modal.getWalletProvider();
if (provider) {
this.#web3Instance = new BrowserProvider(provider);
}
this.#currentProviderType = this.#w3modal?.getWalletProviderType();
this.#globalChannel.next({
data: { providerType: this.#currentProviderType },
event: WEB3_EVENTS.CONNECTED,
});
if (this.#web3Instance) {
// need to find a different way
//this.#_registerProviderChangeEvents(this.#web3Instance);
}
} else {
if (this.#web3Instance) {
this.#web3Instance.removeAllListeners(['accountsChanged', 'chainChanged']);
}
}
});
}
/*
* Get current Web3Connector state
*
* @returns {Object} State object containing:
* - providerType: Current provider type
* - connected: Whether provider is connected
* - address: Current selected address
* - chainId: Current chain ID
*/
get state() {
return {
providerType: this.#currentProviderType,
connected: this.#w3modal.getIsConnected(),
address: this.#w3modal.getAddress(),
chainId: this.#w3modal.getChainId(),
};
}
/**
* Get access to the web3 provider instance
*/
get provider() {
if (this.#web3Instance) {
return this.#web3Instance;
}
}
get walletProvider() {
if (this.#w3modal) {
return this.#w3modal.getWalletProvider();
}
}
/*
* Disconnect from the current web3 provider
*
* Resets the web3 instance and provider type to null.
* Calls Web3Modal disconnect if connected.
* Emits DISCONNECTED event.
*
* @returns {Promise<void>}
* Promise resolves when disconnected.
*/
async disconnect(): Promise<void> {
this.#web3Instance = null;
this.#currentProviderType = null;
if (this.#w3modal && this.#w3modal.getIsConnected()) {
await this.#w3modal?.disconnect();
}
this.#globalChannel.next({
data: undefined,
event: WEB3_EVENTS.DISCONNECTED,
});
}
/**
* Enforce personal_sign method for message signature
* @param message - Human readable string to sign
*/
@validate(z.string().min(3))
async signMessage(message: string) {
const signer = await this.getSigner();
if (signer) {
return signer.signMessage(message);
}
}
async getSigner() {
if (!this.#web3Instance) {
this.#log.warn('Must provide a signer!');
return;
}
return this.#web3Instance.getSigner();
}
/*
* Resolve an address to an ENS name
*/
@validate(z.string().min(20))
async lookupAddress(address: string) {
if (!this.#web3Instance) {
return Promise.resolve({ ens: null });
}
const ens = await this.#web3Instance.lookupAddress(address);
return { ens };
}
getRequiredNetwork() {
if (!this.network) {
throw new Error('The required ethereum network was not set!');
}
return createFormattedValue({ name: this.network, chainId: this.networkId[this.network] });
}
async switchToRequiredNetwork() {
if (this.#web3Instance instanceof ethers.BrowserProvider) {
const result = await this.#web3Instance.send('wallet_switchEthereumChain', [
{ chainId: this.#networkId },
]);
return createFormattedValue(result);
}
const err: Error & {
code?: number;
} = new Error(`Method wallet_switchEthereumChain not supported on the current provider`);
err.code = PROVIDER_ERROR_CODES.WrongNetwork;
throw err;
}
async #_getCurrentAddress() {
return this.#w3modal.getAddress() || null;
}
getCurrentEthAddress() {
return this.#_getCurrentAddress();
}
checkCurrentNetwork() {
return this.#_checkCurrentNetwork();
}
/**
* Ensures that the web3 provider is connected to the specified network
*/
async #_checkCurrentNetwork() {
if (!this.#w3modal.getIsConnected()) {
throw new Error('Must connect first to a provider!');
}
const network = this.#w3modal.getState();
if (network.selectedNetworkId !== this.networkId[this.network]) {
const error: Error & { code?: number } = new Error(
`Please change the ethereum network to ${this.network}!`,
);
error.code = PROVIDER_ERROR_CODES.WrongNetwork;
throw error;
}
this.#log.info(`currently on network: ${network.selectedNetworkId}`);
}
/*
* Register event listeners for provider changes
*
* @param {Object} provider - The web3 provider instance
* @param {Function} provider.on - The provider's 'on' method to add listeners
*
* Registers listeners for:
* - accountsChanged - Emits event with new ETH address
* - chainChanged - Emits event with new chain ID
*
* Throws errors if:
* - Provider not specified
* - Provider does not support .on() method
*/
#_registerProviderChangeEvents(provider: {
on?: (event: string, listener: (...args: unknown[]) => void) => void;
}) {
if (!provider) {
throw new Error('Provider not specified');
}
if (!provider.on) {
throw new Error('Provider does not support on method');
}
provider.on('accountsChanged', ethAddress => {
this.#log.warn(`ethereum address changed ${ethAddress}`);
this.#globalChannel.next({
data: {
ethAddress,
},
event: WEB3_EVENTS.ACCOUNT_CHANGED,
});
});
provider.on('chainChanged', chainId => {
this.#log.warn(`ethereum chain ID changed ${chainId}`);
this.#globalChannel.next({
data: {
chainId,
},
event: WEB3_EVENTS.CHAIN_CHANGED,
});
});
}
}
export default Web3Connector;