BlueWallet/screen/receive/details.js

300 lines
9.7 KiB
JavaScript
Raw Normal View History

2018-01-30 23:42:38 +01:00
import React, { Component } from 'react';
2019-12-15 07:10:22 +01:00
import { View, InteractionManager, Platform, TextInput, KeyboardAvoidingView, Keyboard, StyleSheet, ScrollView } from 'react-native';
2019-02-14 06:15:56 +01:00
import QRCode from 'react-native-qrcode-svg';
import bip21 from 'bip21';
2019-01-21 14:55:39 +01:00
import {
BlueLoading,
SafeBlueArea,
BlueCopyTextToClipboard,
BlueButton,
BlueButtonLink,
BlueNavigationStyle,
is,
2019-12-15 07:10:22 +01:00
BlueBitcoinAmount,
BlueText,
BlueSpacing20,
BlueAlertWalletExportReminder,
2019-01-21 14:55:39 +01:00
} from '../../BlueComponents';
2018-03-18 00:09:33 +01:00
import PropTypes from 'prop-types';
import Privacy from '../../Privacy';
2019-08-06 06:20:33 +02:00
import Share from 'react-native-share';
2019-12-15 07:10:22 +01:00
import { Chain, BitcoinUnit } from '../../models/bitcoinUnits';
import Modal from 'react-native-modal';
2018-06-25 00:22:46 +02:00
/** @type {AppStorage} */
2019-12-15 07:10:22 +01:00
const BlueApp = require('../../BlueApp');
const loc = require('../../loc');
2018-01-30 23:42:38 +01:00
export default class ReceiveDetails extends Component {
static navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
title: loc.receive.header,
headerLeft: null,
});
2018-01-30 23:42:38 +01:00
constructor(props) {
super(props);
2019-10-01 00:13:22 +02:00
let secret = props.navigation.state.params.secret || '';
2018-01-30 23:42:38 +01:00
this.state = {
secret: secret,
address: '',
2019-12-15 07:10:22 +01:00
customLabel: '',
customAmount: 0,
2019-05-02 22:33:03 +02:00
bip21encoded: undefined,
2019-12-15 07:10:22 +01:00
isCustom: false,
isCustomModalVisible: false,
2018-03-17 21:39:21 +01:00
};
2018-06-25 00:22:46 +02:00
}
renderReceiveDetails = async () => {
this.wallet.setUserHasSavedExport(true);
await BlueApp.saveToDisk();
let address;
if (this.wallet.getAddressAsync) {
if (this.wallet.chain === Chain.ONCHAIN) {
try {
address = await Promise.race([this.wallet.getAddressAsync(), BlueApp.sleep(1000)]);
} catch (_) {}
if (!address) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
address = this.wallet._getExternalAddressByIndex(this.wallet.next_free_address_index);
} else {
BlueApp.saveToDisk(); // caching whatever getAddressAsync() generated internally
2019-10-01 00:13:22 +02:00
}
this.setState({
address: address,
});
} else if (this.wallet.chain === Chain.OFFCHAIN) {
try {
await Promise.race([this.wallet.getAddressAsync(), BlueApp.sleep(1000)]);
address = this.wallet.getAddress();
} catch (_) {}
if (!address) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
address = this.wallet.getAddress();
} else {
BlueApp.saveToDisk(); // caching whatever getAddressAsync() generated internally
2019-09-12 04:05:01 +02:00
}
2019-05-02 22:33:03 +02:00
}
this.setState({
address: address,
});
} else if (this.wallet.getAddress) {
this.setState({
address: this.wallet.getAddress(),
});
}
2019-05-02 22:33:03 +02:00
InteractionManager.runAfterInteractions(async () => {
const bip21encoded = bip21.encode(this.state.address);
this.setState({ bip21encoded });
2019-05-02 22:33:03 +02:00
});
};
componentDidMount() {
Privacy.enableBlur();
console.log('receive/details - componentDidMount');
for (let w of BlueApp.getWallets()) {
if (w.getSecret() === this.state.secret) {
// found our wallet
this.wallet = w;
}
}
if (this.wallet) {
if (!this.wallet.getUserHasSavedExport()) {
BlueAlertWalletExportReminder({
onSuccess: this.renderReceiveDetails,
onFailure: () => {
this.props.navigation.goBack();
this.props.navigation.navigate('WalletExport', {
2020-03-12 18:26:05 +01:00
wallet: this.wallet,
});
},
});
} else {
this.renderReceiveDetails();
}
}
2018-01-30 23:42:38 +01:00
}
2019-12-15 07:10:22 +01:00
componentWillUnmount() {
Privacy.disableBlur();
}
2019-12-15 07:10:22 +01:00
renderCustomAmountModal = () => {
return (
<Modal
isVisible={this.state.isCustomModalVisible}
style={styles.bottomModal}
onBackdropPress={() => {
Keyboard.dismiss();
this.setState({ isCustomModalVisible: false });
}}
>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'position' : null}>
<View style={styles.modalContent}>
<BlueBitcoinAmount amount={this.state.customAmount || ''} onChangeText={text => this.setState({ customAmount: text })} />
<View
style={{
flexDirection: 'row',
borderColor: '#d2d2d2',
borderBottomColor: '#d2d2d2',
borderWidth: 1.0,
borderBottomWidth: 0.5,
backgroundColor: '#f5f5f5',
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 8,
borderRadius: 4,
}}
>
<TextInput
onChangeText={text => this.setState({ customLabel: text })}
placeholder={loc.receive.details.label}
value={this.state.customLabel || ''}
numberOfLines={1}
style={{ flex: 1, marginHorizontal: 8, minHeight: 33 }}
/>
</View>
<BlueSpacing20 />
<View>
<BlueButton
title={loc.receive.details.create}
onPress={() => {
this.setState({
isCustom: true,
isCustomModalVisible: false,
bip21encoded: bip21.encode(this.state.address, { amount: this.state.customAmount, label: this.state.customLabel }),
});
}}
/>
<BlueSpacing20 />
<BlueButtonLink
title="Reset"
onPress={() => {
this.setState({
isCustom: false,
isCustomModalVisible: false,
customAmount: '',
customLabel: '',
bip21encoded: bip21.encode(this.state.addresss),
});
}}
/>
</View>
<BlueSpacing20 />
</View>
</KeyboardAvoidingView>
</Modal>
);
};
showCustomAmountModal = () => {
this.setState({ isCustomModalVisible: true });
};
2018-01-30 23:42:38 +01:00
render() {
return (
2018-05-12 22:27:34 +02:00
<SafeBlueArea style={{ flex: 1 }}>
<ScrollView contentContainerStyle={{ justifyContent: 'space-between' }}>
2019-07-06 10:03:42 +02:00
<View style={{ marginTop: 32, alignItems: 'center', paddingHorizontal: 16 }}>
2019-12-15 07:10:22 +01:00
{this.state.isCustom && (
<>
<BlueText
style={{ color: '#0c2550', fontWeight: '600', fontSize: 36, textAlign: 'center', paddingBottom: 24 }}
numberOfLines={1}
>
{this.state.customAmount} {BitcoinUnit.BTC}
</BlueText>
<BlueText style={{ color: '#0c2550', fontWeight: '600', textAlign: 'center', paddingBottom: 24 }} numberOfLines={1}>
{this.state.customLabel}
</BlueText>
</>
)}
2019-05-02 22:33:03 +02:00
{this.state.bip21encoded === undefined ? (
2019-08-06 18:31:11 +02:00
<View style={{ alignItems: 'center', width: 300, height: 300 }}>
<BlueLoading />
</View>
2019-05-02 22:33:03 +02:00
) : (
<QRCode
value={this.state.bip21encoded}
logo={require('../../img/qr-code.png')}
size={(is.ipad() && 300) || 300}
logoSize={90}
color={BlueApp.settings.foregroundColor}
logoBackgroundColor={BlueApp.settings.brandingColor}
ecl={'H'}
2019-08-06 06:20:33 +02:00
getRef={c => (this.qrCodeSVG = c)}
2019-05-02 22:33:03 +02:00
/>
)}
<BlueCopyTextToClipboard text={this.state.isCustom ? this.state.bip21encoded : this.state.address} />
</View>
2019-07-06 10:03:42 +02:00
<View style={{ alignItems: 'center', alignContent: 'flex-end', marginBottom: 24 }}>
2019-12-15 07:10:22 +01:00
<BlueButtonLink title={loc.receive.details.setAmount} onPress={this.showCustomAmountModal} />
<View>
2019-05-02 22:33:03 +02:00
<BlueButton
icon={{
name: 'share-alternative',
type: 'entypo',
color: BlueApp.settings.buttonTextColor,
}}
onPress={async () => {
2019-08-06 06:20:33 +02:00
if (this.qrCodeSVG === undefined) {
2019-12-15 07:10:22 +01:00
Share.open({ message: this.state.bip21encoded }).catch(error => console.log(error));
2019-08-06 06:20:33 +02:00
} else {
InteractionManager.runAfterInteractions(async () => {
this.qrCodeSVG.toDataURL(data => {
let shareImageBase64 = {
2019-12-15 07:10:22 +01:00
message: this.state.bip21encoded,
2019-08-06 06:20:33 +02:00
url: `data:image/png;base64,${data}`,
};
Share.open(shareImageBase64).catch(error => console.log(error));
});
});
}
2019-05-02 22:33:03 +02:00
}}
title={loc.receive.details.share}
/>
</View>
</View>
2019-12-15 07:10:22 +01:00
{this.renderCustomAmountModal()}
</ScrollView>
2018-01-30 23:42:38 +01:00
</SafeBlueArea>
);
}
}
2018-03-18 00:09:33 +01:00
2019-12-15 07:10:22 +01:00
const styles = StyleSheet.create({
modalContent: {
backgroundColor: '#FFFFFF',
padding: 22,
justifyContent: 'center',
alignItems: 'center',
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
borderColor: 'rgba(0, 0, 0, 0.1)',
minHeight: 350,
height: 350,
},
bottomModal: {
justifyContent: 'flex-end',
margin: 0,
},
});
2018-03-18 00:09:33 +01:00
ReceiveDetails.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.func,
navigate: PropTypes.func,
2018-03-18 00:09:33 +01:00
state: PropTypes.shape({
params: PropTypes.shape({
secret: PropTypes.string,
2018-03-18 00:09:33 +01:00
}),
}),
}),
};