Skip to content

Commit

Permalink
credential processing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Nov 2, 2024
1 parent 4352b3a commit 5747746
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
};

Expand All @@ -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 : ' ')}
/>
</Stack>
<LoadingButton
Expand All @@ -63,7 +64,7 @@ export const ChooseDRepModal = ({ onSubmit }: ChooseDRepModallProps) => {
fullWidth
// @ts-ignore
variant="primary"
disabled={error || drepId.length === 0}
disabled={error != null || drepId.length === 0}
>
{strings.confirm}
</LoadingButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
},
})
);

Expand Down Expand Up @@ -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),
Expand Down
25 changes: 20 additions & 5 deletions packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' };
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/yoroi-extension/app/coreUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ export function timeCached<R>(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<T>(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]
Expand Down
1 change: 1 addition & 0 deletions packages/yoroi-extension/app/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5747746

Please sign in to comment.