BlueWallet/screen/send/broadcast.js

233 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-04-28 18:27:35 +02:00
import React, { useState } from 'react';
import PropTypes from 'prop-types';
2021-03-11 04:27:51 +01:00
import { ActivityIndicator, Alert, KeyboardAvoidingView, Linking, StyleSheet, Platform, TextInput, View, Keyboard } from 'react-native';
2020-04-28 18:27:35 +02:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import { useRoute, useTheme, useNavigation } from '@react-navigation/native';
import * as bitcoin from 'bitcoinjs-lib';
2020-12-25 17:09:53 +01:00
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
2020-04-28 18:27:35 +02:00
import { HDSegwitBech32Wallet } from '../../class';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
2020-04-28 18:27:35 +02:00
import {
2020-12-25 17:09:53 +01:00
BlueBigCheckmark,
2020-04-28 18:27:35 +02:00
BlueButton,
2020-12-25 17:09:53 +01:00
BlueButtonLink,
BlueCard,
BlueFormLabel,
2020-04-28 18:27:35 +02:00
BlueSpacing10,
BlueSpacing20,
BlueTextCentered,
2020-12-25 17:09:53 +01:00
SafeBlueArea,
2020-04-28 18:27:35 +02:00
} from '../../BlueComponents';
import BlueElectrum from '../../blue_modules/BlueElectrum';
import Notifications from '../../blue_modules/notifications';
2021-03-11 04:27:51 +01:00
const scanqr = require('../../helpers/scan-qr');
2020-04-28 18:27:35 +02:00
const BROADCAST_RESULT = Object.freeze({
2020-12-08 15:19:26 +01:00
none: 'Input transaction hex',
2020-04-28 18:27:35 +02:00
pending: 'pending',
success: 'success',
error: 'error',
});
2020-07-15 19:32:59 +02:00
const Broadcast = () => {
const { name } = useRoute();
const { navigate } = useNavigation();
const [tx, setTx] = useState();
const [txHex, setTxHex] = useState();
const { colors } = useTheme();
2020-04-28 18:27:35 +02:00
const [broadcastResult, setBroadcastResult] = useState(BROADCAST_RESULT.none);
const stylesHooks = StyleSheet.create({
input: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
});
2020-04-28 18:27:35 +02:00
const handleUpdateTxHex = nextValue => setTxHex(nextValue.trim());
2020-04-28 18:27:35 +02:00
const handleBroadcast = async () => {
2021-03-11 04:27:51 +01:00
Keyboard.dismiss();
2020-04-28 18:27:35 +02:00
setBroadcastResult(BROADCAST_RESULT.pending);
try {
await BlueElectrum.ping();
await BlueElectrum.waitTillConnected();
const walletObj = new HDSegwitBech32Wallet();
const result = await walletObj.broadcastTx(txHex);
if (result) {
const tx = bitcoin.Transaction.fromHex(txHex);
2020-04-28 18:27:35 +02:00
const txid = tx.getId();
setTx(txid);
2020-04-28 18:27:35 +02:00
setBroadcastResult(BROADCAST_RESULT.success);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
Notifications.majorTomToGroundControl([], [], [txid]);
2020-04-28 18:27:35 +02:00
} else {
setBroadcastResult(BROADCAST_RESULT.error);
}
} catch (error) {
2021-03-11 04:27:51 +01:00
Alert.alert(loc.errors.error, error.message);
2020-04-28 18:27:35 +02:00
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
setBroadcastResult(BROADCAST_RESULT.error);
}
};
const handleQRScan = async () => {
const scannedData = await scanqr(navigate, name);
if (!scannedData) return;
if (scannedData.indexOf('+') === -1 && scannedData.indexOf('=') === -1 && scannedData.indexOf('=') === -1) {
// this looks like NOT base64, so maybe its transaction's hex
return handleUpdateTxHex(scannedData);
}
try {
// sould be base64 encoded PSBT
const tx = bitcoin.Psbt.fromBase64(scannedData).extractTransaction();
return handleUpdateTxHex(tx.toHex());
} catch (e) {}
};
2020-07-20 15:38:46 +02:00
let status;
switch (broadcastResult) {
case BROADCAST_RESULT.none:
status = loc.send.broadcastNone;
break;
case BROADCAST_RESULT.pending:
status = loc.send.broadcastPending;
break;
case BROADCAST_RESULT.success:
status = loc.send.broadcastSuccess;
break;
case BROADCAST_RESULT.error:
status = loc.send.broadcastError;
break;
default:
status = broadcastResult;
}
2020-04-28 18:27:35 +02:00
return (
<SafeBlueArea>
2021-02-25 02:56:06 +01:00
<KeyboardAvoidingView
enabled={!Platform.isPad}
behavior={Platform.OS === 'ios' ? 'position' : null}
keyboardShouldPersistTaps="handled"
>
2021-03-02 14:38:02 +01:00
<View style={styles.wrapper} testID="BroadcastView">
2020-04-28 18:27:35 +02:00
{BROADCAST_RESULT.success !== broadcastResult && (
<BlueCard style={styles.mainCard}>
<View style={styles.topFormRow}>
2020-07-20 15:38:46 +02:00
<BlueFormLabel>{status}</BlueFormLabel>
2020-04-28 18:27:35 +02:00
{BROADCAST_RESULT.pending === broadcastResult && <ActivityIndicator size="small" />}
</View>
<View style={[styles.input, stylesHooks.input]}>
<TextInput
style={styles.text}
maxHeight={100}
minHeight={100}
maxWidth="100%"
minWidth="100%"
multiline
editable
placeholderTextColor="#81868e"
value={txHex}
onChangeText={handleUpdateTxHex}
2021-03-11 04:27:51 +01:00
onSubmitEditing={Keyboard.dismiss}
2021-03-01 11:20:01 +01:00
testID="TxHex"
/>
</View>
<BlueSpacing20 />
<BlueButton title={loc.multisig.scan_or_open_file} onPress={handleQRScan} />
<BlueSpacing20 />
2020-04-28 18:27:35 +02:00
2020-07-20 15:38:46 +02:00
<BlueButton
title={loc.send.broadcastButton}
onPress={handleBroadcast}
2021-03-11 04:27:51 +01:00
disabled={broadcastResult === BROADCAST_RESULT.pending || txHex?.length === 0 || txHex === undefined}
2021-03-01 11:20:01 +01:00
testID="BroadcastButton"
2020-07-20 15:38:46 +02:00
/>
<BlueSpacing20 />
2020-04-28 18:27:35 +02:00
</BlueCard>
)}
{BROADCAST_RESULT.success === broadcastResult && <SuccessScreen tx={tx} />}
</View>
</KeyboardAvoidingView>
</SafeBlueArea>
);
2020-07-15 19:32:59 +02:00
};
export default Broadcast;
2021-02-15 09:03:54 +01:00
Broadcast.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.send.create_broadcast }));
2020-04-28 18:27:35 +02:00
const styles = StyleSheet.create({
wrapper: {
marginTop: 16,
alignItems: 'center',
justifyContent: 'flex-start',
},
broadcastResultWrapper: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
width: '100%',
},
mainCard: {
padding: 0,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-start',
},
topFormRow: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingBottom: 10,
paddingTop: 0,
paddingRight: 100,
},
input: {
flexDirection: 'row',
borderWidth: 1,
borderBottomWidth: 0.5,
alignItems: 'center',
borderRadius: 4,
},
text: {
padding: 8,
minHeight: 33,
color: '#81868e',
},
2020-04-28 18:27:35 +02:00
});
const SuccessScreen = ({ tx }) => {
2020-04-28 18:27:35 +02:00
if (!tx) {
return null;
}
2020-04-28 18:27:35 +02:00
return (
<View style={styles.wrapper}>
<BlueCard>
<View style={styles.broadcastResultWrapper}>
<BlueBigCheckmark />
<BlueSpacing20 />
<BlueTextCentered>{loc.settings.success_transaction_broadcasted}</BlueTextCentered>
2020-04-28 18:27:35 +02:00
<BlueSpacing10 />
2022-04-03 17:44:36 +02:00
<BlueButtonLink title={loc.settings.open_link_in_explorer} onPress={() => Linking.openURL(`https://mempool.space/tx/${tx}`)} />
2020-04-28 18:27:35 +02:00
</View>
</BlueCard>
</View>
);
};
2020-04-28 18:27:35 +02:00
SuccessScreen.propTypes = {
tx: PropTypes.string.isRequired,
};