Skip to content

Commit

Permalink
Resolve bug when reject connect wallet, and qrcode modal of Polkadot …
Browse files Browse the repository at this point in the history
…Vault
  • Loading branch information
Thiendekaco committed Mar 1, 2024
1 parent 8e3987e commit 4c5ad35
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 76 deletions.
7 changes: 1 addition & 6 deletions packages/core/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,14 +780,9 @@ export const enable = async (
)
: Promise<WalletConnectState> => {

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) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/views/connect/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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<WalletConnectState>({
address : undefined
})))
])
]);
console.log(valueResponse, '1231')
if(!valueResponse ) return;
const { address, signer, metadata } = valueResponse;
// canceled previous request
if (!address || address.length === 0) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions packages/ledgerPolkadot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
}

}
Expand Down
7 changes: 5 additions & 2 deletions packages/polkadotJs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
60 changes: 38 additions & 22 deletions packages/polkadotVault/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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)){
Expand All @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion packages/polkadotVault/src/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import type { PayloadParams, QRResult } from './types.js';
export const payloadUri$ =
new BehaviorSubject<PayloadParams>({} as PayloadParams);

export const resultQrScan$ = new Subject<QRResult>();
export const resultQrScan$ = new Subject<QRResult | undefined>();

2 changes: 1 addition & 1 deletion packages/polkadotVault/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 7 additions & 23 deletions packages/polkadotVault/src/views/ModalConnect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,10 @@
export let modalStep$: BehaviorSubject<ModalStep>;
export let resultQrScan$: Subject<QRResult>;
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<QRResult | undefined>;
uri = createSignPayload(address, cmd(), transactionPayload , genesisHash )
})
function setStep(update: ModalStep) {
modalStep$.next(update)
Expand All @@ -54,7 +36,9 @@
(label !== 'Polkadot Vault' && type !== 'substrate')
)))}
}
setStep('successStep');
resultQrScan$.next(undefined);
setStep('errorStep');
}
function onSuccessAfterScanQrCode ( result: string) {
Expand Down Expand Up @@ -218,8 +202,8 @@


<div class="modal-connect-container">
{#if ($modalStep$ === 'showQrCode' && uri)}
<QrCodeModal uri={uri} onEnable={onSuccessAfterShowQrCode} onDismiss={closeAllModal}/>
{#if ($modalStep$ === 'showQrCode' )}
<QrCodeModal onEnable={onSuccessAfterShowQrCode} onDismiss={closeAllModal}/>
{/if}
{#if ($modalStep$ === 'scanQrCode' )}
<ScanQrModal onBack={goBack} onSuccess={onSuccessAfterScanQrCode} onDismiss={closeAllModal} isBack={typeAction === 'signTransaction'}/>
Expand Down
33 changes: 27 additions & 6 deletions packages/polkadotVault/src/views/QrCodeModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,48 @@
import { fade } from 'svelte/transition';
import { onMount } from 'svelte';
import { isSVG, getDataString, createFrames } from '../utils.js';
import { isSVG, getDataString, createFrames, createSignPayload } from '../utils.js';
import { logoWallet } from '../icon/index.js';
import { xxhashAsHex } from '@polkadot/util-crypto';
import QrCode from 'svelte-qrcode';
import Modal from './Modal.svelte';
import { payloadUri$ } from '../streams.js';
import type { PayloadParams } from '../types.js';
import { CMD } from '../constants.js';
export let uri : Uint8Array;
let uri : Uint8Array;
export let onEnable: () => void
export let onDismiss: () => void
let node : any;
let frames = [];
let valueHash : any;
onMount(() => {
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;
const uri = createSignPayload(
address, cmd(), transactionPayload , genesisHash
);
valueHash = xxhashAsHex(uri);
frames = createFrames(uri); // encode on demand
const _images = frames.map((frame) => getDataString(frame));
valueHash = _images[0]
});
valueHash = _images[0];
})
</script>

Expand Down Expand Up @@ -128,7 +149,7 @@
}
</style>
{#if (uri && valueHash)}
{#if (valueHash)}
<Modal close={onDismiss} maskClose={true}>
<div class="title" slot="title">
Confirm
Expand Down
9 changes: 5 additions & 4 deletions packages/polkadotVault/src/views/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BehaviorSubject, firstValueFrom, Subject, take } from 'rxjs'
import { payloadUri$, resultQrScan$ } from '../streams.js';
import type { PayloadParams, QRResult, TypeAction } from '../types.js';
import type { ModalStep, PayloadParams, QRResult, TypeAction } from '../types.js';
import ModalConnect from './ModalConnect.svelte';
import type { ModalStep } from '../types.js';
import { ProviderRpcError, ProviderRpcErrorCode, ProviderRpcErrorMessage } from '@subwallet-connect/common';

// eslint-disable-next-line max-len
const modalConnect = async (
typeAction: TypeAction,
payload ?: PayloadParams
): Promise<QRResult> => {
): Promise<QRResult | undefined> => {
// if (options) {
// const error = validateSelectAccountOptions(options)
//
Expand All @@ -24,6 +24,7 @@ const modalConnect = async (
const app = mountModalConnect(typeAction, modalStep$, resultQrScan$)
payload && payloadUri$.next(payload)


resultQrScan$.pipe(take(1)).subscribe(() => {
app.$destroy()
})
Expand All @@ -36,7 +37,7 @@ const modalConnect = async (
const mountModalConnect = (
typeAction: TypeAction,
modalStep$: BehaviorSubject<ModalStep>,
resultQrScan$: Subject<QRResult>,
resultQrScan$: Subject<QRResult | undefined>,
) => {
class ModalConnectEl extends HTMLElement {
constructor() {
Expand Down
7 changes: 5 additions & 2 deletions packages/subwallet-polkadot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WalletInit, EIP1193Provider } from '@subwallet-connect/common'
import { SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet-connect/common";
import {ProviderRpcError, SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet-connect/common";
import { InjectedMetadata, InjectedWindow } from "@polkadot/extension-inject/types";
import EventEmitter from "eventemitter3";
import { Signer } from "@polkadot/types/types";
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions packages/talisman/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WalletInit, EIP1193Provider } from '@subwallet-connect/common'
import { SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet-connect/common";
import {ProviderRpcError, SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet-connect/common";
import { InjectedMetadata, InjectedWindow } from "@polkadot/extension-inject/types";
import EventEmitter from "eventemitter3";
import { Signer } from "@polkadot/types/types";
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4c5ad35

Please sign in to comment.