Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add current wallet getter to solana selector #179

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions sdk/apps/modal-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
"@nightlylabs/nightly-connect-polkadot": "0.0.15",
"@nightlylabs/nightly-connect-solana": "0.0.29",
"@nightlylabs/nightly-connect-sui": "0.0.29",
"@nightlylabs/wallet-selector-aptos": "0.1.1",

"@nightlylabs/wallet-selector-base": "^0.4.0",

"@nightlylabs/wallet-selector-polkadot": "0.2.5",
"@nightlylabs/wallet-selector-solana": "0.3.1",
"@nightlylabs/wallet-selector-sui": "0.3.1",

"@nightlylabs/wallet-selector-aptos": "0.1.2",
"@nightlylabs/wallet-selector-base": "^0.4.1",
"@nightlylabs/wallet-selector-polkadot": "0.2.6",
"@nightlylabs/wallet-selector-solana": "0.3.2",
"@nightlylabs/wallet-selector-sui": "0.3.2",
"@polkadot/api": "^10.10.1",
"@polkadot/extension-inject": "^0.46.5",
"@solana/web3.js": "^1.77.2",
Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/selector-aptos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-aptos",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"type": "module",
"exports": {
Expand All @@ -27,7 +27,7 @@
"@aptos-labs/ts-sdk": "^1.9.1",
"@aptos-labs/wallet-standard": "^0.0.11",
"@nightlylabs/nightly-connect-aptos": "0.0.2",
"@nightlylabs/wallet-selector-base": "0.4.0",
"@nightlylabs/wallet-selector-base": "0.4.1",
"@wallet-standard/core": "^1.0.3",
"eventemitter3": "^5.0.1"
},
Expand Down
38 changes: 37 additions & 1 deletion sdk/packages/selector-aptos/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ConnectionType,
defaultConnectionOptions,
getRecentWalletForNetwork,
ISelectedWallet,
isMobileBrowser,
IWalletListItem,
logoBase64,
Expand Down Expand Up @@ -69,6 +70,8 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
private _detectionIntervalId: NodeJS.Timeout | undefined
private _maxNumberOfChecks = 10

private _selectedWallet: ISelectedWallet | undefined = undefined

get walletsList() {
return this._walletsList
}
Expand All @@ -80,6 +83,10 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
}
}

get selectedWallet() {
return this._selectedWallet
}

get sessionId() {
return this._app?.sessionId
}
Expand Down Expand Up @@ -180,6 +187,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._accountInfo = accountInfo
adapter._networkInfo = networkInfo
adapter.connected = true
Expand Down Expand Up @@ -261,6 +269,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._accountInfo = accountInfo
adapter._networkInfo = networkInfo
adapter.connected = true
Expand Down Expand Up @@ -350,6 +359,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
const { accountInfo, networkInfo } = deserializeConnectData(
this._app.base.clientMetadata
)
this.setSelectedWallet({ isRemote: true })
this._accountInfo = accountInfo
this._networkInfo = networkInfo
this.connected = true
Expand Down Expand Up @@ -405,7 +415,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
this.disconnect()
return
}

this.setSelectedWallet({ isRemote: true })
this._accountInfo = accountInfo
this._networkInfo = networkInfo
this.connected = true
Expand Down Expand Up @@ -529,6 +539,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
clearSessionIdForNetwork(APTOS_NETWORK)
// Refresh app session
this._app = await AppAptos.build(this._appInitData)
this._selectedWallet = undefined

// Add event listener for userConnected
this._app.on('userConnected', async (accountInfo, networkInfo) => {
Expand All @@ -545,6 +556,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
this.disconnect()
return
}
this.setSelectedWallet({ isRemote: true })
this._accountInfo = accountInfo
this._networkInfo = networkInfo
this.connected = true
Expand Down Expand Up @@ -703,6 +715,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
}

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

if (!this._app) {
throw new Error('Wallet not ready')
Expand Down Expand Up @@ -769,6 +782,7 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
}

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

if (typeof wallet?.standardWallet === 'undefined') {
if (this._modal) {
Expand Down Expand Up @@ -856,4 +870,26 @@ export class NightlyConnectAptosAdapter extends EventEmitter<AptosAdapterEvents>
)
}, 500)
}

setSelectedWallet = ({
wallet,
isRemote = false
}: {
wallet?: IWalletListItem
isRemote?: boolean
}) => {
if (!wallet) {
// Connecting to the nightly mobile app
wallet = this.walletsList.find((wallet) => wallet.name === 'Nightly')
}

if (wallet) {
this._selectedWallet = {
name: wallet.name,
image: wallet.image,
homepage: wallet.homepage,
walletType: isRemote ? 'mobile' : wallet.walletType
}
}
}
}
2 changes: 1 addition & 1 deletion sdk/packages/selector-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-base",
"version": "0.4.0",
"version": "0.4.1",
"description": "",
"type": "module",
"exports": {
Expand Down
10 changes: 9 additions & 1 deletion sdk/packages/selector-base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,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 Images } from '../../../bindings/Images'
import { WalletMetadata } from '../../../bindings/WalletMetadata'
import { type WalletType } from '../../../bindings/WalletType'
export { type WalletMetadata } from '../../../bindings/WalletMetadata'

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

export interface ISelectedWallet {
name: string
homepage: string
walletType: WalletType
image: Images
}

export interface NetworkData {
name: string
icon: string
Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/selector-polkadot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-polkadot",
"version": "0.2.5",
"version": "0.2.6",
"description": "",
"type": "module",
"exports": {
Expand All @@ -25,7 +25,7 @@
"license": "ISC",
"dependencies": {
"@nightlylabs/nightly-connect-polkadot": "^0.0.16",
"@nightlylabs/wallet-selector-base": "^0.4.0",
"@nightlylabs/wallet-selector-base": "^0.4.1",
"@polkadot/api": "10.10.1",
"@polkadot/extension-inject": "0.46.5",
"@wallet-standard/core": "^1.0.3",
Expand Down
64 changes: 61 additions & 3 deletions sdk/packages/selector-polkadot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppPolkadot, AppPolkadotInitialize } from '@nightlylabs/nightly-connect
import {
ConnectionOptions,
ConnectionType,
ISelectedWallet,
IWalletListItem,
NightlyConnectSelectorModal,
WalletMetadata,
Expand All @@ -20,9 +21,9 @@ import {

import { type Signer as InjectedSigner } from '@polkadot/api/types'
import { InjectedAccount, type Injected } from '@polkadot/extension-inject/types'
import { IPolkadotWalletListItem, getPolkadotWalletsList } from './detection'
import { networkToData, SupportedNetworks } from './utils'
import EventEmitter from 'eventemitter3'
import { IPolkadotWalletListItem, getPolkadotWalletsList } from './detection'
import { SupportedNetworks, networkToData } from './utils'

export type AppSelectorInitialize = Omit<AppPolkadotInitialize, 'network'> & {
network: SupportedNetworks
Expand Down Expand Up @@ -58,6 +59,8 @@ export class NightlyConnectAdapter

private _loading: boolean

private _selectedWallet: ISelectedWallet | undefined = undefined

// interval used for checking for wallets with delayed detection
private _detectionIntervalId: NodeJS.Timeout | undefined
private _maxNumberOfChecks = 10
Expand Down Expand Up @@ -124,6 +127,10 @@ export class NightlyConnectAdapter
return this._walletsList
}

get selectedWallet() {
return this._selectedWallet
}

set walletsList(list: IPolkadotWalletListItem[]) {
this._walletsList = list
if (this._modal) {
Expand Down Expand Up @@ -212,6 +219,7 @@ export class NightlyConnectAdapter
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._connected = true
adapter.emit('connect', await adapter.accounts.get())
} catch {
Expand Down Expand Up @@ -287,6 +295,7 @@ export class NightlyConnectAdapter
adapter.disconnect()
return
}
adapter.setSelectedWallet({ isRemote: true })
adapter._connected = true
adapter.emit('connect', await adapter.accounts.get())
} catch {
Expand Down Expand Up @@ -358,6 +367,7 @@ export class NightlyConnectAdapter
}

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

if (!this._app) {
throw new Error('Wallet not ready')
Expand Down Expand Up @@ -434,7 +444,9 @@ export class NightlyConnectAdapter
if (this._modal) {
this._modal.setStandardWalletConnectProgress(true)
}
const adapter = this.walletsList.find((w) => w.name === walletName)?.injectedWallet
const wallet = this.walletsList.find((w) => w.name === walletName)
const adapter = wallet?.injectedWallet
this.setSelectedWallet({ wallet })
if (typeof adapter === 'undefined') {
if (this._modal) {
this._modal.setStandardWalletConnectProgress(false)
Expand Down Expand Up @@ -515,6 +527,7 @@ export class NightlyConnectAdapter
if (this._app?.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) {
// Try to eager connect if session is restored
try {
this.setSelectedWallet({ isRemote: true })
this._connected = true
this._connecting = false
this.emit('connect', await this.accounts.get())
Expand Down Expand Up @@ -561,6 +574,7 @@ export class NightlyConnectAdapter
this.disconnect()
return
}
this.setSelectedWallet({ isRemote: true })
this._connected = true
this.emit('connect', await this.accounts.get())
} catch {
Expand Down Expand Up @@ -688,6 +702,28 @@ export class NightlyConnectAdapter
clearRecentWalletForNetwork(this.network)
this._innerStandardAdapter = undefined
this._app = await AppPolkadot.build(this._appInitData)
this._selectedWallet = undefined

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._connected = false
// If user does not pass any accounts, we should disconnect
this.disconnect()
return
}
this.setSelectedWallet({ isRemote: true })
this._connected = true
this.emit('connect', await this.accounts.get())
} catch {
this.disconnect()
}
})

// Update recent wallet
this.walletsList = getPolkadotWalletsList(
Expand All @@ -705,4 +741,26 @@ export class NightlyConnectAdapter
clearInterval(this._detectionIntervalId)
}
}

setSelectedWallet = ({
wallet,
isRemote = false
}: {
wallet?: IWalletListItem
isRemote?: boolean
}) => {
if (!wallet) {
// Connecting to the nightly mobile app
wallet = this.walletsList.find((wallet) => wallet.name === 'Nightly')
}

if (wallet) {
this._selectedWallet = {
name: wallet.name,
image: wallet.image,
homepage: wallet.homepage,
walletType: isRemote ? 'mobile' : wallet.walletType
}
}
}
}
4 changes: 2 additions & 2 deletions sdk/packages/selector-solana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-solana",
"version": "0.3.1",
"version": "0.3.2",
"description": "",
"type": "module",
"exports": {
Expand All @@ -25,7 +25,7 @@
"license": "ISC",
"dependencies": {
"@nightlylabs/nightly-connect-solana": "^0.0.29",
"@nightlylabs/wallet-selector-base": "^0.4.0",
"@nightlylabs/wallet-selector-base": "^0.4.1",
"@solana/wallet-adapter-base": "^0.9.22",
"@solana/wallet-standard": "^1.0.2",
"@solana/web3.js": "^1.77.2",
Expand Down
Loading
Loading