@@ -289,7 +289,10 @@ export const loadWallet = async <S extends Screen>({
289
289
privateKey = await loadPrivateKey ( addressToUse , isHardwareWallet ) ;
290
290
}
291
291
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 ) {
293
296
return null ;
294
297
}
295
298
if ( isHardwareWalletKey ( privateKey ) ) {
@@ -536,7 +539,10 @@ export const oldLoadSeedPhrase = async (): Promise<null | EthereumWalletSeed> =>
536
539
537
540
export const loadAddress = ( ) : Promise < null | EthereumAddress > => keychain . loadString ( addressKey ) as Promise < string | null > ;
538
541
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 > => {
540
546
try {
541
547
const isSeedPhraseMigrated = await keychain . loadString ( oldSeedPhraseMigratedKey ) ;
542
548
@@ -550,8 +556,8 @@ export const loadPrivateKey = async (address: EthereumAddress, hardware: boolean
550
556
551
557
if ( ! privateKey ) {
552
558
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 ;
555
561
}
556
562
privateKey = privateKeyData ?. privateKey ?? null ;
557
563
}
@@ -911,9 +917,12 @@ export const saveKeyForWallet = async (
911
917
* @desc Gets wallet keys for the given address depending wallet type
912
918
* @param address The wallet address.
913
919
* @param hardware If the wallet is a hardware wallet.
914
- * @return null | PrivateKeyData | -1
920
+ * @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
915
921
*/
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 > => {
917
926
if ( hardware ) {
918
927
return await getHardwareKey ( address ) ;
919
928
} else {
@@ -971,9 +980,11 @@ export const saveHardwareKey = async (
971
980
/**
972
981
* @desc Gets wallet private key for a given address.
973
982
* @param address The wallet address.
974
- * @return null | PrivateKeyData | -1
983
+ * @return null | PrivateKeyData | kc.ErrorType.UserCanceled | kc.ErrorType.NotAuthenticated
975
984
*/
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 > => {
977
988
try {
978
989
const key = `${ address } _${ privateKeyKey } ` ;
979
990
const options = { authenticationPrompt } ;
@@ -984,11 +995,26 @@ export const getPrivateKey = async (address: EthereumAddress): Promise<null | Pr
984
995
androidEncryptionPin,
985
996
} ) ;
986
997
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 ;
990
1017
}
991
-
992
1018
return pkey || null ;
993
1019
} catch ( error ) {
994
1020
logger . error ( new RainbowError ( '[wallet]: Error in getPrivateKey' ) , { error } ) ;
@@ -1043,7 +1069,7 @@ export const getSeedPhrase = async (
1043
1069
androidEncryptionPin,
1044
1070
} ) ;
1045
1071
1046
- if ( error === - 2 ) {
1072
+ if ( error === kc . ErrorType . NotAuthenticated ) {
1047
1073
Alert . alert ( lang . t ( 'wallet.authenticate.alert.error' ) , lang . t ( 'wallet.authenticate.alert.current_authentication_not_secure_enough' ) ) ;
1048
1074
return null ;
1049
1075
}
0 commit comments