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 b790220
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
13 changes: 13 additions & 0 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 { base32ToHex } from '../storage/bridge/utils';

export function v4PublicToV2(
v4Key: RustModule.WalletV4.Bip32PublicKey
Expand Down Expand Up @@ -52,16 +53,28 @@ export function transactionHexReplaceWitnessSet(txHex: string, witnessSetHex: st
}

export function dRepToMaybeCredentialHex(s: string): ?string {
const isPotentiallyValidHex = /^(22|23)[0-9a-fA-F]{56}$/.test(s);
return RustModule.WasmScope(Module => {
try {
if (s.startsWith('drep1')) {
if (s.length === 58) {
return dRepToMaybeCredentialHex(base32ToHex(s));
}
return Module.WalletV4.Credential
.from_keyhash(Module.WalletV4.Ed25519KeyHash.from_bech32(s)).to_hex();
}
if (s.startsWith('drep_script1')) {
return Module.WalletV4.Credential
.from_scripthash(Module.WalletV4.ScriptHash.from_bech32(s)).to_hex();
}
if (isPotentiallyValidHex && s.startsWith('22')) {
return Module.WalletV4.Credential
.from_keyhash(Module.WalletV4.Ed25519KeyHash.from_hex(s.substr(2))).to_hex();
}
if (isPotentiallyValidHex && s.startsWith('23')) {
return Module.WalletV4.Credential
.from_keyhash(Module.WalletV4.ScriptHash.from_hex(s.substr(2))).to_hex();
}
} catch {} // eslint-disable-line no-empty
return null;
})
Expand Down
22 changes: 11 additions & 11 deletions packages/yoroi-extension/app/api/ada/lib/storage/bridge/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { $npm$ReactIntl$MessageDescriptor } from 'react-intl';
import { defineMessages } from 'react-intl';
import { bech32 as bech32Module } from 'bech32';
import typeof { CertificateKind } from '@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib';
import { bytesToHex, fail, hexToBytes } from '../../../../../coreUtils';
import { bytesToHex, fail, hexToBytes, maybe } from '../../../../../coreUtils';

export function tryAddressToKind(
address: string,
Expand Down Expand Up @@ -156,11 +156,18 @@ export function normalizeToBase58(addr: string): void | string {
return undefined;
}

// this implementation was copied from the convert function of the bech32 package.
const convertBase32ToHex = (data: any[]) => {
/**
* this implementation was copied from the convert function of the bech32 package.
*/
const convertBase32ToHex = (data: any[]): string => {
return bytesToHex(bech32Module.fromWords(data));
};

export const base32ToHex = (base32: string): ?string => {
const base32Words = bech32Module.decodeUnsafe(base32, base32.length);
return maybe(base32Words?.words, convertBase32ToHex);
};

/* eslint-disable */
const bigIntToBase58 = n => {
const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
Expand Down Expand Up @@ -255,14 +262,7 @@ export function normalizeToAddress(addr: string): void | RustModule.WalletV4.Add
}

// 3) Try decoding bech32...
const bech32Decoded = bech32Module.decodeUnsafe(addr, addr.length);
if (bech32Decoded) {
// 3.1) if successfull, convert the decoded bech32 to base16 and try parsing the hex
const hex = convertBase32ToHex(bech32Decoded.words);
return parseHexAddress(hex);
}

return undefined;
return maybe(base32ToHex(addr), parseHexAddress) ?? undefined;
}

export function toEnterprise(address: string): void | RustModule.WalletV4.EnterpriseAddress {
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

0 comments on commit b790220

Please sign in to comment.