From b102233ade7f66128b6847b13a0b8a2287838f9a Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Thu, 24 Mar 2022 21:41:39 +0800 Subject: [PATCH] fix: bugfix for next id (#5934) * fix: bugfix for next id * fix: bugfix * fix: refactor detach profile with next dot id --- .../background/services/identity/index.ts | 2 +- .../services/identity/persona/sign.ts | 6 +- .../background-script/IdentityService.ts | 4 - .../Personas/PersonaSignRequest/index.tsx | 74 ++++++++++++++++--- .../Personas/components/ProfileList/index.tsx | 46 +++++------- .../src/extension/popups/pages/Wallet/type.ts | 4 + 6 files changed, 90 insertions(+), 46 deletions(-) diff --git a/packages/mask/background/services/identity/index.ts b/packages/mask/background/services/identity/index.ts index f7e4ace2daf0..0196cfdcbe81 100644 --- a/packages/mask/background/services/identity/index.ts +++ b/packages/mask/background/services/identity/index.ts @@ -1,3 +1,3 @@ export { createPersonaByPrivateKey } from './persona/create' -export { signWithPersona, type SignRequest, type SignRequestResult } from './persona/sign' +export { signWithPersona, type SignRequest, type SignRequestResult, generateSignResult } from './persona/sign' export { exportPersonaMnemonicWords, exportPersonaPrivateKey } from './persona/backup' diff --git a/packages/mask/background/services/identity/persona/sign.ts b/packages/mask/background/services/identity/persona/sign.ts index 035752da099d..0e148ffac86e 100644 --- a/packages/mask/background/services/identity/persona/sign.ts +++ b/packages/mask/background/services/identity/persona/sign.ts @@ -9,7 +9,7 @@ import { ECDSASignature, } from 'ethereumjs-util' import { MaskMessages } from '../../../../shared' -import { PersonaIdentifier, fromBase64URL, PopupRoutes } from '@masknet/shared-base' +import { PersonaIdentifier, fromBase64URL, PopupRoutes, ECKeyIdentifier } from '@masknet/shared-base' import { queryPersonasWithPrivateKey } from '../../../../background/database/persona/db' import { openPopupWindow } from '../../../../background/services/helper' import { delay } from '@dimensiondev/kit' @@ -49,6 +49,10 @@ export async function signWithPersona({ message, method, identifier }: SignReque }) }) const signer = await waitForApprove + return generateSignResult(signer, message) +} + +export async function generateSignResult(signer: ECKeyIdentifier, message: string) { const persona = (await queryPersonasWithPrivateKey()).find((x) => x.identifier.equals(signer)) if (!persona) throw new Error('Persona not found') diff --git a/packages/mask/src/extension/background-script/IdentityService.ts b/packages/mask/src/extension/background-script/IdentityService.ts index 7117855f2157..9668da327efb 100644 --- a/packages/mask/src/extension/background-script/IdentityService.ts +++ b/packages/mask/src/extension/background-script/IdentityService.ts @@ -42,7 +42,6 @@ import { ProfileRecord, LinkedProfileDetails, RelationRecord, - detachProfileDB, } from '../../../background/database/persona/db' import { queryPersonasDB as queryPersonasFromIndexedDB, @@ -396,10 +395,8 @@ export async function queryPersonaByPrivateKey(privateKeyString: string) { // #endregion export async function detachProfileWithNextID( - profile: ProfileIdentifier, uuid: string, personaPublicKey: string, - action: NextIDAction, platform: string, identity: string, createdAt: string, @@ -412,6 +409,5 @@ export async function detachProfileWithNextID( await bindProof(uuid, personaPublicKey, NextIDAction.Delete, platform, identity, createdAt, { signature: options?.signature, }) - await detachProfileDB(profile) MaskMessages.events.ownProofChanged.sendToAll(undefined) } diff --git a/packages/mask/src/extension/popups/pages/Personas/PersonaSignRequest/index.tsx b/packages/mask/src/extension/popups/pages/Personas/PersonaSignRequest/index.tsx index 8b0c92ab3b30..e2300aec7244 100644 --- a/packages/mask/src/extension/popups/pages/Personas/PersonaSignRequest/index.tsx +++ b/packages/mask/src/extension/popups/pages/Personas/PersonaSignRequest/index.tsx @@ -6,7 +6,10 @@ import { useNavigate, useLocation } from 'react-router-dom' import { ECKeyIdentifier, Identifier, PopupRoutes } from '@masknet/shared-base' import { useMyPersonas } from '../../../../../components/DataSource/useMyPersonas' import type { Persona } from '../../../../../database' -import { delay } from '@dimensiondev/kit' +import { PersonaContext } from '../hooks/usePersonaContext' +import { MethodAfterPersonaSign } from '../../Wallet/type' +import { useAsyncFn } from 'react-use' +import Services from '../../../../service' const useStyles = makeStyles()(() => ({ container: { @@ -86,7 +89,7 @@ const PersonaSignRequest = memo(() => { const [message, setMessage] = useState() const [selected, setSelected] = useState() const personas = useMyPersonas() - + const { currentPersona } = PersonaContext.useContainer() useEffect(() => { if (!personas.length) return const url = new URLSearchParams(location.search) @@ -104,25 +107,72 @@ const PersonaSignRequest = memo(() => { } }, [personas, location.search]) - const onSign = async () => { + const [, onSign] = useAsyncFn(async () => { + const url = new URLSearchParams(location.search) if (!requestID || !selected) return + const selectedPersona = Identifier.fromString( + selected.identifier.toText(), + ECKeyIdentifier, + ).unwrap() MaskMessages.events.personaSignRequest.sendToBackgroundPage({ requestID, - selectedPersona: Identifier.fromString( - selected.identifier.toText(), - ECKeyIdentifier, - ).unwrap(), + selectedPersona, }) - await delay(200) - window.close() - } + const method = url.get('method') as MethodAfterPersonaSign | undefined + + if (!method) { + window.close() + return + } + + // sign request from popup + switch (method) { + case MethodAfterPersonaSign.DISCONNECT_NEXT_ID: + if (!message) break + const signatureResult = await Services.Identity.generateSignResult(selectedPersona, message) + + const profileIdentifier = url.get('profileIdentifier') + const platform = url.get('platform') + const identity = url.get('identity') + const createdAt = url.get('createdAt') + const uuid = url.get('uuid') + + if ( + !signatureResult || + !profileIdentifier || + !platform || + !identity || + !createdAt || + !uuid || + !currentPersona?.publicHexKey + ) + break + await Services.Identity.detachProfileWithNextID( + uuid, + currentPersona.publicHexKey, + platform, + identity, + createdAt, + { + signature: signatureResult.signature.signature, + }, + ) + const profile = currentPersona.linkedProfiles.find((x) => x.identifier.toText() === profileIdentifier) + if (!profile) break + await Services.Identity.detachProfile(profile.identifier) + break + } + navigate(-1) + }, [location, selected, requestID, message, currentPersona]) const onCancel = async () => { if (!requestID) return + const url = new URLSearchParams(location.search) MaskMessages.events.personaSignRequest.sendToBackgroundPage({ requestID }) - await delay(200) - window.close() + const method = url.get('method') + if (!method) window.close() + navigate(-1) } return ( diff --git a/packages/mask/src/extension/popups/pages/Personas/components/ProfileList/index.tsx b/packages/mask/src/extension/popups/pages/Personas/components/ProfileList/index.tsx index d8a681ee92f9..e06e71a181e2 100644 --- a/packages/mask/src/extension/popups/pages/Personas/components/ProfileList/index.tsx +++ b/packages/mask/src/extension/popups/pages/Personas/components/ProfileList/index.tsx @@ -8,18 +8,21 @@ import { EnhanceableSite, NextIDAction, NextIDPlatform, + PopupRoutes, } from '@masknet/shared-base' import { compact } from 'lodash-unified' import { makeStyles } from '@masknet/theme' -import { useI18N, MaskMessages } from '../../../../../../utils' +import { useI18N } from '../../../../../../utils' import { PersonaContext } from '../../hooks/usePersonaContext' import { useAsyncFn, useAsyncRetry } from 'react-use' import Services from '../../../../../service' import { GrayMasks } from '@masknet/icons' import { DisconnectDialog } from '../DisconnectDialog' import { createPersonaPayload, queryExistedBindingByPersona } from '@masknet/web3-providers' -import { delay } from '@dimensiondev/kit' import classNames from 'classnames' +import { useNavigate } from 'react-router-dom' +import urlcat from 'urlcat' +import { MethodAfterPersonaSign } from '../../../Wallet/type' const useStyles = makeStyles()((theme) => ({ list: { @@ -97,6 +100,7 @@ export interface ProfileListProps {} export const ProfileList = memo(() => { const { currentPersona } = PersonaContext.useContainer() + const navigate = useNavigate() const [unbind, setUnbind] = useState<{ identifier: ProfileIdentifier identity?: string @@ -180,35 +184,21 @@ export const ProfileList = memo(() => { unbind.platform, ) if (!result) return - const signatureResult = await Services.Identity.signWithPersona({ - method: 'eth', - message: result.signPayload, - identifier: currentPersona.identifier.toText(), - }) - - if (!signatureResult) return - - // workaround: should remove this service method - await Services.Identity.detachProfileWithNextID( - unbind.identifier, - result.uuid, - publicHexKey, - NextIDAction.Delete, - unbind.platform, - unbind.identity, - result.createdAt, - { - signature: signatureResult.signature.signature, - }, + navigate( + urlcat(PopupRoutes.PersonaSignRequest, { + requestID: Math.random().toString().slice(3), + message: result.signPayload, + identifier: currentPersona.identifier.toText(), + method: MethodAfterPersonaSign.DISCONNECT_NEXT_ID, + profileIdentifier: unbind.identifier.toText(), + platform: unbind.platform, + identity: unbind.identity, + createdAt: result.createdAt, + uuid: result.uuid, + }), ) - - await delay(2000) - setUnbind(null) - refreshProfileList() } catch { console.log('Disconnect failed') - } finally { - MaskMessages.events.ownProofChanged.sendToAll(undefined) } }, [unbind, currentPersona?.identifier, refreshProfileList]) diff --git a/packages/mask/src/extension/popups/pages/Wallet/type.ts b/packages/mask/src/extension/popups/pages/Wallet/type.ts index 43f356aba4ee..0f0674e1b2c9 100644 --- a/packages/mask/src/extension/popups/pages/Wallet/type.ts +++ b/packages/mask/src/extension/popups/pages/Wallet/type.ts @@ -9,3 +9,7 @@ export enum TransferAddressError { RESOLVE_FAILED = 'RESOLVE_FAILED', NETWORK_NOT_SUPPORT = 'NETWORK_NOT_SUPPORT', } + +export enum MethodAfterPersonaSign { + DISCONNECT_NEXT_ID = 'DISCONNECT_NEXT_ID', +}