You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.3 KiB

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