From 9395c698c6561bbfbbf347f561a6c7c7d2012d02 Mon Sep 17 00:00:00 2001 From: LukassF Date: Wed, 21 Feb 2024 15:21:02 +0100 Subject: [PATCH 01/27] add: lazy loading modal solana selector --- sdk/apps/modal-example/package.json | 4 +- .../modal-example/src/routes/alephCustom.tsx | 2 +- .../src/routes/alephInitOnConnect.tsx | 4 +- sdk/apps/modal-example/src/routes/solana.tsx | 3 +- .../src/routes/solanaInitOnConnect.tsx | 4 +- .../modal-example/src/routes/solanaLazy.tsx | 3 +- sdk/packages/selector-solana/package.json | 6 +- sdk/packages/selector-solana/src/adapter.ts | 569 ++++++++++-------- sdk/pnpm-lock.yaml | 37 +- 9 files changed, 351 insertions(+), 281 deletions(-) diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index 0f5dbf66..fb846521 100644 --- a/sdk/apps/modal-example/package.json +++ b/sdk/apps/modal-example/package.json @@ -17,7 +17,7 @@ "dependencies": { "@mysten/sui.js": "^0.42.0", "@mysten/wallet-adapter-wallet-standard": "^0.8.0", - "@nightlylabs/wallet-selector-solana": "0.2.7", + "@nightlylabs/wallet-selector-solana": "0.2.8", "@nightlylabs/nightly-connect-solana": "0.0.29", "@nightlylabs/nightly-connect-sui": "0.0.29", "@nightlylabs/wallet-selector-sui": "0.2.7", @@ -38,4 +38,4 @@ "engines": { "node": ">=16.8" } -} \ No newline at end of file +} diff --git a/sdk/apps/modal-example/src/routes/alephCustom.tsx b/sdk/apps/modal-example/src/routes/alephCustom.tsx index b411a4fa..36e2ffbe 100644 --- a/sdk/apps/modal-example/src/routes/alephCustom.tsx +++ b/sdk/apps/modal-example/src/routes/alephCustom.tsx @@ -29,7 +29,7 @@ export default function Polkadot() { }, network: 'AlephZero' }, - true, // change this to false to test disabling eager connect + {}, // change this to false to test disabling eager connect document.getElementById('modalAnchor'), { variablesOverride: { diff --git a/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx b/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx index cec5003e..a49026c8 100644 --- a/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx +++ b/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx @@ -13,7 +13,7 @@ export default function Polkadot() { const provider = new WsProvider('wss://ws.test.azero.dev/') onMount(async () => { - const adapter = NightlyConnectAdapter.buildWithInitOnConnect( + const adapter = NightlyConnectAdapter.buildLazy( { appMetadata: { name: 'NC TEST AlephZero', @@ -23,7 +23,7 @@ export default function Polkadot() { }, network: 'AlephZero' }, - true, // change this to false to test disabling eager connect + { initOnConnect: true }, // change this to false to test disabling eager connect document.getElementById('modalAnchor') ) diff --git a/sdk/apps/modal-example/src/routes/solana.tsx b/sdk/apps/modal-example/src/routes/solana.tsx index 4831f50b..0874a831 100644 --- a/sdk/apps/modal-example/src/routes/solana.tsx +++ b/sdk/apps/modal-example/src/routes/solana.tsx @@ -20,7 +20,7 @@ export default function Solana() { additionalInfo: 'Courtesy of Nightly Connect team' } }, - true, + {}, document.getElementById('modalAnchor') ).then((adapter) => { adapter.on('connect', (pk) => { @@ -32,6 +32,7 @@ export default function Solana() { }) adapter.canEagerConnect().then((canEagerConnect) => { + console.log('can eager solana', canEagerConnect) setEager(canEagerConnect) }) diff --git a/sdk/apps/modal-example/src/routes/solanaInitOnConnect.tsx b/sdk/apps/modal-example/src/routes/solanaInitOnConnect.tsx index d89e360f..7803ca4e 100644 --- a/sdk/apps/modal-example/src/routes/solanaInitOnConnect.tsx +++ b/sdk/apps/modal-example/src/routes/solanaInitOnConnect.tsx @@ -10,7 +10,7 @@ export default function SolanaLazy() { const [adapter, setAdapter] = createSignal() const [publicKey, setPublicKey] = createSignal() onMount(() => { - const adapter = NightlyConnectAdapter.buildWithInitOnConnect( + const adapter = NightlyConnectAdapter.buildLazy( { appMetadata: { name: 'NCTestSolana', @@ -20,7 +20,7 @@ export default function SolanaLazy() { }, url: 'https://nc2.nightly.app' }, - true, + { initOnConnect: true }, document.getElementById('modalAnchor') ) diff --git a/sdk/apps/modal-example/src/routes/solanaLazy.tsx b/sdk/apps/modal-example/src/routes/solanaLazy.tsx index 7772d83b..b36b01a2 100644 --- a/sdk/apps/modal-example/src/routes/solanaLazy.tsx +++ b/sdk/apps/modal-example/src/routes/solanaLazy.tsx @@ -21,7 +21,7 @@ export default function SolanaLazy() { }, url: 'https://nc2.nightly.app' }, - true, + {}, document.getElementById('modalAnchor') ) @@ -34,6 +34,7 @@ export default function SolanaLazy() { }) adapter.canEagerConnect().then((canEagerConnect) => { + console.log('can eager connect', canEagerConnect) setEager(canEagerConnect) }) diff --git a/sdk/packages/selector-solana/package.json b/sdk/packages/selector-solana/package.json index bed9c9cb..d486f137 100644 --- a/sdk/packages/selector-solana/package.json +++ b/sdk/packages/selector-solana/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-solana", - "version": "0.2.7", + "version": "0.2.8", "description": "", "type": "module", "exports": { @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-solana": "^0.0.29", - "@nightlylabs/wallet-selector-base": "^0.2.4", + "@nightlylabs/wallet-selector-base": "^0.3.0", "@solana/wallet-adapter-base": "^0.9.22", "@solana/wallet-standard": "^1.0.2", "@solana/web3.js": "^1.77.2", @@ -41,4 +41,4 @@ "tslib": "^2.5.3", "typescript": "^5.1.3" } -} \ No newline at end of file +} diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 18f1e848..c79ea623 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -1,22 +1,22 @@ import { AppSolana, SOLANA_NETWORK } from '@nightlylabs/nightly-connect-solana' import { AppInitData, - clearRecentStandardWalletForNetwork, + clearRecentWalletForNetwork, clearSessionIdForNetwork, - getRecentStandardWalletForNetwork, + getRecentWalletForNetwork, getWalletsList, isMobileBrowser, IWalletListItem, logoBase64, MetadataWallet, NightlyConnectSelectorModal, - persistRecentStandardWalletForNetwork, - persistStandardConnectForNetwork, - isStandardConnectedForNetwork, + persistRecentWalletForNetwork, triggerConnect, - persistStandardDisconnectForNetwork, sleep, - XMLOptions + XMLOptions, + ConnectionType, + ConnectionOptions, + defaultConnectionOptions } from '@nightlylabs/wallet-selector-base' import { BaseMessageSignerWalletAdapter, @@ -55,7 +55,6 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { private _modal: NightlyConnectSelectorModal | undefined private _appInitData: AppInitData - private _eagerConnectForStandardWallets: boolean private _metadataWallets: MetadataWallet[] = [] private _walletsList: IWalletListItem[] = [] @@ -64,22 +63,23 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { private _loading: boolean - private _initOnConnect: boolean + private _connectionOptions: ConnectionOptions = defaultConnectionOptions - constructor( - appInitData: AppInitData, - eagerConnectForStandardWallets?: boolean, - initOnConnect = false - ) { + constructor(appInitData: AppInitData, connectionOptions?: ConnectionOptions) { super() this._connecting = false this._connected = false this._publicKey = null this._appInitData = appInitData - this._eagerConnectForStandardWallets = !!eagerConnectForStandardWallets + if (appInitData.persistent !== false) this._appInitData.persistent = true + this._appSessionActive = false this._loading = false - this._initOnConnect = initOnConnect + this._connectionOptions = { ...this._connectionOptions, ...connectionOptions } + // If not persistent, clear session id + if (!this._appInitData.persistent) { + clearSessionIdForNetwork(SOLANA_NETWORK) + } } get connecting() { @@ -109,6 +109,14 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { } } + get sessionId() { + return this._app?.sessionId + } + + get qrCode() { + return this._modal?.qrCode + } + public static initApp = async ( appInitData: AppInitData ): Promise<[AppSolana, MetadataWallet[]]> => { @@ -154,7 +162,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { public static build = async ( appInitData: AppInitData, - eagerConnectForStandardWallets?: boolean, + connectionOptions?: ConnectionOptions, anchorRef?: HTMLElement | null, uiOverrides?: { variablesOverride?: object @@ -162,7 +170,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { qrConfigOverride?: Partial } ) => { - const adapter = new NightlyConnectAdapter(appInitData, eagerConnectForStandardWallets) + const adapter = new NightlyConnectAdapter(appInitData, connectionOptions) if (adapter._readyState === WalletReadyState.Unsupported) { return adapter @@ -171,21 +179,22 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter.walletsList = getWalletsList( [], solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) - adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList, - appInitData.url ?? 'https://nc2.nightly.app', - { - name: SOLANA_NETWORK, - icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png' - }, - anchorRef, - uiOverrides?.variablesOverride, - uiOverrides?.stylesOverride, - uiOverrides?.qrConfigOverride - ) + if (!adapter._connectionOptions.disableModal) + adapter._modal = new NightlyConnectSelectorModal( + adapter.walletsList, + appInitData.url ?? 'https://nc2.nightly.app', + { + name: SOLANA_NETWORK, + icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png' + }, + anchorRef, + uiOverrides?.variablesOverride, + uiOverrides?.stylesOverride, + uiOverrides?.qrConfigOverride + ) const [app, metadataWallets] = await NightlyConnectAdapter.initApp(appInitData) @@ -195,7 +204,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter.walletsList = getWalletsList( metadataWallets, solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) return adapter @@ -203,7 +212,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { public static buildLazy = ( appInitData: AppInitData, - eagerConnectForStandardWallets?: boolean, + connectionOptions?: ConnectionOptions, anchorRef?: HTMLElement | null, uiOverrides?: { variablesOverride?: object @@ -211,7 +220,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { qrConfigOverride?: Partial } ) => { - const adapter = new NightlyConnectAdapter(appInitData, eagerConnectForStandardWallets) + const adapter = new NightlyConnectAdapter(appInitData, connectionOptions) if (adapter._readyState === WalletReadyState.Unsupported) { return adapter @@ -220,217 +229,226 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter.walletsList = getWalletsList( [], solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined - ) - - adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList, - appInitData.url ?? 'https://nc2.nightly.app', - { - name: SOLANA_NETWORK, - icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png' - }, - anchorRef, - uiOverrides?.variablesOverride, - uiOverrides?.stylesOverride, - uiOverrides?.qrConfigOverride + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) - adapter._loading = true - - NightlyConnectAdapter.initApp(appInitData).then(([app, metadataWallets]) => { - adapter._app = app - adapter._metadataWallets = metadataWallets + // Fetch wallets from registry + adapter.fetchWalletsFromRegistry().then((metadataWallets) => { + adapter._metadataWallets = metadataWallets.map((wallet) => ({ + slug: wallet.slug, + name: wallet.name, + icon: wallet.image.default, + deeplink: wallet.mobile, + link: wallet.homepage, + walletType: wallet.walletType + })) adapter.walletsList = getWalletsList( - metadataWallets, + [], solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) - - adapter._loading = false }) - return adapter - } + if (!adapter._connectionOptions.disableModal) + adapter._modal = new NightlyConnectSelectorModal( + adapter.walletsList, + appInitData.url ?? 'https://nc2.nightly.app', + { + name: SOLANA_NETWORK, + icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png' + }, + anchorRef, + uiOverrides?.variablesOverride, + uiOverrides?.stylesOverride, + uiOverrides?.qrConfigOverride + ) - public static buildWithInitOnConnect = ( - appInitData: AppInitData, - eagerConnectForStandardWallets?: boolean, - anchorRef?: HTMLElement | null, - uiOverrides?: { - variablesOverride?: object - stylesOverride?: string - qrConfigOverride?: Partial - } - ) => { - const adapter = new NightlyConnectAdapter(appInitData, eagerConnectForStandardWallets, true) + // If init on connect is not enabled, we should initialize app + if (!adapter._connectionOptions.initOnConnect) { + adapter._loading = true - if (adapter._readyState === WalletReadyState.Unsupported) { - return adapter - } + NightlyConnectAdapter.initApp(appInitData) + .then(([app, metadataWallets]) => { + adapter._app = app + adapter._metadataWallets = metadataWallets - adapter.walletsList = getWalletsList( - [], - solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined - ) + adapter.walletsList = getWalletsList( + metadataWallets, + solanaWalletsFilter, + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined + ) - adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList, - appInitData.url ?? 'https://nc2.nightly.app', - { - name: SOLANA_NETWORK, - icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png' - }, - anchorRef, - uiOverrides?.variablesOverride, - uiOverrides?.stylesOverride, - uiOverrides?.qrConfigOverride - ) + adapter._loading = false + }) + .catch(() => { + adapter._loading = false + throw new Error('Failed to initialize adapter') + }) + } return adapter } + // Checks if we can restore user session canEagerConnect = async () => { - // 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) { - for (let i = 0; i < 200; i++) { - await sleep(10) + // If eager connect is disabled, we can't eager connect + if (this._connectionOptions.disableEagerConnect) return false - if (!this._loading) { - break - } - } - } + // Get recent wallet for network + const recentWallet = getRecentWalletForNetwork(SOLANA_NETWORK) - if (this._loading) { - false - } - - if (this._app && this._app.hasBeenRestored() && this._app.connectedPublicKeys.length > 0) { - return true - } + // If there is no recent wallet, we can't eager connect + if (recentWallet === null) return false + // If we use wallet standard, we can eager connect if ( - this._eagerConnectForStandardWallets && - getRecentStandardWalletForNetwork(SOLANA_NETWORK) !== null && - isStandardConnectedForNetwork(SOLANA_NETWORK) + recentWallet.walletName !== null && + recentWallet.walletType === ConnectionType.WalletStandard ) { return true } + // If we use nightly connect we need to make sure app is restored + if (recentWallet.walletType === ConnectionType.Nightly) { + if (this._connectionOptions.initOnConnect) { + return false + } + // Wait for app to be restored + if (this._loading) { + for (let i = 0; i < 2000; i++) { + await sleep(10) + if (!this._loading) { + break + } + } + } + + if (this._loading) { + return false + } + + // If app is restored and has connected public keys, we can eager connect + + if (this._app && this._app.hasBeenRestored() && this._app.connectedPublicKeys.length > 0) { + return true + } + } return false } - eagerConnectDeeplink = () => { - if (isMobileBrowser() && this._app) { - const mobileWalletName = getRecentStandardWalletForNetwork(SOLANA_NETWORK) - const wallet = this.walletsList.find((w) => w.name === mobileWalletName) + connectToMobileWallet = (walletName: string) => { + try { + if (this._modal) { + this._modal.setStandardWalletConnectProgress(true) + } + + const wallet = this.walletsList.find((w) => w.name === walletName) + + if (!this._app) { + throw new Error('Wallet not ready') + } if (typeof wallet === 'undefined') { - return + throw new Error('Wallet not found') } if (wallet.deeplink === null) { - return + throw new Error('Deeplink not found') } + + // If we have a native deeplink, we should use it if (wallet.deeplink.native !== null) { this._app.connectDeeplink({ walletName: wallet.name, url: wallet.deeplink.native }) + + this._chosenMobileWalletName = walletName + + triggerConnect( + wallet.deeplink.native, + this._app.sessionId, + this._appInitData.url ?? 'https://nc2.nightly.app' + ) return } + + // If we have a universal deeplink, we should use it if (wallet.deeplink.universal !== null) { this._app.connectDeeplink({ walletName: wallet.name, url: wallet.deeplink.universal }) - return - } - } - } - connectToMobileWallet = (walletName: string) => { - if (this._modal) { - this._modal.setStandardWalletConnectProgress(true) - } - - const wallet = this.walletsList.find((w) => w.name === walletName) - - if (!this._app || typeof wallet === 'undefined') { - return - } - - if (wallet.deeplink === null) { - return - } - - if (wallet.deeplink.native !== null) { - this._app.connectDeeplink({ - walletName: wallet.name, - url: wallet.deeplink.native - }) + this._chosenMobileWalletName = walletName - this._chosenMobileWalletName = walletName - - triggerConnect( - wallet.deeplink.native, - this._app.sessionId, - this._appInitData.url ?? 'https://nc2.nightly.app' - ) - return - } - - if (wallet.deeplink.universal !== null) { - this._app.connectDeeplink({ - walletName: wallet.name, - url: wallet.deeplink.universal - }) - - this._chosenMobileWalletName = walletName - - triggerConnect( - wallet.deeplink.universal, - this._app.sessionId, - this._appInitData.url ?? 'https://nc2.nightly.app' - ) - return + triggerConnect( + wallet.deeplink.universal, + this._app.sessionId, + this._appInitData.url ?? 'https://nc2.nightly.app' + ) + return + } + } catch (err) { + clearRecentWalletForNetwork(SOLANA_NETWORK) + if (this._modal) { + this._modal.setStandardWalletConnectProgress(false) + } + throw err } } - connectToStandardWallet = async (walletName: string, onSuccess: () => void) => { + // Generic connect to standard wallet + connectToStandardWallet = async (walletName: string) => { try { if (this._modal) { this._modal.setStandardWalletConnectProgress(true) } - const wallet = this.walletsList.find((w) => w.name === walletName) - if (typeof wallet?.standardWallet === 'undefined') { + const wallet = this.walletsList.find((w) => w.name === walletName)?.standardWallet + if (typeof wallet === 'undefined') { + if (this._modal) { + this._modal.setStandardWalletConnectProgress(false) + } throw new Error('Wallet not found') } const adapter = new StandardWalletAdapter({ - wallet: wallet.standardWallet as WalletAdapterCompatibleStandardWallet + wallet: wallet as WalletAdapterCompatibleStandardWallet }) await adapter.connect() - persistRecentStandardWalletForNetwork(walletName, SOLANA_NETWORK) - persistStandardConnectForNetwork(SOLANA_NETWORK) + this._innerStandardAdapter = adapter this._publicKey = adapter.publicKey + this._connected = true this._connecting = false this.emit('connect', this._publicKey!) + + persistRecentWalletForNetwork(SOLANA_NETWORK, { + walletName, + walletType: ConnectionType.WalletStandard + }) + this._modal?.closeModal() - onSuccess() - } catch { + } catch (err) { // clear recent wallet - persistStandardDisconnectForNetwork(SOLANA_NETWORK) + clearRecentWalletForNetwork(SOLANA_NETWORK) if (this._modal) { this._modal.setStandardWalletConnectProgress(false) } + + throw err + } + } + + connectToWallet = async (walletName: string) => { + if (isMobileBrowser() && !this.walletsList.find((w) => w.name === walletName)?.standardWallet) { + this.connectToMobileWallet(walletName) + } else { + this.connectToStandardWallet(walletName) } } @@ -438,144 +456,165 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { new Promise((resolve, reject) => { const innerConnect = async () => { try { - if (this.connected || this.connecting) { + if (this.connecting) { + reject('Cannot connect while connecting') + return + } + + if (this.connected) { resolve() return } if (this._readyState !== WalletReadyState.Loadable) throw new WalletNotReadyError() - if (this._initOnConnect) { - this._connecting = true - - if (!this._app) { - try { - const [app, metadataWallets] = await NightlyConnectAdapter.initApp( - this._appInitData - ) - - this._app = app - this._metadataWallets = metadataWallets - - this.walletsList = getWalletsList( - metadataWallets, - solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined - ) - } catch { - if (!this._app) { - this._connecting = false - throw new WalletNotReadyError() - } - } + const recentWallet = getRecentWalletForNetwork(SOLANA_NETWORK) + if (!this._connectionOptions.disableEagerConnect && recentWallet !== null) { + // Eager connect standard if possible + if (recentWallet.walletType === ConnectionType.WalletStandard) { + await this.connectToStandardWallet(recentWallet.walletName) + resolve() + return } - } else { - if (this._loading) { - // we do it to ensure proper connect flow in case if adapter is lazily built, but e. g. solana wallets selector uses its own eager connect - for (let i = 0; i < 200; i++) { - await sleep(10) - if (!this._loading) { - break + // Eager connect remote if possible + if (recentWallet.walletType === ConnectionType.Nightly) { + if (this._app?.hasBeenRestored() && this._app.connectedPublicKeys.length > 0) { + // Try to eager connect if session is restored + try { + this._publicKey = this._app.connectedPublicKeys[0] + this._connected = true + this._connecting = false + this._appSessionActive = true + this.emit('connect', this._publicKey) + 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 } } - - if (this._loading) { - throw new WalletNotReadyError() - } } - - if (!this._app) { - throw new WalletNotReadyError() - } - - this._connecting = true } - if (this._app.hasBeenRestored() && this._app.connectedPublicKeys.length > 0) { - // Try to eager connect if session is restored - try { - this.eagerConnectDeeplink() - this._publicKey = this._app.connectedPublicKeys[0] - this._connected = true - this._connecting = false - this._appSessionActive = true - this.emit('connect', this._publicKey) - 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 - } + if (this._connectionOptions.disableModal) { + reject('Modal is disabled') + return } - const recentName = getRecentStandardWalletForNetwork(SOLANA_NETWORK) - if ( - this._eagerConnectForStandardWallets && - recentName !== null && - isStandardConnectedForNetwork(SOLANA_NETWORK) - ) { - await this.connectToStandardWallet(recentName, resolve) - - if (this._connected) { - return - } + if (this._connectionOptions.initOnConnect) { + this._loading = true + NightlyConnectAdapter.initApp(this._appInitData) + .then(([app, metadataWallets]) => { + this._app = app + this._metadataWallets = metadataWallets + this.walletsList = getWalletsList( + metadataWallets, + solanaWalletsFilter, + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined + ) + this._loading = false + }) + .catch(() => { + this._loading = false + throw new Error('Failed to initialize adapter') + }) } - this._app.on('userConnected', (e) => { - try { - if (this._chosenMobileWalletName) { - persistRecentStandardWalletForNetwork(this._chosenMobileWalletName, SOLANA_NETWORK) - } else { - clearRecentStandardWalletForNetwork(SOLANA_NETWORK) - } - this._publicKey = new PublicKey(e.publicKeys[0]) - this._connected = true - this._connecting = false - this._appSessionActive = true - this.emit('connect', this._publicKey) - this._modal?.closeModal() - resolve() - } catch { - this.disconnect() - } - }) + // Interval that checks if app has connected + let loadingInterval: NodeJS.Timeout + if (this._modal) { + this._connecting = true this._modal.onClose = () => { + clearInterval(loadingInterval) if (this._connecting) { this._connecting = false - const error = new WalletWindowClosedError() - this.emit('error', error) reject(error) } } - this._modal.openModal(this._app.sessionId, (walletName) => { + this._modal.openModal(this._app?.sessionId ?? undefined, async (walletName) => { + // If we are on mobile and wallet is not injected, we should connect to mobile wallet if ( isMobileBrowser() && !this.walletsList.find((w) => w.name === walletName)?.standardWallet ) { this.connectToMobileWallet(walletName) } else { - this.connectToStandardWallet(walletName, resolve) + await this.connectToStandardWallet(walletName) + resolve() } }) + + // loop until app is connected or we timeout + let checks = 0 + loadingInterval = setInterval(async (): Promise => { + checks++ + if (this._app) { + // Clear interval if app is connected + clearInterval(loadingInterval) + if (this._modal) this._modal.sessionId = this._app.sessionId + + this._app.on('userConnected', (e) => { + try { + persistRecentWalletForNetwork(SOLANA_NETWORK, { + walletName: this._chosenMobileWalletName || '', + walletType: ConnectionType.Nightly + }) + + if (!this._app || this._app.connectedPublicKeys.length <= 0) { + this._connecting = false + this._connected = false + // If user does not pass any accounts, we should disconnect + this.disconnect() + return + } + + this._publicKey = new PublicKey(e.publicKeys[0]) + this._connected = true + this._connecting = false + this._appSessionActive = true + this.emit('connect', this._publicKey) + this._modal?.closeModal() + resolve() + } catch { + this.disconnect() + } + }) + } + // timeout after 5 seconds + if (checks > 500) { + clearInterval(loadingInterval) + // reject(new Error('Connecting takes too long')) + // TODO we need to have a way to show error on modal + } + }, 10) } + // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { this._connecting = false this.emit('error', error) reject(error) + } finally { + this._connecting = false } } innerConnect() }) + fetchWalletsFromRegistry: () => ReturnType = async () => { + return AppSolana.getWalletsMetadata( + `${this._appInitData.url ?? 'https://nc2.nightly.app'}/get_wallets_metadata` + ) + } + disconnect = async () => { if (this.connected) { if (this._appSessionActive) { @@ -593,12 +632,12 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { if (this._innerStandardAdapter) { await this._innerStandardAdapter.disconnect() this._innerStandardAdapter = undefined - persistStandardDisconnectForNetwork(SOLANA_NETWORK) + clearRecentWalletForNetwork(SOLANA_NETWORK) } this.walletsList = getWalletsList( this._metadataWallets, solanaWalletsFilter, - getRecentStandardWalletForNetwork(SOLANA_NETWORK) ?? undefined + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) this._publicKey = null this._connected = false diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 55b2d5b7..bb8d5c45 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -144,7 +144,7 @@ importers: specifier: 0.2.0 version: link:../../packages/selector-polkadot '@nightlylabs/wallet-selector-solana': - specifier: 0.2.7 + specifier: 0.2.8 version: link:../../packages/selector-solana '@nightlylabs/wallet-selector-sui': specifier: 0.2.7 @@ -633,7 +633,7 @@ importers: version: link:../../apps/polkadot '@nightlylabs/wallet-selector-base': specifier: ^0.3.0 - version: link:../selector-base + version: 0.3.1 '@polkadot/api': specifier: 10.10.1 version: 10.10.1 @@ -675,8 +675,8 @@ importers: specifier: ^0.0.29 version: link:../../apps/solana '@nightlylabs/wallet-selector-base': - specifier: ^0.2.4 - version: 0.2.4 + specifier: ^0.3.0 + version: 0.3.1 '@solana/wallet-adapter-base': specifier: ^0.9.22 version: 0.9.22(@solana/web3.js@1.77.2) @@ -5140,6 +5140,21 @@ packages: - utf-8-validate dev: false + /@nightlylabs/wallet-selector-base@0.3.1: + resolution: {integrity: sha512-m2hdNkOrQNS52xXYSvko8YvbI60miCU9AHO8HHKfGXiuYgUjAmQSeTm1wLP8LZI4+Mygqbr8Oq9na+HMEgq2XA==} + dependencies: + '@nightlylabs/nightly-connect-base': 0.0.27 + '@nightlylabs/wallet-selector-modal': 0.2.0 + '@wallet-standard/core': 1.0.3 + isomorphic-localstorage: 1.0.2 + transitivePeerDependencies: + - bufferutil + - encoding + - supports-color + - ts-node + - utf-8-validate + dev: false + /@nightlylabs/wallet-selector-modal@0.1.2: resolution: {integrity: sha512-vxy9S2dEf3NARW6LDq2ZKpWMlk5JJFIuwUfSxkuJlgUg2OVSlnDS7vdho3h4DmluRU5GM9vVhaXUGHAVp5sDQg==} dependencies: @@ -5154,6 +5169,20 @@ packages: - ts-node dev: false + /@nightlylabs/wallet-selector-modal@0.2.0: + resolution: {integrity: sha512-BdEk3FhL65z/X0N9ygPjk7uQvV0GGHTWSwXBVob/l48Nok4ikFcV30Dtxk6iSSbErDZ8U4zV3/78cK+m+4lt8A==} + dependencies: + '@nightlylabs/qr-code': 2.0.4 + autoprefixer: 10.4.14(postcss@8.4.24) + lit: 2.7.2 + postcss: 8.4.24 + postcss-lit: 1.1.0(postcss@8.4.24) + tailwindcss: 3.3.2 + transitivePeerDependencies: + - supports-color + - ts-node + dev: false + /@nightlylabs/wallet-selector-solana@0.2.6(bs58@4.0.1)(react@18.2.0): resolution: {integrity: sha512-cVTKk+c6tGv4GeSQMlUaZ2si4A6ySKj41emkGJ8OtuwmtzwUym4Xuh3chXZYgGrMQgvPrX5+erIR4oq2GmGIPg==} dependencies: From 5678d3cc4fe57f4bc09e33250d37aee2eef6c259 Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 12:04:54 +0100 Subject: [PATCH 02/27] add: fetch all wallets and change type --- sdk/apps/modal-example/src/routes/aleph.tsx | 1 - sdk/apps/modal-example/src/routes/solana.tsx | 1 - .../modal-example/src/routes/solanaLazy.tsx | 1 - sdk/packages/selector-base/package.json | 4 +- sdk/packages/selector-base/src/detection.ts | 4 +- sdk/packages/selector-base/src/modal.ts | 18 ++++- sdk/packages/selector-base/src/types.ts | 44 +++++++++++- sdk/packages/selector-polkadot/package.json | 4 +- sdk/packages/selector-polkadot/src/adapter.ts | 27 ++++---- .../selector-polkadot/src/detection.ts | 22 +++--- sdk/packages/selector-solana/package.json | 2 +- sdk/packages/selector-solana/src/adapter.ts | 67 +++++++------------ sdk/pnpm-lock.yaml | 37 ++-------- 13 files changed, 117 insertions(+), 115 deletions(-) diff --git a/sdk/apps/modal-example/src/routes/aleph.tsx b/sdk/apps/modal-example/src/routes/aleph.tsx index ed5ea9e6..1eb90f2d 100644 --- a/sdk/apps/modal-example/src/routes/aleph.tsx +++ b/sdk/apps/modal-example/src/routes/aleph.tsx @@ -29,7 +29,6 @@ export default function Polkadot() { ) adapter.canEagerConnect().then((canEagerConnect: boolean) => { - console.log('canEagerConnect', canEagerConnect) setEager(canEagerConnect) }) setAdapter(adapter) diff --git a/sdk/apps/modal-example/src/routes/solana.tsx b/sdk/apps/modal-example/src/routes/solana.tsx index 0874a831..7220aeb7 100644 --- a/sdk/apps/modal-example/src/routes/solana.tsx +++ b/sdk/apps/modal-example/src/routes/solana.tsx @@ -32,7 +32,6 @@ export default function Solana() { }) adapter.canEagerConnect().then((canEagerConnect) => { - console.log('can eager solana', canEagerConnect) setEager(canEagerConnect) }) diff --git a/sdk/apps/modal-example/src/routes/solanaLazy.tsx b/sdk/apps/modal-example/src/routes/solanaLazy.tsx index b36b01a2..6209f1c0 100644 --- a/sdk/apps/modal-example/src/routes/solanaLazy.tsx +++ b/sdk/apps/modal-example/src/routes/solanaLazy.tsx @@ -34,7 +34,6 @@ export default function SolanaLazy() { }) adapter.canEagerConnect().then((canEagerConnect) => { - console.log('can eager connect', canEagerConnect) setEager(canEagerConnect) }) diff --git a/sdk/packages/selector-base/package.json b/sdk/packages/selector-base/package.json index 034bfb63..f9fb5d82 100644 --- a/sdk/packages/selector-base/package.json +++ b/sdk/packages/selector-base/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-base", - "version": "0.3.0", + "version": "0.3.1", "description": "", "type": "module", "exports": { @@ -39,4 +39,4 @@ "tslib": "^2.5.3", "typescript": "^5.1.3" } -} \ No newline at end of file +} diff --git a/sdk/packages/selector-base/src/detection.ts b/sdk/packages/selector-base/src/detection.ts index 7af379c0..3ef4449c 100644 --- a/sdk/packages/selector-base/src/detection.ts +++ b/sdk/packages/selector-base/src/detection.ts @@ -1,8 +1,8 @@ import { Wallet, getWallets } from '@wallet-standard/core' -import { type IWalletListItem, type MetadataWallet } from './types' +import { WalletMetadata, type IWalletListItem, type MetadataWallet } from './types' export const getWalletsList = ( - presetList: MetadataWallet[], + presetList: WalletMetadata[], walletsFilterCb: (wallet: Wallet) => boolean, recentWalletName?: string ) => { diff --git a/sdk/packages/selector-base/src/modal.ts b/sdk/packages/selector-base/src/modal.ts index 636fce3a..bc733b7a 100644 --- a/sdk/packages/selector-base/src/modal.ts +++ b/sdk/packages/selector-base/src/modal.ts @@ -1,4 +1,8 @@ -import { type XMLOptions, type NightlySelector } from '@nightlylabs/wallet-selector-modal' +import { + type XMLOptions, + type NightlySelector, + WalletSelectorItem +} from '@nightlylabs/wallet-selector-modal' import { type IWalletListItem, type NetworkData } from './types' import { isMobileBrowser } from './utils' @@ -41,7 +45,11 @@ export class NightlyConnectSelectorModal { ) this._walletsList = filtered if (this._modal) { - this._modal.selectorItems = filtered + this._modal.selectorItems = filtered.map((item) => ({ + ...item, + icon: item.image.default, + link: item.homepage + })) as WalletSelectorItem[] } } @@ -61,7 +69,11 @@ export class NightlyConnectSelectorModal { this._modal.relay = this._relay this._modal.chainIcon = this._networkData.icon this._modal.chainName = this._networkData.name - this._modal.selectorItems = this.walletsList + this._modal.selectorItems = this.walletsList.map((item) => ({ + ...item, + icon: item.image.default, + link: item.homepage + })) as WalletSelectorItem[] }) } diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index 0a14a253..55ecbbbb 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -17,7 +17,49 @@ export interface MetadataWallet { walletType: WalletType } -export interface IWalletListItem extends MetadataWallet { +interface Images { + default: string + sm: string + md: string + lg: string +} + +type Network = string + +type Platform = + | 'browser' + | 'ios' + | 'android' + | 'macos' + | 'windows' + | 'linux' + | 'chrome' + | 'firefox' + | 'opera' + | 'edge' + | 'brave' + | 'safari' + | 'other' + +type Version = string + +export interface WalletMetadata { + slug: string + name: string + description?: string + homepage?: string + chains?: Array + version?: Version + walletType: WalletType + mobile: Deeplink | null + desktop: Deeplink | null + image: Images + app?: Record + injectPath?: Record + lastUpdatedTimestamp?: bigint +} + +export interface IWalletListItem extends WalletMetadata { recent?: boolean detected?: boolean standardWallet?: Wallet diff --git a/sdk/packages/selector-polkadot/package.json b/sdk/packages/selector-polkadot/package.json index 030dba3f..4bcdeb0e 100644 --- a/sdk/packages/selector-polkadot/package.json +++ b/sdk/packages/selector-polkadot/package.json @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-polkadot": "^0.0.15", - "@nightlylabs/wallet-selector-base": "^0.3.0", + "@nightlylabs/wallet-selector-base": "^0.3.1", "@polkadot/extension-inject": "0.46.5", "@polkadot/api": "10.10.1", "@wallet-standard/core": "^1.0.3" @@ -40,4 +40,4 @@ "tslib": "^2.5.3", "typescript": "^5.1.3" } -} \ No newline at end of file +} diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index 72fdf76b..468a527a 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -7,6 +7,7 @@ import { import { ConnectionOptions, ConnectionType, + IWalletListItem, NightlyConnectSelectorModal, XMLOptions, clearRecentWalletForNetwork, @@ -112,7 +113,7 @@ export class NightlyConnectAdapter implements Injected { set walletsList(list: IPolkadotWalletListItem[]) { this._walletsList = list if (this._modal) { - this._modal.walletsList = list + this._modal.walletsList = list as IWalletListItem[] } } @@ -155,7 +156,7 @@ export class NightlyConnectAdapter implements Injected { ) if (!adapter._connectionOptions.disableModal) { adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList, + adapter.walletsList as IWalletListItem[], appInitData.url ?? 'https://nc2.nightly.app', networkToData(adapter.network), anchorRef, @@ -204,7 +205,7 @@ export class NightlyConnectAdapter implements Injected { if (!adapter._connectionOptions.disableModal) { adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList, + adapter.walletsList as IWalletListItem[], appInitData.url ?? 'https://nc2.nightly.app', networkToData(adapter.network), anchorRef, @@ -301,21 +302,21 @@ export class NightlyConnectAdapter implements Injected { throw new Error('Wallet not found') } - if (wallet.deeplink === null) { + if (wallet.mobile === null) { throw new Error('Deeplink not found') } // If we have a native deeplink, we should use it - if (wallet.deeplink.native !== null) { + if (wallet.mobile.native !== null) { this._app.connectDeeplink({ walletName: wallet.name, - url: wallet.deeplink.native + url: wallet.mobile.native }) this._chosenMobileWalletName = walletName triggerConnect( - wallet.deeplink.native, + wallet.mobile.native, this._app.sessionId, this._appInitData.url ?? 'https://nc2.nightly.app' ) @@ -323,16 +324,16 @@ export class NightlyConnectAdapter implements Injected { } // If we have a universal deeplink, we should use it - if (wallet.deeplink.universal !== null) { + if (wallet.mobile.universal !== null) { this._app.connectDeeplink({ walletName: wallet.name, - url: wallet.deeplink.universal + url: wallet.mobile.universal }) this._chosenMobileWalletName = walletName triggerConnect( - wallet.deeplink.universal, + wallet.mobile.universal, this._app.sessionId, this._appInitData.url ?? 'https://nc2.nightly.app' ) @@ -340,8 +341,8 @@ export class NightlyConnectAdapter implements Injected { } // Fallback to redirecting to app browser // aka browser inside the app - if (!wallet.deeplink.redirectToAppBrowser) { - const redirectToAppBrowser = wallet.deeplink.redirectToAppBrowser + if (!wallet.mobile.redirectToAppBrowser) { + const redirectToAppBrowser = wallet.mobile.redirectToAppBrowser if (redirectToAppBrowser !== null && redirectToAppBrowser.indexOf('{{url}}') > -1) { const url = redirectToAppBrowser.replace( '{{url}}', @@ -581,7 +582,7 @@ export class NightlyConnectAdapter implements Injected { getRecentWalletForNetwork(this.network)?.walletName ?? undefined ) if (this._modal) { - this._modal.walletsList = this.walletsList + this._modal.walletsList = this.walletsList as IWalletListItem[] } this._connected = false } finally { diff --git a/sdk/packages/selector-polkadot/src/detection.ts b/sdk/packages/selector-polkadot/src/detection.ts index 829a04aa..649a19a7 100644 --- a/sdk/packages/selector-polkadot/src/detection.ts +++ b/sdk/packages/selector-polkadot/src/detection.ts @@ -42,15 +42,7 @@ export const getPolkadotWalletsList = (presetList: WalletMetadata[], recentWalle const walletsData: Record = {} presetList.forEach((wallet) => { - walletsData[wallet.slug.toLocaleLowerCase()] = { - slug: wallet.slug, - name: wallet.name, - icon: wallet.image.default, - deeplink: wallet.mobile, - link: wallet.homepage, - walletType: wallet.walletType, - recent: recentWalletName === wallet.name - } + walletsData[wallet.slug.toLocaleLowerCase()] = wallet }) for (const wallet of windowWallets) { // Check if wallet is already in the list @@ -91,12 +83,18 @@ export const getPolkadotWalletsList = (presetList: WalletMetadata[], recentWalle walletsData[wallet.name.toLocaleLowerCase()] = { slug: wallet.name, name: wallet.name, - icon: wallet.icon as string, - link: '', - deeplink: null, + image: { + default: wallet.icon as string, + sm: wallet.icon as string, + md: wallet.icon as string, + lg: wallet.icon as string + }, + desktop: null, + mobile: null, recent: recentWalletName === wallet.name, detected: true, injectedWallet: wallet, + homepage: '', walletType: 'hybrid' } } diff --git a/sdk/packages/selector-solana/package.json b/sdk/packages/selector-solana/package.json index d486f137..c0d16592 100644 --- a/sdk/packages/selector-solana/package.json +++ b/sdk/packages/selector-solana/package.json @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-solana": "^0.0.29", - "@nightlylabs/wallet-selector-base": "^0.3.0", + "@nightlylabs/wallet-selector-base": "^0.3.1", "@solana/wallet-adapter-base": "^0.9.22", "@solana/wallet-standard": "^1.0.2", "@solana/web3.js": "^1.77.2", diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index c79ea623..6e01379e 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -8,7 +8,6 @@ import { isMobileBrowser, IWalletListItem, logoBase64, - MetadataWallet, NightlyConnectSelectorModal, persistRecentWalletForNetwork, triggerConnect, @@ -16,7 +15,8 @@ import { XMLOptions, ConnectionType, ConnectionOptions, - defaultConnectionOptions + defaultConnectionOptions, + WalletMetadata } from '@nightlylabs/wallet-selector-base' import { BaseMessageSignerWalletAdapter, @@ -56,7 +56,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { private _appInitData: AppInitData - private _metadataWallets: MetadataWallet[] = [] + private _metadataWallets: WalletMetadata[] = [] private _walletsList: IWalletListItem[] = [] private _chosenMobileWalletName: string | undefined @@ -119,24 +119,13 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { public static initApp = async ( appInitData: AppInitData - ): Promise<[AppSolana, MetadataWallet[]]> => { + ): Promise<[AppSolana, WalletMetadata[]]> => { try { return await Promise.all([ AppSolana.build(appInitData), AppSolana.getWalletsMetadata( `${appInitData.url ?? 'https://nc2.nightly.app'}/get_wallets_metadata` ) - .then((list) => - list.map((wallet) => ({ - slug: wallet.slug, - name: wallet.name, - icon: wallet.image.default, - deeplink: wallet.mobile, - link: wallet.homepage, - walletType: wallet.walletType - })) - ) - .catch(() => [] as MetadataWallet[]) ]) } catch { clearSessionIdForNetwork(SOLANA_NETWORK) @@ -145,17 +134,6 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { AppSolana.getWalletsMetadata( `${appInitData.url ?? 'https://nc2.nightly.app'}/get_wallets_metadata` ) - .then((list) => - list.map((wallet) => ({ - slug: wallet.slug, - name: wallet.name, - icon: wallet.image.default, - deeplink: wallet.mobile, - link: wallet.homepage, - walletType: wallet.walletType - })) - ) - .catch(() => [] as MetadataWallet[]) ]) } } @@ -234,14 +212,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { // Fetch wallets from registry adapter.fetchWalletsFromRegistry().then((metadataWallets) => { - adapter._metadataWallets = metadataWallets.map((wallet) => ({ - slug: wallet.slug, - name: wallet.name, - icon: wallet.image.default, - deeplink: wallet.mobile, - link: wallet.homepage, - walletType: wallet.walletType - })) + adapter._metadataWallets = metadataWallets adapter.walletsList = getWalletsList( [], @@ -353,21 +324,21 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { throw new Error('Wallet not found') } - if (wallet.deeplink === null) { + if (wallet.mobile === null) { throw new Error('Deeplink not found') } // If we have a native deeplink, we should use it - if (wallet.deeplink.native !== null) { + if (wallet.mobile.native !== null) { this._app.connectDeeplink({ walletName: wallet.name, - url: wallet.deeplink.native + url: wallet.mobile.native }) this._chosenMobileWalletName = walletName triggerConnect( - wallet.deeplink.native, + wallet.mobile.native, this._app.sessionId, this._appInitData.url ?? 'https://nc2.nightly.app' ) @@ -375,16 +346,16 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { } // If we have a universal deeplink, we should use it - if (wallet.deeplink.universal !== null) { + if (wallet.mobile.universal !== null) { this._app.connectDeeplink({ walletName: wallet.name, - url: wallet.deeplink.universal + url: wallet.mobile.universal }) this._chosenMobileWalletName = walletName triggerConnect( - wallet.deeplink.universal, + wallet.mobile.universal, this._app.sessionId, this._appInitData.url ?? 'https://nc2.nightly.app' ) @@ -609,10 +580,20 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { innerConnect() }) - fetchWalletsFromRegistry: () => ReturnType = async () => { - return AppSolana.getWalletsMetadata( + fetchWalletsFromRegistry: () => Promise = async () => { + this._metadataWallets = await AppSolana.getWalletsMetadata( `${this._appInitData.url ?? 'https://nc2.nightly.app'}/get_wallets_metadata` ) + return this._metadataWallets + } + + fetchAllWallets = async () => { + const metadataWallets = await this.fetchWalletsFromRegistry() + return getWalletsList( + metadataWallets, + solanaWalletsFilter, + getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined + ) } disconnect = async () => { diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index bb8d5c45..9d3c7f09 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -632,8 +632,8 @@ importers: specifier: ^0.0.15 version: link:../../apps/polkadot '@nightlylabs/wallet-selector-base': - specifier: ^0.3.0 - version: 0.3.1 + specifier: ^0.3.1 + version: link:../selector-base '@polkadot/api': specifier: 10.10.1 version: 10.10.1 @@ -675,8 +675,8 @@ importers: specifier: ^0.0.29 version: link:../../apps/solana '@nightlylabs/wallet-selector-base': - specifier: ^0.3.0 - version: 0.3.1 + specifier: ^0.3.1 + version: link:../selector-base '@solana/wallet-adapter-base': specifier: ^0.9.22 version: 0.9.22(@solana/web3.js@1.77.2) @@ -5140,21 +5140,6 @@ packages: - utf-8-validate dev: false - /@nightlylabs/wallet-selector-base@0.3.1: - resolution: {integrity: sha512-m2hdNkOrQNS52xXYSvko8YvbI60miCU9AHO8HHKfGXiuYgUjAmQSeTm1wLP8LZI4+Mygqbr8Oq9na+HMEgq2XA==} - dependencies: - '@nightlylabs/nightly-connect-base': 0.0.27 - '@nightlylabs/wallet-selector-modal': 0.2.0 - '@wallet-standard/core': 1.0.3 - isomorphic-localstorage: 1.0.2 - transitivePeerDependencies: - - bufferutil - - encoding - - supports-color - - ts-node - - utf-8-validate - dev: false - /@nightlylabs/wallet-selector-modal@0.1.2: resolution: {integrity: sha512-vxy9S2dEf3NARW6LDq2ZKpWMlk5JJFIuwUfSxkuJlgUg2OVSlnDS7vdho3h4DmluRU5GM9vVhaXUGHAVp5sDQg==} dependencies: @@ -5169,20 +5154,6 @@ packages: - ts-node dev: false - /@nightlylabs/wallet-selector-modal@0.2.0: - resolution: {integrity: sha512-BdEk3FhL65z/X0N9ygPjk7uQvV0GGHTWSwXBVob/l48Nok4ikFcV30Dtxk6iSSbErDZ8U4zV3/78cK+m+4lt8A==} - dependencies: - '@nightlylabs/qr-code': 2.0.4 - autoprefixer: 10.4.14(postcss@8.4.24) - lit: 2.7.2 - postcss: 8.4.24 - postcss-lit: 1.1.0(postcss@8.4.24) - tailwindcss: 3.3.2 - transitivePeerDependencies: - - supports-color - - ts-node - dev: false - /@nightlylabs/wallet-selector-solana@0.2.6(bs58@4.0.1)(react@18.2.0): resolution: {integrity: sha512-cVTKk+c6tGv4GeSQMlUaZ2si4A6ySKj41emkGJ8OtuwmtzwUym4Xuh3chXZYgGrMQgvPrX5+erIR4oq2GmGIPg==} dependencies: From 5bab9f5b1b7309167641e5af3082b0b9f6e4d65d Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 12:06:19 +0100 Subject: [PATCH 03/27] fix: version --- sdk/apps/modal-example/package.json | 2 +- sdk/packages/selector-solana/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index fb846521..5da3f2a5 100644 --- a/sdk/apps/modal-example/package.json +++ b/sdk/apps/modal-example/package.json @@ -17,7 +17,7 @@ "dependencies": { "@mysten/sui.js": "^0.42.0", "@mysten/wallet-adapter-wallet-standard": "^0.8.0", - "@nightlylabs/wallet-selector-solana": "0.2.8", + "@nightlylabs/wallet-selector-solana": "0.3.0", "@nightlylabs/nightly-connect-solana": "0.0.29", "@nightlylabs/nightly-connect-sui": "0.0.29", "@nightlylabs/wallet-selector-sui": "0.2.7", diff --git a/sdk/packages/selector-solana/package.json b/sdk/packages/selector-solana/package.json index c0d16592..fe77830c 100644 --- a/sdk/packages/selector-solana/package.json +++ b/sdk/packages/selector-solana/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-solana", - "version": "0.2.8", + "version": "0.3.0", "description": "", "type": "module", "exports": { From bb51fc025333668d194857b73706cc786127baac Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 13:52:50 +0100 Subject: [PATCH 04/27] add: solana change event listener and emitter --- sdk/packages/selector-solana/src/adapter.ts | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 6e01379e..e5f53fac 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -21,6 +21,7 @@ import { import { BaseMessageSignerWalletAdapter, WalletAdapterCompatibleStandardWallet, + WalletAdapterEvents, WalletName, WalletNotConnectedError, WalletNotReadyError, @@ -33,6 +34,19 @@ import { import { StandardWalletAdapter } from '@solana/wallet-standard' import { PublicKey, Transaction, TransactionVersion, VersionedTransaction } from '@solana/web3.js' import { solanaWalletsFilter } from './detection' +import { StandardEventsChangeProperties } from '@wallet-standard/core' + +type ArgumentMap = { + [K in keyof T]: T[K] extends (...args: any[]) => void + ? Parameters + : T[K] extends any[] + ? T[K] + : any[] +} + +type NightlyConnectAdapterEvents = WalletAdapterEvents & { + change(properties: StandardEventsChangeProperties): void +} export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { name = 'Nightly Connect' as WalletName<'Nightly Connect'> @@ -138,6 +152,21 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { } } + on( + event: T, + fn: (...args: ArgumentMap[T]) => void, + context?: any + ): this { + return this + } + + emit( + event: T, + ...args: ArgumentMap[Extract] + ): boolean { + return true + } + public static build = async ( appInitData: AppInitData, connectionOptions?: ConnectionOptions, From bba5a77fa1416405d3c8e745130092319af688f3 Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 15:40:00 +0100 Subject: [PATCH 05/27] fix: listeners --- sdk/bindings/WalletMetadata.ts | 28 +++++++++--- sdk/packages/selector-base/src/types.ts | 44 +------------------ sdk/packages/selector-polkadot/src/adapter.ts | 2 +- .../selector-polkadot/src/detection.ts | 6 ++- sdk/packages/selector-solana/src/adapter.ts | 29 +++++++----- 5 files changed, 45 insertions(+), 64 deletions(-) diff --git a/sdk/bindings/WalletMetadata.ts b/sdk/bindings/WalletMetadata.ts index 06106435..78889c7c 100644 --- a/sdk/bindings/WalletMetadata.ts +++ b/sdk/bindings/WalletMetadata.ts @@ -1,9 +1,23 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { Deeplink } from "./Deeplink"; -import type { Images } from "./Images"; -import type { Network } from "./Network"; -import type { Platform } from "./Platform"; -import type { Version } from "./Version"; -import type { WalletType } from "./WalletType"; +import type { Deeplink } from './Deeplink' +import type { Images } from './Images' +import type { Network } from './Network' +import type { Platform } from './Platform' +import type { Version } from './Version' +import type { WalletType } from './WalletType' -export interface WalletMetadata { slug: string, name: string, description: string, homepage: string, chains: Array, version: Version, walletType: WalletType, mobile: Deeplink | null, desktop: Deeplink | null, image: Images, app: Record, injectPath: Record, lastUpdatedTimestamp: bigint, } \ No newline at end of file +export interface WalletMetadata { + slug: string + name: string + description?: string + homepage?: string + chains?: Array + version?: Version + walletType: WalletType + mobile: Deeplink | null + desktop: Deeplink | null + image: Images + app?: Record + injectPath?: Record + lastUpdatedTimestamp?: bigint +} diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index 55ecbbbb..ccba4694 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -2,6 +2,8 @@ import { type AppBaseInitialize } from '@nightlylabs/nightly-connect-base' import { type Deeplink } from '@nightlylabs/nightly-connect-base/dist/types/bindings/Deeplink' import { type Wallet } from '@wallet-standard/core' import { type WalletType } from '../../../bindings/WalletType' +import { type WalletMetadata } from '../../../bindings/WalletMetadata' +export { type WalletMetadata } from '../../../bindings/WalletMetadata' export interface Adapter { connect: () => Promise @@ -17,48 +19,6 @@ export interface MetadataWallet { walletType: WalletType } -interface Images { - default: string - sm: string - md: string - lg: string -} - -type Network = string - -type Platform = - | 'browser' - | 'ios' - | 'android' - | 'macos' - | 'windows' - | 'linux' - | 'chrome' - | 'firefox' - | 'opera' - | 'edge' - | 'brave' - | 'safari' - | 'other' - -type Version = string - -export interface WalletMetadata { - slug: string - name: string - description?: string - homepage?: string - chains?: Array - version?: Version - walletType: WalletType - mobile: Deeplink | null - desktop: Deeplink | null - image: Images - app?: Record - injectPath?: Record - lastUpdatedTimestamp?: bigint -} - export interface IWalletListItem extends WalletMetadata { recent?: boolean detected?: boolean diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index 468a527a..4fbb1ed9 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -113,7 +113,7 @@ export class NightlyConnectAdapter implements Injected { set walletsList(list: IPolkadotWalletListItem[]) { this._walletsList = list if (this._modal) { - this._modal.walletsList = list as IWalletListItem[] + this._modal.walletsList = list } } diff --git a/sdk/packages/selector-polkadot/src/detection.ts b/sdk/packages/selector-polkadot/src/detection.ts index 649a19a7..8395fd11 100644 --- a/sdk/packages/selector-polkadot/src/detection.ts +++ b/sdk/packages/selector-polkadot/src/detection.ts @@ -42,7 +42,10 @@ export const getPolkadotWalletsList = (presetList: WalletMetadata[], recentWalle const walletsData: Record = {} presetList.forEach((wallet) => { - walletsData[wallet.slug.toLocaleLowerCase()] = wallet + walletsData[wallet.slug.toLocaleLowerCase()] = { + ...wallet, + recent: recentWalletName === wallet.name + } }) for (const wallet of windowWallets) { // Check if wallet is already in the list @@ -94,7 +97,6 @@ export const getPolkadotWalletsList = (presetList: WalletMetadata[], recentWalle recent: recentWalletName === wallet.name, detected: true, injectedWallet: wallet, - homepage: '', walletType: 'hybrid' } } diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index e5f53fac..1b33ef3b 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -22,6 +22,7 @@ import { BaseMessageSignerWalletAdapter, WalletAdapterCompatibleStandardWallet, WalletAdapterEvents, + WalletError, WalletName, WalletNotConnectedError, WalletNotReadyError, @@ -36,14 +37,6 @@ import { PublicKey, Transaction, TransactionVersion, VersionedTransaction } from import { solanaWalletsFilter } from './detection' import { StandardEventsChangeProperties } from '@wallet-standard/core' -type ArgumentMap = { - [K in keyof T]: T[K] extends (...args: any[]) => void - ? Parameters - : T[K] extends any[] - ? T[K] - : any[] -} - type NightlyConnectAdapterEvents = WalletAdapterEvents & { change(properties: StandardEventsChangeProperties): void } @@ -154,16 +147,28 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { on( event: T, - fn: (...args: ArgumentMap[T]) => void, + fn: NightlyConnectAdapterEvents[T] extends (...args: infer Args) => void + ? (...args: Args) => void + : never, context?: any ): this { - return this + if (event === 'change') { + // TODO implement on change listener + return this + } else { + return super.on(event, fn, context) + } } emit( event: T, - ...args: ArgumentMap[Extract] + ...args: [publicKey: PublicKey] | [] | [error: WalletError] | [readyState: WalletReadyState] ): boolean { + if (event === 'change') { + // TODO implement change event emitter + } else { + super.emit(event, ...args) + } return true } @@ -244,7 +249,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter._metadataWallets = metadataWallets adapter.walletsList = getWalletsList( - [], + metadataWallets, solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) From bd6a67e82abcabdf30b0db1a82e2a157a5cfd958 Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 16:00:17 +0100 Subject: [PATCH 06/27] fix; pull main --- sdk/pnpm-lock.yaml | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 9d3c7f09..6b837251 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -142,9 +142,9 @@ importers: version: link:../sui '@nightlylabs/wallet-selector-polkadot': specifier: 0.2.0 - version: link:../../packages/selector-polkadot + version: 0.2.0(@polkadot/util@12.5.1) '@nightlylabs/wallet-selector-solana': - specifier: 0.2.8 + specifier: 0.3.0 version: link:../../packages/selector-solana '@nightlylabs/wallet-selector-sui': specifier: 0.2.7 @@ -592,7 +592,7 @@ importers: specifier: 0.0.27 version: link:../../apps/base '@nightlylabs/wallet-selector-modal': - specifier: 0.1.2 + specifier: 0.2.0 version: link:../modal '@wallet-standard/core': specifier: ^1.0.3 @@ -5090,6 +5090,24 @@ packages: - utf-8-validate dev: false + /@nightlylabs/nightly-connect-polkadot@0.0.15: + resolution: {integrity: sha512-WCsumvHwhPipbxPQoswKCwHykwJ48Dffwb9hCf7zjCgEysIBCnA6Dzj/2G80drLqYYpS285nMa8z+3NaXVu2dA==} + dependencies: + '@nightlylabs/nightly-connect-base': 0.0.27 + '@polkadot/api': 10.10.1 + '@polkadot/extension-inject': 0.46.5(@polkadot/api@10.10.1)(@polkadot/util@12.5.1) + '@polkadot/types': 10.10.1 + '@polkadot/util': 12.5.1 + '@polkadot/util-crypto': 12.5.1(@polkadot/util@12.5.1) + eventemitter3: 5.0.1 + uuid: 9.0.0 + transitivePeerDependencies: + - bufferutil + - encoding + - supports-color + - utf-8-validate + dev: false + /@nightlylabs/nightly-connect-solana@0.0.28: resolution: {integrity: sha512-8PBkmuXzWZNPqu6SGT2tsGK4DgD3yswQsUVb3L+GgFGCdQI7eUqyHd2ofWFWzEgj4a1XuixA29ZcSyw20ajgzw==} dependencies: @@ -5140,6 +5158,21 @@ packages: - utf-8-validate dev: false + /@nightlylabs/wallet-selector-base@0.3.1: + resolution: {integrity: sha512-m2hdNkOrQNS52xXYSvko8YvbI60miCU9AHO8HHKfGXiuYgUjAmQSeTm1wLP8LZI4+Mygqbr8Oq9na+HMEgq2XA==} + dependencies: + '@nightlylabs/nightly-connect-base': 0.0.27 + '@nightlylabs/wallet-selector-modal': 0.2.0 + '@wallet-standard/core': 1.0.3 + isomorphic-localstorage: 1.0.2 + transitivePeerDependencies: + - bufferutil + - encoding + - supports-color + - ts-node + - utf-8-validate + dev: false + /@nightlylabs/wallet-selector-modal@0.1.2: resolution: {integrity: sha512-vxy9S2dEf3NARW6LDq2ZKpWMlk5JJFIuwUfSxkuJlgUg2OVSlnDS7vdho3h4DmluRU5GM9vVhaXUGHAVp5sDQg==} dependencies: @@ -5154,6 +5187,37 @@ packages: - ts-node dev: false + /@nightlylabs/wallet-selector-modal@0.2.0: + resolution: {integrity: sha512-BdEk3FhL65z/X0N9ygPjk7uQvV0GGHTWSwXBVob/l48Nok4ikFcV30Dtxk6iSSbErDZ8U4zV3/78cK+m+4lt8A==} + dependencies: + '@nightlylabs/qr-code': 2.0.4 + autoprefixer: 10.4.14(postcss@8.4.24) + lit: 2.7.2 + postcss: 8.4.24 + postcss-lit: 1.1.0(postcss@8.4.24) + tailwindcss: 3.3.2 + transitivePeerDependencies: + - supports-color + - ts-node + dev: false + + /@nightlylabs/wallet-selector-polkadot@0.2.0(@polkadot/util@12.5.1): + resolution: {integrity: sha512-T4C6J+RVBif8H742LdeZfIRZyFZV79CAWL6/w3keQmyi8ZmTfmmxdVaXkSE5Va11j3xt2A298mugbbujluW6bw==} + dependencies: + '@nightlylabs/nightly-connect-polkadot': 0.0.15 + '@nightlylabs/wallet-selector-base': 0.3.1 + '@polkadot/api': 10.10.1 + '@polkadot/extension-inject': 0.46.5(@polkadot/api@10.10.1)(@polkadot/util@12.5.1) + '@wallet-standard/core': 1.0.3 + transitivePeerDependencies: + - '@polkadot/util' + - bufferutil + - encoding + - supports-color + - ts-node + - utf-8-validate + dev: false + /@nightlylabs/wallet-selector-solana@0.2.6(bs58@4.0.1)(react@18.2.0): resolution: {integrity: sha512-cVTKk+c6tGv4GeSQMlUaZ2si4A6ySKj41emkGJ8OtuwmtzwUym4Xuh3chXZYgGrMQgvPrX5+erIR4oq2GmGIPg==} dependencies: From de1f5632c23fde403cf7d2b55eaa494b607f5c71 Mon Sep 17 00:00:00 2001 From: LukassF Date: Thu, 22 Feb 2024 16:35:04 +0100 Subject: [PATCH 07/27] fix: decoupling types --- sdk/bindings/WalletMetadata.ts | 28 +++++-------------- sdk/packages/selector-base/src/detection.ts | 2 +- sdk/packages/selector-base/src/types.ts | 13 +++++++-- .../selector-polkadot/src/detection.ts | 5 ++-- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/sdk/bindings/WalletMetadata.ts b/sdk/bindings/WalletMetadata.ts index 78889c7c..06106435 100644 --- a/sdk/bindings/WalletMetadata.ts +++ b/sdk/bindings/WalletMetadata.ts @@ -1,23 +1,9 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { Deeplink } from './Deeplink' -import type { Images } from './Images' -import type { Network } from './Network' -import type { Platform } from './Platform' -import type { Version } from './Version' -import type { WalletType } from './WalletType' +import type { Deeplink } from "./Deeplink"; +import type { Images } from "./Images"; +import type { Network } from "./Network"; +import type { Platform } from "./Platform"; +import type { Version } from "./Version"; +import type { WalletType } from "./WalletType"; -export interface WalletMetadata { - slug: string - name: string - description?: string - homepage?: string - chains?: Array - version?: Version - walletType: WalletType - mobile: Deeplink | null - desktop: Deeplink | null - image: Images - app?: Record - injectPath?: Record - lastUpdatedTimestamp?: bigint -} +export interface WalletMetadata { slug: string, name: string, description: string, homepage: string, chains: Array, version: Version, walletType: WalletType, mobile: Deeplink | null, desktop: Deeplink | null, image: Images, app: Record, injectPath: Record, lastUpdatedTimestamp: bigint, } \ No newline at end of file diff --git a/sdk/packages/selector-base/src/detection.ts b/sdk/packages/selector-base/src/detection.ts index 3ef4449c..8940b812 100644 --- a/sdk/packages/selector-base/src/detection.ts +++ b/sdk/packages/selector-base/src/detection.ts @@ -1,5 +1,5 @@ import { Wallet, getWallets } from '@wallet-standard/core' -import { WalletMetadata, type IWalletListItem, type MetadataWallet } from './types' +import { type WalletMetadata, type IWalletListItem, type MetadataWallet } from './types' export const getWalletsList = ( presetList: WalletMetadata[], diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index ccba4694..c556e5a7 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -2,8 +2,8 @@ import { type AppBaseInitialize } from '@nightlylabs/nightly-connect-base' import { type Deeplink } from '@nightlylabs/nightly-connect-base/dist/types/bindings/Deeplink' import { type Wallet } from '@wallet-standard/core' import { type WalletType } from '../../../bindings/WalletType' -import { type WalletMetadata } from '../../../bindings/WalletMetadata' -export { type WalletMetadata } from '../../../bindings/WalletMetadata' +import { type WalletMetadata as WalletMetadataOriginal } from '../../../bindings/WalletMetadata' +import { Images } from '../../../bindings/Images' export interface Adapter { connect: () => Promise @@ -19,6 +19,15 @@ export interface MetadataWallet { walletType: WalletType } +export interface WalletMetadata extends Partial { + slug: string + name: string + walletType: WalletType + mobile: Deeplink | null + desktop: Deeplink | null + image: Images +} + export interface IWalletListItem extends WalletMetadata { recent?: boolean detected?: boolean diff --git a/sdk/packages/selector-polkadot/src/detection.ts b/sdk/packages/selector-polkadot/src/detection.ts index 8395fd11..c25797ea 100644 --- a/sdk/packages/selector-polkadot/src/detection.ts +++ b/sdk/packages/selector-polkadot/src/detection.ts @@ -1,7 +1,6 @@ import { type Injected, type InjectedExtension } from '@polkadot/extension-inject/types' import { type WalletIcon } from '@wallet-standard/core' import { appToIcon } from './tempIcons' -import { IWalletListItem } from '@nightlylabs/wallet-selector-base' import { WalletMetadata } from '@nightlylabs/nightly-connect-polkadot' export interface PolkadotWalletInjected { // Default Polkadot standard @@ -32,7 +31,9 @@ export const getPolkadotWallets = (): PolkadotWalletInjected[] => { } } -export interface IPolkadotWalletListItem extends Omit { +export interface IPolkadotWalletListItem extends WalletMetadata { + recent?: boolean + detected?: boolean injectedWallet?: PolkadotWalletInjected } From 11fce53dd826fcb315b1731ba849718f0f8b6c01 Mon Sep 17 00:00:00 2001 From: LukassF Date: Fri, 23 Feb 2024 11:52:48 +0100 Subject: [PATCH 08/27] fix: partial and moved getSolanaWalletsList method to dedicated file --- sdk/apps/modal-example/package.json | 2 +- sdk/packages/selector-base/src/detection.ts | 37 ---------- sdk/packages/selector-base/src/index.ts | 3 +- sdk/packages/selector-base/src/types.ts | 8 +-- sdk/packages/selector-polkadot/src/adapter.ts | 7 +- .../selector-polkadot/src/detection.ts | 14 +++- sdk/packages/selector-solana/src/adapter.ts | 28 +++----- sdk/packages/selector-solana/src/detection.ts | 49 ++++++++++++- sdk/pnpm-lock.yaml | 68 +------------------ 9 files changed, 78 insertions(+), 138 deletions(-) delete mode 100644 sdk/packages/selector-base/src/detection.ts diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index 5da3f2a5..643d5aeb 100644 --- a/sdk/apps/modal-example/package.json +++ b/sdk/apps/modal-example/package.json @@ -22,7 +22,7 @@ "@nightlylabs/nightly-connect-sui": "0.0.29", "@nightlylabs/wallet-selector-sui": "0.2.7", "@nightlylabs/nightly-connect-polkadot": "0.0.14", - "@nightlylabs/wallet-selector-polkadot": "0.2.0", + "@nightlylabs/wallet-selector-polkadot": "0.2.1", "@polkadot/extension-inject": "^0.46.5", "@polkadot/api": "^10.10.1", "@solana/web3.js": "^1.77.2", diff --git a/sdk/packages/selector-base/src/detection.ts b/sdk/packages/selector-base/src/detection.ts deleted file mode 100644 index 8940b812..00000000 --- a/sdk/packages/selector-base/src/detection.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Wallet, getWallets } from '@wallet-standard/core' -import { type WalletMetadata, type IWalletListItem, type MetadataWallet } from './types' - -export const getWalletsList = ( - presetList: WalletMetadata[], - walletsFilterCb: (wallet: Wallet) => boolean, - recentWalletName?: string -) => { - const { get } = getWallets() - const windowWallets = get() - - const walletsData: Record = {} - - presetList.forEach((wallet) => { - walletsData[wallet.name] = { - ...wallet, - recent: recentWalletName === wallet.name - } - }) - - windowWallets.filter(walletsFilterCb).forEach((wallet) => { - walletsData[wallet.name] = { - ...(walletsData?.[wallet.name] ?? { - name: wallet.name, - icon: wallet.icon, - link: '', - deeplink: null, - recent: recentWalletName === wallet.name, - walletType: 'hybrid' - }), - detected: true, - standardWallet: wallet - } - }) - - return Object.values(walletsData) -} diff --git a/sdk/packages/selector-base/src/index.ts b/sdk/packages/selector-base/src/index.ts index 62a90dd8..e31ac429 100644 --- a/sdk/packages/selector-base/src/index.ts +++ b/sdk/packages/selector-base/src/index.ts @@ -1,7 +1,6 @@ -export * from './detection' export * from './persistence' export * from './utils' export * from './types' export * from './logoBase64' export * from './modal' -export { type XMLOptions } from '@nightlylabs/wallet-selector-modal' \ No newline at end of file +export { type XMLOptions } from '@nightlylabs/wallet-selector-modal' diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index c556e5a7..4c828419 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -2,8 +2,9 @@ import { type AppBaseInitialize } from '@nightlylabs/nightly-connect-base' import { type Deeplink } from '@nightlylabs/nightly-connect-base/dist/types/bindings/Deeplink' import { type Wallet } from '@wallet-standard/core' import { type WalletType } from '../../../bindings/WalletType' -import { type WalletMetadata as WalletMetadataOriginal } from '../../../bindings/WalletMetadata' +import { WalletMetadata } from '../../../bindings/WalletMetadata' import { Images } from '../../../bindings/Images' +export { type WalletMetadata } from '../../../bindings/WalletMetadata' export interface Adapter { connect: () => Promise @@ -19,16 +20,13 @@ export interface MetadataWallet { walletType: WalletType } -export interface WalletMetadata extends Partial { +export interface IWalletListItem extends Partial { slug: string name: string walletType: WalletType mobile: Deeplink | null desktop: Deeplink | null image: Images -} - -export interface IWalletListItem extends WalletMetadata { recent?: boolean detected?: boolean standardWallet?: Wallet diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index b9beaa14..ed3ddce4 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -1,14 +1,11 @@ /* eslint-disable @typescript-eslint/no-empty-function */ -import { - AppPolkadot, - AppPolkadotInitialize, - WalletMetadata -} from '@nightlylabs/nightly-connect-polkadot' +import { AppPolkadot, AppPolkadotInitialize } from '@nightlylabs/nightly-connect-polkadot' import { ConnectionOptions, ConnectionType, IWalletListItem, NightlyConnectSelectorModal, + WalletMetadata, XMLOptions, clearRecentWalletForNetwork, clearSessionIdForNetwork, diff --git a/sdk/packages/selector-polkadot/src/detection.ts b/sdk/packages/selector-polkadot/src/detection.ts index c25797ea..c3c14840 100644 --- a/sdk/packages/selector-polkadot/src/detection.ts +++ b/sdk/packages/selector-polkadot/src/detection.ts @@ -1,7 +1,11 @@ import { type Injected, type InjectedExtension } from '@polkadot/extension-inject/types' import { type WalletIcon } from '@wallet-standard/core' import { appToIcon } from './tempIcons' -import { WalletMetadata } from '@nightlylabs/nightly-connect-polkadot' +import { WalletMetadata } from '../../../bindings/WalletMetadata' +import { WalletType } from '../../../bindings/WalletType' +import { Deeplink } from '../../../bindings/Deeplink' +import { Images } from '../../../bindings/Images' + export interface PolkadotWalletInjected { // Default Polkadot standard connect?: (origin: string) => Promise // Is this even used ? @@ -31,7 +35,13 @@ export const getPolkadotWallets = (): PolkadotWalletInjected[] => { } } -export interface IPolkadotWalletListItem extends WalletMetadata { +export interface IPolkadotWalletListItem extends Partial { + slug: string + name: string + walletType: WalletType + mobile: Deeplink | null + desktop: Deeplink | null + image: Images recent?: boolean detected?: boolean injectedWallet?: PolkadotWalletInjected diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 1b33ef3b..0d96ace9 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -4,9 +4,7 @@ import { clearRecentWalletForNetwork, clearSessionIdForNetwork, getRecentWalletForNetwork, - getWalletsList, isMobileBrowser, - IWalletListItem, logoBase64, NightlyConnectSelectorModal, persistRecentWalletForNetwork, @@ -34,7 +32,7 @@ import { } from '@solana/wallet-adapter-base' import { StandardWalletAdapter } from '@solana/wallet-standard' import { PublicKey, Transaction, TransactionVersion, VersionedTransaction } from '@solana/web3.js' -import { solanaWalletsFilter } from './detection' +import { getSolanaWalletsList, IWalletListItem } from './detection' import { StandardEventsChangeProperties } from '@wallet-standard/core' type NightlyConnectAdapterEvents = WalletAdapterEvents & { @@ -188,9 +186,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { return adapter } - adapter.walletsList = getWalletsList( + adapter.walletsList = getSolanaWalletsList( [], - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) @@ -213,9 +210,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter._app = app adapter._metadataWallets = metadataWallets - adapter.walletsList = getWalletsList( + adapter.walletsList = getSolanaWalletsList( metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) @@ -238,9 +234,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { return adapter } - adapter.walletsList = getWalletsList( + adapter.walletsList = getSolanaWalletsList( [], - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) @@ -248,9 +243,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter.fetchWalletsFromRegistry().then((metadataWallets) => { adapter._metadataWallets = metadataWallets - adapter.walletsList = getWalletsList( + adapter.walletsList = getSolanaWalletsList( metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) }) @@ -278,9 +272,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { adapter._app = app adapter._metadataWallets = metadataWallets - adapter.walletsList = getWalletsList( + adapter.walletsList = getSolanaWalletsList( metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) @@ -515,9 +508,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { .then(([app, metadataWallets]) => { this._app = app this._metadataWallets = metadataWallets - this.walletsList = getWalletsList( + this.walletsList = getSolanaWalletsList( metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) this._loading = false @@ -623,9 +615,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { fetchAllWallets = async () => { const metadataWallets = await this.fetchWalletsFromRegistry() - return getWalletsList( + return getSolanaWalletsList( metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) } @@ -649,9 +640,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { this._innerStandardAdapter = undefined clearRecentWalletForNetwork(SOLANA_NETWORK) } - this.walletsList = getWalletsList( + this.walletsList = getSolanaWalletsList( this._metadataWallets, - solanaWalletsFilter, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) this._publicKey = null diff --git a/sdk/packages/selector-solana/src/detection.ts b/sdk/packages/selector-solana/src/detection.ts index 423a9b50..359b14a9 100644 --- a/sdk/packages/selector-solana/src/detection.ts +++ b/sdk/packages/selector-solana/src/detection.ts @@ -1,5 +1,52 @@ -import { type Wallet } from '@wallet-standard/core' +import { getWallets, type Wallet } from '@wallet-standard/core' import { isWalletAdapterCompatibleStandardWallet } from '@solana/wallet-adapter-base' +import { WalletMetadata } from '../../../bindings/WalletMetadata' +import { WalletType } from '../../../bindings/WalletType' +import { Deeplink } from '../../../bindings/Deeplink' +import { Images } from '../../../bindings/Images' + +export interface IWalletListItem extends Partial { + slug: string + name: string + walletType: WalletType + mobile: Deeplink | null + desktop: Deeplink | null + image: Images + recent?: boolean + detected?: boolean + standardWallet?: Wallet +} export const solanaWalletsFilter = (wallet: Wallet) => isWalletAdapterCompatibleStandardWallet(wallet) + +export const getSolanaWalletsList = (presetList: WalletMetadata[], recentWalletName?: string) => { + const { get } = getWallets() + const windowWallets = get() + + const walletsData: Record = {} + + presetList.forEach((wallet) => { + walletsData[wallet.name] = { + ...wallet, + recent: recentWalletName === wallet.name + } + }) + + windowWallets.filter(solanaWalletsFilter).forEach((wallet) => { + walletsData[wallet.name] = { + ...(walletsData?.[wallet.name] ?? { + name: wallet.name, + icon: wallet.icon, + link: '', + deeplink: null, + recent: recentWalletName === wallet.name, + walletType: 'hybrid' + }), + detected: true, + standardWallet: wallet + } + }) + + return Object.values(walletsData) +} diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 6b837251..30d55e12 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -141,8 +141,8 @@ importers: specifier: 0.0.29 version: link:../sui '@nightlylabs/wallet-selector-polkadot': - specifier: 0.2.0 - version: 0.2.0(@polkadot/util@12.5.1) + specifier: 0.2.1 + version: link:../../packages/selector-polkadot '@nightlylabs/wallet-selector-solana': specifier: 0.3.0 version: link:../../packages/selector-solana @@ -5090,24 +5090,6 @@ packages: - utf-8-validate dev: false - /@nightlylabs/nightly-connect-polkadot@0.0.15: - resolution: {integrity: sha512-WCsumvHwhPipbxPQoswKCwHykwJ48Dffwb9hCf7zjCgEysIBCnA6Dzj/2G80drLqYYpS285nMa8z+3NaXVu2dA==} - dependencies: - '@nightlylabs/nightly-connect-base': 0.0.27 - '@polkadot/api': 10.10.1 - '@polkadot/extension-inject': 0.46.5(@polkadot/api@10.10.1)(@polkadot/util@12.5.1) - '@polkadot/types': 10.10.1 - '@polkadot/util': 12.5.1 - '@polkadot/util-crypto': 12.5.1(@polkadot/util@12.5.1) - eventemitter3: 5.0.1 - uuid: 9.0.0 - transitivePeerDependencies: - - bufferutil - - encoding - - supports-color - - utf-8-validate - dev: false - /@nightlylabs/nightly-connect-solana@0.0.28: resolution: {integrity: sha512-8PBkmuXzWZNPqu6SGT2tsGK4DgD3yswQsUVb3L+GgFGCdQI7eUqyHd2ofWFWzEgj4a1XuixA29ZcSyw20ajgzw==} dependencies: @@ -5158,21 +5140,6 @@ packages: - utf-8-validate dev: false - /@nightlylabs/wallet-selector-base@0.3.1: - resolution: {integrity: sha512-m2hdNkOrQNS52xXYSvko8YvbI60miCU9AHO8HHKfGXiuYgUjAmQSeTm1wLP8LZI4+Mygqbr8Oq9na+HMEgq2XA==} - dependencies: - '@nightlylabs/nightly-connect-base': 0.0.27 - '@nightlylabs/wallet-selector-modal': 0.2.0 - '@wallet-standard/core': 1.0.3 - isomorphic-localstorage: 1.0.2 - transitivePeerDependencies: - - bufferutil - - encoding - - supports-color - - ts-node - - utf-8-validate - dev: false - /@nightlylabs/wallet-selector-modal@0.1.2: resolution: {integrity: sha512-vxy9S2dEf3NARW6LDq2ZKpWMlk5JJFIuwUfSxkuJlgUg2OVSlnDS7vdho3h4DmluRU5GM9vVhaXUGHAVp5sDQg==} dependencies: @@ -5187,37 +5154,6 @@ packages: - ts-node dev: false - /@nightlylabs/wallet-selector-modal@0.2.0: - resolution: {integrity: sha512-BdEk3FhL65z/X0N9ygPjk7uQvV0GGHTWSwXBVob/l48Nok4ikFcV30Dtxk6iSSbErDZ8U4zV3/78cK+m+4lt8A==} - dependencies: - '@nightlylabs/qr-code': 2.0.4 - autoprefixer: 10.4.14(postcss@8.4.24) - lit: 2.7.2 - postcss: 8.4.24 - postcss-lit: 1.1.0(postcss@8.4.24) - tailwindcss: 3.3.2 - transitivePeerDependencies: - - supports-color - - ts-node - dev: false - - /@nightlylabs/wallet-selector-polkadot@0.2.0(@polkadot/util@12.5.1): - resolution: {integrity: sha512-T4C6J+RVBif8H742LdeZfIRZyFZV79CAWL6/w3keQmyi8ZmTfmmxdVaXkSE5Va11j3xt2A298mugbbujluW6bw==} - dependencies: - '@nightlylabs/nightly-connect-polkadot': 0.0.15 - '@nightlylabs/wallet-selector-base': 0.3.1 - '@polkadot/api': 10.10.1 - '@polkadot/extension-inject': 0.46.5(@polkadot/api@10.10.1)(@polkadot/util@12.5.1) - '@wallet-standard/core': 1.0.3 - transitivePeerDependencies: - - '@polkadot/util' - - bufferutil - - encoding - - supports-color - - ts-node - - utf-8-validate - dev: false - /@nightlylabs/wallet-selector-solana@0.2.6(bs58@4.0.1)(react@18.2.0): resolution: {integrity: sha512-cVTKk+c6tGv4GeSQMlUaZ2si4A6ySKj41emkGJ8OtuwmtzwUym4Xuh3chXZYgGrMQgvPrX5+erIR4oq2GmGIPg==} dependencies: From e012fc418687639df585bd00fa9a6548a83b3182 Mon Sep 17 00:00:00 2001 From: LukassF Date: Fri, 23 Feb 2024 13:31:53 +0100 Subject: [PATCH 09/27] fix: remove partial --- sdk/packages/modal/package.json | 4 ++-- sdk/packages/selector-base/package.json | 4 ++-- sdk/packages/selector-base/src/types.ts | 12 ++++------ sdk/packages/selector-polkadot/package.json | 2 +- .../selector-polkadot/src/detection.ts | 24 ++++++++----------- sdk/packages/selector-solana/package.json | 2 +- sdk/packages/selector-solana/src/adapter.ts | 5 ++-- sdk/packages/selector-solana/src/detection.ts | 19 ++------------- sdk/pnpm-lock.yaml | 6 ++--- 9 files changed, 29 insertions(+), 49 deletions(-) diff --git a/sdk/packages/modal/package.json b/sdk/packages/modal/package.json index b994bca5..e4f0ff2b 100644 --- a/sdk/packages/modal/package.json +++ b/sdk/packages/modal/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-modal", - "version": "0.2.0", + "version": "0.2.1", "type": "module", "exports": { ".": { @@ -55,4 +55,4 @@ "typescript": "^5.0.2", "vite": "^4.3.9" } -} \ No newline at end of file +} diff --git a/sdk/packages/selector-base/package.json b/sdk/packages/selector-base/package.json index ddd40cb2..9dfb8f4d 100644 --- a/sdk/packages/selector-base/package.json +++ b/sdk/packages/selector-base/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-base", - "version": "0.3.1", + "version": "0.4.0", "description": "", "type": "module", "exports": { @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-base": "0.0.27", - "@nightlylabs/wallet-selector-modal": "0.2.0", + "@nightlylabs/wallet-selector-modal": "0.2.1", "@wallet-standard/core": "^1.0.3", "isomorphic-localstorage": "^1.0.2" }, diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index 4c828419..e920e9ff 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -20,13 +20,11 @@ export interface MetadataWallet { walletType: WalletType } -export interface IWalletListItem extends Partial { - slug: string - name: string - walletType: WalletType - mobile: Deeplink | null - desktop: Deeplink | null - image: Images +export interface IWalletListItem + extends Pick< + WalletMetadata, + 'name' | 'slug' | 'walletType' | 'mobile' | 'desktop' | 'image' | 'homepage' + > { recent?: boolean detected?: boolean standardWallet?: Wallet diff --git a/sdk/packages/selector-polkadot/package.json b/sdk/packages/selector-polkadot/package.json index b3969395..b66c0271 100644 --- a/sdk/packages/selector-polkadot/package.json +++ b/sdk/packages/selector-polkadot/package.json @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-polkadot": "^0.0.15", - "@nightlylabs/wallet-selector-base": "^0.3.1", + "@nightlylabs/wallet-selector-base": "^0.4.0", "@polkadot/extension-inject": "0.46.5", "@polkadot/api": "10.10.1", "@wallet-standard/core": "^1.0.3" diff --git a/sdk/packages/selector-polkadot/src/detection.ts b/sdk/packages/selector-polkadot/src/detection.ts index c3c14840..97d57e42 100644 --- a/sdk/packages/selector-polkadot/src/detection.ts +++ b/sdk/packages/selector-polkadot/src/detection.ts @@ -1,10 +1,7 @@ -import { type Injected, type InjectedExtension } from '@polkadot/extension-inject/types' -import { type WalletIcon } from '@wallet-standard/core' +import { Injected, InjectedExtension } from '@polkadot/extension-inject/types' +import { WalletIcon } from '@wallet-standard/core' import { appToIcon } from './tempIcons' -import { WalletMetadata } from '../../../bindings/WalletMetadata' -import { WalletType } from '../../../bindings/WalletType' -import { Deeplink } from '../../../bindings/Deeplink' -import { Images } from '../../../bindings/Images' +import { WalletMetadata } from '@nightlylabs/wallet-selector-base' export interface PolkadotWalletInjected { // Default Polkadot standard @@ -35,13 +32,11 @@ export const getPolkadotWallets = (): PolkadotWalletInjected[] => { } } -export interface IPolkadotWalletListItem extends Partial { - slug: string - name: string - walletType: WalletType - mobile: Deeplink | null - desktop: Deeplink | null - image: Images +export interface IPolkadotWalletListItem + extends Pick< + WalletMetadata, + 'name' | 'slug' | 'walletType' | 'mobile' | 'desktop' | 'image' | 'homepage' + > { recent?: boolean detected?: boolean injectedWallet?: PolkadotWalletInjected @@ -108,7 +103,8 @@ export const getPolkadotWalletsList = (presetList: WalletMetadata[], recentWalle recent: recentWalletName === wallet.name, detected: true, injectedWallet: wallet, - walletType: 'hybrid' + walletType: 'hybrid', + homepage: '' } } diff --git a/sdk/packages/selector-solana/package.json b/sdk/packages/selector-solana/package.json index fe77830c..a63cdc6b 100644 --- a/sdk/packages/selector-solana/package.json +++ b/sdk/packages/selector-solana/package.json @@ -25,7 +25,7 @@ "license": "ISC", "dependencies": { "@nightlylabs/nightly-connect-solana": "^0.0.29", - "@nightlylabs/wallet-selector-base": "^0.3.1", + "@nightlylabs/wallet-selector-base": "^0.4.0", "@solana/wallet-adapter-base": "^0.9.22", "@solana/wallet-standard": "^1.0.2", "@solana/web3.js": "^1.77.2", diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 0d96ace9..6bd10e5e 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -14,7 +14,8 @@ import { ConnectionType, ConnectionOptions, defaultConnectionOptions, - WalletMetadata + WalletMetadata, + IWalletListItem } from '@nightlylabs/wallet-selector-base' import { BaseMessageSignerWalletAdapter, @@ -32,7 +33,7 @@ import { } from '@solana/wallet-adapter-base' import { StandardWalletAdapter } from '@solana/wallet-standard' import { PublicKey, Transaction, TransactionVersion, VersionedTransaction } from '@solana/web3.js' -import { getSolanaWalletsList, IWalletListItem } from './detection' +import { getSolanaWalletsList } from './detection' import { StandardEventsChangeProperties } from '@wallet-standard/core' type NightlyConnectAdapterEvents = WalletAdapterEvents & { diff --git a/sdk/packages/selector-solana/src/detection.ts b/sdk/packages/selector-solana/src/detection.ts index 359b14a9..23a75fd6 100644 --- a/sdk/packages/selector-solana/src/detection.ts +++ b/sdk/packages/selector-solana/src/detection.ts @@ -1,21 +1,6 @@ -import { getWallets, type Wallet } from '@wallet-standard/core' +import { getWallets, Wallet } from '@wallet-standard/core' import { isWalletAdapterCompatibleStandardWallet } from '@solana/wallet-adapter-base' -import { WalletMetadata } from '../../../bindings/WalletMetadata' -import { WalletType } from '../../../bindings/WalletType' -import { Deeplink } from '../../../bindings/Deeplink' -import { Images } from '../../../bindings/Images' - -export interface IWalletListItem extends Partial { - slug: string - name: string - walletType: WalletType - mobile: Deeplink | null - desktop: Deeplink | null - image: Images - recent?: boolean - detected?: boolean - standardWallet?: Wallet -} +import { IWalletListItem, WalletMetadata } from '@nightlylabs/wallet-selector-base' export const solanaWalletsFilter = (wallet: Wallet) => isWalletAdapterCompatibleStandardWallet(wallet) diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 30d55e12..468594e4 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -592,7 +592,7 @@ importers: specifier: 0.0.27 version: link:../../apps/base '@nightlylabs/wallet-selector-modal': - specifier: 0.2.0 + specifier: 0.2.1 version: link:../modal '@wallet-standard/core': specifier: ^1.0.3 @@ -632,7 +632,7 @@ importers: specifier: ^0.0.15 version: link:../../apps/polkadot '@nightlylabs/wallet-selector-base': - specifier: ^0.3.1 + specifier: ^0.4.0 version: link:../selector-base '@polkadot/api': specifier: 10.10.1 @@ -675,7 +675,7 @@ importers: specifier: ^0.0.29 version: link:../../apps/solana '@nightlylabs/wallet-selector-base': - specifier: ^0.3.1 + specifier: ^0.4.0 version: link:../selector-base '@solana/wallet-adapter-base': specifier: ^0.9.22 From ae9b42a723672b28dc99c919b4eeec0570666982 Mon Sep 17 00:00:00 2001 From: LukassF Date: Fri, 23 Feb 2024 13:33:14 +0100 Subject: [PATCH 10/27] remove redundant imports --- sdk/packages/selector-base/src/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/packages/selector-base/src/types.ts b/sdk/packages/selector-base/src/types.ts index e920e9ff..39676310 100644 --- a/sdk/packages/selector-base/src/types.ts +++ b/sdk/packages/selector-base/src/types.ts @@ -3,7 +3,6 @@ import { type Deeplink } from '@nightlylabs/nightly-connect-base/dist/types/bind import { type Wallet } from '@wallet-standard/core' import { type WalletType } from '../../../bindings/WalletType' import { WalletMetadata } from '../../../bindings/WalletMetadata' -import { Images } from '../../../bindings/Images' export { type WalletMetadata } from '../../../bindings/WalletMetadata' export interface Adapter { From 7cff49a69a36020bcaa702cd44d558ca996e2950 Mon Sep 17 00:00:00 2001 From: LukassF Date: Fri, 23 Feb 2024 14:25:32 +0100 Subject: [PATCH 11/27] add: showing error in modal --- .../nightly-desktop-main/nightly-desktop-main.stories.ts | 2 +- .../components/nightly-desktop-main/nightly-desktop-main.ts | 4 ++-- .../src/components/nightly-mobile-qr/nightly-mobile-qr.ts | 4 ++-- .../components/nightly-selector/nightly-selector.stories.ts | 2 +- .../modal/src/components/nightly-selector/nightly-selector.ts | 4 ++-- sdk/packages/selector-base/src/modal.ts | 4 ++++ sdk/packages/selector-polkadot/src/adapter.ts | 4 ++-- sdk/packages/selector-solana/src/adapter.ts | 2 +- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.stories.ts b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.stories.ts index 53f1a457..6f376ae7 100644 --- a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.stories.ts +++ b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.stories.ts @@ -154,7 +154,7 @@ export const Error: Story = (args: NightlyModalArgs) => { if (!args.sessionId) setTimeout(() => { - updateArgs({ timeoutError: true }) + updateArgs({ timeoutError: 'error' }) }, 5000) return html` = {} - @property({ type: Boolean }) - timeoutError = false + @property({ type: String }) + timeoutError = '' @state() copyMessage = 'Copy' diff --git a/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts b/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts index 2bc8bdb9..f205668c 100644 --- a/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts +++ b/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts @@ -24,8 +24,8 @@ export class NightlyMobileQr extends LitElement { @property({ type: Object }) qrConfigOverride: Partial = {} - @property({ type: Boolean }) - timeoutError = false + @property({ type: String }) + timeoutError = '' @state() qrSource: string | undefined = undefined diff --git a/sdk/packages/modal/src/components/nightly-selector/nightly-selector.stories.ts b/sdk/packages/modal/src/components/nightly-selector/nightly-selector.stories.ts index 5a5b2fdf..2e983040 100644 --- a/sdk/packages/modal/src/components/nightly-selector/nightly-selector.stories.ts +++ b/sdk/packages/modal/src/components/nightly-selector/nightly-selector.stories.ts @@ -159,7 +159,7 @@ export const Error: Story = (args: NightlyModalArgs) => { if (!args.sessionId) setTimeout(() => { - updateArgs({ timeoutError: true }) + updateArgs({ timeoutError: 'error' }) }, 5000) return open diff --git a/sdk/packages/modal/src/components/nightly-selector/nightly-selector.ts b/sdk/packages/modal/src/components/nightly-selector/nightly-selector.ts index 89830bae..c3ea2f20 100644 --- a/sdk/packages/modal/src/components/nightly-selector/nightly-selector.ts +++ b/sdk/packages/modal/src/components/nightly-selector/nightly-selector.ts @@ -47,8 +47,8 @@ export class NightlySelector extends LitElement { @property({ type: Object }) qrConfigOverride: Partial = {} - @property({ type: Boolean }) - timeoutError = false + @property({ type: String }) + timeoutError = '' // state diff --git a/sdk/packages/selector-base/src/modal.ts b/sdk/packages/selector-base/src/modal.ts index 4af34d4d..69d962a8 100644 --- a/sdk/packages/selector-base/src/modal.ts +++ b/sdk/packages/selector-base/src/modal.ts @@ -61,6 +61,10 @@ export class NightlyConnectSelectorModal { if (this._modal && id) this._modal.sessionId = id } + set timeoutError(error: string) { + if (this._modal && error) this._modal.timeoutError = error + } + createSelectorElement = ( variablesOverride?: object, stylesOverride?: string, diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index ed3ddce4..f5031a2f 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -161,7 +161,7 @@ export class NightlyConnectAdapter implements Injected { ) if (!adapter._connectionOptions.disableModal) { adapter._modal = new NightlyConnectSelectorModal( - adapter.walletsList as IWalletListItem[], + adapter.walletsList, appInitData.url ?? 'https://nc2.nightly.app', networkToData(adapter.network), anchorRef, @@ -553,7 +553,7 @@ export class NightlyConnectAdapter implements Injected { if (checks > 500) { clearInterval(loadingInterval) // reject(new Error('Connecting takes too long')) - // TODO we need to have a way to show error on modal + if (this._modal) this._modal.timeoutError = 'Connecting is taking too long' } }, 10) } diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 6bd10e5e..d44c3449 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -588,7 +588,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { if (checks > 500) { clearInterval(loadingInterval) // reject(new Error('Connecting takes too long')) - // TODO we need to have a way to show error on modal + if (this._modal) this._modal.timeoutError = 'Connecting is taking too long' } }, 10) } From cdd47df842b0de5d9c6dbdb3ebbd7e4da61f3c25 Mon Sep 17 00:00:00 2001 From: LukassF Date: Fri, 23 Feb 2024 14:59:58 +0100 Subject: [PATCH 12/27] add: polkadot event emitter --- sdk/packages/selector-polkadot/package.json | 5 ++-- sdk/packages/selector-polkadot/src/adapter.ts | 30 +++++++++++++++++-- sdk/pnpm-lock.yaml | 3 ++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/sdk/packages/selector-polkadot/package.json b/sdk/packages/selector-polkadot/package.json index b66c0271..3f23bef3 100644 --- a/sdk/packages/selector-polkadot/package.json +++ b/sdk/packages/selector-polkadot/package.json @@ -26,9 +26,10 @@ "dependencies": { "@nightlylabs/nightly-connect-polkadot": "^0.0.15", "@nightlylabs/wallet-selector-base": "^0.4.0", - "@polkadot/extension-inject": "0.46.5", "@polkadot/api": "10.10.1", - "@wallet-standard/core": "^1.0.3" + "@polkadot/extension-inject": "0.46.5", + "@wallet-standard/core": "^1.0.3", + "eventemitter3": "^5.0.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.0", diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index f5031a2f..71596097 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -22,10 +22,21 @@ import { type Signer as InjectedSigner } from '@polkadot/api/types' import { type Injected } from '@polkadot/extension-inject/types' import { IPolkadotWalletListItem, getPolkadotWalletsList } from './detection' import { networkToData, SupportedNetworks } from './utils' +import EventEmitter from 'eventemitter3' + export type AppSelectorInitialize = Omit & { network: SupportedNetworks } -export class NightlyConnectAdapter implements Injected { + +type NightlyConnectAdapterEvents = { + connect(publicKey: string): void + disconnect(): void +} + +export class NightlyConnectAdapter + extends EventEmitter + implements Injected +{ name = 'Nightly Connect' url = 'https://nightly.app' icon = logoBase64 @@ -47,7 +58,10 @@ export class NightlyConnectAdapter implements Injected { private _loading: boolean + private _publicKeyAdress: string | undefined + constructor(appInitData: AppSelectorInitialize, connectionOptions?: ConnectionOptions) { + super() this._connecting = false this._connected = false this._appInitData = appInitData @@ -107,6 +121,10 @@ export class NightlyConnectAdapter implements Injected { return this._walletsList } + get publicKeyAdress() { + return this._publicKeyAdress + } + set walletsList(list: IPolkadotWalletListItem[]) { this._walletsList = list if (this._modal) { @@ -405,6 +423,8 @@ export class NightlyConnectAdapter implements Injected { this._connected = true this._connecting = false + this._publicKeyAdress = (await this.accounts.get())[0].address + this.emit('connect', this._publicKeyAdress) persistRecentWalletForNetwork(this.network, { walletName, @@ -454,8 +474,10 @@ export class NightlyConnectAdapter implements Injected { if (this._app?.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) { // Try to eager connect if session is restored try { + this._publicKeyAdress = (await this.accounts.get())[0].address this._connected = true this._connecting = false + this.emit('connect', this._publicKeyAdress) resolve() return } catch (error) { @@ -524,7 +546,7 @@ export class NightlyConnectAdapter implements Injected { // Clear interval if app is connected clearInterval(loadingInterval) if (this._modal) this._modal.sessionId = this._app.sessionId - this._app.on('userConnected', () => { + this._app.on('userConnected', async () => { try { persistRecentWalletForNetwork(this.network, { walletName: this._chosenMobileWalletName || '', @@ -538,8 +560,11 @@ export class NightlyConnectAdapter implements Injected { this.disconnect() return } + + this._publicKeyAdress = (await this.accounts.get())[0].address this._connected = true this._connecting = false + this.emit('connect', this._publicKeyAdress) this._modal?.closeModal() resolve() } catch { @@ -602,6 +627,7 @@ export class NightlyConnectAdapter implements Injected { this._connected = false } finally { this._connecting = false + this.emit('disconnect') } } } diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 468594e4..656120e7 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -643,6 +643,9 @@ importers: '@wallet-standard/core': specifier: ^1.0.3 version: 1.0.3 + eventemitter3: + specifier: ^5.0.1 + version: 5.0.1 devDependencies: '@rollup/plugin-commonjs': specifier: ^25.0.0 From 6f25b320af4476b756ce985513e8fca503172f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?NB=F0=9F=98=88?= Date: Fri, 23 Feb 2024 16:58:28 +0100 Subject: [PATCH 13/27] fix type on connect emit --- sdk/apps/modal-example/package.json | 4 ++-- sdk/packages/selector-polkadot/package.json | 4 ++-- sdk/packages/selector-polkadot/src/adapter.ts | 20 +++++-------------- sdk/pnpm-lock.yaml | 2 +- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index 643d5aeb..e1277f6d 100644 --- a/sdk/apps/modal-example/package.json +++ b/sdk/apps/modal-example/package.json @@ -22,7 +22,7 @@ "@nightlylabs/nightly-connect-sui": "0.0.29", "@nightlylabs/wallet-selector-sui": "0.2.7", "@nightlylabs/nightly-connect-polkadot": "0.0.14", - "@nightlylabs/wallet-selector-polkadot": "0.2.1", + "@nightlylabs/wallet-selector-polkadot": "0.2.2", "@polkadot/extension-inject": "^0.46.5", "@polkadot/api": "^10.10.1", "@solana/web3.js": "^1.77.2", @@ -38,4 +38,4 @@ "engines": { "node": ">=16.8" } -} +} \ No newline at end of file diff --git a/sdk/packages/selector-polkadot/package.json b/sdk/packages/selector-polkadot/package.json index 3f23bef3..b1feef76 100644 --- a/sdk/packages/selector-polkadot/package.json +++ b/sdk/packages/selector-polkadot/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-polkadot", - "version": "0.2.1", + "version": "0.2.2", "description": "", "type": "module", "exports": { @@ -41,4 +41,4 @@ "tslib": "^2.5.3", "typescript": "^5.1.3" } -} +} \ No newline at end of file diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index 71596097..f5c617c5 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -19,7 +19,7 @@ import { } from '@nightlylabs/wallet-selector-base' import { type Signer as InjectedSigner } from '@polkadot/api/types' -import { type Injected } from '@polkadot/extension-inject/types' +import { InjectedAccount, type Injected } from '@polkadot/extension-inject/types' import { IPolkadotWalletListItem, getPolkadotWalletsList } from './detection' import { networkToData, SupportedNetworks } from './utils' import EventEmitter from 'eventemitter3' @@ -29,7 +29,7 @@ export type AppSelectorInitialize = Omit & { } type NightlyConnectAdapterEvents = { - connect(publicKey: string): void + connect(publicKey: InjectedAccount[]): void disconnect(): void } @@ -58,8 +58,6 @@ export class NightlyConnectAdapter private _loading: boolean - private _publicKeyAdress: string | undefined - constructor(appInitData: AppSelectorInitialize, connectionOptions?: ConnectionOptions) { super() this._connecting = false @@ -121,10 +119,6 @@ export class NightlyConnectAdapter return this._walletsList } - get publicKeyAdress() { - return this._publicKeyAdress - } - set walletsList(list: IPolkadotWalletListItem[]) { this._walletsList = list if (this._modal) { @@ -423,8 +417,7 @@ export class NightlyConnectAdapter this._connected = true this._connecting = false - this._publicKeyAdress = (await this.accounts.get())[0].address - this.emit('connect', this._publicKeyAdress) + this.emit('connect', await this.accounts.get()) persistRecentWalletForNetwork(this.network, { walletName, @@ -474,10 +467,9 @@ export class NightlyConnectAdapter if (this._app?.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) { // Try to eager connect if session is restored try { - this._publicKeyAdress = (await this.accounts.get())[0].address this._connected = true this._connecting = false - this.emit('connect', this._publicKeyAdress) + this.emit('connect', await this.accounts.get()) resolve() return } catch (error) { @@ -560,11 +552,9 @@ export class NightlyConnectAdapter this.disconnect() return } - - this._publicKeyAdress = (await this.accounts.get())[0].address this._connected = true this._connecting = false - this.emit('connect', this._publicKeyAdress) + this.emit('connect', await this.accounts.get()) this._modal?.closeModal() resolve() } catch { diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 656120e7..4e8721f2 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -141,7 +141,7 @@ importers: specifier: 0.0.29 version: link:../sui '@nightlylabs/wallet-selector-polkadot': - specifier: 0.2.1 + specifier: 0.2.2 version: link:../../packages/selector-polkadot '@nightlylabs/wallet-selector-solana': specifier: 0.3.0 From dad6ed417d363893930e11cdd98c946837b983db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?NB=F0=9F=98=88?= Date: Fri, 23 Feb 2024 17:18:27 +0100 Subject: [PATCH 14/27] fix hooks on connect --- .../src/routes/alephInitOnConnect.tsx | 4 +- sdk/packages/selector-polkadot/src/adapter.ts | 111 +++++++++++++----- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx b/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx index a49026c8..755df366 100644 --- a/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx +++ b/sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx @@ -26,7 +26,9 @@ export default function Polkadot() { { initOnConnect: true }, // change this to false to test disabling eager connect document.getElementById('modalAnchor') ) - + adapter.on('connect', (a) => { + console.log('adapter connected', a) + }) setAdapter(adapter) ApiPromise.create({ diff --git a/sdk/packages/selector-polkadot/src/adapter.ts b/sdk/packages/selector-polkadot/src/adapter.ts index f5c617c5..8e31fccf 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -191,7 +191,26 @@ export class NightlyConnectAdapter metadataWallets, getRecentWalletForNetwork(adapter.network)?.walletName ?? undefined ) + // Add event listener for userConnected + app.on('userConnected', async () => { + try { + persistRecentWalletForNetwork(adapter.network, { + walletName: adapter._chosenMobileWalletName || '', + walletType: ConnectionType.Nightly + }) + if (!adapter._app || adapter._app.accounts.activeAccounts.length <= 0) { + adapter._connected = false + // If user does not pass any accounts, we should disconnect + adapter.disconnect() + return + } + adapter._connected = true + adapter.emit('connect', await adapter.accounts.get()) + } catch { + adapter.disconnect() + } + }) return adapter } @@ -245,6 +264,26 @@ export class NightlyConnectAdapter ) adapter._loading = false + // Add event listener for userConnected + app.on('userConnected', async () => { + try { + persistRecentWalletForNetwork(adapter.network, { + walletName: adapter._chosenMobileWalletName || '', + walletType: ConnectionType.Nightly + }) + + if (!adapter._app || adapter._app.accounts.activeAccounts.length <= 0) { + adapter._connected = false + // If user does not pass any accounts, we should disconnect + adapter.disconnect() + return + } + adapter._connected = true + adapter.emit('connect', await adapter.accounts.get()) + } catch { + adapter.disconnect() + } + }) }) .catch(() => { adapter._loading = false @@ -487,21 +526,42 @@ export class NightlyConnectAdapter return } if (this._connectionOptions.initOnConnect) { - this._loading = true - NightlyConnectAdapter.initApp(this._appInitData) - .then(([app, metadataWallets]) => { - this._app = app - this._metadataWallets = metadataWallets - this.walletsList = getPolkadotWalletsList( - metadataWallets, - getRecentWalletForNetwork(this.network)?.walletName ?? undefined - ) - this._loading = false - }) - .catch(() => { - this._loading = false - throw new Error('Failed to initialize adapter') + try { + this._loading = true + const [app, metadataWallets] = await NightlyConnectAdapter.initApp(this._appInitData) + + this._app = app + this._metadataWallets = metadataWallets + + this.walletsList = getPolkadotWalletsList( + metadataWallets, + getRecentWalletForNetwork(this.network)?.walletName ?? undefined + ) + // Add event listener for userConnected + app.on('userConnected', async () => { + try { + persistRecentWalletForNetwork(this.network, { + walletName: this._chosenMobileWalletName || '', + walletType: ConnectionType.Nightly + }) + + if (!this._app || this._app.accounts.activeAccounts.length <= 0) { + this._connected = false + // If user does not pass any accounts, we should disconnect + this.disconnect() + return + } + this._connected = true + this.emit('connect', await this.accounts.get()) + } catch { + this.disconnect() + } }) + } catch (error) { + this._loading = false + reject(Error('Failed to initialize adapter')) + return + } } // Interval that checks if app has connected let loadingInterval: NodeJS.Timeout @@ -538,27 +598,20 @@ export class NightlyConnectAdapter // Clear interval if app is connected clearInterval(loadingInterval) if (this._modal) this._modal.sessionId = this._app.sessionId + // We already have hook for userConnected + // This is just for resolving promise this._app.on('userConnected', async () => { try { - persistRecentWalletForNetwork(this.network, { - walletName: this._chosenMobileWalletName || '', - walletType: ConnectionType.Nightly - }) - if (!this._app || this._app.accounts.activeAccounts.length <= 0) { - this._connecting = false - this._connected = false - // If user does not pass any accounts, we should disconnect - this.disconnect() - return + reject(new Error('No accounts found')) } this._connected = true - this._connecting = false - this.emit('connect', await this.accounts.get()) this._modal?.closeModal() resolve() - } catch { - this.disconnect() + } catch (error) { + reject(error) + } finally { + this._connecting = false } }) return @@ -576,8 +629,6 @@ export class NightlyConnectAdapter } catch (error: any) { this._connecting = false reject(error) - } finally { - this._connecting = false } } From 492aeb3ec35928fcec21054c957c899562d9ae55 Mon Sep 17 00:00:00 2001 From: LukassF Date: Mon, 26 Feb 2024 10:23:42 +0100 Subject: [PATCH 15/27] fix: modal display error --- .../nightly-desktop-main.ts | 34 +++++++++---------- sdk/packages/selector-solana/src/adapter.ts | 3 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts index 39582c49..4a9d8efb 100644 --- a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts +++ b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts @@ -15,7 +15,7 @@ export class NightlyDesktopMain extends LitElement { @property({ type: Function }) // eslint-disable-next-line @typescript-eslint/no-empty-function - onWalletClick: (name: string) => void = () => {} + onWalletClick: (name: string) => void = () => { } @property({ type: String }) chainIcon = '' @@ -49,11 +49,11 @@ export class NightlyDesktopMain extends LitElement { onCopy = () => { navigator.clipboard.writeText( 'nc:' + - this.sessionId + - '?network=' + - this.chainName.replace(/\s/g, '') + - '&relay=' + - this.relay + this.sessionId + + '?network=' + + this.chainName.replace(/\s/g, '') + + '&relay=' + + this.relay ) this.copyMessage = 'Copied!' clearTimeout(this.timeoutRef) @@ -67,11 +67,11 @@ export class NightlyDesktopMain extends LitElement { this.qrSource = svgToBase64( generateQrCodeXml( 'nc:' + - this.sessionId + - '?network=' + - this.chainName.replace(/\s/g, '') + - '&relay=' + - this.relay, + this.sessionId + + '?network=' + + this.chainName.replace(/\s/g, '') + + '&relay=' + + this.relay, { width: 500, height: 500, @@ -113,11 +113,11 @@ export class NightlyDesktopMain extends LitElement { - ${!this.isSessionIdImmediatelyDefined && - html`

Generating QR code...

-
`} + ` : html``}
{ const metadataWallets = await this.fetchWalletsFromRegistry() - return getSolanaWalletsList( + this.walletsList = getSolanaWalletsList( metadataWallets, getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined ) + return this.walletsList } disconnect = async () => { From d7ecc4c5b9bc5d5b1ff825e43b47da3e7efb0f00 Mon Sep 17 00:00:00 2001 From: LukassF Date: Mon, 26 Feb 2024 10:26:19 +0100 Subject: [PATCH 16/27] fix: linter errors --- .../nightly-desktop-main.ts | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts index 4a9d8efb..46ef5af6 100644 --- a/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts +++ b/sdk/packages/modal/src/components/nightly-desktop-main/nightly-desktop-main.ts @@ -15,7 +15,7 @@ export class NightlyDesktopMain extends LitElement { @property({ type: Function }) // eslint-disable-next-line @typescript-eslint/no-empty-function - onWalletClick: (name: string) => void = () => { } + onWalletClick: (name: string) => void = () => {} @property({ type: String }) chainIcon = '' @@ -49,11 +49,11 @@ export class NightlyDesktopMain extends LitElement { onCopy = () => { navigator.clipboard.writeText( 'nc:' + - this.sessionId + - '?network=' + - this.chainName.replace(/\s/g, '') + - '&relay=' + - this.relay + this.sessionId + + '?network=' + + this.chainName.replace(/\s/g, '') + + '&relay=' + + this.relay ) this.copyMessage = 'Copied!' clearTimeout(this.timeoutRef) @@ -67,11 +67,11 @@ export class NightlyDesktopMain extends LitElement { this.qrSource = svgToBase64( generateQrCodeXml( 'nc:' + - this.sessionId + - '?network=' + - this.chainName.replace(/\s/g, '') + - '&relay=' + - this.relay, + this.sessionId + + '?network=' + + this.chainName.replace(/\s/g, '') + + '&relay=' + + this.relay, { width: 500, height: 500, @@ -113,23 +113,24 @@ export class NightlyDesktopMain extends LitElement {
- ${!this.isSessionIdImmediatelyDefined ? - html`
- Loading -

Generating QR code...

-
` : html``} + ${!this.isSessionIdImmediatelyDefined + ? html`
+ Loading +

Generating QR code...

+
` + : html``}
Date: Mon, 26 Feb 2024 14:42:27 +0100 Subject: [PATCH 17/27] fix: hooks in solana selector --- .../nightly-mobile-qr/nightly-mobile-qr.ts | 27 +++--- sdk/packages/selector-polkadot/src/adapter.ts | 66 ++++++------- sdk/packages/selector-solana/src/adapter.ts | 92 +++++++++++++++---- 3 files changed, 120 insertions(+), 65 deletions(-) diff --git a/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts b/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts index f205668c..24e24022 100644 --- a/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts +++ b/sdk/packages/modal/src/components/nightly-mobile-qr/nightly-mobile-qr.ts @@ -76,18 +76,21 @@ export class NightlyMobileQr extends LitElement {
- ${!this.isSessionIdImmediatelyDefined && - html`
- - Loading -

Generating QR code...

-
`} + ${!this.isSessionIdImmediatelyDefined + ? html`
+ + Loading +

Generating QR code...

+
` + : html``}