Skip to content

Commit

Permalink
chore: add passphrase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Nov 22, 2023
1 parent 055b3a8 commit bb59c47
Show file tree
Hide file tree
Showing 16 changed files with 1,237 additions and 64 deletions.
15 changes: 11 additions & 4 deletions packages/connect-examples/expo-example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { enableFreeze, enableScreens } from 'react-native-screens';
import { View } from 'react-native';
import HomeScreen from './src/views/HomeScreen';
import MockScreen from './src/views/MockConnect';
import { getHardwareSDKInstance } from './src/utils/hardwareInstance';
import PassphraseTestScreen from './src/views/PassphraseTestScreen';
import SDKProvider from './src/provider/SDKProvider';

getHardwareSDKInstance();

const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Mock" component={MockScreen} />
</Stack.Navigator>
<SDKProvider>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="PassphraseTest" component={PassphraseTestScreen} />
<Stack.Screen name="Mock" component={MockScreen} />
</Stack.Navigator>
</SDKProvider>
</NavigationContainer>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useCallback } from 'react';
import { StyleSheet, Text, TextInput, View } from 'react-native';

export type CommonInputProps = {
value: string;
onChange: (value: string) => void;
label: string;
type?: 'number' | 'text';
};
export const CommonInput = ({ value, onChange, label, type }: CommonInputProps) => {
const onChangeCallback = useCallback(
(text: string) => {
if (type === 'number') {
onChange(text.replace(/[^\d]+/, ''));
} else {
onChange(text);
}
},
[onChange, type]
);

return (
<View>
<Text style={styles.label}>{label}</Text>
<TextInput
style={styles.input}
keyboardType="numeric"
value={value}
onChangeText={onChangeCallback}
/>
</View>
);
};
const styles = StyleSheet.create({
input: {
borderColor: '#E0E0E0',
borderWidth: 1,
borderRadius: 4,
padding: 6,
margin: 2,
fontSize: 16,
},
label: {
fontWeight: 'bold',
marginBottom: 4,
fontSize: 14,
},
});
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
import { useContext } from 'react';
import { StyleSheet, Switch, Text, TextInput, View } from 'react-native';
import { StyleSheet, Switch, Text, View } from 'react-native';
import { useCommonParams } from '../provider/CommonParamsProvider';

type SwitchInputProps = {
value: boolean;
onToggle: (value: boolean) => void;
label: string;
};
const SwitchInput = ({ value, onToggle, label }: SwitchInputProps) => (
<View style={styles.commonParamItem}>
<Text style={styles.label}>{label}</Text>
<Switch value={value} onValueChange={onToggle} />
</View>
);

type NumberInputProps = {
value: string;
onChange: (value: string) => void;
label: string;
};
const NumberInput = ({ value, onChange, label }: NumberInputProps) => (
<View style={styles.commonParamItem}>
<Text style={styles.label}>{label}</Text>
<TextInput
style={styles.input}
keyboardType="numeric"
value={value}
onChangeText={text => onChange(text.replace(/[^\d]+/, ''))}
/>
</View>
);
import { CommonInput } from './CommonInput';
import { SwitchInput } from './SwitchInput';

export default function CommonParamsView() {
const { commonParams, setCommonParams: setOptionalParams } = useCommonParams();
Expand All @@ -47,23 +19,27 @@ export default function CommonParamsView() {
value={!!commonParams.keepSession}
onToggle={value => handleSetParam('keepSession', value)}
/>
<NumberInput
<CommonInput
label="重试次数"
type="number"
value={commonParams.retryCount?.toString() ?? ''}
onChange={value => handleSetParam('retryCount', parseInt(value))}
/>
<NumberInput
<CommonInput
label="重试间隔时长"
type="number"
value={commonParams.pollIntervalTime?.toString() ?? ''}
onChange={value => handleSetParam('pollIntervalTime', parseInt(value))}
/>
<NumberInput
<CommonInput
label="连接超时事件"
type="number"
value={commonParams.timeout?.toString() ?? ''}
onChange={value => handleSetParam('timeout', parseInt(value))}
/>
<NumberInput
<CommonInput
label="passphrase State"
type="text"
value={commonParams.passphraseState ?? ''}
onChange={value => handleSetParam('passphraseState', value)}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useContext, useEffect, useState } from 'react';
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import {
CoreMessage,
DEVICE,
Expand All @@ -8,6 +8,7 @@ import {
UI_RESPONSE,
} from '@onekeyfe/hd-core';
import { View } from 'react-native';
import { useIsFocused } from '@react-navigation/native';
import HardwareSDKContext from '../provider/HardwareSDKContext';
import { ReceivePin } from './ReceivePin';

Expand All @@ -18,6 +19,13 @@ export default function HandleSDKEvents() {
const [showPinInput, setShowPinInput] = useState(false);
const [pinValue, setPinValue] = useState('');

const focus = useIsFocused();
const focusRef = useRef<boolean>(false);

useEffect(() => {
focusRef.current = focus;
}, [focus]);

useEffect(() => {
// 监听 SDK 事件
if (registerListener) {
Expand All @@ -30,6 +38,7 @@ export default function HandleSDKEvents() {
});

SDK.on(UI_EVENT, (message: CoreMessage) => {
if (!focusRef.current) return;
console.log('TopLEVEL EVENT ===>>>>: ', message);
if (message.type === UI_REQUEST.REQUEST_PIN) {
// setShowPinInput(true);
Expand Down Expand Up @@ -75,7 +84,12 @@ export default function HandleSDKEvents() {
console.log('example get disconnect event: ', message);
});
registerListener = true;
}, [SDK, HardwareLowLevelSDK]);

return () => {
registerListener = false;
SDK.removeAllListeners();
};
}, [SDK, HardwareLowLevelSDK, focus]);

// 输入 pin 码的确认回调
const onConfirmPin = useCallback(
Expand Down
Loading

0 comments on commit bb59c47

Please sign in to comment.