Skip to content

Commit

Permalink
fix: polkadot eager connect no modal
Browse files Browse the repository at this point in the history
  • Loading branch information
LukassF committed Feb 16, 2024
1 parent 94a7e2f commit 8d5acbf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 97 deletions.
2 changes: 1 addition & 1 deletion sdk/apps/modal-example/src/routes/aleph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function Polkadot() {
document.getElementById('modalAnchor')
)

adapter.canEagerConnect().then((canEagerConnect) => {
adapter.canEagerConnect().then((canEagerConnect: boolean) => {
setEager(canEagerConnect)
})
setAdapter(adapter)
Expand Down
39 changes: 13 additions & 26 deletions sdk/packages/selector-base/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { getSessionIdLocalStorageKey } from '@nightlylabs/nightly-connect-base'
import { ILocalStorage, getStorage } from 'isomorphic-localstorage'
import { AccountWalletType } from './types'

let _localStorage: ILocalStorage | null = null

type WalletInfo = {
walletName: string
walletType: AccountWalletType
}

export const getLocalStorage = () => {
if (_localStorage === null) {
_localStorage = getStorage('./nightly-connect-session')
Expand All @@ -13,21 +19,24 @@ export const getLocalStorage = () => {

// recent wallet from standard

export const persistRecentStandardWalletForNetwork = (walletName: string, network: string) => {
export const persistRecentWalletForNetwork = (network: string, walletInfo: WalletInfo) => {
const storage = getLocalStorage()

storage.setItem('NIGHTLY_CONNECT_SELECTOR_RECENT_STANDARD_WALLET_' + network, walletName)
storage.setItem(
'NIGHTLY_CONNECT_SELECTOR_RECENT_STANDARD_WALLET_' + network,
JSON.stringify(walletInfo)
)
}

export const getRecentStandardWalletForNetwork = (network: string) => {
export const getRecentWalletForNetwork = (network: string) => {
const storage = getLocalStorage()

const item = storage.getItem('NIGHTLY_CONNECT_SELECTOR_RECENT_STANDARD_WALLET_' + network)

return item
}

export const clearRecentStandardWalletForNetwork = (network: string) => {
export const clearRecentWalletForNetwork = (network: string) => {
const storage = getLocalStorage()

storage.removeItem('NIGHTLY_CONNECT_SELECTOR_RECENT_STANDARD_WALLET_' + network)
Expand All @@ -40,25 +49,3 @@ export const clearSessionIdForNetwork = (network: string) => {

storage.removeItem(getSessionIdLocalStorageKey(network))
}

// info if any wallet from standard is connected

export const persistStandardConnectForNetwork = (network: string) => {
const storage = getLocalStorage()

storage.setItem('NIGHTLY_CONNECT_SELECTOR_IS_DESKTOP_CONNECTED_' + network, 'true')
}

export const isStandardConnectedForNetwork = (network: string) => {
const storage = getLocalStorage()

const item = storage.getItem('NIGHTLY_CONNECT_SELECTOR_IS_DESKTOP_CONNECTED_' + network)

return item !== null
}

export const persistStandardDisconnectForNetwork = (network: string) => {
const storage = getLocalStorage()

storage.removeItem('NIGHTLY_CONNECT_SELECTOR_IS_DESKTOP_CONNECTED_' + network)
}
5 changes: 5 additions & 0 deletions sdk/packages/selector-base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ export enum ConnectionType {
Nightly = 'Nightly',
WalletStandard = 'WalletStandard'
}

export enum AccountWalletType {
Standard = 'Standard',
Relay = 'Relay'
}
133 changes: 63 additions & 70 deletions sdk/packages/selector-polkadot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import {
import {
NightlyConnectSelectorModal,
XMLOptions,
clearRecentStandardWalletForNetwork,
clearRecentWalletForNetwork,
clearSessionIdForNetwork,
getRecentStandardWalletForNetwork,
getRecentWalletForNetwork,
isMobileBrowser,
isStandardConnectedForNetwork,
// isStandardConnectedForNetwork,
logoBase64,
persistRecentStandardWalletForNetwork,
persistStandardConnectForNetwork,
persistStandardDisconnectForNetwork,
persistRecentWalletForNetwork,
// persistStandardConnectForNetwork,
// persistStandardDisconnectForNetwork,
sleep,
triggerConnect
triggerConnect,
AccountWalletType
} from '@nightlylabs/wallet-selector-base'

import { type Signer as InjectedSigner } from '@polkadot/api/types'
Expand Down Expand Up @@ -157,7 +158,7 @@ export class NightlyConnectAdapter implements Injected {

adapter.walletsList = getPolkadotWalletsList(
[],
getRecentStandardWalletForNetwork(adapter.network) ?? undefined
getRecentWalletForNetwork(adapter.network) ?? undefined
)
adapter._modal = new NightlyConnectSelectorModal(
adapter.walletsList,
Expand All @@ -176,7 +177,7 @@ export class NightlyConnectAdapter implements Injected {

adapter.walletsList = getPolkadotWalletsList(
metadataWallets,
getRecentStandardWalletForNetwork(adapter.network) ?? undefined
getRecentWalletForNetwork(adapter.network) ?? undefined
)

return adapter
Expand All @@ -200,7 +201,7 @@ export class NightlyConnectAdapter implements Injected {

adapter.walletsList = getPolkadotWalletsList(
[],
getRecentStandardWalletForNetwork(adapter.network) ?? undefined
getRecentWalletForNetwork(adapter.network) ?? undefined
)
adapter._modal = new NightlyConnectSelectorModal(
adapter.walletsList,
Expand All @@ -220,7 +221,7 @@ export class NightlyConnectAdapter implements Injected {
adapter._metadataWallets = metadataWallets
adapter.walletsList = getPolkadotWalletsList(
metadataWallets,
getRecentStandardWalletForNetwork(adapter.network) ?? undefined
getRecentWalletForNetwork(adapter.network) ?? undefined
)

adapter._loading = false
Expand Down Expand Up @@ -250,7 +251,7 @@ export class NightlyConnectAdapter implements Injected {

adapter.walletsList = getPolkadotWalletsList(
[],
getRecentStandardWalletForNetwork(adapter.network) ?? undefined
getRecentWalletForNetwork(adapter.network) ?? undefined
)
adapter._modal = new NightlyConnectSelectorModal(
adapter.walletsList,
Expand All @@ -266,9 +267,7 @@ export class NightlyConnectAdapter implements Injected {
}
// ensureLoaded = async () => {}
canEagerConnect = async () => {
if (!this._useEagerConnect) {
return false
}
if (getRecentWalletForNetwork(this.network) == null || !this._useEagerConnect) return false

// utility for case if somebody wants to fire connect asap, but doesn't want to show modal if e. g. there was no user connected on the device yet
if (this._loading) {
Expand All @@ -289,19 +288,14 @@ export class NightlyConnectAdapter implements Injected {
return true
}

if (
getRecentStandardWalletForNetwork(this.network) !== null &&
isStandardConnectedForNetwork(this.network)
) {
return true
}
if (getRecentWalletForNetwork(this.network) !== null) return true

return false
}

eagerConnectDeeplink = () => {
if (isMobileBrowser() && this._app) {
const mobileWalletName = getRecentStandardWalletForNetwork(this.network)
const mobileWalletName = getRecentWalletForNetwork(this.network)
const wallet = this.walletsList.find((w) => w.name === mobileWalletName)

if (typeof wallet === 'undefined') {
Expand Down Expand Up @@ -411,8 +405,11 @@ export class NightlyConnectAdapter implements Injected {
throw new Error('No accounts found')
}

persistRecentStandardWalletForNetwork(walletName, this.network)
persistStandardConnectForNetwork(this.network)
persistRecentWalletForNetwork(this.network, {
walletName,
walletType: AccountWalletType.Standard
})
// persistStandardConnectForNetwork(this.network)
this._innerStandardAdapter = {
...inject,
signer: {
Expand All @@ -433,7 +430,7 @@ export class NightlyConnectAdapter implements Injected {
onSuccess()
} catch {
// clear recent wallet
persistStandardDisconnectForNetwork(this.network)
clearRecentWalletForNetwork(this.network)
if (this._modal) {
this._modal.setStandardWalletConnectProgress(false)
}
Expand All @@ -449,12 +446,38 @@ export class NightlyConnectAdapter implements Injected {
return
}

let intervalId: NodeJS.Timeout
const recentWallet = getRecentWalletForNetwork(this.network)
if (this._useEagerConnect && recentWallet !== null) {
await this.connectToStandardWallet(JSON.parse(recentWallet).walletName, resolve)

if (this._connected) {
return resolve()
}
}

if (this._app?.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) {
// Try to eager connect if session is restored
try {
this.eagerConnectDeeplink()
this._connected = true
this._connecting = false
this._appSessionActive = true
resolve()
return
} catch (error) {
// If we fail because of whatever reason
// Reset session since it might be corrupted
const [app] = await NightlyConnectAdapter.initApp(this._appInitData)
this._app = app
}
}

let loadingInterval: NodeJS.Timeout

// opening modal and waiting for sessionId
if (this._modal) {
this._modal.onClose = () => {
if (intervalId) clearInterval(intervalId)
clearInterval(loadingInterval)

if (this._connecting) {
this._connecting = false
Expand All @@ -468,60 +491,30 @@ export class NightlyConnectAdapter implements Injected {
!this.walletsList.find((w) => w.name === walletName)?.injectedWallet
) {
this.connectToMobileWallet(walletName)
if (intervalId) clearInterval(intervalId)
clearInterval(loadingInterval)
} else {
this.connectToStandardWallet(walletName, resolve)
if (intervalId) clearInterval(intervalId)
clearInterval(loadingInterval)
}
})

// checking whether sessionId is defined
let checks = 0
intervalId = setInterval(async (): Promise<void> => {
loadingInterval = setInterval(async (): Promise<void> => {
checks++
if (this._app) {
if (this._modal) this._modal.sessionId = this._app.sessionId
if (intervalId) clearInterval(intervalId)

if (this._app.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) {
// Try to eager connect if session is restored
try {
this.eagerConnectDeeplink()
this._connected = true
this._connecting = false
this._appSessionActive = true
resolve()
return
} catch (error) {
// If we fail because of whatever reason
// Reset session since it might be corrupted
const [app] = await NightlyConnectAdapter.initApp(this._appInitData)
this._app = app
}
}

const recentName = getRecentStandardWalletForNetwork(this.network)
if (
this._useEagerConnect &&
recentName !== null &&
isStandardConnectedForNetwork(this.network)
) {
await this.connectToStandardWallet(recentName, resolve)

if (this._connected) {
return
}
}
clearInterval(loadingInterval)

this._app.on('userConnected', () => {
try {
if (this._chosenMobileWalletName) {
persistRecentStandardWalletForNetwork(
this._chosenMobileWalletName,
this.network
)
persistRecentWalletForNetwork(this.network, {
walletName: this._chosenMobileWalletName,
walletType: AccountWalletType.Standard
})
} else {
clearRecentStandardWalletForNetwork(this.network)
clearRecentWalletForNetwork(this.network)
}
if (!this._app || this._app.accounts.activeAccounts.length <= 0) {
this._connecting = false
Expand All @@ -543,7 +536,7 @@ export class NightlyConnectAdapter implements Injected {

// fallback when connecting takes too long
if (checks > 500) {
if (intervalId) clearInterval(intervalId)
clearInterval(loadingInterval)
reject(new Error('Connecting takes too long'))
}
}, 10)
Expand All @@ -563,7 +556,7 @@ export class NightlyConnectAdapter implements Injected {

this.walletsList = getPolkadotWalletsList(
metadataWallets,
getRecentStandardWalletForNetwork(this.network) ?? undefined
getRecentWalletForNetwork(this.network) ?? undefined
)
} catch (e) {
this._connecting = false
Expand Down Expand Up @@ -593,12 +586,12 @@ export class NightlyConnectAdapter implements Injected {
this._app = await AppPolkadot.build(this._appInitData)
if (this._innerStandardAdapter) {
this._innerStandardAdapter = undefined
persistStandardDisconnectForNetwork(this.network)
clearRecentWalletForNetwork(this.network)
}
// Update recent wallet
this.walletsList = getPolkadotWalletsList(
this._metadataWallets,
getRecentStandardWalletForNetwork(this.network) ?? undefined
getRecentWalletForNetwork(this.network) ?? undefined
)
if (this._modal) {
this._modal.walletsList = this.walletsList
Expand Down

0 comments on commit 8d5acbf

Please sign in to comment.