BlueWallet/screen/receive/details.js

395 lines
12 KiB
JavaScript
Raw Normal View History

import React, { useCallback, useContext, useRef, useState } from 'react';
import {
InteractionManager,
Keyboard,
2020-11-17 09:43:38 +01:00
KeyboardAvoidingView,
Platform,
ScrollView,
2020-11-17 09:43:38 +01:00
StatusBar,
StyleSheet,
TextInput,
View,
} from 'react-native';
2019-02-14 06:15:56 +01:00
import QRCode from 'react-native-qrcode-svg';
import { useNavigation, useRoute, useTheme, useFocusEffect } from '@react-navigation/native';
2020-11-17 09:43:38 +01:00
import Share from 'react-native-share';
2019-01-21 14:55:39 +01:00
import {
2020-11-30 05:18:54 +01:00
BlueLoading,
2019-01-21 14:55:39 +01:00
BlueCopyTextToClipboard,
BlueButton,
2020-07-15 19:32:59 +02:00
SecondButton,
2020-11-30 05:18:54 +01:00
BlueButtonLink,
2019-01-21 14:55:39 +01:00
is,
2019-12-15 07:10:22 +01:00
BlueBitcoinAmount,
BlueText,
BlueSpacing20,
BlueAlertWalletExportReminder,
2019-01-21 14:55:39 +01:00
} from '../../BlueComponents';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
2020-11-17 09:43:38 +01:00
import BottomModal from '../../components/BottomModal';
2021-01-19 04:44:55 +01:00
import Privacy from '../../blue_modules/Privacy';
2019-12-15 07:10:22 +01:00
import { Chain, BitcoinUnit } from '../../models/bitcoinUnits';
2021-01-22 17:34:47 +01:00
import HandoffComponent from '../../components/handoff';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import Notifications from '../../blue_modules/notifications';
import ToolTipMenu from '../../components/TooltipMenu';
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
2020-06-09 16:08:18 +02:00
const currency = require('../../blue_modules/currency');
2018-01-30 23:42:38 +01:00
2020-04-20 06:03:36 +02:00
const ReceiveDetails = () => {
2020-11-03 21:23:34 +01:00
const { walletID } = useRoute().params;
const { wallets, saveToDisk, sleep } = useContext(BlueStorageContext);
2020-11-03 21:23:34 +01:00
const wallet = wallets.find(w => w.getID() === walletID);
2020-04-20 06:03:36 +02:00
const [address, setAddress] = useState('');
const [customLabel, setCustomLabel] = useState();
const [customAmount, setCustomAmount] = useState(0);
2020-06-09 16:08:18 +02:00
const [customUnit, setCustomUnit] = useState(BitcoinUnit.BTC);
2020-04-20 06:03:36 +02:00
const [bip21encoded, setBip21encoded] = useState();
const [isCustom, setIsCustom] = useState(false);
const [isCustomModalVisible, setIsCustomModalVisible] = useState(false);
const [showAddress, setShowAddress] = useState(false);
2020-04-20 06:03:36 +02:00
const { navigate, goBack } = useNavigation();
2020-07-15 19:32:59 +02:00
const { colors } = useTheme();
const toolTip = useRef();
const qrCode = useRef();
2020-07-15 19:32:59 +02:00
const styles = StyleSheet.create({
modalContent: {
backgroundColor: colors.modal,
2020-07-15 19:32:59 +02:00
padding: 22,
justifyContent: 'center',
alignItems: 'center',
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
borderTopColor: colors.foregroundColor,
borderWidth: colors.borderWidth,
minHeight: 350,
height: 350,
},
customAmount: {
flexDirection: 'row',
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
2020-07-15 19:32:59 +02:00
borderWidth: 1.0,
borderBottomWidth: 0.5,
backgroundColor: colors.inputBackgroundColor,
2020-07-15 19:32:59 +02:00
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 8,
borderRadius: 4,
},
customAmountText: {
flex: 1,
marginHorizontal: 8,
color: colors.foregroundColor,
2020-07-15 19:32:59 +02:00
minHeight: 33,
},
2020-08-03 19:26:16 +02:00
qrCodeContainer: { borderWidth: 6, borderRadius: 8, borderColor: '#FFFFFF' },
2020-07-15 19:32:59 +02:00
root: {
flex: 1,
backgroundColor: colors.elevated,
2020-07-15 19:32:59 +02:00
},
scroll: {
justifyContent: 'space-between',
},
scrollBody: {
marginTop: 32,
alignItems: 'center',
paddingHorizontal: 16,
},
amount: {
color: colors.foregroundColor,
2020-07-15 19:32:59 +02:00
fontWeight: '600',
fontSize: 36,
textAlign: 'center',
paddingBottom: 24,
},
label: {
color: colors.foregroundColor,
2020-07-15 19:32:59 +02:00
fontWeight: '600',
textAlign: 'center',
paddingBottom: 24,
},
loading: {
alignItems: 'center',
width: 300,
height: 300,
backgroundColor: colors.elevated,
2020-07-15 19:32:59 +02:00
},
share: {
marginBottom: 24,
marginHorizontal: 16,
2020-07-15 19:32:59 +02:00
},
modalButton: {
backgroundColor: colors.modalButton,
2020-07-15 19:32:59 +02:00
paddingVertical: 14,
paddingHorizontal: 70,
maxWidth: '80%',
borderRadius: 50,
fontWeight: '700',
},
});
2018-01-30 23:42:38 +01:00
const handleShareQRCode = () => {
qrCode.current.toDataURL(data => {
const shareImageBase64 = {
url: `data:image/png;base64,${data}`,
};
Share.open(shareImageBase64).catch(error => console.log(error));
});
};
const showToolTipMenu = () => {
toolTip.current.showMenu();
};
const renderReceiveDetails = () => {
return (
<ScrollView style={styles.root} contentContainerStyle={styles.scroll} keyboardShouldPersistTaps="always">
<View style={styles.scrollBody}>
{isCustom && (
<>
<BlueText testID="CustomAmountText" style={styles.amount} numberOfLines={1}>
{getDisplayAmount()}
</BlueText>
<BlueText testID="CustomAmountDescriptionText" style={styles.label} numberOfLines={1}>
{customLabel}
</BlueText>
</>
)}
<TouchableWithoutFeedback style={styles.qrCodeContainer} testID="BitcoinAddressQRCodeContainer" onLongPress={showToolTipMenu}>
<ToolTipMenu
ref={toolTip}
anchorRef={qrCode}
actions={[
{
id: 'shareQRCode',
text: loc.receive.details_share,
onPress: handleShareQRCode,
},
]}
onPress={handleShareQRCode}
/>
2020-08-03 19:26:16 +02:00
<QRCode
value={bip21encoded}
logo={require('../../img/qr-code.png')}
size={(is.ipad() && 300) || 300}
logoSize={90}
color="#000000"
logoBackgroundColor={colors.brandingColor}
backgroundColor="#FFFFFF"
ecl="H"
getRef={qrCode}
2020-08-03 19:26:16 +02:00
/>
</TouchableWithoutFeedback>
<BlueCopyTextToClipboard text={isCustom ? bip21encoded : address} />
</View>
<View style={styles.share}>
<BlueButtonLink testID="SetCustomAmountButton" title={loc.receive.details_setAmount} onPress={showCustomAmountModal} />
<View>
<SecondButton onPress={handleShareButtonPressed} title={loc.receive.details_share} />
</View>
</View>
{renderCustomAmountModal()}
</ScrollView>
);
};
const obtainWalletAddress = useCallback(async () => {
Privacy.enableBlur();
2020-04-20 06:03:36 +02:00
console.log('receive/details - componentDidMount');
wallet.setUserHasSavedExport(true);
await saveToDisk();
let address;
2020-04-20 06:03:36 +02:00
if (wallet.getAddressAsync) {
if (wallet.chain === Chain.ONCHAIN) {
try {
address = await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
} catch (_) {}
if (!address) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
address = wallet._getExternalAddressByIndex(wallet.getNextFreeAddressIndex());
} else {
saveToDisk(); // caching whatever getAddressAsync() generated internally
2019-10-01 00:13:22 +02:00
}
2020-04-20 06:03:36 +02:00
} else if (wallet.chain === Chain.OFFCHAIN) {
try {
await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
2020-04-20 06:03:36 +02:00
address = wallet.getAddress();
} catch (_) {}
if (!address) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
2020-04-20 06:03:36 +02:00
address = wallet.getAddress();
} else {
saveToDisk(); // caching whatever getAddressAsync() generated internally
2019-09-12 04:05:01 +02:00
}
2019-05-02 22:33:03 +02:00
}
setAddressBIP21Encoded(address);
await Notifications.tryToObtainPermissions();
Notifications.majorTomToGroundControl([address], [], []);
2020-04-20 06:03:36 +02:00
} else if (wallet.getAddress) {
setAddressBIP21Encoded(wallet.getAddress());
await Notifications.tryToObtainPermissions();
Notifications.majorTomToGroundControl([wallet.getAddress()], [], []);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const setAddressBIP21Encoded = address => {
const bip21encoded = DeeplinkSchemaMatch.bip21encode(address);
setAddress(address);
setBip21encoded(bip21encoded);
setShowAddress(true);
};
useFocusEffect(
useCallback(() => {
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
if (!wallet.getUserHasSavedExport()) {
BlueAlertWalletExportReminder({
onSuccess: obtainWalletAddress,
onFailure: () => {
goBack();
navigate('WalletExportRoot', {
screen: 'WalletExport',
params: {
walletID: wallet.getID(),
},
});
},
});
} else {
obtainWalletAddress();
}
}
});
return () => {
task.cancel();
Privacy.disableBlur();
};
}, [goBack, navigate, obtainWalletAddress, wallet]),
);
2018-01-30 23:42:38 +01:00
2020-04-20 06:03:36 +02:00
const dismissCustomAmountModal = () => {
Keyboard.dismiss();
setIsCustomModalVisible(false);
};
2020-04-20 06:03:36 +02:00
const showCustomAmountModal = () => {
setIsCustomModalVisible(true);
};
const createCustomAmountAddress = () => {
setIsCustom(true);
setIsCustomModalVisible(false);
2020-06-09 16:08:18 +02:00
let amount = customAmount;
switch (customUnit) {
case BitcoinUnit.BTC:
// nop
break;
case BitcoinUnit.SATS:
amount = currency.satoshiToBTC(customAmount);
break;
case BitcoinUnit.LOCAL_CURRENCY:
if (BlueBitcoinAmount.conversionCache[amount + BitcoinUnit.LOCAL_CURRENCY]) {
// cache hit! we reuse old value that supposedly doesnt have rounding errors
amount = currency.satoshiToBTC(BlueBitcoinAmount.conversionCache[amount + BitcoinUnit.LOCAL_CURRENCY]);
} else {
amount = currency.fiatToBTC(customAmount);
}
break;
}
setBip21encoded(DeeplinkSchemaMatch.bip21encode(address, { amount, label: customLabel }));
2020-07-23 15:28:22 +02:00
setShowAddress(true);
2020-04-20 06:03:36 +02:00
};
const renderCustomAmountModal = () => {
2019-12-15 07:10:22 +01:00
return (
2020-11-17 09:43:38 +01:00
<BottomModal isVisible={isCustomModalVisible} onClose={dismissCustomAmountModal}>
2021-02-25 02:56:06 +01:00
<KeyboardAvoidingView enabled={!Platform.isPad} behavior={Platform.OS === 'ios' ? 'position' : null}>
2019-12-15 07:10:22 +01:00
<View style={styles.modalContent}>
2020-06-09 16:08:18 +02:00
<BlueBitcoinAmount
unit={customUnit}
amount={customAmount || ''}
onChangeText={setCustomAmount}
onAmountUnitChange={setCustomUnit}
/>
<View style={styles.customAmount}>
2019-12-15 07:10:22 +01:00
<TextInput
2020-04-20 06:03:36 +02:00
onChangeText={setCustomLabel}
2020-06-09 17:55:19 +02:00
placeholderTextColor="#81868e"
2020-07-20 15:38:46 +02:00
placeholder={loc.receive.details_label}
2020-04-20 06:03:36 +02:00
value={customLabel || ''}
2019-12-15 07:10:22 +01:00
numberOfLines={1}
style={styles.customAmountText}
testID="CustomAmountDescription"
2019-12-15 07:10:22 +01:00
/>
</View>
<BlueSpacing20 />
<View>
<BlueButton
testID="CustomAmountSaveButton"
style={styles.modalButton}
title={loc.receive.details_create}
onPress={createCustomAmountAddress}
/>
2019-12-15 07:10:22 +01:00
<BlueSpacing20 />
</View>
<BlueSpacing20 />
</View>
</KeyboardAvoidingView>
2020-11-17 09:43:38 +01:00
</BottomModal>
2019-12-15 07:10:22 +01:00
);
};
2020-04-20 06:03:36 +02:00
const handleShareButtonPressed = () => {
2020-07-06 23:53:08 +02:00
Share.open({ message: bip21encoded }).catch(error => console.log(error));
2019-12-15 07:10:22 +01:00
};
2020-06-09 16:08:18 +02:00
/**
* @returns {string} BTC amount, accounting for current `customUnit` and `customUnit`
*/
const getDisplayAmount = () => {
switch (customUnit) {
case BitcoinUnit.BTC:
return customAmount + ' BTC';
case BitcoinUnit.SATS:
return currency.satoshiToBTC(customAmount) + ' BTC';
case BitcoinUnit.LOCAL_CURRENCY:
return currency.fiatToBTC(customAmount) + ' BTC';
}
return customAmount + ' ' + customUnit;
};
2020-04-20 06:03:36 +02:00
return (
2020-07-15 19:32:59 +02:00
<View style={styles.root}>
<StatusBar barStyle="light-content" />
2021-01-19 04:40:11 +01:00
{address !== undefined && showAddress && (
<HandoffComponent
2020-04-20 06:03:36 +02:00
title={`Bitcoin Transaction ${address}`}
type="io.bluewallet.bluewallet"
url={`https://blockstream.info/address/${address}`}
/>
)}
2020-11-30 05:18:54 +01:00
{showAddress ? renderReceiveDetails() : <BlueLoading />}
2020-07-15 19:32:59 +02:00
</View>
2020-04-20 06:03:36 +02:00
);
};
2021-02-15 09:03:54 +01:00
ReceiveDetails.navigationOptions = navigationStyle(
{
closeButton: true,
headerLeft: null,
},
opts => ({ ...opts, title: loc.receive.header }),
);
2020-04-20 06:03:36 +02:00
export default ReceiveDetails;