Skip to content

Commit 308f96d

Browse files
Guillermo Alejandro Gallardo DiezGuillermo Alejandro Gallardo Diez
Guillermo Alejandro Gallardo Diez
authored and
Guillermo Alejandro Gallardo Diez
committed
fix: bitte wallet login
1 parent 03387cc commit 308f96d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

packages/core/src/lib/constants.ts

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

99
export const CONTRACT = "contract";
10+
export const PENDING_CONTRACT = "contract:pending";
11+
1012
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

+55-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import type {
1111
} from "../../wallet";
1212
import type { StorageService } from "../storage/storage.service.types";
1313
import type { Options } from "../../options.types";
14-
import type { ModuleState, Store } from "../../store.types";
14+
import type { ContractState, 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,
2123
REMEMBER_RECENT_WALLETS,
2224
REMEMBER_RECENT_WALLETS_STATE,
2325
} from "../../constants";
@@ -75,10 +77,49 @@ export class WalletModules {
7577

7678
private async resolveStorageState() {
7779
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+
);
7886
const rememberRecentWallets = await jsonStorage.getItem<string>(
7987
REMEMBER_RECENT_WALLETS
8088
);
8189

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+
82123
const { contract, selectedWalletId } = this.store.getState();
83124
const accounts = await this.validateWallet(selectedWalletId);
84125

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

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+
152206
if (selectedWalletId && selectedWalletId !== walletId) {
153207
await this.signOutWallet(selectedWalletId);
154208
}

0 commit comments

Comments
 (0)