Skip to content

Commit 8e24521

Browse files
brunobar79BrodyHughes
authored andcommitted
Fix wrong messages during MWP Flow (#6094)
* add logging for pkey errors * fix keychain error handling * checksum address before keychain lookup * remove invalid chainId param from tx * Update src/keychain/index.ts * Update src/keychain/index.ts Co-authored-by: Jin <[email protected]> * Explicitly return UserCanceled error for code strings 10 and 13 * Remove returning -3 explicit error code * Use enum values instead of primitives for error codes for readability --------- Co-authored-by: Jin <[email protected]> (cherry picked from commit 82dca43)
1 parent 9ccbb6b commit 8e24521

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

src/keychain/index.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export async function get(key: string, options: KeychainOptions = {}): Promise<R
115115
error: e.toString(),
116116
},
117117
});
118+
118119
switch (e.toString()) {
119120
/*
120121
* Can happen if the user initially had biometrics enabled, installed
@@ -170,14 +171,21 @@ export async function get(key: string, options: KeychainOptions = {}): Promise<R
170171
return _get(attempts + 1);
171172
}
172173
default: {
173-
logger.error(new RainbowError(`[keychain]: _get() handled unknown error`), {
174-
message: e.toString(),
175-
});
176-
177-
return {
178-
value: undefined,
179-
error: ErrorType.Unknown,
180-
};
174+
// Avoid logging user cancelled operations
175+
if (e.toString().includes('code: 10') || e.toString().includes('code: 13')) {
176+
return {
177+
value: undefined,
178+
error: ErrorType.UserCanceled,
179+
};
180+
} else {
181+
logger.error(new RainbowError(`[keychain]: _get() handled unknown error`), {
182+
message: e.toString(),
183+
});
184+
return {
185+
value: undefined,
186+
error: ErrorType.Unknown,
187+
};
188+
}
181189
}
182190
}
183191
}

src/model/wallet.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ export const loadWallet = async <S extends Screen>({
289289
privateKey = await loadPrivateKey(addressToUse, isHardwareWallet);
290290
}
291291

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

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

539-
export const loadPrivateKey = async (address: EthereumAddress, hardware: boolean): Promise<null | EthereumPrivateKey | -1 | -2> => {
542+
export const loadPrivateKey = async (
543+
address: EthereumAddress,
544+
hardware: boolean
545+
): Promise<null | EthereumPrivateKey | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
540546
try {
541547
const isSeedPhraseMigrated = await keychain.loadString(oldSeedPhraseMigratedKey);
542548

@@ -550,8 +556,8 @@ export const loadPrivateKey = async (address: EthereumAddress, hardware: boolean
550556

551557
if (!privateKey) {
552558
const privateKeyData = await getKeyForWallet(address, hardware);
553-
if (privateKeyData === -1) {
554-
return -1;
559+
if (privateKeyData === kc.ErrorType.UserCanceled || privateKeyData === kc.ErrorType.NotAuthenticated) {
560+
return privateKeyData;
555561
}
556562
privateKey = privateKeyData?.privateKey ?? null;
557563
}
@@ -911,9 +917,12 @@ export const saveKeyForWallet = async (
911917
* @desc Gets wallet keys for the given address depending wallet type
912918
* @param address The wallet address.
913919
* @param hardware If the wallet is a hardware wallet.
914-
* @return null | PrivateKeyData | -1
920+
* @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
915921
*/
916-
export const getKeyForWallet = async (address: EthereumAddress, hardware: boolean): Promise<null | PrivateKeyData | -1> => {
922+
export const getKeyForWallet = async (
923+
address: EthereumAddress,
924+
hardware: boolean
925+
): Promise<null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
917926
if (hardware) {
918927
return await getHardwareKey(address);
919928
} else {
@@ -971,9 +980,11 @@ export const saveHardwareKey = async (
971980
/**
972981
* @desc Gets wallet private key for a given address.
973982
* @param address The wallet address.
974-
* @return null | PrivateKeyData | -1
983+
* @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
975984
*/
976-
export const getPrivateKey = async (address: EthereumAddress): Promise<null | PrivateKeyData | -1> => {
985+
export const getPrivateKey = async (
986+
address: EthereumAddress
987+
): Promise<null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated> => {
977988
try {
978989
const key = `${address}_${privateKeyKey}`;
979990
const options = { authenticationPrompt };
@@ -984,11 +995,26 @@ export const getPrivateKey = async (address: EthereumAddress): Promise<null | Pr
984995
androidEncryptionPin,
985996
});
986997

987-
if (error === -2) {
988-
Alert.alert(lang.t('wallet.authenticate.alert.error'), lang.t('wallet.authenticate.alert.current_authentication_not_secure_enough'));
989-
return null;
998+
switch (error) {
999+
case kc.ErrorType.UserCanceled:
1000+
// User Cancelled - We want to bubble up this error code. No need to track it.
1001+
return kc.ErrorType.UserCanceled;
1002+
case kc.ErrorType.NotAuthenticated:
1003+
// Alert the user and bubble up the error code.
1004+
Alert.alert(
1005+
lang.t('wallet.authenticate.alert.error'),
1006+
lang.t('wallet.authenticate.alert.current_authentication_not_secure_enough')
1007+
);
1008+
return kc.ErrorType.NotAuthenticated;
1009+
case kc.ErrorType.Unavailable:
1010+
// This means we couldn't find any matches for this key.
1011+
logger.error(new RainbowError('KC unavailable for PKEY lookup'), { error });
1012+
break;
1013+
default:
1014+
// This is an unknown error
1015+
logger.error(new RainbowError('KC unknown error for PKEY lookup'), { error });
1016+
break;
9901017
}
991-
9921018
return pkey || null;
9931019
} catch (error) {
9941020
logger.error(new RainbowError('[wallet]: Error in getPrivateKey'), { error });
@@ -1043,7 +1069,7 @@ export const getSeedPhrase = async (
10431069
androidEncryptionPin,
10441070
});
10451071

1046-
if (error === -2) {
1072+
if (error === kc.ErrorType.NotAuthenticated) {
10471073
Alert.alert(lang.t('wallet.authenticate.alert.error'), lang.t('wallet.authenticate.alert.current_authentication_not_secure_enough'));
10481074
return null;
10491075
}

src/screens/SignTransactionSheet.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import { useNonceForDisplay } from '@/hooks/useNonceForDisplay';
7272
import { useProviderSetup } from '@/hooks/useProviderSetup';
7373
import { useTransactionSubmission } from '@/hooks/useSubmitTransaction';
7474
import { useConfirmTransaction } from '@/hooks/useConfirmTransaction';
75+
import { toChecksumAddress } from 'ethereumjs-util';
7576

7677
type SignTransactionSheetParams = {
7778
transactionDetails: RequestData;
@@ -332,7 +333,7 @@ export const SignTransactionSheet = () => {
332333
screen: SCREEN_FOR_REQUEST_SOURCE[source],
333334
operation: TimeToSignOperation.KeychainRead,
334335
})({
335-
address: accountInfo.address,
336+
address: toChecksumAddress(accountInfo.address),
336337
provider: providerToUse,
337338
timeTracking: {
338339
screen: SCREEN_FOR_REQUEST_SOURCE[source],

src/utils/ethereumUtils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ const calculateL1FeeOptimism = async (tx: RainbowTransaction, provider: Provider
501501
newTx.nonce = Number(await provider.getTransactionCount(newTx.from));
502502
}
503503

504+
// @ts-expect-error operand should be optional
505+
delete newTx?.chainId;
504506
// @ts-expect-error operand should be optional
505507
delete newTx?.from;
506508
// @ts-expect-error gas is not in type RainbowTransaction

0 commit comments

Comments
 (0)