BlueWallet/screen/send/isItMyAddress.js

169 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-05-31 19:22:22 +02:00
import React, { useRef, useState } from 'react';
import { useRoute } from '@react-navigation/native';
2024-05-20 11:54:13 +02:00
import { Keyboard, KeyboardAvoidingView, Platform, StyleSheet, TextInput, View } from 'react-native';
import { BlueButtonLink, BlueCard, BlueSpacing10, BlueSpacing20, BlueText } from '../../BlueComponents';
2023-11-15 09:40:22 +01:00
import Button from '../../components/Button';
2024-05-20 11:54:13 +02:00
import navigationStyle from '../../components/navigationStyle';
import SafeArea from '../../components/SafeArea';
2024-05-20 11:54:13 +02:00
import { useTheme } from '../../components/themes';
import { requestCameraAuthorization } from '../../helpers/scan-qr';
import loc from '../../loc';
2024-05-31 19:22:22 +02:00
import { useStorage } from '../../hooks/context/useStorage';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
const IsItMyAddress = () => {
/** @type {AbstractWallet[]} */
2024-05-31 19:22:22 +02:00
const { wallets } = useStorage();
const { navigate } = useExtendedNavigation();
2020-12-07 19:24:24 +01:00
const { name } = useRoute();
const { colors } = useTheme();
const scanButtonRef = useRef();
const [address, setAddress] = useState('');
const [result, setResult] = useState('');
const [resultCleanAddress, setResultCleanAddress] = useState();
2020-12-07 19:24:24 +01:00
const stylesHooks = StyleSheet.create({
input: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
2020-12-07 19:24:24 +01:00
});
const handleUpdateAddress = nextValue => setAddress(nextValue.trim());
2020-12-07 19:55:50 +01:00
const checkAddress = () => {
Keyboard.dismiss();
const cleanAddress = address.replace('bitcoin:', '').replace('BITCOIN:', '').replace('bitcoin=', '').split('?')[0];
const _result = [];
for (const w of wallets) {
if (w.weOwnAddress(cleanAddress)) {
setResultCleanAddress(cleanAddress);
_result.push(loc.formatString(loc.is_it_my_address.owns, { label: w.getLabel(), address: cleanAddress }));
}
}
if (_result.length === 0) {
setResult(_result.push(loc.is_it_my_address.no_wallet_owns_address));
setResultCleanAddress();
}
setResult(_result.join('\n\n'));
};
2020-12-07 19:55:50 +01:00
const onBarScanned = value => {
setAddress(value);
setResultCleanAddress(value);
};
2020-12-07 19:55:50 +01:00
const importScan = () => {
2023-10-17 15:35:10 +02:00
requestCameraAuthorization().then(() => {
2023-10-17 15:39:32 +02:00
navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: name,
onBarScanned,
showFileImportButton: true,
},
});
});
2023-10-17 15:39:32 +02:00
};
const clearAddressInput = () => {
setAddress('');
setResult();
setResultCleanAddress();
};
const viewQRCode = () => {
navigate('ReceiveDetailsRoot', {
screen: 'ReceiveDetails',
params: {
address: resultCleanAddress,
},
});
};
return (
<SafeArea style={styles.blueArea}>
2021-02-25 02:56:06 +01:00
<KeyboardAvoidingView
enabled={!Platform.isPad}
behavior={Platform.OS === 'ios' ? 'position' : null}
keyboardShouldPersistTaps="handled"
>
<View style={styles.wrapper}>
<BlueCard style={styles.mainCard}>
<View style={[styles.input, stylesHooks.input]}>
<TextInput
style={styles.text}
maxHeight={100}
minHeight={100}
maxWidth="100%"
minWidth="100%"
multiline
editable
placeholder={loc.is_it_my_address.enter_address}
placeholderTextColor="#81868e"
value={address}
onChangeText={handleUpdateAddress}
2021-03-02 14:38:02 +01:00
testID="AddressInput"
/>
</View>
<BlueSpacing10 />
<BlueButtonLink ref={scanButtonRef} title={loc.wallets.import_scan_qr} onPress={importScan} />
<BlueSpacing10 />
2023-11-15 09:40:22 +01:00
<Button title={loc.send.input_clear} onPress={clearAddressInput} />
<BlueSpacing20 />
{resultCleanAddress && (
<>
2023-11-15 09:40:22 +01:00
<Button title={loc.is_it_my_address.view_qrcode} onPress={viewQRCode} />
<BlueSpacing20 />
</>
)}
2023-11-15 13:54:04 +01:00
<Button
2021-03-02 14:38:02 +01:00
disabled={address.trim().length === 0}
title={loc.is_it_my_address.check_address}
onPress={checkAddress}
testID="CheckAddress"
/>
<BlueSpacing20 />
2021-03-02 14:38:02 +01:00
<BlueText testID="Result">{result}</BlueText>
</BlueCard>
</View>
</KeyboardAvoidingView>
</SafeArea>
);
};
export default IsItMyAddress;
2021-02-15 09:03:54 +01:00
IsItMyAddress.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.is_it_my_address.title }));
const styles = StyleSheet.create({
wrapper: {
marginTop: 16,
alignItems: 'center',
justifyContent: 'flex-start',
},
mainCard: {
padding: 0,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-start',
},
input: {
flexDirection: 'row',
borderWidth: 1,
borderBottomWidth: 0.5,
alignItems: 'center',
borderRadius: 4,
},
text: {
padding: 8,
minHeight: 33,
color: '#81868e',
},
});