From 4c5ad3571a2dd21d8eb4a5bad32d6df09939caf6 Mon Sep 17 00:00:00 2001 From: Thiendekaco Date: Fri, 1 Mar 2024 01:11:30 +0700 Subject: [PATCH] Resolve bug when reject connect wallet, and qrcode modal of Polkadot Vault --- packages/core/src/provider.ts | 7 +-- packages/core/src/views/connect/Index.svelte | 15 ++++- packages/ledgerPolkadot/src/index.ts | 6 +- packages/polkadotJs/src/index.ts | 7 ++- packages/polkadotVault/src/index.ts | 60 ++++++++++++------- packages/polkadotVault/src/streams.ts | 3 +- packages/polkadotVault/src/types.ts | 2 +- .../src/views/ModalConnect.svelte | 30 +++------- .../src/views/QrCodeModal.svelte | 33 ++++++++-- packages/polkadotVault/src/views/index.ts | 9 +-- packages/subwallet-polkadot/src/index.ts | 7 ++- packages/talisman/src/index.ts | 7 ++- 12 files changed, 110 insertions(+), 76 deletions(-) diff --git a/packages/core/src/provider.ts b/packages/core/src/provider.ts index 63a8cfe1b..d8a9af9eb 100644 --- a/packages/core/src/provider.ts +++ b/packages/core/src/provider.ts @@ -780,14 +780,9 @@ export const enable = async ( ) : Promise => { - try { const accounts = await provider.enable(); - return accounts - - }catch (e) { - console.log('error', (e as Error).message); - } + return accounts; } export const signDummy = async (wallet : WalletState) => { diff --git a/packages/core/src/views/connect/Index.svelte b/packages/core/src/views/connect/Index.svelte index bff0e187b..5fb8236b4 100644 --- a/packages/core/src/views/connect/Index.svelte +++ b/packages/core/src/views/connect/Index.svelte @@ -264,15 +264,21 @@ }) try { - const { address, signer, metadata } = await Promise.race([ + + + const valueResponse = await Promise.race([ // resolved account type === 'evm' ? await requestAccounts(provider as EIP1193Provider) : await enable(provider as SubstrateProvider) , // or connect wallet is called again whilst waiting for response firstValueFrom(cancelPreviousConnect$.pipe(mapTo({ address : undefined }))) - ]) + ]); + console.log(valueResponse, '1231') + if(!valueResponse ) return; + + const { address, signer, metadata } = valueResponse; // canceled previous request if (!address || address.length === 0) { @@ -372,9 +378,12 @@ } catch (error) { const { code, message } = error as { code: number; message: string } scrollToTop() + console.log(error); // user rejected account access - if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_REJECTED || message === ProviderRpcErrorMessage.ACCOUNT_ACCESS_REJECTED) { + if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_REJECTED + || message === ProviderRpcErrorMessage.ACCOUNT_ACCESS_REJECTED + ) { connectionRejected = true if (autoSelect.disableModals) { diff --git a/packages/ledgerPolkadot/src/index.ts b/packages/ledgerPolkadot/src/index.ts index fb8d9438c..00bdef8e3 100644 --- a/packages/ledgerPolkadot/src/index.ts +++ b/packages/ledgerPolkadot/src/index.ts @@ -267,7 +267,7 @@ function ledgerPolkadot({ const accounts = await this.getAccounts() if (!accounts || !Array.isArray(accounts)) { - throw new Error('No accounts were returned from Keepkey device') + throw new Error('No accounts were returned from Ledger device') } if (!accounts.length) { throw new ProviderRpcError({ @@ -285,14 +285,12 @@ function ledgerPolkadot({ } } catch (error) { const {name} = error as { name: string } - console.log(error) - // This error indicates that the keepkey is paired with another app + throw new ProviderRpcError({ code: 4001, message: errorMessages[ERROR_BUSY] }) - // This error indicates that for some reason we can't claim the usb device } } diff --git a/packages/polkadotJs/src/index.ts b/packages/polkadotJs/src/index.ts index 388792065..95b953bab 100644 --- a/packages/polkadotJs/src/index.ts +++ b/packages/polkadotJs/src/index.ts @@ -1,4 +1,4 @@ -import type { WalletInit, EIP1193Provider } from '@subwallet-connect/common' +import {WalletInit, EIP1193Provider, ProviderRpcError } from '@subwallet-connect/common' import { SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet-connect/common"; import { InjectedMetadata, InjectedWindow } from "@polkadot/extension-inject/types"; import EventEmitter from "eventemitter3"; @@ -62,7 +62,10 @@ function SubWallet (): WalletInit { ) } } catch (e) { - console.log('error', (e as Error).message); + throw new ProviderRpcError({ + code: 4001, + message: 'User rejected the request.' + }) } }, async signDummy(address: string, data: string, diff --git a/packages/polkadotVault/src/index.ts b/packages/polkadotVault/src/index.ts index 3f690469d..66da78fd6 100644 --- a/packages/polkadotVault/src/index.ts +++ b/packages/polkadotVault/src/index.ts @@ -1,4 +1,5 @@ -import type { SubstrateProvider, WalletInit, WalletInterfaceSubstrate } from '@subwallet-connect/common' +import type { SubstrateProvider, WalletInit, WalletInterfaceSubstrate } from '@subwallet-connect/common'; +import { ProviderRpcErrorMessage } from '@subwallet-connect/common'; import EventEmitter from 'eventemitter3'; import type { Signer } from '@polkadot/types/types'; import type { PayloadParams, RequestArguments } from './types.js'; @@ -44,16 +45,11 @@ function PolkadotVault (): WalletInit { } async enable() { - - try { const account = await this.request({ method: 'polkadot_requestAccounts' }) return { address: [account as string] } - } catch (e) { - console.log('error', (e as Error).message); - } } async signDummy(address: string, data: string, signer: Signer) { @@ -70,26 +66,40 @@ function PolkadotVault (): WalletInit { async request ({ method, params } : RequestArguments) { if(method === 'polkadot_requestAccounts') { - const account = await modalConnect('getAccount'); - console.log(account) - const { - address, - genesisHash, - isSubstrate - } = generateAccount(account); - - if(!isSubstrate){ + try { + const account = await modalConnect('getAccount'); + + if(!account) { + throw new ProviderRpcError({ + code: 4001, + message: ProviderRpcErrorMessage.ACCOUNT_ACCESS_REJECTED + }) + } + + const { + address, + genesisHash, + isSubstrate + } = generateAccount(account); + + if(!isSubstrate){ + throw new ProviderRpcError({ + code: 4001, + message: 'Type wallet is invalid' + }) + } + + const uniqueChainNetwork = chains.find(({ id, namespace }) => namespace === 'substrate' && genesisHash.includes(id)); + if(uniqueChainNetwork){ + this.emit('chainChanged', uniqueChainNetwork.id) + } + return address; + }catch (e) { throw new ProviderRpcError({ code: 4001, - message: 'Type wallet is invalid' + message: ProviderRpcErrorMessage.ACCOUNT_ACCESS_REJECTED }) } - - const uniqueChainNetwork = chains.find(({ id, namespace }) => namespace === 'substrate' && genesisHash.includes(id)); - if(uniqueChainNetwork){ - this.emit('chainChanged', uniqueChainNetwork.id) - } - return address; } if(method === 'polkadot_signMessage') { if(!( params && Array.isArray(params) && params.length >= 3)){ @@ -106,6 +116,12 @@ function PolkadotVault (): WalletInit { address: params[0], transactionPayload: u8aWrapBytes(params[1]) } as PayloadParams); + if(!result) { + throw new ProviderRpcError({ + code: 4001, + message: 'User reject this request' + }) + } return { signature: result }; }catch (e) { throw new ProviderRpcError({ diff --git a/packages/polkadotVault/src/streams.ts b/packages/polkadotVault/src/streams.ts index e8f23edea..91edc1e2f 100644 --- a/packages/polkadotVault/src/streams.ts +++ b/packages/polkadotVault/src/streams.ts @@ -5,4 +5,5 @@ import type { PayloadParams, QRResult } from './types.js'; export const payloadUri$ = new BehaviorSubject({} as PayloadParams); -export const resultQrScan$ = new Subject(); +export const resultQrScan$ = new Subject(); + diff --git a/packages/polkadotVault/src/types.ts b/packages/polkadotVault/src/types.ts index a02186f51..47d44a257 100644 --- a/packages/polkadotVault/src/types.ts +++ b/packages/polkadotVault/src/types.ts @@ -6,7 +6,7 @@ export interface RequestArguments { } -export type ModalStep = 'showQrCode' | 'scanQrCode' | 'successStep'; +export type ModalStep = 'showQrCode' | 'scanQrCode' | 'successStep' | 'errorStep'; export interface Account { isSubstrate: boolean diff --git a/packages/polkadotVault/src/views/ModalConnect.svelte b/packages/polkadotVault/src/views/ModalConnect.svelte index b9139023c..48f18a64c 100644 --- a/packages/polkadotVault/src/views/ModalConnect.svelte +++ b/packages/polkadotVault/src/views/ModalConnect.svelte @@ -11,28 +11,10 @@ export let modalStep$: BehaviorSubject; - export let resultQrScan$: Subject; - - let uri : Uint8Array - - payloadUri$.subscribe(({ - address, - genesisHash, - transactionPayload, - isMessage, - } : PayloadParams) => { - const cmd = () => { - if (isMessage) { - return CMD.SUBSTRATE.SIGN_MSG; - } else { - return CMD.SUBSTRATE.SIGN_IMMORTAL; - } - } - if(!address) return; + export let resultQrScan$: Subject; + - uri = createSignPayload(address, cmd(), transactionPayload , genesisHash ) - }) function setStep(update: ModalStep) { modalStep$.next(update) @@ -54,7 +36,9 @@ (label !== 'Polkadot Vault' && type !== 'substrate') )))} } - setStep('successStep'); + + resultQrScan$.next(undefined); + setStep('errorStep'); } function onSuccessAfterScanQrCode ( result: string) { @@ -218,8 +202,8 @@