@etherplay/connect 0.0.27 → 0.0.28
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/dist/index.d.ts +49 -3169
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -87
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -7
- package/dist/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +186 -136
- package/src/utils.ts +0 -9
- package/src/provider.ts +0 -100
package/src/index.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import type {AlchemyMechanism, OriginAccount} from '@etherplay/alchemy';
|
|
2
|
+
import type {WalletConnector, WalletHandle, WalletProvider} from '@etherplay/wallet-connector';
|
|
3
|
+
import {EthereumWalletConnector, type UnderlyingEthereumProvider} from '@etherplay/wallet-connector-ethereum';
|
|
2
4
|
import {writable} from 'svelte/store';
|
|
3
5
|
import {createPopupLauncher, type PopupPromise} from './popup.js';
|
|
4
|
-
import type {EIP1193ChainId, EIP1193WindowWalletProvider} from 'eip-1193';
|
|
5
6
|
import {
|
|
6
7
|
fromEntropyKeyToMnemonic,
|
|
7
|
-
fromMnemonicToFirstAccount,
|
|
8
8
|
fromSignatureToKey,
|
|
9
9
|
originKeyMessage,
|
|
10
10
|
originPublicKeyPublicationMessage,
|
|
11
11
|
} from '@etherplay/alchemy';
|
|
12
|
-
import {
|
|
13
|
-
import {createProvider} from './provider.js';
|
|
12
|
+
import {withTimeout} from './utils.js';
|
|
14
13
|
|
|
15
14
|
export {fromEntropyKeyToMnemonic, originPublicKeyPublicationMessage, originKeyMessage};
|
|
16
15
|
export type {OriginAccount};
|
|
@@ -30,8 +29,8 @@ export type Mechanism = AlchemyMechanism | WalletMechanism<string | undefined, `
|
|
|
30
29
|
|
|
31
30
|
export type FullfilledMechanism = AlchemyMechanism | WalletMechanism<string, `0x${string}`>;
|
|
32
31
|
|
|
33
|
-
export type WalletState = {
|
|
34
|
-
provider:
|
|
32
|
+
export type WalletState<WalletProviderType> = {
|
|
33
|
+
provider: WalletProvider<WalletProviderType>;
|
|
35
34
|
accounts: `0x${string}`[];
|
|
36
35
|
accountChanged?: `0x${string}`;
|
|
37
36
|
chainId: string;
|
|
@@ -39,13 +38,13 @@ export type WalletState = {
|
|
|
39
38
|
switchingChain: 'addingChain' | 'switchingChain' | false;
|
|
40
39
|
} & ({status: 'connected'} | {status: 'locked'; unlocking: boolean} | {status: 'disconnected'; connecting: boolean});
|
|
41
40
|
|
|
42
|
-
type WalletConnected = {
|
|
41
|
+
type WalletConnected<WalletProviderType> = {
|
|
43
42
|
step: 'WaitingForSignature';
|
|
44
43
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
45
|
-
wallet: WalletState
|
|
44
|
+
wallet: WalletState<WalletProviderType>;
|
|
46
45
|
};
|
|
47
46
|
|
|
48
|
-
type SignedIn =
|
|
47
|
+
type SignedIn<WalletProviderType> =
|
|
49
48
|
| {
|
|
50
49
|
step: 'SignedIn';
|
|
51
50
|
mechanism: AlchemyMechanism;
|
|
@@ -56,16 +55,16 @@ type SignedIn =
|
|
|
56
55
|
step: 'SignedIn';
|
|
57
56
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
58
57
|
account: OriginAccount;
|
|
59
|
-
wallet: WalletState
|
|
58
|
+
wallet: WalletState<WalletProviderType>;
|
|
60
59
|
};
|
|
61
60
|
|
|
62
|
-
export type Connection = {
|
|
61
|
+
export type Connection<WalletProviderType> = {
|
|
63
62
|
// The connection can have an error in every state.
|
|
64
63
|
// a banner or other mechanism to show error should be used.
|
|
65
64
|
// error should be dismissable
|
|
66
65
|
error?: {message: string; cause?: any};
|
|
67
66
|
// wallets represent the web3 wallet installed on the user browser
|
|
68
|
-
wallets:
|
|
67
|
+
wallets: WalletHandle<WalletProviderType>[];
|
|
69
68
|
} & ( // loading can be true initially as the system will try to auto-login and fetch installed web3 wallet // Start in Idle
|
|
70
69
|
| {
|
|
71
70
|
step: 'Idle';
|
|
@@ -102,54 +101,127 @@ export type Connection = {
|
|
|
102
101
|
| {
|
|
103
102
|
step: 'ChooseWalletAccount';
|
|
104
103
|
mechanism: WalletMechanism<string, undefined>;
|
|
105
|
-
wallet: WalletState
|
|
104
|
+
wallet: WalletState<WalletProviderType>;
|
|
106
105
|
}
|
|
107
106
|
// Once the wallet is connected, the system will need a signature
|
|
108
107
|
// this state represent the fact and require another user interaction to request the signature
|
|
109
108
|
| {
|
|
110
109
|
step: 'WalletConnected';
|
|
111
110
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
112
|
-
wallet: WalletState
|
|
111
|
+
wallet: WalletState<WalletProviderType>;
|
|
113
112
|
}
|
|
114
113
|
// This state is triggered once the signature is requested, the user will have to confirm with its wallet
|
|
115
|
-
| WalletConnected
|
|
114
|
+
| WalletConnected<WalletProviderType>
|
|
116
115
|
// Finally the user is fully signed in
|
|
117
116
|
// wallet?.accountChanged if set, represent the fact that the user has changed its web3-wallet accounnt.
|
|
118
117
|
// wallet?.invalidChainId if set, represent the fact that the wallet is connected to a different chain.
|
|
119
118
|
// wallet?.switchingChain if set, represent the fact that the user is currently switching chain.
|
|
120
119
|
// a notification could be shown to the user so that he can switch the app to use that other account.
|
|
121
|
-
| SignedIn
|
|
120
|
+
| SignedIn<WalletProviderType>
|
|
122
121
|
);
|
|
123
122
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
name: string;
|
|
127
|
-
icon: string;
|
|
128
|
-
rdns: string;
|
|
129
|
-
}
|
|
123
|
+
const storageKeyAccount = '__origin_account';
|
|
124
|
+
const storageKeyLastWallet = '__last_wallet';
|
|
130
125
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
export type ConnectionStore<WalletProviderType> = {
|
|
127
|
+
subscribe: (run: (value: Connection<WalletProviderType>) => void) => () => void;
|
|
128
|
+
connect: (
|
|
129
|
+
mechanism?: Mechanism,
|
|
130
|
+
options?: {
|
|
131
|
+
requireUserConfirmationBeforeSignatureRequest?: boolean;
|
|
132
|
+
doNotStoreLocally?: boolean;
|
|
133
|
+
requestSignatureRightAway?: boolean;
|
|
134
|
+
},
|
|
135
|
+
) => Promise<void>;
|
|
136
|
+
cancel: () => void;
|
|
137
|
+
back: (step: 'MechanismToChoose' | 'Idle' | 'WalletToChoose') => void;
|
|
138
|
+
requestSignature: () => Promise<void>;
|
|
139
|
+
connectToAddress: (
|
|
140
|
+
address: `0x${string}`,
|
|
141
|
+
options?: {requireUserConfirmationBeforeSignatureRequest: boolean},
|
|
142
|
+
) => void;
|
|
143
|
+
disconnect: () => void;
|
|
144
|
+
getSignatureForPublicKeyPublication: () => Promise<`0x${string}`>;
|
|
145
|
+
switchWalletChain: (
|
|
146
|
+
chainId: string,
|
|
147
|
+
config?: {
|
|
148
|
+
readonly rpcUrls?: readonly string[];
|
|
149
|
+
readonly blockExplorerUrls?: readonly string[];
|
|
150
|
+
readonly chainName?: string;
|
|
151
|
+
readonly iconUrls?: readonly string[];
|
|
152
|
+
readonly nativeCurrency?: {
|
|
153
|
+
name: string;
|
|
154
|
+
symbol: string;
|
|
155
|
+
decimals: number;
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
) => Promise<void>;
|
|
159
|
+
unlock: () => Promise<void>;
|
|
160
|
+
ensureConnected: {
|
|
161
|
+
(
|
|
162
|
+
step: 'WalletConnected',
|
|
163
|
+
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
164
|
+
options?: {
|
|
165
|
+
requireUserConfirmationBeforeSignatureRequest?: boolean;
|
|
166
|
+
doNotStoreLocally?: boolean;
|
|
167
|
+
requestSignatureRightAway?: boolean;
|
|
168
|
+
},
|
|
169
|
+
): Promise<WalletConnected<WalletProviderType>>;
|
|
170
|
+
(
|
|
171
|
+
step: 'SignedIn',
|
|
172
|
+
mechanism?: Mechanism,
|
|
173
|
+
options?: {
|
|
174
|
+
requireUserConfirmationBeforeSignatureRequest?: boolean;
|
|
175
|
+
doNotStoreLocally?: boolean;
|
|
176
|
+
requestSignatureRightAway?: boolean;
|
|
177
|
+
},
|
|
178
|
+
): Promise<SignedIn<WalletProviderType>>;
|
|
179
|
+
(
|
|
180
|
+
mechanism?: Mechanism,
|
|
181
|
+
options?: {
|
|
182
|
+
requireUserConfirmationBeforeSignatureRequest?: boolean;
|
|
183
|
+
doNotStoreLocally?: boolean;
|
|
184
|
+
requestSignatureRightAway?: boolean;
|
|
185
|
+
},
|
|
186
|
+
): Promise<SignedIn<WalletProviderType>>;
|
|
187
|
+
};
|
|
188
|
+
provider: WalletProviderType;
|
|
189
|
+
};
|
|
135
190
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
191
|
+
// Function overloads for proper typing
|
|
192
|
+
export function createConnection<WalletProviderType>(settings: {
|
|
193
|
+
walletHost: string;
|
|
194
|
+
autoConnect?: boolean;
|
|
195
|
+
autoConnectWallet?: boolean;
|
|
196
|
+
walletConnector: WalletConnector<WalletProviderType>;
|
|
197
|
+
requestSignatureAutomaticallyIfPossible?: boolean;
|
|
198
|
+
alwaysUseCurrentAccount?: boolean;
|
|
199
|
+
node: {url: string; chainId: string; prioritizeWalletProvider?: boolean; requestsPerSecond?: number};
|
|
200
|
+
}): ConnectionStore<WalletProviderType>;
|
|
140
201
|
|
|
141
|
-
const storageKeyAccount = '__origin_account';
|
|
142
|
-
const storageKeyLastWallet = '__last_wallet';
|
|
143
202
|
export function createConnection(settings: {
|
|
144
203
|
walletHost: string;
|
|
145
204
|
autoConnect?: boolean;
|
|
146
205
|
autoConnectWallet?: boolean;
|
|
206
|
+
walletConnector?: undefined;
|
|
207
|
+
requestSignatureAutomaticallyIfPossible?: boolean;
|
|
208
|
+
alwaysUseCurrentAccount?: boolean;
|
|
209
|
+
node: {url: string; chainId: string; prioritizeWalletProvider?: boolean; requestsPerSecond?: number};
|
|
210
|
+
}): ConnectionStore<UnderlyingEthereumProvider>;
|
|
211
|
+
|
|
212
|
+
export function createConnection<WalletProviderType = UnderlyingEthereumProvider>(settings: {
|
|
213
|
+
walletHost: string;
|
|
214
|
+
autoConnect?: boolean;
|
|
215
|
+
autoConnectWallet?: boolean;
|
|
216
|
+
walletConnector?: WalletConnector<WalletProviderType>;
|
|
147
217
|
requestSignatureAutomaticallyIfPossible?: boolean;
|
|
148
218
|
alwaysUseCurrentAccount?: boolean;
|
|
149
219
|
node: {url: string; chainId: string; prioritizeWalletProvider?: boolean; requestsPerSecond?: number};
|
|
150
220
|
}) {
|
|
221
|
+
const walletConnector =
|
|
222
|
+
settings.walletConnector || (new EthereumWalletConnector() as unknown as WalletConnector<WalletProviderType>);
|
|
151
223
|
const alwaysOnChainId = settings.node.chainId;
|
|
152
|
-
const
|
|
224
|
+
const alwaysOnProviderWrapper = walletConnector.createAlwaysOnProvider({
|
|
153
225
|
endpoint: settings.node.url,
|
|
154
226
|
chainId: settings.node.chainId,
|
|
155
227
|
prioritizeWalletProvider: settings.node.prioritizeWalletProvider,
|
|
@@ -165,9 +237,9 @@ export function createConnection(settings: {
|
|
|
165
237
|
}
|
|
166
238
|
const requestSignatureAutomaticallyIfPossible = settings.requestSignatureAutomaticallyIfPossible || false;
|
|
167
239
|
|
|
168
|
-
let $connection: Connection = {step: 'Idle', loading: true, wallet: undefined, wallets: []};
|
|
169
|
-
const _store = writable<Connection
|
|
170
|
-
function set(connection: Connection) {
|
|
240
|
+
let $connection: Connection<WalletProviderType> = {step: 'Idle', loading: true, wallet: undefined, wallets: []};
|
|
241
|
+
const _store = writable<Connection<WalletProviderType>>($connection);
|
|
242
|
+
function set(connection: Connection<WalletProviderType>) {
|
|
171
243
|
$connection = connection;
|
|
172
244
|
_store.set($connection);
|
|
173
245
|
return $connection;
|
|
@@ -183,35 +255,23 @@ export function createConnection(settings: {
|
|
|
183
255
|
}
|
|
184
256
|
}
|
|
185
257
|
|
|
186
|
-
let _wallet: {provider:
|
|
258
|
+
let _wallet: {provider: WalletProvider<WalletProviderType>; chainId: string} | undefined;
|
|
187
259
|
|
|
188
260
|
let popup: PopupPromise<OriginAccount> | undefined;
|
|
189
261
|
|
|
190
262
|
function fetchWallets() {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// TODO ?
|
|
195
|
-
(window as any).addEventListener('eip6963:announceProvider', (event: EIP6963AnnounceProviderEvent) => {
|
|
196
|
-
const {detail} = event;
|
|
197
|
-
// const { info, provider } = detail;
|
|
198
|
-
// const { uuid, name, icon, rdns } = info;
|
|
199
|
-
// console.log('provider', provider);
|
|
200
|
-
// console.log(`isDefault: ${provider === defaultProvider}`);
|
|
201
|
-
// console.log('info', info);
|
|
202
|
-
const existingWallets = $connection.wallets;
|
|
203
|
-
existingWallets.push(detail);
|
|
263
|
+
walletConnector.fetchWallets((detail) => {
|
|
264
|
+
const existingWallets = $connection.wallets;
|
|
265
|
+
existingWallets.push(detail);
|
|
204
266
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
});
|
|
267
|
+
set({
|
|
268
|
+
...$connection,
|
|
269
|
+
wallets: existingWallets,
|
|
209
270
|
});
|
|
210
|
-
|
|
211
|
-
}
|
|
271
|
+
});
|
|
212
272
|
}
|
|
213
273
|
|
|
214
|
-
function waitForWallet(name: string): Promise<
|
|
274
|
+
function waitForWallet(name: string): Promise<WalletHandle<WalletProviderType>> {
|
|
215
275
|
return new Promise((resolve, reject) => {
|
|
216
276
|
const timeout = setTimeout(() => {
|
|
217
277
|
clearInterval(interval);
|
|
@@ -241,16 +301,17 @@ export function createConnection(settings: {
|
|
|
241
301
|
if (mechanismUsed.type == 'wallet') {
|
|
242
302
|
const walletMechanism = mechanismUsed as WalletMechanism<string, `0x${string}`>;
|
|
243
303
|
waitForWallet(walletMechanism.name)
|
|
244
|
-
.then(async (walletDetails:
|
|
245
|
-
const walletProvider = walletDetails.
|
|
246
|
-
const chainIdAsHex = await withTimeout(walletProvider.
|
|
304
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
305
|
+
const walletProvider = walletDetails.walletProvider;
|
|
306
|
+
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
247
307
|
const chainId = Number(chainIdAsHex).toString();
|
|
248
308
|
_wallet = {provider: walletProvider, chainId};
|
|
249
|
-
|
|
309
|
+
// TODO
|
|
310
|
+
alwaysOnProviderWrapper.setWalletProvider(walletProvider.underlyingProvider);
|
|
250
311
|
watchForChainIdChange(_wallet.provider);
|
|
251
312
|
let accounts: `0x${string}`[] = [];
|
|
252
313
|
// try {
|
|
253
|
-
accounts = await withTimeout(walletProvider.
|
|
314
|
+
accounts = await withTimeout(walletProvider.getAccounts());
|
|
254
315
|
accounts = accounts.map((v) => v.toLowerCase() as `0x${string}`);
|
|
255
316
|
// } catch {}
|
|
256
317
|
// // TODO try catch ? and use logic of onAccountChanged
|
|
@@ -269,7 +330,7 @@ export function createConnection(settings: {
|
|
|
269
330
|
switchingChain: false,
|
|
270
331
|
},
|
|
271
332
|
});
|
|
272
|
-
|
|
333
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
273
334
|
// TODO use the same logic before hand
|
|
274
335
|
onAccountChanged(accounts);
|
|
275
336
|
watchForAccountChange(walletProvider);
|
|
@@ -294,17 +355,18 @@ export function createConnection(settings: {
|
|
|
294
355
|
const lastWallet = getLastWallet();
|
|
295
356
|
if (lastWallet) {
|
|
296
357
|
waitForWallet(lastWallet.name)
|
|
297
|
-
.then(async (walletDetails:
|
|
298
|
-
const walletProvider = walletDetails.
|
|
299
|
-
const chainIdAsHex = await withTimeout(walletProvider.
|
|
358
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
359
|
+
const walletProvider = walletDetails.walletProvider;
|
|
360
|
+
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
300
361
|
const chainId = Number(chainIdAsHex).toString();
|
|
301
362
|
_wallet = {provider: walletProvider, chainId};
|
|
302
|
-
|
|
363
|
+
// TODO
|
|
364
|
+
alwaysOnProviderWrapper.setWalletProvider(walletProvider.underlyingProvider);
|
|
303
365
|
watchForChainIdChange(_wallet.provider);
|
|
304
366
|
|
|
305
367
|
let accounts: `0x${string}`[] = [];
|
|
306
368
|
// try {
|
|
307
|
-
accounts = await withTimeout(walletProvider.
|
|
369
|
+
accounts = await withTimeout(walletProvider.getAccounts());
|
|
308
370
|
accounts = accounts.map((v) => v.toLowerCase() as `0x${string}`);
|
|
309
371
|
// } catch {}
|
|
310
372
|
// // TODO try catch ? and use logic of onAccountChanged
|
|
@@ -322,7 +384,7 @@ export function createConnection(settings: {
|
|
|
322
384
|
switchingChain: false,
|
|
323
385
|
},
|
|
324
386
|
});
|
|
325
|
-
|
|
387
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
326
388
|
// TODO use the same logic before hand
|
|
327
389
|
onAccountChanged(accounts);
|
|
328
390
|
watchForAccountChange(walletProvider);
|
|
@@ -380,7 +442,7 @@ export function createConnection(settings: {
|
|
|
380
442
|
|
|
381
443
|
let signaturePending: {reject: (error: unknown) => void; id: number} | undefined = undefined;
|
|
382
444
|
let signatureCounter = 0;
|
|
383
|
-
function _requestSignature(provider:
|
|
445
|
+
function _requestSignature(provider: WalletProvider<WalletProviderType>, msg: string, address: `0x${string}`) {
|
|
384
446
|
const id = ++signatureCounter;
|
|
385
447
|
if (signaturePending) {
|
|
386
448
|
const tmp = signaturePending;
|
|
@@ -403,10 +465,7 @@ export function createConnection(settings: {
|
|
|
403
465
|
// }),
|
|
404
466
|
// );
|
|
405
467
|
provider
|
|
406
|
-
.
|
|
407
|
-
method: 'personal_sign',
|
|
408
|
-
params: [msg, address],
|
|
409
|
-
})
|
|
468
|
+
.signMessage(msg, address)
|
|
410
469
|
.then((signature) => {
|
|
411
470
|
if (signaturePending?.id === id) {
|
|
412
471
|
signaturePending = undefined;
|
|
@@ -429,7 +488,6 @@ export function createConnection(settings: {
|
|
|
429
488
|
|
|
430
489
|
const provider = $connection.wallet.provider;
|
|
431
490
|
const message = originKeyMessage(origin);
|
|
432
|
-
const msg = hashMessage(message);
|
|
433
491
|
|
|
434
492
|
set({
|
|
435
493
|
...$connection,
|
|
@@ -438,7 +496,7 @@ export function createConnection(settings: {
|
|
|
438
496
|
|
|
439
497
|
let signature: `0x${string}`;
|
|
440
498
|
try {
|
|
441
|
-
signature = await _requestSignature(provider,
|
|
499
|
+
signature = await _requestSignature(provider, message, $connection.mechanism.address);
|
|
442
500
|
} catch (err) {
|
|
443
501
|
console.error(err);
|
|
444
502
|
if ((err as any)?.cause?.code === 111111) {
|
|
@@ -461,7 +519,7 @@ export function createConnection(settings: {
|
|
|
461
519
|
|
|
462
520
|
const originKey = fromSignatureToKey(signature);
|
|
463
521
|
const originMnemonic = fromEntropyKeyToMnemonic(originKey);
|
|
464
|
-
const originAccount =
|
|
522
|
+
const originAccount = walletConnector.accountGenerator.fromMnemonicToAccount(originMnemonic, 0);
|
|
465
523
|
|
|
466
524
|
const account = {
|
|
467
525
|
address: $connection.mechanism.address as `0x${string}`,
|
|
@@ -475,6 +533,7 @@ export function createConnection(settings: {
|
|
|
475
533
|
metadata: {},
|
|
476
534
|
mechanismUsed: $connection.mechanism,
|
|
477
535
|
savedPublicKeyPublicationSignature: undefined,
|
|
536
|
+
accountType: walletConnector.accountGenerator.type,
|
|
478
537
|
};
|
|
479
538
|
set({
|
|
480
539
|
...$connection,
|
|
@@ -542,7 +601,7 @@ export function createConnection(settings: {
|
|
|
542
601
|
unlocking: false,
|
|
543
602
|
},
|
|
544
603
|
});
|
|
545
|
-
|
|
604
|
+
alwaysOnProviderWrapper.setWalletStatus('locked');
|
|
546
605
|
} else {
|
|
547
606
|
const disconnected = accountsFormated.find((v) => v == addressSignedIn) ? false : true;
|
|
548
607
|
|
|
@@ -555,7 +614,7 @@ export function createConnection(settings: {
|
|
|
555
614
|
connecting: false,
|
|
556
615
|
},
|
|
557
616
|
});
|
|
558
|
-
|
|
617
|
+
alwaysOnProviderWrapper.setWalletStatus('disconnected');
|
|
559
618
|
} else {
|
|
560
619
|
set({
|
|
561
620
|
...$connection,
|
|
@@ -564,7 +623,7 @@ export function createConnection(settings: {
|
|
|
564
623
|
status: 'connected',
|
|
565
624
|
},
|
|
566
625
|
});
|
|
567
|
-
|
|
626
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
568
627
|
}
|
|
569
628
|
}
|
|
570
629
|
|
|
@@ -600,15 +659,15 @@ export function createConnection(settings: {
|
|
|
600
659
|
try {
|
|
601
660
|
const provider = $connection.wallet?.provider;
|
|
602
661
|
if (provider) {
|
|
603
|
-
let accounts = await withTimeout(provider.
|
|
662
|
+
let accounts = await withTimeout(provider.getAccounts());
|
|
604
663
|
if (accounts.length == 0) {
|
|
605
664
|
onAccountChanged(accounts);
|
|
606
665
|
}
|
|
607
666
|
}
|
|
608
667
|
} catch {}
|
|
609
668
|
}
|
|
610
|
-
function watchForAccountChange(walletProvider:
|
|
611
|
-
walletProvider.
|
|
669
|
+
function watchForAccountChange(walletProvider: WalletProvider<WalletProviderType>) {
|
|
670
|
+
walletProvider.listenForAccountsChanged(onAccountChanged);
|
|
612
671
|
// we also poll accounts for checking lock status as Metamask does not notify it
|
|
613
672
|
if (lockCheckInterval) {
|
|
614
673
|
clearInterval(lockCheckInterval);
|
|
@@ -616,19 +675,19 @@ export function createConnection(settings: {
|
|
|
616
675
|
}
|
|
617
676
|
lockCheckInterval = setInterval(checkLockStatus, 1000);
|
|
618
677
|
}
|
|
619
|
-
function stopWatchingForAccountChange(walletProvider:
|
|
620
|
-
walletProvider.
|
|
678
|
+
function stopWatchingForAccountChange(walletProvider: WalletProvider<WalletProviderType>) {
|
|
679
|
+
walletProvider.stopListenForAccountsChanged(onAccountChanged);
|
|
621
680
|
if (lockCheckInterval) {
|
|
622
681
|
clearInterval(lockCheckInterval);
|
|
623
682
|
lockCheckInterval = undefined;
|
|
624
683
|
}
|
|
625
684
|
}
|
|
626
685
|
|
|
627
|
-
function watchForChainIdChange(walletProvider:
|
|
628
|
-
walletProvider.
|
|
686
|
+
function watchForChainIdChange(walletProvider: WalletProvider<WalletProviderType>) {
|
|
687
|
+
walletProvider.listenForChainChanged(onChainChanged);
|
|
629
688
|
}
|
|
630
|
-
function stopWatchingForChainIdChange(walletProvider:
|
|
631
|
-
walletProvider.
|
|
689
|
+
function stopWatchingForChainIdChange(walletProvider: WalletProvider<WalletProviderType>) {
|
|
690
|
+
walletProvider.stopListenForChainChanged(onChainChanged);
|
|
632
691
|
}
|
|
633
692
|
|
|
634
693
|
type ConnectionOptions = {
|
|
@@ -649,7 +708,7 @@ export function createConnection(settings: {
|
|
|
649
708
|
const wallet = $connection.wallets.find((v) => v.info.name == walletName || v.info.uuid == walletName);
|
|
650
709
|
if (wallet) {
|
|
651
710
|
if (_wallet) {
|
|
652
|
-
|
|
711
|
+
alwaysOnProviderWrapper.setWalletProvider(undefined);
|
|
653
712
|
stopWatchingForAccountChange(_wallet.provider);
|
|
654
713
|
stopWatchingForChainIdChange(_wallet.provider);
|
|
655
714
|
}
|
|
@@ -666,16 +725,17 @@ export function createConnection(settings: {
|
|
|
666
725
|
wallet: undefined,
|
|
667
726
|
});
|
|
668
727
|
try {
|
|
669
|
-
const provider = wallet.
|
|
670
|
-
const chainIdAsHex = await withTimeout(provider.
|
|
728
|
+
const provider = wallet.walletProvider;
|
|
729
|
+
const chainIdAsHex = await withTimeout(provider.getChainId());
|
|
671
730
|
const chainId = Number(chainIdAsHex).toString();
|
|
672
731
|
_wallet = {
|
|
673
732
|
chainId,
|
|
674
733
|
provider,
|
|
675
734
|
};
|
|
676
|
-
|
|
735
|
+
// TODO
|
|
736
|
+
alwaysOnProviderWrapper.setWalletProvider(_wallet.provider.underlyingProvider);
|
|
677
737
|
watchForChainIdChange(_wallet.provider);
|
|
678
|
-
let accounts = await withTimeout(provider.
|
|
738
|
+
let accounts = await withTimeout(provider.getAccounts());
|
|
679
739
|
accounts = accounts.map((v) => v.toLowerCase()) as `0x${string}`[];
|
|
680
740
|
if (accounts.length === 0) {
|
|
681
741
|
set({
|
|
@@ -684,7 +744,7 @@ export function createConnection(settings: {
|
|
|
684
744
|
wallets: $connection.wallets,
|
|
685
745
|
wallet: undefined,
|
|
686
746
|
});
|
|
687
|
-
accounts = await provider.
|
|
747
|
+
accounts = await provider.requestAccounts();
|
|
688
748
|
accounts = accounts.map((v) => v.toLowerCase()) as `0x${string}`[];
|
|
689
749
|
if (accounts.length > 0) {
|
|
690
750
|
const nextStep =
|
|
@@ -701,7 +761,7 @@ export function createConnection(settings: {
|
|
|
701
761
|
}
|
|
702
762
|
}
|
|
703
763
|
|
|
704
|
-
const newState: Connection =
|
|
764
|
+
const newState: Connection<WalletProviderType> =
|
|
705
765
|
nextStep === 'ChooseWalletAccount'
|
|
706
766
|
? {
|
|
707
767
|
step: nextStep,
|
|
@@ -743,12 +803,12 @@ export function createConnection(settings: {
|
|
|
743
803
|
watchForAccountChange(_wallet.provider);
|
|
744
804
|
|
|
745
805
|
set(newState);
|
|
746
|
-
|
|
806
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
747
807
|
saveLastWallet(newState.mechanism);
|
|
748
808
|
await requestSignature();
|
|
749
809
|
} else {
|
|
750
810
|
set(newState);
|
|
751
|
-
|
|
811
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
752
812
|
if (newState.step === 'WalletConnected') {
|
|
753
813
|
saveLastWallet(newState.mechanism);
|
|
754
814
|
}
|
|
@@ -777,7 +837,7 @@ export function createConnection(settings: {
|
|
|
777
837
|
!settings?.alwaysUseCurrentAccount && !specificAddress && accounts.length > 1
|
|
778
838
|
? 'ChooseWalletAccount'
|
|
779
839
|
: 'WalletConnected';
|
|
780
|
-
const newState: Connection =
|
|
840
|
+
const newState: Connection<WalletProviderType> =
|
|
781
841
|
nextStep === 'ChooseWalletAccount'
|
|
782
842
|
? {
|
|
783
843
|
step: nextStep,
|
|
@@ -816,14 +876,14 @@ export function createConnection(settings: {
|
|
|
816
876
|
!options?.requireUserConfirmationBeforeSignatureRequest
|
|
817
877
|
) {
|
|
818
878
|
set(newState);
|
|
819
|
-
|
|
879
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
820
880
|
saveLastWallet(newState.mechanism);
|
|
821
881
|
watchForAccountChange(_wallet.provider);
|
|
822
882
|
await requestSignature();
|
|
823
883
|
} else {
|
|
824
884
|
watchForAccountChange(_wallet.provider);
|
|
825
885
|
set(newState);
|
|
826
|
-
|
|
886
|
+
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
827
887
|
if (newState.step === 'WalletConnected') {
|
|
828
888
|
saveLastWallet(newState.mechanism);
|
|
829
889
|
}
|
|
@@ -916,21 +976,27 @@ export function createConnection(settings: {
|
|
|
916
976
|
step: 'WalletConnected',
|
|
917
977
|
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
918
978
|
options?: ConnectionOptions,
|
|
919
|
-
): Promise<WalletConnected
|
|
920
|
-
function ensureConnected(
|
|
921
|
-
|
|
979
|
+
): Promise<WalletConnected<WalletProviderType>>;
|
|
980
|
+
function ensureConnected(
|
|
981
|
+
step: 'SignedIn',
|
|
982
|
+
mechanism?: Mechanism,
|
|
983
|
+
options?: ConnectionOptions,
|
|
984
|
+
): Promise<SignedIn<WalletProviderType>>;
|
|
985
|
+
function ensureConnected(mechanism?: Mechanism, options?: ConnectionOptions): Promise<SignedIn<WalletProviderType>>;
|
|
922
986
|
async function ensureConnected<Step extends 'WalletConnected' | 'SignedIn' = 'SignedIn'>(
|
|
923
987
|
stepOrMechanism?: Step | Mechanism,
|
|
924
988
|
mechanismOrOptions?: Mechanism | ConnectionOptions,
|
|
925
989
|
options?: ConnectionOptions,
|
|
926
990
|
) {
|
|
927
|
-
const step = typeof stepOrMechanism === 'string' ? stepOrMechanism :
|
|
991
|
+
const step = typeof stepOrMechanism === 'string' ? stepOrMechanism : 'SignedIn';
|
|
928
992
|
let mechanism = typeof stepOrMechanism === 'string' ? (mechanismOrOptions as Mechanism) : stepOrMechanism;
|
|
929
993
|
if (!mechanism && step === 'WalletConnected') {
|
|
930
994
|
mechanism = {type: 'wallet'};
|
|
931
995
|
}
|
|
932
996
|
options = typeof stepOrMechanism === 'string' ? options : (mechanismOrOptions as ConnectionOptions);
|
|
933
|
-
const promise = new Promise<
|
|
997
|
+
const promise = new Promise<
|
|
998
|
+
Step extends 'WalletConnected' ? WalletConnected<WalletProviderType> : SignedIn<WalletProviderType>
|
|
999
|
+
>((resolve, reject) => {
|
|
934
1000
|
let forceConnect = false;
|
|
935
1001
|
if (
|
|
936
1002
|
$connection.step == 'WalletConnected' &&
|
|
@@ -969,7 +1035,7 @@ export function createConnection(settings: {
|
|
|
969
1035
|
deleteOriginAccount();
|
|
970
1036
|
deleteLastWallet();
|
|
971
1037
|
if (_wallet) {
|
|
972
|
-
|
|
1038
|
+
alwaysOnProviderWrapper.setWalletProvider(undefined);
|
|
973
1039
|
stopWatchingForAccountChange(_wallet.provider);
|
|
974
1040
|
stopWatchingForChainIdChange(_wallet.provider);
|
|
975
1041
|
}
|
|
@@ -1025,6 +1091,8 @@ export function createConnection(settings: {
|
|
|
1025
1091
|
throw new Error(`mechanism ${(settings.mechanism as any).type} not supported`);
|
|
1026
1092
|
}
|
|
1027
1093
|
|
|
1094
|
+
popupURL.searchParams.append('account-type', walletConnector.accountGenerator.type);
|
|
1095
|
+
|
|
1028
1096
|
// if (settings.extraParams) {
|
|
1029
1097
|
// for (const [key, value] of Object.entries(settings.extraParams)) {
|
|
1030
1098
|
// popupURL.searchParams.append(`${key}`, value);
|
|
@@ -1072,11 +1140,7 @@ export function createConnection(settings: {
|
|
|
1072
1140
|
throw new Error(`no provider`);
|
|
1073
1141
|
}
|
|
1074
1142
|
const message = originPublicKeyPublicationMessage(origin, account.signer.publicKey);
|
|
1075
|
-
|
|
1076
|
-
return _wallet.provider.request({
|
|
1077
|
-
method: 'personal_sign',
|
|
1078
|
-
params: [msg, account.address],
|
|
1079
|
-
});
|
|
1143
|
+
return _wallet.provider.signMessage(message, account.address);
|
|
1080
1144
|
}
|
|
1081
1145
|
|
|
1082
1146
|
if (account.savedPublicKeyPublicationSignature) {
|
|
@@ -1103,7 +1167,7 @@ export function createConnection(settings: {
|
|
|
1103
1167
|
});
|
|
1104
1168
|
|
|
1105
1169
|
try {
|
|
1106
|
-
await wallet.provider.
|
|
1170
|
+
await wallet.provider.requestAccounts().then(onAccountChanged);
|
|
1107
1171
|
} catch {
|
|
1108
1172
|
set({
|
|
1109
1173
|
...$connection,
|
|
@@ -1144,14 +1208,7 @@ export function createConnection(settings: {
|
|
|
1144
1208
|
...$connection,
|
|
1145
1209
|
wallet: {...$connection.wallet, switchingChain: 'switchingChain'},
|
|
1146
1210
|
});
|
|
1147
|
-
const result = await wallet.provider.
|
|
1148
|
-
method: 'wallet_switchEthereumChain',
|
|
1149
|
-
params: [
|
|
1150
|
-
{
|
|
1151
|
-
chainId: ('0x' + parseInt(chainId).toString(16)) as EIP1193ChainId,
|
|
1152
|
-
},
|
|
1153
|
-
],
|
|
1154
|
-
});
|
|
1211
|
+
const result = await wallet.provider.switchChain(('0x' + parseInt(chainId).toString(16)) as `0x${string}`);
|
|
1155
1212
|
if (!result) {
|
|
1156
1213
|
if ($connection.wallet) {
|
|
1157
1214
|
set({
|
|
@@ -1197,18 +1254,13 @@ export function createConnection(settings: {
|
|
|
1197
1254
|
}
|
|
1198
1255
|
// logger.info(`wallet_switchEthereumChain: could not switch, try adding the chain via "wallet_addEthereumChain"`);
|
|
1199
1256
|
try {
|
|
1200
|
-
const result = await wallet.provider.
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
blockExplorerUrls: config.blockExplorerUrls,
|
|
1208
|
-
iconUrls: config.iconUrls,
|
|
1209
|
-
nativeCurrency: config.nativeCurrency,
|
|
1210
|
-
},
|
|
1211
|
-
],
|
|
1257
|
+
const result = await wallet.provider.addChain({
|
|
1258
|
+
chainId: ('0x' + parseInt(chainId).toString(16)) as `0x${string}`,
|
|
1259
|
+
rpcUrls: config.rpcUrls,
|
|
1260
|
+
chainName: config.chainName,
|
|
1261
|
+
blockExplorerUrls: config.blockExplorerUrls,
|
|
1262
|
+
iconUrls: config.iconUrls,
|
|
1263
|
+
nativeCurrency: config.nativeCurrency,
|
|
1212
1264
|
});
|
|
1213
1265
|
if (!result) {
|
|
1214
1266
|
if ($connection.wallet) {
|
|
@@ -1292,8 +1344,6 @@ export function createConnection(settings: {
|
|
|
1292
1344
|
switchWalletChain,
|
|
1293
1345
|
unlock,
|
|
1294
1346
|
ensureConnected,
|
|
1295
|
-
provider:
|
|
1347
|
+
provider: alwaysOnProviderWrapper.provider,
|
|
1296
1348
|
};
|
|
1297
1349
|
}
|
|
1298
|
-
|
|
1299
|
-
export type ConnectionStore = ReturnType<typeof createConnection>;
|