BlueWallet/screen/send/confirm.js

247 lines
8.3 KiB
JavaScript
Raw Normal View History

/* global alert */
import React, { Component } from 'react';
import { ActivityIndicator, FlatList, TouchableOpacity, StyleSheet, View } from 'react-native';
import { Text } from 'react-native-elements';
import { BlueButton, BlueText, SafeBlueArea, BlueCard, BlueSpacing40, BlueNavigationStyle } from '../../BlueComponents';
import { BitcoinUnit } from '../../models/bitcoinUnits';
import PropTypes from 'prop-types';
2019-03-05 02:04:40 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import Biometric from '../../class/biometrics';
2019-10-05 12:11:54 +02:00
import { HDSegwitBech32Wallet } from '../../class';
let loc = require('../../loc');
let EV = require('../../events');
let currency = require('../../currency');
let BlueElectrum = require('../../BlueElectrum');
let Bignumber = require('bignumber.js');
/** @type {AppStorage} */
const BlueApp = require('../../BlueApp');
export default class Confirm extends Component {
static navigationOptions = () => ({
...BlueNavigationStyle(null, false),
title: loc.send.confirm.header,
});
constructor(props) {
super(props);
this.state = {
isLoading: false,
fee: props.navigation.getParam('fee'),
feeSatoshi: new Bignumber(props.navigation.getParam('fee')).multipliedBy(100000000).toNumber(),
memo: props.navigation.getParam('memo'),
recipients: props.navigation.getParam('recipients'),
size: Math.round(props.navigation.getParam('tx').length / 2),
tx: props.navigation.getParam('tx'),
satoshiPerByte: props.navigation.getParam('satoshiPerByte'),
fromWallet: props.navigation.getParam('fromWallet'),
};
}
async componentDidMount() {
console.log('send/confirm - componentDidMount');
2019-09-16 23:11:22 +02:00
console.log('address = ', this.state.recipients);
}
broadcast() {
this.setState({ isLoading: true }, async () => {
2019-02-28 03:50:31 +01:00
try {
2019-07-13 17:21:03 +02:00
await BlueElectrum.ping();
await BlueElectrum.waitTillConnected();
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
if (isBiometricsEnabled) {
if (!(await Biometric.unlockWithBiometrics())) {
return;
}
}
2019-02-28 03:50:31 +01:00
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
if (result && result.code) {
if (result.code === 1) {
const message = result.message.split('\n');
throw new Error(`${message[0]}: ${message[2]}`);
}
} else {
console.log('broadcast result = ', result);
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
let amount = 0;
2019-09-16 23:11:22 +02:00
const recipients = this.state.recipients;
if (recipients[0].amount === BitcoinUnit.MAX) {
amount = this.state.fromWallet.getBalance() - this.state.feeSatoshi;
} else {
for (const recipient of recipients) {
2019-09-27 13:36:08 +02:00
amount += recipient.amount ? +recipient.amount : recipient.value;
2019-09-16 23:11:22 +02:00
}
}
2019-09-16 23:11:22 +02:00
2019-10-05 12:11:54 +02:00
if (this.state.fromWallet.type === HDSegwitBech32Wallet.type) {
amount = loc.formatBalanceWithoutSuffix(amount, BitcoinUnit.BTC, false);
}
2019-02-28 03:50:31 +01:00
this.props.navigation.navigate('Success', {
fee: Number(this.state.fee),
amount,
2019-02-28 03:50:31 +01:00
dismissModal: () => this.props.navigation.dismiss(),
});
this.setState({ isLoading: false });
2019-02-28 03:50:31 +01:00
}
} catch (error) {
2019-05-03 14:36:11 +02:00
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
2019-02-28 03:50:31 +01:00
this.setState({ isLoading: false });
alert(error.message);
}
});
}
_renderItem = ({ index, item }) => {
return (
<>
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
<Text
style={{
color: '#0f5cc0',
fontSize: 36,
fontWeight: '600',
}}
>
{item.amount === BitcoinUnit.MAX
? currency.satoshiToBTC(this.state.fromWallet.getBalance() - this.state.feeSatoshi)
2019-09-27 13:36:08 +02:00
: item.amount || currency.satoshiToBTC(item.value)}
</Text>
<Text
style={{
color: '#0f5cc0',
fontSize: 16,
marginHorizontal: 4,
paddingBottom: 6,
fontWeight: '600',
alignSelf: 'flex-end',
}}
>
{' ' + BitcoinUnit.BTC}
</Text>
</View>
<BlueCard>
<Text style={styles.transactionDetailsTitle}>{loc.send.create.to}</Text>
<Text style={styles.transactionDetailsSubtitle}>{item.address}</Text>
</BlueCard>
{this.state.recipients.length > 1 && (
<BlueText style={{ alignSelf: 'flex-end', marginRight: 18, marginVertical: 8 }}>
{index + 1} of {this.state.recipients.length}
</BlueText>
)}
</>
);
};
renderSeparator = () => {
return <View style={{ backgroundColor: BlueApp.settings.inputBorderColor, height: 0.5, margin: 16 }} />;
};
render() {
return (
<SafeBlueArea style={{ flex: 1, paddingTop: 19 }}>
<View style={{ marginTop: 16, alignItems: 'center', justifyContent: 'space-between' }}>
<FlatList
scrollEnabled={this.state.recipients.length > 1}
extraData={this.state.recipients}
data={this.state.recipients}
renderItem={this._renderItem}
keyExtractor={(_item, index) => `${index}`}
ItemSeparatorComponent={this.renderSeparator}
style={{ maxHeight: '55%' }}
/>
<View style={{ flexDirection: 'row', justifyContent: 'center', paddingTop: 16, paddingBottom: 16 }}>
<BlueCard>
<Text
style={{
color: '#37c0a1',
fontSize: 14,
marginHorizontal: 4,
paddingBottom: 6,
fontWeight: '500',
alignSelf: 'center',
}}
>
{loc.send.create.fee}: {loc.formatBalance(this.state.feeSatoshi, BitcoinUnit.BTC)} (
{currency.satoshiToLocalCurrency(this.state.feeSatoshi)})
</Text>
<BlueSpacing40 />
{this.state.isLoading ? (
<ActivityIndicator />
) : (
<BlueButton onPress={() => this.broadcast()} title={loc.send.confirm.sendNow} />
)}
<TouchableOpacity
style={{ marginVertical: 24 }}
onPress={async () => {
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
if (isBiometricsEnabled) {
if (!(await Biometric.unlockWithBiometrics())) {
return;
}
}
this.props.navigation.navigate('CreateTransaction', {
fee: this.state.fee,
recipients: this.state.recipients,
memo: this.state.memo,
tx: this.state.tx,
satoshiPerByte: this.state.satoshiPerByte,
wallet: this.state.fromWallet,
feeSatoshi: this.state.feeSatoshi,
});
}}
>
<Text style={{ color: '#0c2550', fontSize: 15, fontWeight: '500', alignSelf: 'center' }}>
{loc.transactions.details.transaction_details}
</Text>
</TouchableOpacity>
</BlueCard>
</View>
</View>
</SafeBlueArea>
);
}
}
const styles = StyleSheet.create({
transactionDetailsTitle: {
color: '#0c2550',
fontWeight: '500',
fontSize: 17,
marginBottom: 2,
},
transactionDetailsSubtitle: {
color: '#9aa0aa',
fontWeight: '500',
fontSize: 15,
marginBottom: 20,
},
});
Confirm.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.func,
getParam: PropTypes.func,
navigate: PropTypes.func,
dismiss: PropTypes.func,
state: PropTypes.shape({
params: PropTypes.shape({
amount: PropTypes.string,
fee: PropTypes.number,
address: PropTypes.string,
memo: PropTypes.string,
fromWallet: PropTypes.shape({
fromAddress: PropTypes.string,
fromSecret: PropTypes.string,
}),
}),
}),
}),
};