2018-10-27 17:13:09 +02:00
|
|
|
/* global alert */
|
|
|
|
import React, { Component } from 'react';
|
2019-09-03 05:28:52 +02:00
|
|
|
import { ActivityIndicator, FlatList, TouchableOpacity, StyleSheet, View } from 'react-native';
|
2018-10-27 17:13:09 +02:00
|
|
|
import { Text } from 'react-native-elements';
|
2019-09-03 05:28:52 +02:00
|
|
|
import { BlueButton, BlueText, SafeBlueArea, BlueCard, BlueSpacing40, BlueNavigationStyle } from '../../BlueComponents';
|
2018-10-27 17:13:09 +02:00
|
|
|
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';
|
2019-09-25 05:42:21 +02:00
|
|
|
import Biometric from '../../class/biometrics';
|
2020-04-22 17:13:18 +02:00
|
|
|
import {
|
|
|
|
HDLegacyElectrumSeedP2PKHWallet,
|
|
|
|
HDLegacyP2PKHWallet,
|
|
|
|
HDSegwitBech32Wallet,
|
|
|
|
HDSegwitP2SHWallet,
|
|
|
|
HDLegacyBreadwalletWallet,
|
|
|
|
LegacyWallet,
|
|
|
|
SegwitP2SHWallet,
|
|
|
|
SegwitBech32Wallet,
|
|
|
|
} from '../../class';
|
2020-06-01 14:54:23 +02:00
|
|
|
const loc = require('../../loc');
|
|
|
|
const EV = require('../../events');
|
|
|
|
const currency = require('../../currency');
|
|
|
|
const BlueElectrum = require('../../BlueElectrum');
|
|
|
|
const Bignumber = require('bignumber.js');
|
2019-09-03 05:28:52 +02:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
const BlueApp = require('../../BlueApp');
|
2018-10-27 17:13:09 +02:00
|
|
|
|
|
|
|
export default class Confirm extends Component {
|
2018-10-29 23:11:48 +01:00
|
|
|
static navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(null, false),
|
|
|
|
title: loc.send.confirm.header,
|
|
|
|
});
|
2018-10-27 17:13:09 +02:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isLoading: false,
|
2020-05-27 13:12:17 +02:00
|
|
|
fee: props.route.params.fee,
|
|
|
|
feeSatoshi: new Bignumber(props.route.params.fee).multipliedBy(100000000).toNumber(),
|
|
|
|
memo: props.route.params.memo,
|
|
|
|
recipients: props.route.params.recipients,
|
|
|
|
size: Math.round(props.route.params.tx.length / 2),
|
|
|
|
tx: props.route.params.tx,
|
|
|
|
satoshiPerByte: props.route.params.satoshiPerByte,
|
|
|
|
fromWallet: props.route.params.fromWallet,
|
2018-10-27 17:13:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2018-12-11 23:52:46 +01:00
|
|
|
console.log('send/confirm - componentDidMount');
|
2019-09-16 23:11:22 +02:00
|
|
|
console.log('address = ', this.state.recipients);
|
2019-10-06 13:40:21 +02:00
|
|
|
this.isBiometricUseCapableAndEnabled = await Biometric.isBiometricUseCapableAndEnabled();
|
2018-10-27 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-07-13 02:22:50 +02:00
|
|
|
await BlueElectrum.waitTillConnected();
|
2019-09-25 05:42:21 +02:00
|
|
|
|
2019-10-06 13:40:21 +02:00
|
|
|
if (this.isBiometricUseCapableAndEnabled) {
|
2019-09-25 05:42:21 +02:00
|
|
|
if (!(await Biometric.unlockWithBiometrics())) {
|
2019-12-28 20:42:59 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-09-25 05:42:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
const result = await this.state.fromWallet.broadcastTx(this.state.tx);
|
2020-04-28 18:27:35 +02:00
|
|
|
if (!result) {
|
|
|
|
throw new Error(`Broadcast failed`);
|
2019-02-28 03:50:31 +01:00
|
|
|
} else {
|
|
|
|
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
|
2019-09-03 05:28:52 +02:00
|
|
|
let amount = 0;
|
2019-09-16 23:11:22 +02:00
|
|
|
const recipients = this.state.recipients;
|
2020-04-22 17:13:18 +02:00
|
|
|
if (recipients[0].amount === BitcoinUnit.MAX || !recipients[0].amount) {
|
2019-09-16 23:11:22 +02:00
|
|
|
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-03 05:28:52 +02:00
|
|
|
}
|
2019-09-16 23:11:22 +02:00
|
|
|
|
2020-04-22 17:13:18 +02:00
|
|
|
// wallets that support new createTransaction() instead of deprecated createTx()
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
HDSegwitBech32Wallet.type,
|
|
|
|
HDSegwitP2SHWallet.type,
|
|
|
|
HDLegacyP2PKHWallet.type,
|
|
|
|
HDLegacyBreadwalletWallet.type,
|
|
|
|
HDLegacyElectrumSeedP2PKHWallet.type,
|
|
|
|
LegacyWallet.type,
|
|
|
|
SegwitP2SHWallet.type,
|
|
|
|
SegwitBech32Wallet.type,
|
|
|
|
].includes(this.state.fromWallet.type)
|
|
|
|
) {
|
2019-10-05 12:11:54 +02:00
|
|
|
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),
|
2019-09-03 05:28:52 +02:00
|
|
|
amount,
|
2020-05-27 13:12:17 +02:00
|
|
|
dismissModal: () => this.props.navigation.dangerouslyGetParent().pop(),
|
2019-02-28 03:50:31 +01:00
|
|
|
});
|
2019-09-03 05:28:52 +02:00
|
|
|
this.setState({ isLoading: false });
|
2019-02-28 03:50:31 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-05-24 11:17:26 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', {
|
|
|
|
ignoreAndroidSystemSettings: false,
|
|
|
|
});
|
2019-02-28 03:50:31 +01:00
|
|
|
this.setState({ isLoading: false });
|
|
|
|
alert(error.message);
|
2018-10-27 17:13:09 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:28:52 +02:00
|
|
|
_renderItem = ({ index, item }) => {
|
2018-10-27 17:13:09 +02:00
|
|
|
return (
|
2019-09-03 05:28:52 +02:00
|
|
|
<>
|
2020-05-24 11:17:26 +02:00
|
|
|
<View style={styles.valueWrap}>
|
2020-06-01 14:54:23 +02:00
|
|
|
<Text testID="TransactionValue" style={styles.valueValue}>
|
2020-04-24 16:03:15 +02:00
|
|
|
{!item.value || item.value === BitcoinUnit.MAX
|
2019-09-03 05:28:52 +02:00
|
|
|
? currency.satoshiToBTC(this.state.fromWallet.getBalance() - this.state.feeSatoshi)
|
2019-09-27 13:36:08 +02:00
|
|
|
: item.amount || currency.satoshiToBTC(item.value)}
|
2019-09-03 05:28:52 +02:00
|
|
|
</Text>
|
2020-05-24 11:17:26 +02:00
|
|
|
<Text style={styles.valueUnit}>{' ' + BitcoinUnit.BTC}</Text>
|
2019-09-03 05:28:52 +02:00
|
|
|
</View>
|
2018-10-27 17:13:09 +02:00
|
|
|
<BlueCard>
|
|
|
|
<Text style={styles.transactionDetailsTitle}>{loc.send.create.to}</Text>
|
2019-09-03 05:28:52 +02:00
|
|
|
<Text style={styles.transactionDetailsSubtitle}>{item.address}</Text>
|
2018-10-27 17:13:09 +02:00
|
|
|
</BlueCard>
|
2019-09-03 05:28:52 +02:00
|
|
|
{this.state.recipients.length > 1 && (
|
2020-05-24 11:17:26 +02:00
|
|
|
<BlueText style={styles.valueOf}>
|
2019-09-03 05:28:52 +02:00
|
|
|
{index + 1} of {this.state.recipients.length}
|
|
|
|
</BlueText>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
renderSeparator = () => {
|
2020-05-24 11:17:26 +02:00
|
|
|
return <View style={styles.separator} />;
|
2019-09-03 05:28:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<SafeBlueArea style={styles.root}>
|
|
|
|
<View style={styles.rootWrap}>
|
2019-09-03 05:28:52 +02:00
|
|
|
<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}
|
2020-05-24 11:17:26 +02:00
|
|
|
style={styles.flat}
|
2019-09-03 05:28:52 +02:00
|
|
|
/>
|
2020-05-24 11:17:26 +02:00
|
|
|
<View style={styles.cardContainer}>
|
2019-09-03 05:28:52 +02:00
|
|
|
<BlueCard>
|
2020-05-24 11:17:26 +02:00
|
|
|
<Text style={styles.cardText}>
|
2019-09-03 05:28:52 +02:00
|
|
|
{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
|
2020-06-01 14:54:23 +02:00
|
|
|
testID="TransactionDetailsButton"
|
2020-05-24 11:17:26 +02:00
|
|
|
style={styles.txDetails}
|
2019-09-25 05:42:21 +02:00
|
|
|
onPress={async () => {
|
2019-10-06 13:40:21 +02:00
|
|
|
if (this.isBiometricUseCapableAndEnabled) {
|
2019-09-25 05:42:21 +02:00
|
|
|
if (!(await Biometric.unlockWithBiometrics())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:28:52 +02:00
|
|
|
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,
|
2019-09-25 05:42:21 +02:00
|
|
|
});
|
|
|
|
}}
|
2019-09-03 05:28:52 +02:00
|
|
|
>
|
2020-05-24 11:17:26 +02:00
|
|
|
<Text style={styles.txText}>{loc.transactions.details.transaction_details}</Text>
|
2019-09-03 05:28:52 +02:00
|
|
|
</TouchableOpacity>
|
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
</View>
|
2018-10-27 17:13:09 +02:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
transactionDetailsTitle: {
|
|
|
|
color: '#0c2550',
|
|
|
|
fontWeight: '500',
|
|
|
|
fontSize: 17,
|
|
|
|
marginBottom: 2,
|
|
|
|
},
|
|
|
|
transactionDetailsSubtitle: {
|
|
|
|
color: '#9aa0aa',
|
|
|
|
fontWeight: '500',
|
|
|
|
fontSize: 15,
|
|
|
|
marginBottom: 20,
|
|
|
|
},
|
2020-05-24 11:17:26 +02:00
|
|
|
valueWrap: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
valueValue: {
|
|
|
|
color: '#0f5cc0',
|
|
|
|
fontSize: 36,
|
|
|
|
fontWeight: '600',
|
|
|
|
},
|
|
|
|
valueUnit: {
|
|
|
|
color: '#0f5cc0',
|
|
|
|
fontSize: 16,
|
|
|
|
marginHorizontal: 4,
|
|
|
|
paddingBottom: 6,
|
|
|
|
fontWeight: '600',
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
},
|
|
|
|
valueOf: {
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
marginRight: 18,
|
|
|
|
marginVertical: 8,
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
backgroundColor: BlueApp.settings.inputBorderColor,
|
|
|
|
height: 0.5,
|
|
|
|
margin: 16,
|
|
|
|
},
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 19,
|
|
|
|
},
|
|
|
|
rootWrap: {
|
|
|
|
marginTop: 16,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
|
|
|
flat: {
|
|
|
|
maxHeight: '55%',
|
|
|
|
},
|
|
|
|
cardContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingTop: 16,
|
|
|
|
paddingBottom: 16,
|
|
|
|
},
|
|
|
|
cardText: {
|
|
|
|
color: '#37c0a1',
|
|
|
|
fontSize: 14,
|
|
|
|
marginHorizontal: 4,
|
|
|
|
paddingBottom: 6,
|
|
|
|
fontWeight: '500',
|
|
|
|
alignSelf: 'center',
|
|
|
|
},
|
|
|
|
txDetails: {
|
|
|
|
marginVertical: 24,
|
|
|
|
},
|
|
|
|
txText: {
|
|
|
|
color: '#0c2550',
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: '500',
|
|
|
|
alignSelf: 'center',
|
|
|
|
},
|
2018-10-27 17:13:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Confirm.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
2019-02-01 16:28:43 +01:00
|
|
|
goBack: PropTypes.func,
|
|
|
|
navigate: PropTypes.func,
|
2020-05-27 13:12:17 +02:00
|
|
|
dangerouslyGetParent: PropTypes.func,
|
|
|
|
}),
|
|
|
|
route: PropTypes.shape({
|
|
|
|
params: PropTypes.object,
|
2018-10-27 17:13:09 +02:00
|
|
|
}),
|
|
|
|
};
|