From 57477469b1b1737cae4da16180e6f7245678c634 Mon Sep 17 00:00:00 2001 From: vantuz-subhuman Date: Sat, 2 Nov 2024 14:20:00 +0300 Subject: [PATCH] credential processing fix --- .../governace/common/ChooseDRepModal.tsx | 19 +++++++------- .../features/governace/common/useStrings.tsx | 5 ++++ .../app/api/ada/lib/cardanoCrypto/utils.js | 25 +++++++++++++++---- packages/yoroi-extension/app/coreUtils.js | 4 +-- .../app/i18n/locales/en-US.json | 1 + 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/packages/yoroi-extension/app/UI/features/governace/common/ChooseDRepModal.tsx b/packages/yoroi-extension/app/UI/features/governace/common/ChooseDRepModal.tsx index dc5f1f46b8..010c68700e 100644 --- a/packages/yoroi-extension/app/UI/features/governace/common/ChooseDRepModal.tsx +++ b/packages/yoroi-extension/app/UI/features/governace/common/ChooseDRepModal.tsx @@ -14,7 +14,7 @@ type ChooseDRepModallProps = { export const ChooseDRepModal = ({ onSubmit }: ChooseDRepModallProps) => { const [drepId, setDrepId] = React.useState(''); - const [error, setError] = React.useState(false); + const [error, setError] = React.useState<'invalid' | 'unsupported' | null>(null); const { dRepIdChanged, governanceVoteChanged, ampli } = useGovernance(); const { isLoading } = useModal(); const strings = useStrings(); @@ -25,15 +25,16 @@ export const ChooseDRepModal = ({ onSubmit }: ChooseDRepModallProps) => { }, []); React.useEffect(() => { - setError(false); + setError(null); }, [drepId]); const confirmDRep = () => { - const dRepCredentialHex: string | null = dRepToMaybeCredentialHex(drepId); - if (dRepCredentialHex == null) { - setError(true); + const dRepCredentialHex: { type: 'ok', credential: string } | { type: 'invalid' } | { type: 'unsupported' } = + dRepToMaybeCredentialHex(drepId); + if (dRepCredentialHex.type === 'ok') { + onSubmit?.(drepId, dRepCredentialHex.credential); } else { - onSubmit?.(drepId, dRepCredentialHex); + setError(dRepCredentialHex.type); } }; @@ -53,8 +54,8 @@ export const ChooseDRepModal = ({ onSubmit }: ChooseDRepModallProps) => { setDrepId(event.target.value); }} value={drepId} - error={error} - helperText={error ? strings.incorectFormat : ' '} + error={error != null} + helperText={error === 'invalid' ? strings.incorectFormat : (error === 'unsupported' ? strings.unsupportedFormat : ' ')} /> { fullWidth // @ts-ignore variant="primary" - disabled={error || drepId.length === 0} + disabled={error != null || drepId.length === 0} > {strings.confirm} diff --git a/packages/yoroi-extension/app/UI/features/governace/common/useStrings.tsx b/packages/yoroi-extension/app/UI/features/governace/common/useStrings.tsx index 8e39c9abd5..b65fcad7d5 100644 --- a/packages/yoroi-extension/app/UI/features/governace/common/useStrings.tsx +++ b/packages/yoroi-extension/app/UI/features/governace/common/useStrings.tsx @@ -141,6 +141,10 @@ export const messages = Object.freeze( id: 'global.labels.incorectFormat', defaultMessage: '!!!Incorrect format', }, + unsupportedFormat: { + id: 'global.labels.drep.unsupportedFormat', + defaultMessage: '!!!Unsupported format (please use a different source of drep ID)', + }, }) ); @@ -176,6 +180,7 @@ export const useStrings = () => { wrongPassword: intl.formatMessage(messages.wrongPassword), identifyDrep: intl.formatMessage(messages.identifyDrep), incorectFormat: intl.formatMessage(messages.incorectFormat), + unsupportedFormat: intl.formatMessage(messages.unsupportedFormat), statusPending: intl.formatMessage(messages.statusPending), thanksForParticipation: intl.formatMessage(messages.thanksForParticipation), theTransactionCanTake: intl.formatMessage(messages.theTransactionCanTake), diff --git a/packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js b/packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js index 42b7ab8120..9ac2a4794e 100644 --- a/packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js +++ b/packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js @@ -2,6 +2,7 @@ import { RustModule } from './rustLoader'; import { bytesToHex, hexToBytes } from '../../../../coreUtils'; +import { isHex } from '@emurgo/yoroi-lib/dist/internals/utils/index'; export function v4PublicToV2( v4Key: RustModule.WalletV4.Bip32PublicKey @@ -51,19 +52,33 @@ export function transactionHexReplaceWitnessSet(txHex: string, witnessSetHex: st }); } -export function dRepToMaybeCredentialHex(s: string): ?string { +export function dRepToMaybeCredentialHex(s: string): + {| type: 'ok', credential: string |} + | {| type: 'invalid' |} + | {| type: 'unsupported' |} +{ + if (/^(22|23)[0-9a-fA-F]{56}$/.test(s)) { + return { type: 'unsupported' }; + } return RustModule.WasmScope(Module => { try { if (s.startsWith('drep1')) { - return Module.WalletV4.Credential + if (s.length === 58) { + return { type: 'unsupported' }; + } + const credential = Module.WalletV4.Credential .from_keyhash(Module.WalletV4.Ed25519KeyHash.from_bech32(s)).to_hex(); + return { type: 'ok', credential }; } if (s.startsWith('drep_script1')) { - return Module.WalletV4.Credential + + const credential = Module.WalletV4.Credential .from_scripthash(Module.WalletV4.ScriptHash.from_bech32(s)).to_hex(); + return { type: 'ok', credential }; } - } catch {} // eslint-disable-line no-empty - return null; + } catch { + } // eslint-disable-line no-empty + return { type: 'invalid' }; }) } diff --git a/packages/yoroi-extension/app/coreUtils.js b/packages/yoroi-extension/app/coreUtils.js index 7ad9dcef29..1d60daf755 100644 --- a/packages/yoroi-extension/app/coreUtils.js +++ b/packages/yoroi-extension/app/coreUtils.js @@ -222,9 +222,9 @@ export function timeCached(fun: () => R, ttl: number): () => R { * In case the value is an object, this creates a copy and hides some potentially sensitive fields from it. * * @param v - any value - * @return {T} - same value or a copy in case the value is an object + * @return same value or a copy in case the value is an object */ -export function sanitizeForLog(v: T): T { +export function sanitizeForLog(v: any): any { if (v != null && typeof v === 'object') { let r = Object.keys(v).reduce((o, k) => ({ ...o, [k]: sanitizeForLog(v[k]) }) , {}) // $FlowIgnore[incompatible-use] diff --git a/packages/yoroi-extension/app/i18n/locales/en-US.json b/packages/yoroi-extension/app/i18n/locales/en-US.json index 1354e9f4e1..291d953ec5 100644 --- a/packages/yoroi-extension/app/i18n/locales/en-US.json +++ b/packages/yoroi-extension/app/i18n/locales/en-US.json @@ -171,6 +171,7 @@ "global.labels.upgrade": "Upgrade", "global.labels.wrongPassword": "Wrong password", "global.labels.incorectFormat": "Incorrect format", + "global.labels.drep.unsupportedFormat": "Unsupported format (please use a different source of drep ID)", "global.language.chinese.simplified": "简体中文", "global.language.chinese.traditional": "繁體中文", "global.language.czech": "Czech",