Skip to content

Commit

Permalink
add current wallet getter for solana
Browse files Browse the repository at this point in the history
  • Loading branch information
LukassF committed May 15, 2024
1 parent d5d51c6 commit b67b1dd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
5 changes: 4 additions & 1 deletion sdk/packages/selector-base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,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 { WalletMetadata } from '../../../bindings/WalletMetadata'
import { type WalletType } from '../../../bindings/WalletType'
export { type WalletMetadata } from '../../../bindings/WalletMetadata'

export interface Adapter {
Expand All @@ -29,6 +29,9 @@ export interface IWalletListItem
standardWallet?: Wallet
}

export interface ISelectedWallet
extends Pick<IWalletListItem, 'name' | 'slug' | 'walletType' | 'image' | 'homepage'> {}

export interface NetworkData {
name: string
icon: string
Expand Down
58 changes: 45 additions & 13 deletions sdk/packages/selector-solana/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { AppSolana, SOLANA_NETWORK } from '@nightlylabs/nightly-connect-solana'
import {
AppInitData,
ConnectionOptions,
ConnectionType,
ISelectedWallet,
IWalletListItem,
NightlyConnectSelectorModal,
WalletMetadata,
XMLOptions,
clearRecentWalletForNetwork,
clearSessionIdForNetwork,
defaultConnectionOptions,
getRecentWalletForNetwork,
isMobileBrowser,
logoBase64,
NightlyConnectSelectorModal,
persistRecentWalletForNetwork,
triggerConnect,
sleep,
XMLOptions,
ConnectionType,
ConnectionOptions,
defaultConnectionOptions,
WalletMetadata,
IWalletListItem
triggerConnect
} from '@nightlylabs/wallet-selector-base'
import {
BaseMessageSignerWalletAdapter,
WalletAdapterCompatibleStandardWallet,
WalletAdapterEvents,
WalletError,
WalletName,
WalletNotConnectedError,
WalletNotReadyError,
Expand All @@ -33,8 +33,8 @@ import {
} from '@solana/wallet-adapter-base'
import { StandardWalletAdapter } from '@solana/wallet-standard'
import { PublicKey, Transaction, TransactionVersion, VersionedTransaction } from '@solana/web3.js'
import { getSolanaWalletsList } from './detection'
import { StandardEventsChangeProperties } from '@wallet-standard/core'
import { getSolanaWalletsList } from './detection'

type NightlyConnectAdapterEvents = WalletAdapterEvents & {
change(properties: StandardEventsChangeProperties): void
Expand Down Expand Up @@ -74,6 +74,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
private _detectionIntervalId: NodeJS.Timeout | undefined
private _maxNumberOfChecks = 10

private _selectedWallet: ISelectedWallet | undefined = undefined

private _connectionOptions: ConnectionOptions = defaultConnectionOptions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _eventHandlers: Map<string, (...args: any[]) => void> = new Map()
Expand Down Expand Up @@ -114,6 +116,10 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
return this._walletsList
}

get selectedWallet() {
return this._selectedWallet
}

set walletsList(list: IWalletListItem[]) {
this._walletsList = list
if (this._modal) {
Expand Down Expand Up @@ -240,6 +246,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._publicKey = adapter._app.connectedPublicKeys[0]
adapter._connected = true
adapter.emit('connect', adapter._publicKey)
Expand Down Expand Up @@ -326,6 +333,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._publicKey = adapter._app.connectedPublicKeys[0]
adapter._connected = true
adapter.emit('connect', adapter._publicKey)
Expand Down Expand Up @@ -399,6 +407,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
}

const wallet = this.walletsList.find((w) => w.name === walletName)
this.setSelectedWallet({ walletName })

if (!this._app) {
throw new Error('Wallet not ready')
Expand Down Expand Up @@ -450,6 +459,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
if (this._modal) {
this._modal.setStandardWalletConnectProgress(false)
}
this._selectedWallet = undefined

throw err
}
}
Expand All @@ -461,16 +472,19 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
this._modal.setStandardWalletConnectProgress(true)
}

const wallet = this.walletsList.find((w) => w.name === walletName)?.standardWallet
if (typeof wallet === 'undefined') {
const wallet = this.walletsList.find((w) => w.name === walletName)
this.setSelectedWallet({ walletName })
const standardWallet = wallet?.standardWallet

if (typeof standardWallet === 'undefined') {
if (this._modal) {
this._modal.setStandardWalletConnectProgress(false)
}
throw new Error('Wallet not found')
}

const adapter = new StandardWalletAdapter({
wallet: wallet as WalletAdapterCompatibleStandardWallet
wallet: standardWallet as WalletAdapterCompatibleStandardWallet
})

await adapter.connect()
Expand All @@ -497,6 +511,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
if (this._modal) {
this._modal.setStandardWalletConnectProgress(false)
}
this._selectedWallet = undefined

throw err
}
Expand Down Expand Up @@ -544,6 +559,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
this._connected = true
this._connecting = false
this._appSessionActive = true
this.setSelectedWallet({ isRemote: true })
this.emit('connect', this._publicKey)
resolve()
return
Expand Down Expand Up @@ -590,6 +606,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
this.disconnect()
return
}
this.setSelectedWallet({ isRemote: true })
this._publicKey = this._app.connectedPublicKeys[0]
this._connected = true
this.emit('connect', this._publicKey)
Expand Down Expand Up @@ -720,6 +737,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
clearSessionIdForNetwork(SOLANA_NETWORK)
this._appSessionActive = false
this._loading = true
this._selectedWallet = undefined
try {
this._app = await AppSolana.build(this._appInitData)
// Add event listener for userConnected
Expand All @@ -736,6 +754,7 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
this.disconnect()
return
}
this.setSelectedWallet({ isRemote: true })
this._publicKey = this._app.connectedPublicKeys[0]
this._connected = true
this.emit('connect', this._publicKey)
Expand Down Expand Up @@ -837,4 +856,17 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
throw error
}
}

setSelectedWallet = ({ walletName = 'Nightly', isRemote = false }) => {
const wallet = this.walletsList.find((wallet) => wallet.name === walletName)
if (wallet) {
this._selectedWallet = {
name: wallet.name,
image: wallet.image,
homepage: wallet.homepage,
slug: wallet.slug,
walletType: isRemote ? 'mobile' : wallet.walletType
}
}
}
}

0 comments on commit b67b1dd

Please sign in to comment.