Skip to content

Commit

Permalink
Merge pull request #8 from argentlabs/fix/last-connected
Browse files Browse the repository at this point in the history
Fix/last connected wallet
  • Loading branch information
bluecco authored Oct 10, 2023
2 parents e9591f4 + 47d86c2 commit 7c2062b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/connectors/argentMobile/modal/starknet/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class StarknetAdapter
extends NamespaceAdapter
implements ConnectedStarknetWindowObject
{
id = "starknetkit"
name = "Starknet Kit"
id = "argentMobile"
name = "Argent Mobile"
version = "0.1.0"
icon = ""
provider: ProviderInterface
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/lastConnected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const setStarknetLastConnectedWallet = (id: string) => {
localStorage.setItem("starknetLastConnectedWallet", id)
}

export const removeStarknetLastConnectedWallet = () => {
localStorage.removeItem("starknetLastConnectedWallet")
}
18 changes: 13 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { mapModalWallets } from "./helpers/mapModalWallets"
import { resetWalletConnect } from "./helpers/resetWalletConnect"
import Modal from "./modal/Modal.svelte"
import type { ConnectOptions, ModalWallet } from "./types/modal"
import {
removeStarknetLastConnectedWallet,
setStarknetLastConnectedWallet,
} from "./helpers/lastConnected"

export const connect = async ({
modalMode = "canAsk",
Expand All @@ -28,29 +32,29 @@ export const connect = async ({
})
: connectors

const lastWallet = await sn.getLastConnectedWallet()
const lastWalletId = localStorage.getItem("starknetLastConnectedWallet")
if (modalMode === "neverAsk") {
const connector = availableConnectors.find((c) => c.id === lastWallet?.id)
const connector = availableConnectors.find((c) => c.id === lastWalletId)
await connector?.connect()
return connector?.wallet || null
}

const installedWallets = await sn.getAvailableWallets(restOptions)

// we return/display wallet options once per first-dapp (ever) connect
if (modalMode === "canAsk" && lastWallet) {
if (modalMode === "canAsk" && lastWalletId) {
const preAuthorizedWallets = await sn.getPreAuthorizedWallets({
...restOptions,
})

const wallet =
preAuthorizedWallets.find((w) => w.id === lastWallet?.id) ??
preAuthorizedWallets.find((w) => w.id === lastWalletId) ??
installedWallets.length === 1
? installedWallets[0]
: undefined

if (wallet) {
const connector = availableConnectors.find((c) => c.id === lastWallet?.id)
const connector = availableConnectors.find((c) => c.id === lastWalletId)
await connector?.connect()
return wallet
} // otherwise fallback to modal
Expand All @@ -70,6 +74,9 @@ export const connect = async ({
dappName,
callback: async (value: StarknetWindowObject | null) => {
try {
if (value.id !== "argentWebWallet") {
setStarknetLastConnectedWallet(value.id)
}
resolve(value)
} finally {
setTimeout(() => modal.$destroy())
Expand All @@ -84,5 +91,6 @@ export const connect = async ({

export function disconnect(options: DisconnectOptions = {}): Promise<void> {
resetWalletConnect()
removeStarknetLastConnectedWallet()
return sn.disconnect(options)
}

0 comments on commit 7c2062b

Please sign in to comment.