This commit is contained in:
noumantahir 2024-11-11 15:46:18 +05:00
parent 61a371f4ad
commit 52aa88c7c0
6 changed files with 297 additions and 334 deletions

View File

@ -1,28 +1,18 @@
import React, { useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { debounce } from 'lodash';
import EStyleSheet from 'react-native-extended-stylesheet';
import Animated, {
FadeOut,
LinearTransition,
ZoomIn,
ZoomOut,
} from 'react-native-reanimated';
import Animated, { FadeOut, LinearTransition, ZoomIn, ZoomOut } from 'react-native-reanimated';
import styles from '../styles/hiveAuthModal.styles';
import { lookupAccounts } from '../../../providers/hive/dhive';
import { FormInput, MainButton } from '../..';
import HIVE_AUTH_ICON from '../../../assets/HiveAuth_logo.png';
interface AuthInputContentProps {
initUsername?: string,
handleAuthRequest: (username: string) => void
initUsername?: string;
handleAuthRequest: (username: string) => void;
}
export const AuthInputContent = ({ initUsername, handleAuthRequest }: AuthInputContentProps) => {
const intl = useIntl();
@ -35,9 +25,9 @@ export const AuthInputContent = ({ initUsername, handleAuthRequest }: AuthInputC
useEffect(() => {
if (initUsername) {
setUsername(initUsername)
setUsername(initUsername);
}
}, [initUsername])
}, [initUsername]);
useEffect(() => {
debouncedCheckValidity(username);
@ -61,9 +51,9 @@ export const AuthInputContent = ({ initUsername, handleAuthRequest }: AuthInputC
const onSignInPress = () => {
if (handleAuthRequest) {
handleAuthRequest(username)
}
handleAuthRequest(username);
}
};
return (
<Animated.View
@ -94,7 +84,7 @@ export const AuthInputContent = ({ initUsername, handleAuthRequest }: AuthInputC
<Animated.View entering={ZoomIn} exiting={ZoomOut}>
<MainButton
text={intl.formatMessage({ id: 'login.signin_with_hiveauth' })}
textStyle={{color:EStyleSheet.value("$primaryBlack")}}
textStyle={{ color: EStyleSheet.value('$primaryBlack') }}
style={{
backgroundColor: 'transparent',
marginTop: 12,
@ -108,5 +98,5 @@ export const AuthInputContent = ({ initUsername, handleAuthRequest }: AuthInputC
</Animated.View>
)}
</Animated.View>
)
);
};

View File

@ -1,4 +1,3 @@
import React from 'react';
import { Text, ActivityIndicator } from 'react-native';
@ -14,16 +13,12 @@ import styles from '../styles/hiveAuthModal.styles';
import { Icon } from '../..';
import { HiveAuthStatus } from '../hooks/useHiveAuth';
interface StatusContentProps {
status:HiveAuthStatus,
statusText:string
status: HiveAuthStatus;
statusText: string;
}
export const StatusContent = ({ status, statusText }: StatusContentProps) => {
const _renderResultIcon = (iconName: string, colorId: string) => (
<Animated.View entering={ZoomIn.springify().duration(500)} exiting={ZoomOut}>
<Icon
@ -36,7 +31,6 @@ export const StatusContent = ({ status, statusText}:StatusContentProps) => {
</Animated.View>
);
return (
<>
{status === HiveAuthStatus.SUCCESS && _renderResultIcon('checkcircleo', '$primaryGreen')}
@ -67,5 +61,5 @@ export const StatusContent = ({ status, statusText}:StatusContentProps) => {
</Text>
</Animated.View>
</>
)
}
);
};

View File

@ -1,7 +1,6 @@
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { View } from 'react-native';
import { useIntl } from 'react-intl';
import { useNavigation } from '@react-navigation/native';
import ActionSheet from 'react-native-actions-sheet';
@ -15,13 +14,11 @@ import { AuthInputContent } from '../children/authInputContent';
import { StatusContent } from '../children/statusContent';
import { HiveAuthStatus, useHiveAuth } from '../hooks/useHiveAuth';
interface HiveAuthModalProps {
onClose?: () => void;
}
export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) => {
const intl = useIntl();
const navigation = useNavigation();
const hiveAuth = useHiveAuth();
@ -36,7 +33,6 @@ export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) =
showModal: (_username?: string) => {
setInitUsername(_username);
bottomSheetModalRef.current?.show();
},
broadcastActiveOps: (opsArray: any) => {
if (opsArray) {
@ -46,10 +42,7 @@ export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) =
},
}));
const handleAuthRequest = async (username: string) => {
const success = await hiveAuth.authenticate(username);
// isLoggedInt
@ -67,10 +60,8 @@ export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) =
});
}
}
};
const _closeModal = () => {
bottomSheetModalRef.current?.hide();
if (onClose) {
@ -78,27 +69,25 @@ export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) =
}
};
const _renderContent = () => {
const _content = hiveAuth.status === HiveAuthStatus.INPUT
? <AuthInputContent initUsername={initUsername} handleAuthRequest={handleAuthRequest} />
: <StatusContent status={hiveAuth.status} statusText={hiveAuth.statusText} />
const _content =
hiveAuth.status === HiveAuthStatus.INPUT ? (
<AuthInputContent initUsername={initUsername} handleAuthRequest={handleAuthRequest} />
) : (
<StatusContent status={hiveAuth.status} statusText={hiveAuth.statusText} />
);
return (
<View style={styles.container}>
<ModalHeader
title={intl.formatMessage({ id: 'hiveauth.title' })}
isCloseButton={true}
onClosePress={_closeModal} />
onClosePress={_closeModal}
/>
<View style={styles.content}>
{_content}
<View style={styles.content}>{_content}</View>
</View>
</View>
)
);
};
return (
@ -107,7 +96,7 @@ export const HiveAuthModal = forwardRef(({ onClose }: HiveAuthModalProps, ref) =
gestureEnabled={false}
hideUnderlay={true}
onClose={() => {
hiveAuth.reset()
hiveAuth.reset();
setInitUsername(undefined);
}}
containerStyle={styles.sheetContent}

View File

@ -1,22 +1,20 @@
import { useEffect, useState } from 'react';
import { Linking, Keyboard } from 'react-native';
import HAS from 'hive-auth-wrapper';
import { v4 as uuidv4 } from 'uuid';
import { HiveSignerMessage } from 'utils/hive-signer-helper';
import { Operation } from '@hiveio/dhive';
import assert from 'assert';
import { useIntl } from 'react-intl';
import { getDigitPinCode } from '../../../providers/hive/dhive';
import { loginWithHiveAuth } from '../../../providers/hive/auth';
import { useAppSelector, usePostLoginActions } from '../../../hooks';
import AUTH_TYPE from '../../../constants/authType';
import { decryptKey } from '../../../utils/crypto';
import { delay } from '../../../utils/editor';
import { Operation } from '@hiveio/dhive';
import assert from 'assert';
import { useIntl } from 'react-intl';
import bugsnapInstance from '../../../config/bugsnag';
const APP_META = {
name: 'Ecency',
description: 'Decentralised Social Blogging',
@ -24,8 +22,7 @@ const APP_META = {
};
const HAS_AUTH_URI = 'has://auth_req';
const HAS_SIGN_URI = 'has://sign_req'
const HAS_SIGN_URI = 'has://sign_req';
export enum HiveAuthStatus {
INPUT = 0,
@ -34,8 +31,6 @@ export enum HiveAuthStatus {
ERROR = 3,
}
export const useHiveAuth = () => {
const intl = useIntl();
const postLoginActions = usePostLoginActions();
@ -55,8 +50,6 @@ export const useHiveAuth = () => {
});
}, []);
/**
* authenticates user via installed hive auth or keychain app
* compiles and set account data in redux store
@ -141,8 +134,6 @@ export const useHiveAuth = () => {
await delay(2000);
return true;
} catch (error) {
setStatusText(intl.formatMessage({ id: error.message || 'hiveauth.auth_fail' }));
setStatus(HiveAuthStatus.ERROR);
@ -153,7 +144,6 @@ export const useHiveAuth = () => {
}
};
/**
* Broadcasts ops array using hive auth app,
* uses hive auth key from current account data
@ -162,23 +152,27 @@ export const useHiveAuth = () => {
*/
const broadcast = async (opsArray: Operation[]) => {
try {
assert(opsArray, intl.formatMessage({ id: 'hiveauth.missing_op_arr' }));
assert(currentAccount.local.authType === AUTH_TYPE.HIVE_AUTH, intl.formatMessage({ id: 'hiveauth.invalid_auth_type' }))
assert(
currentAccount.local.authType === AUTH_TYPE.HIVE_AUTH,
intl.formatMessage({ id: 'hiveauth.invalid_auth_type' }),
);
setStatus(HiveAuthStatus.PROCESSING);
setStatusText(intl.formatMessage({ id: 'hiveauth.initiating' }));
await delay(1000);
const _hiveAuthObj = {
username: currentAccount.username,
expiry: currentAccount.local.hiveAuthExpiry,
key: decryptKey(currentAccount.local.hiveAuthKey, getDigitPinCode(pinHash)),
};
assert(_hiveAuthObj.key, intl.formatMessage({id:"hiveauth.decrypt_fail"}))
assert(_hiveAuthObj.expiry > new Date().getTime(), intl.formatMessage({id:'hiveauth.expired'}) )
assert(_hiveAuthObj.key, intl.formatMessage({ id: 'hiveauth.decrypt_fail' }));
assert(
_hiveAuthObj.expiry > new Date().getTime(),
intl.formatMessage({ id: 'hiveauth.expired' }),
);
const _cdWait = async (evt: any) => {
console.log('sign wait', evt);
@ -205,7 +199,6 @@ export const useHiveAuth = () => {
await delay(2000);
return true;
} catch (error) {
setStatus(HiveAuthStatus.ERROR);
setStatusText(intl.formatMessage({ id: error.message || 'hiveauth.transaction_fail' }));
@ -214,20 +207,18 @@ export const useHiveAuth = () => {
bugsnapInstance.notify(error);
return false;
}
}
};
const reset = () => {
setStatus(HiveAuthStatus.INPUT);
setStatusText('')
}
setStatusText('');
};
return {
authenticate,
broadcast,
reset,
status,
statusText
}
}
statusText,
};
};

View File

@ -31,7 +31,6 @@ const decryptKeyNew = (data, key) => {
return ret;
};
// stamping mechanism will help distinguish old legacy data and new encrypted data
// second purpose is to avoid necrypting empty strings
const getStampedData = (data) => {