Skip to content

Commit 9aee736

Browse files
Guillermo Alejandro Gallardo DiezGuillermo Alejandro Gallardo Diez
Guillermo Alejandro Gallardo Diez
authored and
Guillermo Alejandro Gallardo Diez
committed
Merge branch 'v9-beta' of github.com:near/wallet-selector into v9-beta
2 parents 5fe6d45 + e5bad24 commit 9aee736

File tree

9 files changed

+84
-175
lines changed

9 files changed

+84
-175
lines changed

examples/react/components/Content.tsx

-26
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const BOATLOAD_OF_GAS = "30000000000000";
2424

2525
const Content: React.FC = () => {
2626
const {
27-
accounts,
2827
signedAccountId,
2928
signOut,
3029
signIn,
@@ -51,28 +50,6 @@ const Content: React.FC = () => {
5150
getMessages().then(setMessages);
5251
}, [getMessages]);
5352

54-
const handleSwitchAccount = () => {
55-
if (!accounts) {
56-
throw new Error("No accounts found");
57-
}
58-
59-
if (!walletSelector) {
60-
throw new Error("No selector found");
61-
}
62-
const currentIndex = accounts.findIndex(
63-
(x) => x.accountId === signedAccountId
64-
);
65-
const nextIndex = currentIndex < accounts.length - 1 ? currentIndex + 1 : 0;
66-
67-
const nextAccountId = accounts[nextIndex].accountId;
68-
69-
walletSelector.then((selector) => {
70-
selector.setActiveAccount(nextAccountId);
71-
});
72-
73-
alert("Switched account to " + nextAccountId);
74-
};
75-
7653
const addMessages = useCallback(
7754
async (message: string, donation: string, multiple: boolean) => {
7855
if (!multiple) {
@@ -233,9 +210,6 @@ const Content: React.FC = () => {
233210
<div>
234211
<button onClick={signIn}>Switch Wallet</button>
235212
<button onClick={handleSignMessage}>Sign Message</button>
236-
{accounts && accounts.length > 1 && (
237-
<button onClick={handleSwitchAccount}>Switch Account</button>
238-
)}
239213
<button onClick={signOut}>Log out {signedAccountId}</button>
240214
</div>
241215
<Form

examples/react/pages/index.tsx

+4-21
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ import { setupMathWallet } from "@near-wallet-selector/math-wallet";
1010
import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
1111
import { setupMeteorWalletApp } from "@near-wallet-selector/meteor-wallet-app";
1212
import { setupHotWallet } from "@near-wallet-selector/hot-wallet";
13-
import { setupNearFi } from "@near-wallet-selector/nearfi";
14-
// import { setupNightly } from "@near-wallet-selector/nightly";
13+
import { setupNightly } from "@near-wallet-selector/nightly";
1514
import { setupSender } from "@near-wallet-selector/sender";
1615
import { setupBitgetWallet } from "@near-wallet-selector/bitget-wallet";
17-
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
1816
import { setupWelldoneWallet } from "@near-wallet-selector/welldone-wallet";
19-
import { setupNeth } from "@near-wallet-selector/neth";
2017
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
2118
import { setupLedger } from "@near-wallet-selector/ledger";
2219
import { setupXDEFI } from "@near-wallet-selector/xdefi";
@@ -25,13 +22,13 @@ import { setupNearMobileWallet } from "@near-wallet-selector/near-mobile-wallet"
2522
import { setupBitteWallet } from "@near-wallet-selector/bitte-wallet";
2623
import { setupOKXWallet } from "@near-wallet-selector/okx-wallet";
2724
import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets";
25+
import { setupCoin98Wallet } from "@near-wallet-selector/coin98-wallet";
2826

2927
import { CONTRACT_ID, NETWORK_ID } from "../constants";
30-
import { setupCoin98Wallet } from "@near-wallet-selector/coin98-wallet";
3128

3229
const walletSelectorConfig = {
3330
network: NETWORK_ID,
34-
debug: true,
31+
debug: false,
3532
modules: [
3633
setupEthereumWallets({
3734
wagmiConfig: wagmiAdapter.wagmiConfig,
@@ -43,29 +40,15 @@ const walletSelectorConfig = {
4340
setupMyNearWallet(),
4441
setupLedger(),
4542
setupSender(),
46-
// setupNightly(),
43+
setupNightly(),
4744
setupBitgetWallet(),
4845
setupMathWallet(),
4946
setupMeteorWalletApp({ contractId: CONTRACT_ID }),
5047
setupOKXWallet(),
5148
setupWelldoneWallet(),
5249
setupCoin98Wallet(),
53-
setupNearFi(),
5450
setupRamperWallet(),
55-
setupNeth({
56-
gas: "300000000000000",
57-
bundle: false,
58-
}),
5951
setupXDEFI(),
60-
setupWalletConnect({
61-
projectId: "c4f79cc...",
62-
metadata: {
63-
name: "NEAR Wallet Selector",
64-
description: "Example dApp used by NEAR Wallet Selector",
65-
url: "https://github.com/near/wallet-selector",
66-
icons: ["https://avatars.githubusercontent.com/u/37784886"],
67-
},
68-
}),
6952
setupNearMobileWallet(),
7053
],
7154
};

examples/react/wallets/web3modal.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export const wagmiAdapter = new WagmiAdapter({
5050
connectors,
5151
networks: [near],
5252
});
53-
reconnect(wagmiAdapter.wagmiConfig);
5453

5554
export const web3Modal = createAppKit({
5655
adapters: [wagmiAdapter],
@@ -68,3 +67,5 @@ export const web3Modal = createAppKit({
6867
coinbasePreference: "eoaOnly", // Smart accounts (Safe contract) not available on NEAR Protocol, only EOA.
6968
allWallets: "SHOW",
7069
});
70+
71+
reconnect(wagmiAdapter.wagmiConfig);

packages/core/src/lib/constants.ts

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ export const REMEMBER_RECENT_WALLETS_STATE = {
77
};
88

99
export const CONTRACT = "contract";
10-
export const PENDING_CONTRACT = "contract:pending";
11-
1210
export const SELECTED_WALLET_ID = `selectedWalletId`;
13-
export const PENDING_SELECTED_WALLET_ID = `selectedWalletId:pending`;

packages/core/src/lib/services/wallet-modules/wallet-modules.service.ts

+52-107
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import type {
1111
} from "../../wallet";
1212
import type { StorageService } from "../storage/storage.service.types";
1313
import type { Options } from "../../options.types";
14-
import type { ContractState, ModuleState, Store } from "../../store.types";
14+
import type { ModuleState, Store } from "../../store.types";
1515
import { EventEmitter } from "../event-emitter/event-emitter.service";
1616
import type { WalletSelectorEvents } from "../../wallet-selector.types";
1717
import { Logger, logger } from "../logger/logger.service";
1818
import {
1919
RECENTLY_SIGNED_IN_WALLETS,
2020
PACKAGE_NAME,
21-
PENDING_CONTRACT,
22-
PENDING_SELECTED_WALLET_ID,
2321
REMEMBER_RECENT_WALLETS,
2422
REMEMBER_RECENT_WALLETS_STATE,
2523
} from "../../constants";
@@ -77,49 +75,10 @@ export class WalletModules {
7775

7876
private async resolveStorageState() {
7977
const jsonStorage = new JsonStorage(this.storage, PACKAGE_NAME);
80-
const pendingSelectedWalletId = await jsonStorage.getItem<string>(
81-
PENDING_SELECTED_WALLET_ID
82-
);
83-
const pendingContract = await jsonStorage.getItem<ContractState>(
84-
PENDING_CONTRACT
85-
);
8678
const rememberRecentWallets = await jsonStorage.getItem<string>(
8779
REMEMBER_RECENT_WALLETS
8880
);
8981

90-
if (pendingSelectedWalletId && pendingContract) {
91-
const accounts = await this.validateWallet(pendingSelectedWalletId);
92-
93-
await jsonStorage.removeItem(PENDING_SELECTED_WALLET_ID);
94-
await jsonStorage.removeItem(PENDING_CONTRACT);
95-
96-
if (accounts.length) {
97-
const { selectedWalletId } = this.store.getState();
98-
const selectedWallet = await this.getWallet(selectedWalletId);
99-
100-
if (selectedWallet && pendingSelectedWalletId !== selectedWalletId) {
101-
await selectedWallet.signOut().catch((err) => {
102-
logger.log("Failed to sign out existing wallet");
103-
logger.error(err);
104-
});
105-
}
106-
107-
let recentlySignedInWalletsFromPending: Array<string> = [];
108-
if (rememberRecentWallets === REMEMBER_RECENT_WALLETS_STATE.ENABLED) {
109-
recentlySignedInWalletsFromPending =
110-
await this.setWalletAsRecentlySignedIn(pendingSelectedWalletId);
111-
}
112-
return {
113-
accounts,
114-
contract: pendingContract,
115-
selectedWalletId: pendingSelectedWalletId,
116-
recentlySignedInWallets: recentlySignedInWalletsFromPending,
117-
rememberRecentWallets:
118-
rememberRecentWallets || REMEMBER_RECENT_WALLETS_STATE.ENABLED,
119-
};
120-
}
121-
}
122-
12382
const { contract, selectedWalletId } = this.store.getState();
12483
const accounts = await this.validateWallet(selectedWalletId);
12584

@@ -188,21 +147,8 @@ export class WalletModules {
188147
{ accounts, contractId, methodNames }: WalletEvents["signedIn"]
189148
) {
190149
const { selectedWalletId, rememberRecentWallets } = this.store.getState();
191-
const jsonStorage = new JsonStorage(this.storage, PACKAGE_NAME);
192150
const contract = { contractId, methodNames };
193151

194-
if (!accounts.length) {
195-
const module = this.getModule(walletId)!;
196-
// We can't guarantee the user will actually sign in with browser wallets.
197-
// Best we can do is set in storage and validate on init.
198-
if (module.type === "browser") {
199-
await jsonStorage.setItem(PENDING_SELECTED_WALLET_ID, walletId);
200-
await jsonStorage.setItem<ContractState>(PENDING_CONTRACT, contract);
201-
}
202-
203-
return;
204-
}
205-
206152
if (selectedWalletId && selectedWalletId !== walletId) {
207153
await this.signOutWallet(selectedWalletId);
208154
}
@@ -389,49 +335,66 @@ export class WalletModules {
389335
}
390336

391337
async setup() {
392-
const modules: Array<ModuleState> = [];
338+
for (let i = 0; i < this.factories.length; i++) {
339+
const factory = this.factories[i];
340+
factory({ options: this.options })
341+
.then(async (module) => {
342+
// Filter out wallets that aren't available.
343+
if (!module) {
344+
return;
345+
}
393346

394-
for (let i = 0; i < this.factories.length; i += 1) {
395-
const module = await this.factories[i]({ options: this.options }).catch(
396-
(err) => {
397-
logger.log("Failed to setup module");
398-
logger.error(err);
347+
const moduleState = {
348+
id: module.id,
349+
type: module.type,
350+
metadata: module.metadata,
351+
listIndex: i,
352+
wallet: async () => {
353+
let instance = this.instances[module.id];
399354

400-
return null;
401-
}
402-
);
355+
if (instance) {
356+
return instance;
357+
}
403358

404-
// Filter out wallets that aren't available.
405-
if (!module) {
406-
continue;
407-
}
359+
instance = await this.setupInstance(module);
408360

409-
// Skip duplicated module.
410-
if (modules.some((x) => x.id === module.id)) {
411-
continue;
412-
}
361+
this.instances[module.id] = instance;
413362

414-
modules.push({
415-
id: module.id,
416-
type: module.type,
417-
metadata: module.metadata,
418-
wallet: async () => {
419-
let instance = this.instances[module.id];
363+
return instance;
364+
},
365+
};
420366

421-
if (instance) {
422-
return instance;
423-
}
367+
this.modules.push(moduleState);
424368

425-
instance = await this.setupInstance(module);
369+
this.store.dispatch({
370+
type: "ADD_WALLET_MODULE",
371+
payload: {
372+
module: moduleState,
373+
},
374+
});
426375

427-
this.instances[module.id] = instance;
376+
if (moduleState.type !== "instant-link") {
377+
return;
378+
}
428379

429-
return instance;
430-
},
431-
});
432-
}
380+
const wallet = (await moduleState.wallet()) as InstantLinkWallet;
381+
if (!wallet.metadata.runOnStartup) {
382+
return;
383+
}
433384

434-
this.modules = modules;
385+
try {
386+
await wallet.signIn({ contractId: wallet.getContractId() });
387+
} catch (err) {
388+
logger.error(
389+
"Failed to sign in to wallet. " + wallet.metadata.name + err
390+
);
391+
}
392+
})
393+
.catch((err) => {
394+
logger.log("Failed to setup module");
395+
logger.error(err);
396+
});
397+
}
435398

436399
const {
437400
accounts,
@@ -442,32 +405,14 @@ export class WalletModules {
442405
} = await this.resolveStorageState();
443406

444407
this.store.dispatch({
445-
type: "SETUP_WALLET_MODULES",
408+
type: "SETUP",
446409
payload: {
447-
modules,
448410
accounts,
449411
contract,
450412
selectedWalletId,
451413
recentlySignedInWallets,
452414
rememberRecentWallets,
453415
},
454416
});
455-
456-
for (let i = 0; i < this.modules.length; i++) {
457-
if (this.modules[i].type !== "instant-link") {
458-
continue;
459-
}
460-
461-
const wallet = (await this.modules[i].wallet()) as InstantLinkWallet;
462-
if (!wallet.metadata.runOnStartup) {
463-
continue;
464-
}
465-
466-
try {
467-
await wallet.signIn({ contractId: wallet.getContractId() });
468-
} catch (err) {
469-
logger.error("Failed to sign in to wallet. " + err);
470-
}
471-
}
472417
}
473418
}

packages/core/src/lib/store.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ const reducer = (
2323
logger.log("Store Action", action);
2424

2525
switch (action.type) {
26-
case "SETUP_WALLET_MODULES": {
26+
case "SETUP": {
2727
const {
28-
modules,
2928
accounts,
3029
contract,
3130
selectedWalletId,
@@ -42,14 +41,25 @@ const reducer = (
4241

4342
return {
4443
...state,
45-
modules,
4644
accounts: accountStates,
4745
contract,
4846
selectedWalletId,
4947
recentlySignedInWallets,
5048
rememberRecentWallets,
5149
};
5250
}
51+
case "ADD_WALLET_MODULE": {
52+
const { module } = action.payload;
53+
let modules = [...state.modules, module];
54+
55+
// sort by listIndex
56+
modules = modules.sort((a, b) => a.listIndex - b.listIndex);
57+
58+
return {
59+
...state,
60+
modules,
61+
};
62+
}
5363
case "WALLET_CONNECTED": {
5464
const { walletId, contract, accounts, recentlySignedInWallets } =
5565
action.payload;

0 commit comments

Comments
 (0)