Skip to content

Commit

Permalink
Use enum values instead of primitives for error codes for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Sep 10, 2024
1 parent 0eca5cb commit 8aa984e
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/model/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ export const loadWallet = async <S extends Screen>({
privateKey = await loadPrivateKey(addressToUse, isHardwareWallet);
}

// -1 means the user cancelled, so we don't wanna do anything
// -2 Means the user is not authenticated (maybe removed biometrics).
// kc.ErrorType.UserCanceled means the user cancelled, so we don't wanna do anything
// kc.ErrorType.NotAuthenticated means the user is not authenticated (maybe removed biometrics).
// In this case we show an alert inside loadPrivateKey
if (privateKey === -1 || privateKey === -2) {
if (privateKey === kc.ErrorType.UserCanceled || privateKey === kc.ErrorType.NotAuthenticated) {
return null;
}
if (isHardwareWalletKey(privateKey)) {
Expand Down Expand Up @@ -539,7 +539,10 @@ export const oldLoadSeedPhrase = async (): Promise<null | EthereumWalletSeed> =>

export const loadAddress = (): Promise<null | EthereumAddress> => keychain.loadString(addressKey) as Promise<string | null>;

export const loadPrivateKey = async (address: EthereumAddress, hardware: boolean): Promise<null | EthereumPrivateKey | -1 | -2> => {
export const loadPrivateKey = async (
address: EthereumAddress,
hardware: boolean
): Promise<null | EthereumPrivateKey | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
try {
const isSeedPhraseMigrated = await keychain.loadString(oldSeedPhraseMigratedKey);

Expand All @@ -553,7 +556,7 @@ export const loadPrivateKey = async (address: EthereumAddress, hardware: boolean

if (!privateKey) {
const privateKeyData = await getKeyForWallet(address, hardware);
if (privateKeyData === -1 || privateKeyData === -2) {
if (privateKeyData === kc.ErrorType.UserCanceled || privateKeyData === kc.ErrorType.NotAuthenticated) {
return privateKeyData;
}
privateKey = privateKeyData?.privateKey ?? null;
Expand Down Expand Up @@ -914,9 +917,12 @@ export const saveKeyForWallet = async (
* @desc Gets wallet keys for the given address depending wallet type
* @param address The wallet address.
* @param hardware If the wallet is a hardware wallet.
* @return null | PrivateKeyData | -1
* @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
*/
export const getKeyForWallet = async (address: EthereumAddress, hardware: boolean): Promise<null | PrivateKeyData | -1 | -2> => {
export const getKeyForWallet = async (
address: EthereumAddress,
hardware: boolean
): Promise<null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
if (hardware) {
return await getHardwareKey(address);
} else {
Expand Down Expand Up @@ -974,9 +980,11 @@ export const saveHardwareKey = async (
/**
* @desc Gets wallet private key for a given address.
* @param address The wallet address.
* @return null | PrivateKeyData | -1
* @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
*/
export const getPrivateKey = async (address: EthereumAddress): Promise<null | PrivateKeyData | -1 | -2> => {
export const getPrivateKey = async (
address: EthereumAddress
): Promise<null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
try {
const key = `${address}_${privateKeyKey}`;
const options = { authenticationPrompt };
Expand All @@ -988,17 +996,17 @@ export const getPrivateKey = async (address: EthereumAddress): Promise<null | Pr
});

switch (error) {
case -1:
case kc.ErrorType.UserCanceled:
// User Cancelled - We want to bubble up this error code. No need to track it.
return -1;
case -2:
return kc.ErrorType.UserCanceled;
case kc.ErrorType.NotAuthenticated:
// Alert the user and bubble up the error code.
Alert.alert(
lang.t('wallet.authenticate.alert.error'),
lang.t('wallet.authenticate.alert.current_authentication_not_secure_enough')
);
return -2;
case -3:
return kc.ErrorType.NotAuthenticated;
case kc.ErrorType.Unavailable:
// This means we couldn't find any matches for this key.
logger.error(new RainbowError('KC unavailable for PKEY lookup'), { error });
break;
Expand Down Expand Up @@ -1061,7 +1069,7 @@ export const getSeedPhrase = async (
androidEncryptionPin,
});

if (error === -2) {
if (error === kc.ErrorType.NotAuthenticated) {
Alert.alert(lang.t('wallet.authenticate.alert.error'), lang.t('wallet.authenticate.alert.current_authentication_not_secure_enough'));
return null;
}
Expand Down

0 comments on commit 8aa984e

Please sign in to comment.