BlueWallet/screen/send/confirm.js

331 lines
10 KiB
JavaScript
Raw Normal View History

/* global alert */
import React, { Component } from 'react';
2020-09-21 21:32:20 +02:00
import { ActivityIndicator, FlatList, TouchableOpacity, StyleSheet, Switch, View } from 'react-native';
import { Text } from 'react-native-elements';
2020-09-21 21:32:20 +02:00
import { PayjoinClient } from 'payjoin-client';
import PayjoinTransaction from '../../class/payjoin-transaction';
2020-12-04 14:39:47 +01:00
import { BlueButton, BlueText, SafeBlueArea, BlueCard, BlueSpacing40, BlueNavigationStyle } from '../../BlueComponents';
import { BitcoinUnit } from '../../models/bitcoinUnits';
2020-12-04 14:39:47 +01:00
import PropTypes from 'prop-types';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import Biometric from '../../class/biometrics';
2020-07-20 15:38:46 +02:00
import loc, { formatBalance, formatBalanceWithoutSuffix } from '../../loc';
2020-07-15 19:32:59 +02:00
import { BlueCurrentTheme } from '../../components/themes';
import Notifications from '../../blue_modules/notifications';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2020-06-09 16:08:18 +02:00
const currency = require('../../blue_modules/currency');
const BlueElectrum = require('../../blue_modules/BlueElectrum');
const Bignumber = require('bignumber.js');
const bitcoin = require('bitcoinjs-lib');
export default class Confirm extends Component {
static contextType = BlueStorageContext;
constructor(props) {
super(props);
this.state = {
isLoading: false,
2020-09-21 21:32:20 +02:00
isPayjoinEnabled: false,
payjoinUrl: props.route.params.fromWallet.allowPayJoin() ? props.route.params?.payjoinUrl : false,
psbt: props.route.params?.psbt,
fee: props.route.params?.fee,
2020-05-27 13:12:17 +02:00
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,
};
}
async componentDidMount() {
console.log('send/confirm - componentDidMount');
2019-09-16 23:11:22 +02:00
console.log('address = ', this.state.recipients);
if (!this.state.recipients || !this.state.recipients.length) alert('Internal error: recipients list empty (this should never happen)');
2019-10-06 13:40:21 +02:00
this.isBiometricUseCapableAndEnabled = await Biometric.isBiometricUseCapableAndEnabled();
}
2020-11-24 17:10:38 +01:00
/**
* we need to look into `recipients`, find destination address and return its outputScript
* (needed for payjoin)
*
* @return {string}
*/
getPaymentScript() {
for (const recipient of this.state.recipients) {
return bitcoin.address.toOutputScript(recipient.address);
}
}
2020-09-21 21:32:20 +02:00
send() {
this.setState({ isLoading: true }, async () => {
2019-02-28 03:50:31 +01:00
try {
2020-09-21 21:32:20 +02:00
const txids2watch = [];
if (!this.state.isPayjoinEnabled) {
await this.broadcast(this.state.tx);
} else {
const wallet = new PayjoinTransaction(this.state.psbt, txHex => this.broadcast(txHex), this.state.fromWallet);
2020-11-24 17:10:38 +01:00
const paymentScript = this.getPaymentScript();
2020-09-21 21:32:20 +02:00
const payjoinClient = new PayjoinClient({
2020-11-24 17:10:38 +01:00
paymentScript,
2020-09-21 21:32:20 +02:00
wallet,
payjoinUrl: this.state.payjoinUrl,
});
await payjoinClient.run();
const payjoinPsbt = wallet.getPayjoinPsbt();
if (payjoinPsbt) {
const tx = payjoinPsbt.extractTransaction();
txids2watch.push(tx.getId());
}
}
2020-09-21 21:32:20 +02:00
const txid = bitcoin.Transaction.fromHex(this.state.tx).getId();
txids2watch.push(txid);
Notifications.majorTomToGroundControl([], [], txids2watch);
2020-09-21 21:32:20 +02:00
let amount = 0;
const recipients = this.state.recipients;
for (const recipient of recipients) {
amount += recipient.value;
2020-09-21 21:32:20 +02:00
}
2019-10-05 12:11:54 +02:00
amount = formatBalanceWithoutSuffix(amount, BitcoinUnit.BTC, false);
2020-09-21 21:32:20 +02:00
this.props.navigation.navigate('Success', {
fee: Number(this.state.fee),
amount,
});
this.setState({ isLoading: false });
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep to make sure network propagates
this.context.fetchAndSaveWalletTransactions(this.state.fromWallet.getID());
2019-02-28 03:50:31 +01:00
} catch (error) {
ReactNativeHapticFeedback.trigger('notificationError', {
ignoreAndroidSystemSettings: false,
});
2019-02-28 03:50:31 +01:00
this.setState({ isLoading: false });
alert(error.message);
}
});
}
2020-09-21 21:32:20 +02:00
async broadcast(tx) {
await BlueElectrum.ping();
await BlueElectrum.waitTillConnected();
if (this.isBiometricUseCapableAndEnabled) {
if (!(await Biometric.unlockWithBiometrics())) {
return;
}
}
const result = await this.state.fromWallet.broadcastTx(tx);
if (!result) {
throw new Error(loc.errors.broadcast);
}
return result;
}
_renderItem = ({ index, item }) => {
return (
<>
<View style={styles.valueWrap}>
<Text testID="TransactionValue" style={styles.valueValue}>
{currency.satoshiToBTC(item.value)}
</Text>
<Text style={styles.valueUnit}>{' ' + BitcoinUnit.BTC}</Text>
</View>
<Text style={styles.transactionAmountFiat}>{currency.satoshiToLocalCurrency(item.value)}</Text>
<BlueCard>
2020-07-20 15:38:46 +02:00
<Text style={styles.transactionDetailsTitle}>{loc.send.create_to}</Text>
2020-08-11 20:16:24 +02:00
<Text testID="TransactionAddress" style={styles.transactionDetailsSubtitle}>
{item.address}
</Text>
</BlueCard>
{this.state.recipients.length > 1 && (
<BlueText style={styles.valueOf}>
2020-07-20 15:38:46 +02:00
{loc.formatString(loc._.of, { number: index + 1, total: this.state.recipients.length })}
</BlueText>
)}
</>
);
};
renderSeparator = () => {
return <View style={styles.separator} />;
};
render() {
return (
<SafeBlueArea style={styles.root}>
<View style={styles.rootWrap}>
<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={styles.flat}
/>
<View style={styles.cardContainer}>
<BlueCard>
<Text style={styles.cardText}>
2020-07-20 15:38:46 +02:00
{loc.send.create_fee}: {formatBalance(this.state.feeSatoshi, BitcoinUnit.BTC)} (
{currency.satoshiToLocalCurrency(this.state.feeSatoshi)})
</Text>
<BlueSpacing40 />
2020-09-21 21:32:20 +02:00
{!!this.state.payjoinUrl && (
<View style={styles.payjoinWrapper}>
<Text style={styles.payjoinText}>Payjoin</Text>
<Switch
testID="PayjoinSwitch"
value={this.state.isPayjoinEnabled}
onValueChange={isPayjoinEnabled => this.setState({ isPayjoinEnabled })}
/>
2020-09-21 21:32:20 +02:00
</View>
)}
2020-09-21 21:32:20 +02:00
{this.state.isLoading ? <ActivityIndicator /> : <BlueButton onPress={() => this.send()} title={loc.send.confirm_sendNow} />}
<TouchableOpacity
testID="TransactionDetailsButton"
style={styles.txDetails}
onPress={async () => {
2019-10-06 13:40:21 +02:00
if (this.isBiometricUseCapableAndEnabled) {
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,
});
}}
>
2020-07-20 15:38:46 +02:00
<Text style={styles.txText}>{loc.transactions.details_transaction_details}</Text>
</TouchableOpacity>
</BlueCard>
</View>
</View>
</SafeBlueArea>
);
}
}
const styles = StyleSheet.create({
transactionDetailsTitle: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.foregroundColor,
fontWeight: '500',
fontSize: 17,
marginBottom: 2,
},
transactionDetailsSubtitle: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.feeText,
fontWeight: '500',
fontSize: 15,
marginBottom: 20,
},
2020-06-09 16:08:18 +02:00
transactionAmountFiat: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.feeText,
2020-06-09 16:08:18 +02:00
fontWeight: '500',
fontSize: 15,
marginVertical: 20,
textAlign: 'center',
},
valueWrap: {
flexDirection: 'row',
justifyContent: 'center',
},
valueValue: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.alternativeTextColor2,
fontSize: 36,
fontWeight: '600',
},
valueUnit: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.alternativeTextColor2,
fontSize: 16,
marginHorizontal: 4,
paddingBottom: 6,
fontWeight: '600',
alignSelf: 'flex-end',
},
valueOf: {
alignSelf: 'flex-end',
marginRight: 18,
marginVertical: 8,
},
separator: {
height: 0.5,
margin: 16,
},
root: {
flex: 1,
paddingTop: 19,
2020-07-15 19:32:59 +02:00
backgroundColor: BlueCurrentTheme.colors.elevated,
},
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: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.buttonTextColor,
fontSize: 15,
fontWeight: '500',
alignSelf: 'center',
},
2020-09-21 21:32:20 +02:00
payjoinWrapper: {
flexDirection: 'row',
marginHorizontal: 20,
marginBottom: 10,
justifyContent: 'space-between',
alignItems: 'center',
},
payjoinText: { color: '#81868e', fontSize: 14 },
});
Confirm.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.func,
2020-09-21 21:32:20 +02:00
dismiss: PropTypes.func,
navigate: PropTypes.func,
2020-05-27 13:12:17 +02:00
dangerouslyGetParent: PropTypes.func,
}),
route: PropTypes.shape({
params: PropTypes.object,
}),
};
2020-07-15 19:32:59 +02:00
2020-12-04 14:39:47 +01:00
Confirm.navigationOptions = () => ({
...BlueNavigationStyle(null, false),
2020-07-20 15:38:46 +02:00
title: loc.send.confirm_header,
2020-07-15 19:32:59 +02:00
});