diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index 475e6bd3..ce0efa01 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.26", "@nightlylabs/wallet-selector-sui": "0.2.3", "@nightlylabs/nightly-connect-polkadot": "0.0.6", - "@nightlylabs/wallet-selector-polkadot": "0.1.3", + "@nightlylabs/wallet-selector-polkadot": "0.1.4", "@polkadot/extension-inject": "^0.46.5", "@polkadot/api": "^10.9.1", "@solana/web3.js": "^1.77.2", diff --git a/sdk/packages/selector-polkadot/package.json b/sdk/packages/selector-polkadot/package.json index 5de2ac65..19769b08 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.1.3", + "version": "0.1.4", "description": "", "type": "module", "exports": { @@ -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 316beeca..3e25cbb2 100644 --- a/sdk/packages/selector-polkadot/src/adapter.ts +++ b/sdk/packages/selector-polkadot/src/adapter.ts @@ -225,16 +225,21 @@ export class NightlyConnectAdapter implements Injected { adapter._loading = true - NightlyConnectAdapter.initApp(appInitData).then(([app, metadataWallets]) => { - adapter._app = app - adapter._metadataWallets = metadataWallets - adapter.walletsList = getPolkadotWalletsList( - metadataWallets, - getRecentStandardWalletForNetwork(adapter.network) ?? undefined - ) - - adapter._loading = false - }) + NightlyConnectAdapter.initApp(appInitData) + .then(([app, metadataWallets]) => { + adapter._app = app + adapter._metadataWallets = metadataWallets + adapter.walletsList = getPolkadotWalletsList( + metadataWallets, + getRecentStandardWalletForNetwork(adapter.network) ?? undefined + ) + + adapter._loading = false + }) + .catch(() => { + adapter._loading = false + throw new Error('Failed to initialize adapter') + }) return adapter } @@ -399,7 +404,10 @@ export class NightlyConnectAdapter implements Injected { try { // @ts-expect-error we want to pass network to enable const inject = await adapter!.enable!('Nightly Connect', this.network) // TODO should we also use connect? - + // Assert that there is at least one account + if ((await inject.accounts.get()).length <= 0) { + throw new Error('No accounts found') + } persistRecentStandardWalletForNetwork(walletName, this.network) persistStandardConnectForNetwork(this.network) this._innerStandardAdapter = { @@ -421,6 +429,8 @@ export class NightlyConnectAdapter implements Injected { this._modal?.closeModal() onSuccess() } catch { + // clear recent wallet + persistStandardDisconnectForNetwork(this.network) if (this._modal) { this._modal.setStandardWalletConnectProgress(false) } @@ -453,10 +463,11 @@ export class NightlyConnectAdapter implements Injected { getRecentStandardWalletForNetwork(this.network) ?? undefined ) } catch (e) { + this._connecting = false if (!this._app) { - this._connecting = false throw new Error('Wallet not ready') } + throw e } } } else { @@ -511,6 +522,11 @@ export class NightlyConnectAdapter implements Injected { } else { clearRecentStandardWalletForNetwork(this.network) } + if (!this._app || this._app.accounts.activeAccounts.length <= 0) { + this._connecting = false + // If user does not pass any accounts, we should disconnect + this.disconnect() + } this._connected = true this._connecting = false this._appSessionActive = true @@ -551,37 +567,22 @@ export class NightlyConnectAdapter implements Injected { }) disconnect = async () => { - if (this.connected) { - if (this._appSessionActive) { - clearSessionIdForNetwork(this.network) - this._appSessionActive = false - this._loading = true - AppPolkadot.build(this._appInitData) - .then( - (app) => { - this._app = app - }, - (err) => { - console.log(err) - } - ) - .finally(() => { - this._loading = false - }) - } - if (this._innerStandardAdapter) { - this._innerStandardAdapter = undefined - persistStandardDisconnectForNetwork(this.network) - } - // Update recent wallet - this.walletsList = getPolkadotWalletsList( - this._metadataWallets, - getRecentStandardWalletForNetwork(this.network) ?? undefined - ) - if (this._modal) { - this._modal.walletsList = this.walletsList - } - this._connected = false + // Some apps might use disconnect to reset state / recreate session + clearSessionIdForNetwork(this.network) + this._appSessionActive = false + this._app = await AppPolkadot.build(this._appInitData) + if (this._innerStandardAdapter) { + this._innerStandardAdapter = undefined + persistStandardDisconnectForNetwork(this.network) + } + // Update recent wallet + this.walletsList = getPolkadotWalletsList( + this._metadataWallets, + getRecentStandardWalletForNetwork(this.network) ?? undefined + ) + if (this._modal) { + this._modal.walletsList = this.walletsList } + this._connected = false } } diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 979c23c2..8a910e88 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -29,7 +29,7 @@ importers: version: 2.8.0 turbo: specifier: latest - version: 1.10.14 + version: 1.10.7 typescript: specifier: ^4.9.0 version: 4.9.4 @@ -138,7 +138,7 @@ importers: specifier: 0.0.26 version: link:../sui '@nightlylabs/wallet-selector-polkadot': - specifier: 0.1.3 + specifier: 0.1.4 version: link:../../packages/selector-polkadot '@nightlylabs/wallet-selector-solana': specifier: 0.2.3 @@ -16338,64 +16338,65 @@ packages: typescript: 5.1.6 dev: false - /turbo-darwin-64@1.10.14: - resolution: {integrity: sha512-I8RtFk1b9UILAExPdG/XRgGQz95nmXPE7OiGb6ytjtNIR5/UZBS/xVX/7HYpCdmfriKdVwBKhalCoV4oDvAGEg==} + /turbo-darwin-64@1.10.7: + resolution: {integrity: sha512-N2MNuhwrl6g7vGuz4y3fFG2aR1oCs0UZ5HKl8KSTn/VC2y2YIuLGedQ3OVbo0TfEvygAlF3QGAAKKtOCmGPNKA==} cpu: [x64] os: [darwin] requiresBuild: true dev: true optional: true - /turbo-darwin-arm64@1.10.14: - resolution: {integrity: sha512-KAdUWryJi/XX7OD0alOuOa0aJ5TLyd4DNIYkHPHYcM6/d7YAovYvxRNwmx9iv6Vx6IkzTnLeTiUB8zy69QkG9Q==} + /turbo-darwin-arm64@1.10.7: + resolution: {integrity: sha512-WbJkvjU+6qkngp7K4EsswOriO3xrNQag7YEGRtfLoDdMTk4O4QTeU6sfg2dKfDsBpTidTvEDwgIYJhYVGzrz9Q==} cpu: [arm64] os: [darwin] requiresBuild: true dev: true optional: true - /turbo-linux-64@1.10.14: - resolution: {integrity: sha512-BOBzoREC2u4Vgpap/WDxM6wETVqVMRcM8OZw4hWzqCj2bqbQ6L0wxs1LCLWVrghQf93JBQtIGAdFFLyCSBXjWQ==} + /turbo-linux-64@1.10.7: + resolution: {integrity: sha512-x1CF2CDP1pDz/J8/B2T0hnmmOQI2+y11JGIzNP0KtwxDM7rmeg3DDTtDM/9PwGqfPotN9iVGgMiMvBuMFbsLhg==} cpu: [x64] os: [linux] requiresBuild: true dev: true optional: true - /turbo-linux-arm64@1.10.14: - resolution: {integrity: sha512-D8T6XxoTdN5D4V5qE2VZG+/lbZX/89BkAEHzXcsSUTRjrwfMepT3d2z8aT6hxv4yu8EDdooZq/2Bn/vjMI32xw==} + /turbo-linux-arm64@1.10.7: + resolution: {integrity: sha512-JtnBmaBSYbs7peJPkXzXxsRGSGBmBEIb6/kC8RRmyvPAMyqF8wIex0pttsI+9plghREiGPtRWv/lfQEPRlXnNQ==} cpu: [arm64] os: [linux] requiresBuild: true dev: true optional: true - /turbo-windows-64@1.10.14: - resolution: {integrity: sha512-zKNS3c1w4i6432N0cexZ20r/aIhV62g69opUn82FLVs/zk3Ie0GVkSB6h0rqIvMalCp7enIR87LkPSDGz9K4UA==} + /turbo-windows-64@1.10.7: + resolution: {integrity: sha512-7A/4CByoHdolWS8dg3DPm99owfu1aY/W0V0+KxFd0o2JQMTQtoBgIMSvZesXaWM57z3OLsietFivDLQPuzE75w==} cpu: [x64] os: [win32] requiresBuild: true dev: true optional: true - /turbo-windows-arm64@1.10.14: - resolution: {integrity: sha512-rkBwrTPTxNSOUF7of8eVvvM+BkfkhA2OvpHM94if8tVsU+khrjglilp8MTVPHlyS9byfemPAmFN90oRIPB05BA==} + /turbo-windows-arm64@1.10.7: + resolution: {integrity: sha512-D36K/3b6+hqm9IBAymnuVgyePktwQ+F0lSXr2B9JfAdFPBktSqGmp50JNC7pahxhnuCLj0Vdpe9RqfnJw5zATA==} cpu: [arm64] os: [win32] requiresBuild: true dev: true optional: true - /turbo@1.10.14: - resolution: {integrity: sha512-hr9wDNYcsee+vLkCDIm8qTtwhJ6+UAMJc3nIY6+PNgUTtXcQgHxCq8BGoL7gbABvNWv76CNbK5qL4Lp9G3ZYRA==} + /turbo@1.10.7: + resolution: {integrity: sha512-xm0MPM28TWx1e6TNC3wokfE5eaDqlfi0G24kmeHupDUZt5Wd0OzHFENEHMPqEaNKJ0I+AMObL6nbSZonZBV2HA==} hasBin: true + requiresBuild: true optionalDependencies: - turbo-darwin-64: 1.10.14 - turbo-darwin-arm64: 1.10.14 - turbo-linux-64: 1.10.14 - turbo-linux-arm64: 1.10.14 - turbo-windows-64: 1.10.14 - turbo-windows-arm64: 1.10.14 + turbo-darwin-64: 1.10.7 + turbo-darwin-arm64: 1.10.7 + turbo-linux-64: 1.10.7 + turbo-linux-arm64: 1.10.7 + turbo-windows-64: 1.10.7 + turbo-windows-arm64: 1.10.7 dev: true /tweetnacl@1.0.3: