2024-05-20 11:54:13 +02:00
|
|
|
import React, { Component } from 'react';
|
2021-02-25 02:56:06 +01:00
|
|
|
import {
|
|
|
|
ActivityIndicator,
|
2024-05-20 11:54:13 +02:00
|
|
|
KeyboardAvoidingView,
|
2021-02-25 02:56:06 +01:00
|
|
|
Linking,
|
2024-05-20 11:54:13 +02:00
|
|
|
Platform,
|
2021-02-25 02:56:06 +01:00
|
|
|
ScrollView,
|
|
|
|
StyleSheet,
|
2024-05-20 11:54:13 +02:00
|
|
|
TextInput,
|
|
|
|
TouchableOpacity,
|
|
|
|
View,
|
2021-02-25 02:56:06 +01:00
|
|
|
} from 'react-native';
|
2024-05-31 19:22:22 +02:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-06-12 18:46:44 +02:00
|
|
|
import { Text } from '@rneui/themed';
|
2024-05-20 11:54:13 +02:00
|
|
|
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
|
|
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
|
|
|
|
import Notifications from '../../blue_modules/notifications';
|
2024-06-08 18:18:56 +02:00
|
|
|
import { BlueCard, BlueSpacing, BlueSpacing20, BlueText } from '../../BlueComponents';
|
2019-07-13 17:20:38 +02:00
|
|
|
import { HDSegwitBech32Transaction, HDSegwitBech32Wallet } from '../../class';
|
2024-02-07 20:24:24 +01:00
|
|
|
import presentAlert from '../../components/Alert';
|
2023-11-15 09:40:22 +01:00
|
|
|
import Button from '../../components/Button';
|
2024-05-20 11:54:13 +02:00
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
2023-12-27 07:52:11 +01:00
|
|
|
import SafeArea from '../../components/SafeArea';
|
2024-05-20 11:54:13 +02:00
|
|
|
import { BlueCurrentTheme } from '../../components/themes';
|
|
|
|
import loc from '../../loc';
|
2024-05-31 19:22:22 +02:00
|
|
|
import { StorageContext } from '../../components/Context/StorageProvider';
|
2024-06-06 19:11:08 +02:00
|
|
|
import { popToTop } from '../../NavigationService';
|
2024-06-08 18:18:56 +02:00
|
|
|
import ReplaceFeeSuggestions from '../../components/ReplaceFeeSuggestions';
|
2019-07-13 17:20:38 +02:00
|
|
|
|
2020-05-24 11:17:26 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 20,
|
|
|
|
},
|
|
|
|
explain: {
|
|
|
|
paddingBottom: 16,
|
|
|
|
},
|
|
|
|
center: {
|
|
|
|
alignItems: 'center',
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
hex: {
|
2020-09-14 12:49:08 +02:00
|
|
|
color: BlueCurrentTheme.colors.buttonAlternativeTextColor,
|
2020-05-24 11:17:26 +02:00
|
|
|
fontWeight: '500',
|
|
|
|
},
|
|
|
|
hexInput: {
|
|
|
|
borderColor: '#ebebeb',
|
|
|
|
backgroundColor: '#d2f8d6',
|
|
|
|
borderRadius: 4,
|
|
|
|
marginTop: 20,
|
|
|
|
color: '#37c0a1',
|
|
|
|
fontWeight: '500',
|
|
|
|
fontSize: 14,
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
paddingBottom: 16,
|
|
|
|
paddingTop: 16,
|
|
|
|
},
|
|
|
|
action: {
|
|
|
|
marginVertical: 24,
|
|
|
|
},
|
|
|
|
actionText: {
|
|
|
|
color: '#9aa0aa',
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: '500',
|
|
|
|
alignSelf: 'center',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-13 17:20:38 +02:00
|
|
|
export default class CPFP extends Component {
|
2024-05-31 19:22:22 +02:00
|
|
|
static contextType = StorageContext;
|
2019-07-13 17:20:38 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
let txid;
|
|
|
|
let wallet;
|
2020-05-27 13:12:17 +02:00
|
|
|
if (props.route.params) txid = props.route.params.txid;
|
|
|
|
if (props.route.params) wallet = props.route.params.wallet;
|
2019-07-13 17:20:38 +02:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
stage: 1,
|
|
|
|
txid,
|
|
|
|
wallet,
|
2021-08-24 06:33:32 +02:00
|
|
|
isElectrumDisabled: true,
|
2019-07-13 17:20:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:33:32 +02:00
|
|
|
broadcast = () => {
|
2019-07-13 17:20:38 +02:00
|
|
|
this.setState({ isLoading: true }, async () => {
|
|
|
|
try {
|
|
|
|
await BlueElectrum.ping();
|
|
|
|
await BlueElectrum.waitTillConnected();
|
2020-06-01 14:54:23 +02:00
|
|
|
const result = await this.state.wallet.broadcastTx(this.state.txhex);
|
2019-07-13 17:20:38 +02:00
|
|
|
if (result) {
|
2019-07-14 13:40:31 +02:00
|
|
|
this.onSuccessBroadcast();
|
2019-07-13 17:20:38 +02:00
|
|
|
} else {
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2019-07-13 17:20:38 +02:00
|
|
|
this.setState({ isLoading: false });
|
2024-02-07 20:24:24 +01:00
|
|
|
presentAlert({ message: loc.errors.broadcast });
|
2019-07-13 17:20:38 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2019-07-13 17:20:38 +02:00
|
|
|
this.setState({ isLoading: false });
|
2024-02-07 20:24:24 +01:00
|
|
|
presentAlert({ message: error.message });
|
2019-07-13 17:20:38 +02:00
|
|
|
}
|
|
|
|
});
|
2021-08-24 06:33:32 +02:00
|
|
|
};
|
2019-07-13 17:20:38 +02:00
|
|
|
|
2019-07-14 13:40:31 +02:00
|
|
|
onSuccessBroadcast() {
|
2020-10-24 19:20:59 +02:00
|
|
|
this.context.txMetadata[this.state.newTxid] = { memo: 'Child pays for parent (CPFP)' };
|
|
|
|
Notifications.majorTomToGroundControl([], [], [this.state.newTxid]);
|
2020-12-02 06:45:56 +01:00
|
|
|
this.context.sleep(4000).then(() => this.context.fetchAndSaveWalletTransactions(this.state.wallet.getID()));
|
2024-06-06 19:11:08 +02:00
|
|
|
this.props.navigation.navigate('Success', { onDonePressed: () => popToTop(), amount: undefined });
|
2019-07-14 13:40:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 17:20:38 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
console.log('transactions/CPFP - componentDidMount');
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
|
|
|
newFeeRate: '',
|
|
|
|
nonReplaceable: false,
|
|
|
|
});
|
2021-02-24 19:21:37 +01:00
|
|
|
try {
|
|
|
|
await this.checkPossibilityOfCPFP();
|
|
|
|
} catch (_) {
|
|
|
|
// if anything goes wrong we just show "this is not bumpable" message
|
|
|
|
this.setState({ nonReplaceable: true, isLoading: false });
|
|
|
|
}
|
2019-07-13 17:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async checkPossibilityOfCPFP() {
|
|
|
|
if (this.state.wallet.type !== HDSegwitBech32Wallet.type) {
|
|
|
|
return this.setState({ nonReplaceable: true, isLoading: false });
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
const tx = new HDSegwitBech32Transaction(null, this.state.txid, this.state.wallet);
|
2019-07-14 13:40:31 +02:00
|
|
|
if ((await tx.isToUsTransaction()) && (await tx.getRemoteConfirmationsNum()) === 0) {
|
2020-06-01 14:54:23 +02:00
|
|
|
const info = await tx.getInfo();
|
2019-07-14 13:40:31 +02:00
|
|
|
return this.setState({ nonReplaceable: false, feeRate: info.feeRate + 1, isLoading: false, tx });
|
|
|
|
// 1 sat makes a lot of difference, since sometimes because of rounding created tx's fee might be insufficient
|
2019-07-13 17:20:38 +02:00
|
|
|
} else {
|
|
|
|
return this.setState({ nonReplaceable: true, isLoading: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createTransaction() {
|
2023-07-25 15:50:04 +02:00
|
|
|
const newFeeRate = parseInt(this.state.newFeeRate, 10);
|
2019-07-13 17:20:38 +02:00
|
|
|
if (newFeeRate > this.state.feeRate) {
|
|
|
|
/** @type {HDSegwitBech32Transaction} */
|
|
|
|
const tx = this.state.tx;
|
|
|
|
this.setState({ isLoading: true });
|
|
|
|
try {
|
2020-06-01 14:54:23 +02:00
|
|
|
const { tx: newTx } = await tx.createCPFPbumpFee(newFeeRate);
|
2019-07-14 13:40:31 +02:00
|
|
|
this.setState({ stage: 2, txhex: newTx.toHex(), newTxid: newTx.getId() });
|
2019-07-13 17:20:38 +02:00
|
|
|
this.setState({ isLoading: false });
|
|
|
|
} catch (_) {
|
|
|
|
this.setState({ isLoading: false });
|
2024-02-07 20:24:24 +01:00
|
|
|
presentAlert({ message: loc.errors.error + ': ' + _.message });
|
2019-07-13 17:20:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:49:08 +02:00
|
|
|
renderStage1(text) {
|
2020-05-15 12:41:57 +02:00
|
|
|
return (
|
2021-02-25 02:56:06 +01:00
|
|
|
<KeyboardAvoidingView enabled={!Platform.isPad} behavior="position">
|
2023-12-27 07:52:11 +01:00
|
|
|
<SafeArea style={styles.root}>
|
2020-09-14 12:49:08 +02:00
|
|
|
<BlueSpacing />
|
2020-09-15 15:17:01 +02:00
|
|
|
<BlueCard style={styles.center}>
|
|
|
|
<BlueText>{text}</BlueText>
|
|
|
|
<BlueSpacing20 />
|
2024-06-08 18:18:56 +02:00
|
|
|
<ReplaceFeeSuggestions onFeeSelected={fee => this.setState({ newFeeRate: fee })} transactionMinimum={this.state.feeRate} />
|
2020-09-15 15:17:01 +02:00
|
|
|
<BlueSpacing />
|
2023-11-15 09:40:22 +01:00
|
|
|
<Button
|
2020-09-15 15:17:01 +02:00
|
|
|
disabled={this.state.newFeeRate <= this.state.feeRate}
|
|
|
|
onPress={() => this.createTransaction()}
|
|
|
|
title={loc.transactions.cpfp_create}
|
|
|
|
/>
|
|
|
|
</BlueCard>
|
2023-12-27 07:52:11 +01:00
|
|
|
</SafeArea>
|
2020-09-15 15:17:01 +02:00
|
|
|
</KeyboardAvoidingView>
|
2020-05-15 13:07:08 +02:00
|
|
|
);
|
|
|
|
}
|
2019-07-14 13:40:31 +02:00
|
|
|
|
|
|
|
renderStage2() {
|
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<View style={styles.root}>
|
|
|
|
<BlueCard style={styles.center}>
|
2020-07-20 15:38:46 +02:00
|
|
|
<BlueText style={styles.hex}>{loc.send.create_this_is_hex}</BlueText>
|
2020-05-24 11:17:26 +02:00
|
|
|
<TextInput style={styles.hexInput} height={112} multiline editable value={this.state.txhex} />
|
2019-07-14 13:40:31 +02:00
|
|
|
|
2021-06-24 14:50:57 +02:00
|
|
|
<TouchableOpacity accessibilityRole="button" style={styles.action} onPress={() => Clipboard.setString(this.state.txhex)}>
|
2020-07-20 15:38:46 +02:00
|
|
|
<Text style={styles.actionText}>{loc.send.create_copy}</Text>
|
2019-07-14 13:40:31 +02:00
|
|
|
</TouchableOpacity>
|
2021-06-24 14:50:57 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
accessibilityRole="button"
|
|
|
|
style={styles.action}
|
|
|
|
onPress={() => Linking.openURL('https://coinb.in/?verify=' + this.state.txhex)}
|
|
|
|
>
|
2020-07-20 15:38:46 +02:00
|
|
|
<Text style={styles.actionText}>{loc.send.create_verify}</Text>
|
2019-07-14 13:40:31 +02:00
|
|
|
</TouchableOpacity>
|
2023-11-15 09:40:22 +01:00
|
|
|
<Button disabled={this.context.isElectrumDisabled} onPress={this.broadcast} title={loc.send.confirm_sendNow} />
|
2019-07-14 13:40:31 +02:00
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:49:08 +02:00
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return (
|
|
|
|
<View style={styles.root}>
|
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.stage === 2) {
|
|
|
|
return this.renderStage2();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.nonReplaceable) {
|
|
|
|
return (
|
2023-12-27 07:52:11 +01:00
|
|
|
<SafeArea style={styles.root}>
|
2019-07-13 17:20:38 +02:00
|
|
|
<BlueSpacing20 />
|
2020-09-14 12:49:08 +02:00
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
|
|
|
|
<BlueText h4>{loc.transactions.cpfp_no_bump}</BlueText>
|
2023-12-27 07:52:11 +01:00
|
|
|
</SafeArea>
|
2020-09-14 12:49:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-12-27 07:52:11 +01:00
|
|
|
<SafeArea style={styles.explain}>
|
2020-09-14 12:49:08 +02:00
|
|
|
<ScrollView>{this.renderStage1(loc.transactions.cpfp_exp)}</ScrollView>
|
2023-12-27 07:52:11 +01:00
|
|
|
</SafeArea>
|
2019-07-13 17:20:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CPFP.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
popToTop: PropTypes.func,
|
|
|
|
navigate: PropTypes.func,
|
2020-05-27 13:12:17 +02:00
|
|
|
}),
|
|
|
|
route: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
txid: PropTypes.string,
|
|
|
|
wallet: PropTypes.object,
|
2019-07-13 17:20:38 +02:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|
2021-02-15 09:03:54 +01:00
|
|
|
CPFP.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.transactions.cpfp_title }));
|