BlueWallet/screen/send/details.js

262 lines
7.5 KiB
JavaScript
Raw Normal View History

2018-01-30 22:42:38 +00:00
import React, { Component } from 'react';
2018-08-18 23:10:12 +01:00
import { ActivityIndicator, View } from 'react-native';
2018-03-18 02:48:23 +00:00
import { Text, FormValidationMessage } from 'react-native-elements';
2018-03-17 22:39:21 +02:00
import {
BlueSpacing20,
2018-08-18 23:10:12 +01:00
BlueHeaderDefaultSub,
2018-03-17 22:39:21 +02:00
BlueButton,
SafeBlueArea,
BlueText,
BlueFormInput,
2018-05-06 18:12:14 +01:00
BlueFormInputAddress,
2018-03-17 22:39:21 +02:00
} from '../../BlueComponents';
2018-03-18 02:48:23 +00:00
import PropTypes from 'prop-types';
2018-03-24 21:24:20 +00:00
const bip21 = require('bip21');
2018-03-17 22:39:21 +02:00
let EV = require('../../events');
2018-01-30 22:42:38 +00:00
let BigNumber = require('bignumber.js');
2018-08-08 01:05:34 +01:00
/** @type {AppStorage} */
2018-03-18 02:48:23 +00:00
let BlueApp = require('../../BlueApp');
2018-05-28 20:18:11 +01:00
let loc = require('../../loc');
2018-01-30 22:42:38 +00:00
2018-03-24 21:24:20 +00:00
const btcAddressRx = /^[a-zA-Z0-9]{26,35}$/;
2018-03-20 02:49:32 +02:00
2018-01-30 22:42:38 +00:00
export default class SendDetails extends Component {
static navigationOptions = {
header: ({ navigation }) => {
return <BlueHeaderDefaultSub leftText={loc.send.details.title} onClose={() => navigation.goBack(null)} />;
},
};
2018-01-30 22:42:38 +00:00
constructor(props) {
super(props);
console.log('props.navigation.state.params=', props.navigation.state.params);
2018-03-17 22:39:21 +02:00
let startTime = Date.now();
let address;
2018-07-07 14:04:32 +01:00
if (props.navigation.state.params) address = props.navigation.state.params.address;
let memo = false;
if (props.navigation.state.params) memo = props.navigation.state.params.memo;
2018-03-17 22:39:21 +02:00
let fromAddress;
2018-07-07 14:04:32 +01:00
if (props.navigation.state.params) fromAddress = props.navigation.state.params.fromAddress;
2018-08-08 01:05:34 +01:00
let fromSecret;
if (props.navigation.state.params.fromSecret) fromSecret = props.navigation.state.params.fromSecret;
2018-03-17 22:39:21 +02:00
let fromWallet = {};
let startTime2 = Date.now();
2018-01-30 22:42:38 +00:00
for (let w of BlueApp.getWallets()) {
2018-08-08 01:05:34 +01:00
if (w.getSecret() === fromSecret) {
fromWallet = w;
break;
}
2018-01-30 22:42:38 +00:00
if (w.getAddress() === fromAddress) {
2018-03-17 22:39:21 +02:00
fromWallet = w;
2018-01-30 22:42:38 +00:00
}
}
2018-03-17 22:39:21 +02:00
let endTime2 = Date.now();
console.log('getAddress() took', (endTime2 - startTime2) / 1000, 'sec');
console.log({ memo });
2018-01-30 22:42:38 +00:00
this.state = {
errorMessage: false,
2018-01-30 22:42:38 +00:00
fromAddress: fromAddress,
fromWallet: fromWallet,
2018-08-08 01:05:34 +01:00
fromSecret: fromSecret,
2018-01-30 22:42:38 +00:00
isLoading: true,
2018-03-17 22:39:21 +02:00
address: address,
2018-01-30 22:42:38 +00:00
amount: '',
memo,
2018-01-30 22:42:38 +00:00
fee: '',
2018-03-17 22:39:21 +02:00
};
2018-01-30 22:42:38 +00:00
2018-03-17 22:39:21 +02:00
EV(EV.enum.CREATE_TRANSACTION_NEW_DESTINATION_ADDRESS, data => {
console.log('received event with ', data);
2018-03-20 02:49:32 +02:00
if (btcAddressRx.test(data)) {
this.setState({
address: data,
2018-03-24 21:24:20 +00:00
});
2018-03-20 02:49:32 +02:00
} else {
2018-03-24 21:24:20 +00:00
const { address, options } = bip21.decode(data);
2018-03-20 02:49:32 +02:00
if (btcAddressRx.test(address)) {
this.setState({
address,
amount: options.amount,
2018-03-24 21:24:20 +00:00
memo: options.label,
});
2018-03-20 02:49:32 +02:00
}
}
2018-03-17 22:39:21 +02:00
});
let endTime = Date.now();
console.log('constructor took', (endTime - startTime) / 1000, 'sec');
2018-01-30 22:42:38 +00:00
}
async componentDidMount() {
2018-03-17 22:39:21 +02:00
let startTime = Date.now();
console.log('send/details - componentDidMount');
2018-01-30 22:42:38 +00:00
this.setState({
isLoading: false,
2018-03-17 22:39:21 +02:00
});
let endTime = Date.now();
console.log('componentDidMount took', (endTime - startTime) / 1000, 'sec');
2018-01-30 22:42:38 +00:00
}
recalculateAvailableBalance(balance, amount, fee) {
2018-03-17 22:39:21 +02:00
if (!amount) amount = 0;
if (!fee) fee = 0;
let availableBalance;
2018-01-30 22:42:38 +00:00
try {
2018-03-17 22:39:21 +02:00
availableBalance = new BigNumber(balance);
availableBalance = availableBalance.sub(amount);
availableBalance = availableBalance.sub(fee);
availableBalance = availableBalance.toString(10);
2018-01-30 22:42:38 +00:00
} catch (err) {
2018-03-17 22:39:21 +02:00
return balance;
2018-01-30 22:42:38 +00:00
}
2018-03-17 22:39:21 +02:00
return (availableBalance === 'NaN' && balance) || availableBalance;
2018-01-30 22:42:38 +00:00
}
createTransaction() {
2018-05-28 20:18:11 +01:00
if (!this.state.amount || this.state.amount === '0') {
this.setState({
2018-05-28 20:18:11 +01:00
errorMessage: loc.send.details.amount_fiels_is_not_valid,
2018-03-17 22:39:21 +02:00
});
console.log('validation error');
return;
}
if (!this.state.fee) {
this.setState({
2018-05-28 20:18:11 +01:00
errorMessage: loc.send.details.fee_fiels_is_not_valid,
2018-03-17 22:39:21 +02:00
});
console.log('validation error');
return;
}
if (!this.state.address) {
this.setState({
2018-05-28 20:18:11 +01:00
errorMessage: loc.send.details.address_fiels_is_not_valid,
2018-03-17 22:39:21 +02:00
});
console.log('validation error');
return;
}
if (this.recalculateAvailableBalance(this.state.fromWallet.getBalance(), this.state.amount, this.state.fee) < 0) {
this.setState({
errorMessage: loc.send.details.amount_fiels_is_not_valid,
});
console.log('validation error');
return;
}
this.setState({
2018-03-17 22:39:21 +02:00
errorMessage: '',
});
2018-01-30 22:42:38 +00:00
this.props.navigation.navigate('CreateTransaction', {
2018-03-17 22:39:21 +02:00
amount: this.state.amount,
2018-01-30 22:42:38 +00:00
fee: this.state.fee,
address: this.state.address,
memo: this.state.memo,
2018-03-17 22:39:21 +02:00
fromAddress: this.state.fromAddress,
2018-08-08 01:05:34 +01:00
fromSecret: this.state.fromSecret,
2018-03-17 22:39:21 +02:00
});
2018-01-30 22:42:38 +00:00
}
render() {
if (this.state.isLoading) {
return (
2018-03-17 22:39:21 +02:00
<View style={{ flex: 1, paddingTop: 20 }}>
2018-01-30 22:42:38 +00:00
<ActivityIndicator />
</View>
);
}
if (!this.state.fromWallet.getAddress) {
return (
2018-03-17 22:39:21 +02:00
<View style={{ flex: 1, paddingTop: 20 }}>
2018-07-07 14:04:32 +01:00
<Text>System error: Source wallet not found (this should never happen)</Text>
2018-01-30 22:42:38 +00:00
</View>
);
}
return (
<SafeBlueArea style={{ flex: 1 }}>
<View>
2018-05-06 18:12:14 +01:00
<BlueFormInputAddress
2018-03-17 22:39:21 +02:00
onChangeText={text => this.setState({ address: text })}
2018-05-28 20:18:11 +01:00
placeholder={loc.send.details.receiver_placeholder}
2018-03-17 22:39:21 +02:00
value={this.state.address}
2018-01-30 22:42:38 +00:00
/>
2018-03-17 22:39:21 +02:00
<BlueFormInput
2018-07-07 14:04:32 +01:00
onChangeText={text => this.setState({ amount: text.replace(',', '.') })}
2018-03-17 22:39:21 +02:00
keyboardType={'numeric'}
2018-05-28 20:18:11 +01:00
placeholder={loc.send.details.amount_placeholder}
2018-03-17 22:39:21 +02:00
value={this.state.amount + ''}
2018-01-30 22:42:38 +00:00
/>
2018-03-17 22:39:21 +02:00
<BlueFormInput
2018-07-07 14:04:32 +01:00
onChangeText={text => this.setState({ fee: text.replace(',', '.') })}
2018-03-17 22:39:21 +02:00
keyboardType={'numeric'}
2018-05-28 20:18:11 +01:00
placeholder={loc.send.details.fee_placeholder}
2018-03-17 22:39:21 +02:00
value={this.state.fee + ''}
2018-01-30 22:42:38 +00:00
/>
2018-03-17 22:39:21 +02:00
<BlueFormInput
onChangeText={text => this.setState({ memo: text })}
2018-05-28 20:18:11 +01:00
placeholder={loc.send.details.memo_placeholder}
2018-03-17 22:39:21 +02:00
value={this.state.memo}
2018-01-30 22:42:38 +00:00
/>
2018-03-17 22:39:21 +02:00
<BlueSpacing20 />
<BlueText style={{ paddingLeft: 20 }}>
2018-05-28 20:18:11 +01:00
{loc.send.details.remaining_balance}:{' '}
{this.recalculateAvailableBalance(this.state.fromWallet.getBalance(), this.state.amount, this.state.fee)} BTC
2018-03-17 22:39:21 +02:00
</BlueText>
</View>
2018-01-30 22:42:38 +00:00
<FormValidationMessage>{this.state.errorMessage}</FormValidationMessage>
2018-05-12 21:27:34 +01:00
<View style={{ flex: 1, flexDirection: 'row' }}>
2018-03-17 22:39:21 +02:00
<View style={{ flex: 0.33 }}>
2018-07-07 14:04:32 +01:00
<BlueButton onPress={() => this.props.navigation.goBack()} title={loc.send.details.cancel} />
2018-01-30 22:42:38 +00:00
</View>
2018-03-17 22:39:21 +02:00
<View style={{ flex: 0.33 }}>
<BlueButton
2018-06-24 23:22:46 +01:00
icon={{
name: 'qrcode',
type: 'font-awesome',
color: BlueApp.settings.buttonTextColor,
}}
2018-03-17 22:39:21 +02:00
style={{}}
2018-05-28 20:18:11 +01:00
title={loc.send.details.scan}
2018-03-17 22:39:21 +02:00
onPress={() => this.props.navigation.navigate('ScanQrAddress')}
2018-01-30 22:42:38 +00:00
/>
</View>
2018-03-17 22:39:21 +02:00
<View style={{ flex: 0.33 }}>
2018-07-07 14:04:32 +01:00
<BlueButton onPress={() => this.createTransaction()} title={loc.send.details.create} />
2018-01-30 22:42:38 +00:00
</View>
</View>
</SafeBlueArea>
);
}
}
2018-03-18 02:48:23 +00:00
SendDetails.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.function,
navigate: PropTypes.func,
state: PropTypes.shape({
params: PropTypes.shape({
address: PropTypes.string,
fromAddress: PropTypes.string,
2018-08-08 01:05:34 +01:00
fromSecret: PropTypes.string,
memo: PropTypes.string,
2018-03-18 02:48:23 +00:00
}),
}),
}),
};