Skip to content

Commit

Permalink
fix: adding key storage info in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
juanky201271 committed Sep 9, 2024
1 parent d67512a commit 3666899
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
3 changes: 3 additions & 0 deletions __tests__/App.snapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ jest.mock('react-native-keychain', () => ({
SECURITY_RULES: {
NONE: 'none',
},
STORAGE_TYPE: {
AES: 'AES',
},
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions __tests__/LoadedApp.snapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ jest.mock('react-native-keychain', () => ({
SECURITY_RULES: {
NONE: 'none',
},
STORAGE_TYPE: {
AES: 'AES',
},
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions __tests__/LoadingApp.snapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ jest.mock('react-native-keychain', () => ({
SECURITY_RULES: {
NONE: 'none',
},
STORAGE_TYPE: {
AES: 'AES',
},
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
Expand Down
5 changes: 4 additions & 1 deletion __tests__/Settings.snapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ jest.mock('react-native-keychain', () => ({
SECURITY_RULES: {
NONE: 'none',
},
STORAGE_TYPE: {
AES: 'AES',
},
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
Expand Down Expand Up @@ -101,7 +104,7 @@ describe('Component Settings - test', () => {
setSecurityOption={onSetOption}
setSelectServerOption={onSetOption}
setRescanMenuOption={onSetOption}
set_recoveryWalletInfoOnDevice_option={onSetOption}
setRecoveryWalletInfoOnDeviceOption={onSetOption}
/>
</ContextAppLoadedProvider>,
);
Expand Down
32 changes: 27 additions & 5 deletions app/recoveryWalletInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const options = {
authenticationType: Keychain.AUTHENTICATION_TYPE.BIOMETRICS,
securityLevel: Keychain.SECURITY_LEVEL.SECURE_SOFTWARE,
rules: Keychain.SECURITY_RULES.NONE,
};
storage: Keychain.STORAGE_TYPE.AES,
} as Keychain.Options;

export const saveRecoveryWalletInfo = async (keys: WalletType): Promise<void> => {
if (!keys.seed) {
Expand All @@ -36,12 +37,14 @@ export const saveRecoveryWalletInfo = async (keys: WalletType): Promise<void> =>

export const getRecoveryWalletInfo = async (): Promise<WalletType> => {
try {
const credentials = await Keychain.getGenericPassword({ service: GlobalConst.serviceKeyChain });
const credentials = await Keychain.getGenericPassword(options);
if (credentials) {
console.log('keys read correctly');
console.log('keys read correctly', credentials);
console.log(await Keychain.getSupportedBiometryType());
console.log(await Keychain.getSecurityLevel());
//console.log('key:', credentials.username);
//console.log('value:', credentials.password);
if (credentials.username === GlobalConst.keyKeyChain) {
if (credentials.username === GlobalConst.keyKeyChain && credentials.service === GlobalConst.serviceKeyChain) {
return JSON.parse(credentials.password) as WalletType;
} else {
console.log('no match the key');
Expand All @@ -55,6 +58,25 @@ export const getRecoveryWalletInfo = async (): Promise<WalletType> => {
return {} as WalletType;
};

export const getStorageRecoveryWalletInfo = async (): Promise<string> => {
try {
const credentials = await Keychain.getGenericPassword(options);
if (credentials) {
console.log('keys read correctly', credentials);
if (credentials.username === GlobalConst.keyKeyChain && credentials.service === GlobalConst.serviceKeyChain) {
return credentials.storage;
} else {
console.log('no match the key');
}
} else {
console.log('Error no keys stored');
}
} catch (error) {
console.log('Error getting keys:', error);
}
return '';
};

export const hasRecoveryWalletInfo = async (): Promise<boolean> => {
const keys: WalletType = await getRecoveryWalletInfo();
if (keys.seed) {
Expand Down Expand Up @@ -84,7 +106,7 @@ export const createUpdateRecoveryWalletInfo = async (keys: WalletType): Promise<
export const removeRecoveryWalletInfo = async (): Promise<void> => {
if (await hasRecoveryWalletInfo()) {
// have Wallet Keys
const removed: boolean = await Keychain.resetGenericPassword({ service: GlobalConst.serviceKeyChain });
const removed: boolean = await Keychain.resetGenericPassword(options);
if (!removed) {
// error removing keys
console.log('error removing keys');
Expand Down
13 changes: 8 additions & 5 deletions components/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { isEqual } from 'lodash';
import ChainTypeToggle from '../Components/ChainTypeToggle';
import CheckBox from '@react-native-community/checkbox';
import RNPickerSelect from 'react-native-picker-select';
import { hasRecoveryWalletInfo } from '../../app/recoveryWalletInfo';
import { getStorageRecoveryWalletInfo, hasRecoveryWalletInfo } from '../../app/recoveryWalletInfo';

type SettingsProps = {
closeModal: () => void;
Expand Down Expand Up @@ -190,14 +190,16 @@ const Settings: React.FunctionComponent<SettingsProps> = ({
const [listIcon, setListIcon] = useState<IconDefinition>(farCircle);
const [disabled, setDisabled] = useState<boolean>();
const [titleViewHeight, setTitleViewHeight] = useState<number>(0);
const [recoveryWalletInfoSaved, setRecoveryWalletInfoSaved] = useState<boolean>(false);
const [hasRecoveryWalletInfoSaved, setHasRecoveryWalletInfoSaved] = useState<boolean>(false);
const [storageRecoveryWalletInfo, setStorageRecoveryWalletInfo] = useState<string>('');

const slideAnim = useSharedValue(0);

useEffect(() => {
(async () => {
if (await hasRecoveryWalletInfo()) {
setRecoveryWalletInfoSaved(true);
setHasRecoveryWalletInfoSaved(true);
setStorageRecoveryWalletInfo(await getStorageRecoveryWalletInfo());
}
})();
}, [translate]);
Expand Down Expand Up @@ -931,10 +933,11 @@ const Settings: React.FunctionComponent<SettingsProps> = ({
)}
</View>

{recoveryWalletInfoSaved && (
{hasRecoveryWalletInfoSaved && (
<View style={{ display: 'flex' }}>
<FadeText style={{ color: colors.primary, textAlign: 'center', marginVertical: 10, padding: 5 }}>
{translate('settings.walletkeyssaved') as string}
{(translate('settings.walletkeyssaved') as string) +
(storageRecoveryWalletInfo ? ' [' + storageRecoveryWalletInfo + ']' : '')}
</FadeText>
</View>
)}
Expand Down

0 comments on commit 3666899

Please sign in to comment.