2024-05-18 12:48:03 -04:00
|
|
|
import React, { useMemo, useRef } from 'react';
|
2021-04-19 06:24:04 -04:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
2023-10-23 21:28:44 -04:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
2021-02-04 04:05:37 -03:00
|
|
|
import { ListItem } from 'react-native-elements';
|
|
|
|
import { AddressTypeBadge } from './AddressTypeBadge';
|
2021-04-19 06:24:04 -04:00
|
|
|
import loc, { formatBalance } from '../../loc';
|
|
|
|
import TooltipMenu from '../TooltipMenu';
|
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
|
|
|
import Share from 'react-native-share';
|
2023-10-23 21:28:44 -04:00
|
|
|
import { useTheme } from '../themes';
|
2024-02-03 17:18:56 +00:00
|
|
|
import { BitcoinUnit } from '../../models/bitcoinUnits';
|
2024-05-18 12:48:03 -04:00
|
|
|
import { useStorage } from '../../blue_modules/storage-context';
|
2024-02-07 15:24:24 -04:00
|
|
|
import presentAlert from '../Alert';
|
2024-02-05 20:49:16 -04:00
|
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
|
2024-03-03 09:56:22 -04:00
|
|
|
import QRCodeComponent from '../QRCodeComponent';
|
2024-03-31 20:59:14 +01:00
|
|
|
import confirm from '../../helpers/confirm';
|
2024-05-17 18:34:39 -04:00
|
|
|
import { useBiometrics } from '../../hooks/useBiometrics';
|
2024-05-18 12:48:03 -04:00
|
|
|
import { Action } from '../types';
|
2024-02-03 17:18:56 +00:00
|
|
|
|
|
|
|
interface AddressItemProps {
|
|
|
|
// todo: fix `any` after addresses.js is converted to the church of holy typescript
|
|
|
|
item: any;
|
|
|
|
balanceUnit: BitcoinUnit;
|
|
|
|
walletID: string;
|
|
|
|
allowSignVerifyMessage: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AddressItem = ({ item, balanceUnit, walletID, allowSignVerifyMessage }: AddressItemProps) => {
|
2024-05-18 12:48:03 -04:00
|
|
|
const { wallets } = useStorage();
|
2021-02-04 04:05:37 -03:00
|
|
|
const { colors } = useTheme();
|
2024-05-17 18:34:39 -04:00
|
|
|
const { isBiometricUseCapableAndEnabled, unlockWithBiometrics } = useBiometrics();
|
2021-02-04 04:05:37 -03:00
|
|
|
|
2021-07-06 06:37:15 -03:00
|
|
|
const hasTransactions = item.transactions > 0;
|
|
|
|
|
2021-04-19 06:24:04 -04:00
|
|
|
const stylesHook = StyleSheet.create({
|
2021-02-04 04:05:37 -03:00
|
|
|
container: {
|
|
|
|
borderBottomColor: colors.lightBorder,
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
list: {
|
|
|
|
color: colors.buttonTextColor,
|
|
|
|
},
|
2021-04-16 15:14:06 +02:00
|
|
|
index: {
|
|
|
|
color: colors.alternativeTextColor,
|
|
|
|
},
|
|
|
|
balance: {
|
|
|
|
color: colors.alternativeTextColor,
|
|
|
|
},
|
2021-07-06 06:37:15 -03:00
|
|
|
address: {
|
|
|
|
color: hasTransactions ? colors.darkGray : colors.buttonTextColor,
|
|
|
|
},
|
2021-02-04 04:05:37 -03:00
|
|
|
});
|
|
|
|
|
2021-07-06 06:37:15 -03:00
|
|
|
const { navigate } = useNavigation();
|
|
|
|
|
2021-10-06 23:15:32 -04:00
|
|
|
const menuRef = useRef();
|
2021-07-06 06:37:15 -03:00
|
|
|
const navigateToReceive = () => {
|
2024-02-03 17:18:56 +00:00
|
|
|
// @ts-ignore wtf
|
2021-10-06 23:15:32 -04:00
|
|
|
menuRef.current?.dismissMenu();
|
2024-02-03 17:18:56 +00:00
|
|
|
// @ts-ignore wtf
|
2021-07-06 06:37:15 -03:00
|
|
|
navigate('ReceiveDetailsRoot', {
|
|
|
|
screen: 'ReceiveDetails',
|
|
|
|
params: {
|
|
|
|
walletID,
|
|
|
|
address: item.address,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const navigateToSignVerify = () => {
|
2024-02-03 17:18:56 +00:00
|
|
|
// @ts-ignore wtf
|
2021-10-06 23:15:32 -04:00
|
|
|
menuRef.current?.dismissMenu();
|
2024-02-03 17:18:56 +00:00
|
|
|
// @ts-ignore wtf
|
2021-07-06 06:37:15 -03:00
|
|
|
navigate('SignVerifyRoot', {
|
|
|
|
screen: 'SignVerify',
|
|
|
|
params: {
|
|
|
|
walletID,
|
|
|
|
address: item.address,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-05-18 12:48:03 -04:00
|
|
|
const menuActions = useMemo(() => getAvailableActions({ allowSignVerifyMessage }), [allowSignVerifyMessage]);
|
|
|
|
|
2021-02-04 04:05:37 -03:00
|
|
|
const balance = formatBalance(item.balance, balanceUnit, true);
|
|
|
|
|
2021-04-19 06:24:04 -04:00
|
|
|
const handleCopyPress = () => {
|
|
|
|
Clipboard.setString(item.address);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSharePress = () => {
|
|
|
|
Share.open({ message: item.address }).catch(error => console.log(error));
|
|
|
|
};
|
|
|
|
|
2024-02-03 17:18:56 +00:00
|
|
|
const handleCopyPrivkeyPress = () => {
|
2024-03-15 23:05:15 +03:00
|
|
|
const wallet = wallets.find(w => w.getID() === walletID);
|
2024-02-03 17:18:56 +00:00
|
|
|
if (!wallet) {
|
2024-02-07 15:24:24 -04:00
|
|
|
presentAlert({ message: 'Internal error: cant find wallet' });
|
2024-02-03 17:18:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const wif = wallet._getWIFbyAddress(item.address);
|
|
|
|
if (!wif) {
|
2024-02-07 15:24:24 -04:00
|
|
|
presentAlert({ message: 'Internal error: cant get WIF from the wallet' });
|
2024-02-03 17:18:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-02-05 20:49:16 -04:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.Selection);
|
2024-02-03 17:18:56 +00:00
|
|
|
Clipboard.setString(wif);
|
|
|
|
} catch (error: any) {
|
2024-02-07 15:24:24 -04:00
|
|
|
presentAlert({ message: error.message });
|
2024-02-03 17:18:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onToolTipPress = async (id: string) => {
|
2021-08-16 12:10:04 -04:00
|
|
|
if (id === AddressItem.actionKeys.CopyToClipboard) {
|
2021-08-16 00:43:04 -04:00
|
|
|
handleCopyPress();
|
2021-08-16 12:10:04 -04:00
|
|
|
} else if (id === AddressItem.actionKeys.Share) {
|
2021-08-16 00:43:04 -04:00
|
|
|
handleSharePress();
|
2021-08-16 12:10:04 -04:00
|
|
|
} else if (id === AddressItem.actionKeys.SignVerify) {
|
2021-08-16 00:43:04 -04:00
|
|
|
navigateToSignVerify();
|
2024-02-03 17:18:56 +00:00
|
|
|
} else if (id === AddressItem.actionKeys.ExportPrivateKey) {
|
|
|
|
if (await confirm(loc.addresses.sensitive_private_key)) {
|
2024-05-17 18:34:39 -04:00
|
|
|
if (await isBiometricUseCapableAndEnabled()) {
|
|
|
|
if (!(await unlockWithBiometrics())) {
|
2024-02-03 17:18:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCopyPrivkeyPress();
|
|
|
|
}
|
2021-08-16 00:43:04 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-03 09:56:22 -04:00
|
|
|
const renderPreview = () => {
|
|
|
|
return <QRCodeComponent value={item.address} isMenuAvailable={false} />;
|
|
|
|
};
|
|
|
|
|
2021-02-04 04:05:37 -03:00
|
|
|
const render = () => {
|
|
|
|
return (
|
2021-10-06 23:15:32 -04:00
|
|
|
<TooltipMenu
|
|
|
|
title={item.address}
|
|
|
|
ref={menuRef}
|
2024-05-18 12:48:03 -04:00
|
|
|
actions={menuActions}
|
2021-10-06 23:15:32 -04:00
|
|
|
onPressMenuItem={onToolTipPress}
|
2024-03-03 09:56:22 -04:00
|
|
|
renderPreview={renderPreview}
|
2021-10-21 14:54:32 -04:00
|
|
|
onPress={navigateToReceive}
|
2021-10-06 23:15:32 -04:00
|
|
|
>
|
2023-07-25 15:31:09 +01:00
|
|
|
<ListItem key={item.key} containerStyle={stylesHook.container}>
|
2021-04-19 06:24:04 -04:00
|
|
|
<ListItem.Content style={stylesHook.list}>
|
|
|
|
<ListItem.Title style={stylesHook.list} numberOfLines={1} ellipsizeMode="middle">
|
|
|
|
<Text style={[styles.index, stylesHook.index]}>{item.index + 1}</Text>{' '}
|
|
|
|
<Text style={[stylesHook.address, styles.address]}>{item.address}</Text>
|
|
|
|
</ListItem.Title>
|
2021-07-06 06:37:15 -03:00
|
|
|
<View style={styles.subtitle}>
|
|
|
|
<Text style={[stylesHook.list, styles.balance, stylesHook.balance]}>{balance}</Text>
|
|
|
|
</View>
|
2021-04-19 06:24:04 -04:00
|
|
|
</ListItem.Content>
|
2024-02-03 17:18:56 +00:00
|
|
|
<View>
|
2021-07-06 06:37:15 -03:00
|
|
|
<AddressTypeBadge isInternal={item.isInternal} hasTransactions={hasTransactions} />
|
|
|
|
<Text style={[stylesHook.list, styles.balance, stylesHook.balance]}>
|
|
|
|
{loc.addresses.transactions}: {item.transactions}
|
|
|
|
</Text>
|
|
|
|
</View>
|
2021-04-19 06:24:04 -04:00
|
|
|
</ListItem>
|
2021-08-16 00:43:04 -04:00
|
|
|
</TooltipMenu>
|
2021-02-04 04:05:37 -03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return render();
|
|
|
|
};
|
|
|
|
|
2021-08-16 12:10:04 -04:00
|
|
|
AddressItem.actionKeys = {
|
|
|
|
Share: 'share',
|
|
|
|
CopyToClipboard: 'copyToClipboard',
|
|
|
|
SignVerify: 'signVerify',
|
2024-02-03 17:18:56 +00:00
|
|
|
ExportPrivateKey: 'exportPrivateKey',
|
2021-08-16 12:10:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
AddressItem.actionIcons = {
|
|
|
|
Signature: {
|
|
|
|
iconType: 'SYSTEM',
|
|
|
|
iconValue: 'signature',
|
|
|
|
},
|
|
|
|
Share: {
|
|
|
|
iconType: 'SYSTEM',
|
|
|
|
iconValue: 'square.and.arrow.up',
|
|
|
|
},
|
|
|
|
Clipboard: {
|
|
|
|
iconType: 'SYSTEM',
|
2021-08-26 21:31:36 -04:00
|
|
|
iconValue: 'doc.on.doc',
|
2021-08-16 12:10:04 -04:00
|
|
|
},
|
2024-02-03 17:18:56 +00:00
|
|
|
ExportPrivateKey: {
|
|
|
|
iconType: 'SYSTEM',
|
|
|
|
iconValue: 'key',
|
|
|
|
},
|
2021-08-16 12:10:04 -04:00
|
|
|
};
|
|
|
|
|
2021-04-19 06:24:04 -04:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
address: {
|
2021-07-06 06:37:15 -03:00
|
|
|
fontWeight: 'bold',
|
2021-04-19 06:24:04 -04:00
|
|
|
marginHorizontal: 40,
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
fontSize: 15,
|
|
|
|
},
|
|
|
|
balance: {
|
|
|
|
marginTop: 8,
|
|
|
|
marginLeft: 14,
|
|
|
|
},
|
2021-07-06 06:37:15 -03:00
|
|
|
subtitle: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
width: '100%',
|
|
|
|
},
|
2021-04-19 06:24:04 -04:00
|
|
|
});
|
|
|
|
|
2024-05-18 12:48:03 -04:00
|
|
|
const getAvailableActions = ({ allowSignVerifyMessage }: { allowSignVerifyMessage: boolean }): Action[] | Action[][] => {
|
|
|
|
const actions = [
|
|
|
|
{
|
|
|
|
id: AddressItem.actionKeys.CopyToClipboard,
|
|
|
|
text: loc.transactions.details_copy,
|
|
|
|
icon: AddressItem.actionIcons.Clipboard,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: AddressItem.actionKeys.Share,
|
|
|
|
text: loc.receive.details_share,
|
|
|
|
icon: AddressItem.actionIcons.Share,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (allowSignVerifyMessage) {
|
|
|
|
actions.push({
|
|
|
|
id: AddressItem.actionKeys.SignVerify,
|
|
|
|
text: loc.addresses.sign_title,
|
|
|
|
icon: AddressItem.actionIcons.Signature,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allowSignVerifyMessage) {
|
|
|
|
actions.push({
|
|
|
|
id: AddressItem.actionKeys.ExportPrivateKey,
|
|
|
|
text: loc.addresses.copy_private_key,
|
|
|
|
icon: AddressItem.actionIcons.ExportPrivateKey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
2021-02-04 04:05:37 -03:00
|
|
|
};
|
2024-05-18 12:48:03 -04:00
|
|
|
|
2021-02-04 04:05:37 -03:00
|
|
|
export { AddressItem };
|