2020-04-28 18:27:35 +02:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-12-13 02:51:39 +01:00
|
|
|
import { ActivityIndicator, Linking, StyleSheet, View, KeyboardAvoidingView, Platform, TextInput } from 'react-native';
|
2020-04-28 18:27:35 +02:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
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';
|
2020-07-01 13:56:52 +02:00
|
|
|
import BlueElectrum from '../../blue_modules/BlueElectrum';
|
2020-10-24 19:20:59 +02:00
|
|
|
import Notifications from '../../blue_modules/notifications';
|
2020-12-13 02:51:39 +01:00
|
|
|
import { useTheme } from '@react-navigation/native';
|
2020-04-28 18:27:35 +02:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
|
|
|
|
|
|
|
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 = () => {
|
2020-12-13 02:51:39 +01:00
|
|
|
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);
|
2020-12-13 02:51:39 +01:00
|
|
|
const stylesHooks = StyleSheet.create({
|
|
|
|
blueArea: {
|
|
|
|
backgroundColor: colors.background,
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
color: colors.foregroundColor,
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
borderColor: colors.formBorder,
|
|
|
|
borderBottomColor: colors.formBorder,
|
|
|
|
backgroundColor: colors.inputBackgroundColor,
|
|
|
|
},
|
|
|
|
});
|
2020-04-28 18:27:35 +02:00
|
|
|
const handleUpdateTxHex = nextValue => setTxHex(nextValue.trim());
|
|
|
|
const handleBroadcast = async () => {
|
|
|
|
setBroadcastResult(BROADCAST_RESULT.pending);
|
|
|
|
try {
|
|
|
|
await BlueElectrum.ping();
|
|
|
|
await BlueElectrum.waitTillConnected();
|
|
|
|
const walletObj = new HDSegwitBech32Wallet();
|
|
|
|
const result = await walletObj.broadcastTx(txHex);
|
|
|
|
if (result) {
|
2020-06-01 14:54:23 +02:00
|
|
|
const tx = bitcoin.Transaction.fromHex(txHex);
|
2020-04-28 18:27:35 +02:00
|
|
|
const txid = tx.getId();
|
|
|
|
setTx(txid);
|
2020-12-13 02:51:39 +01:00
|
|
|
|
2020-04-28 18:27:35 +02:00
|
|
|
setBroadcastResult(BROADCAST_RESULT.success);
|
2020-12-13 02:51:39 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
|
2020-10-24 19:20:59 +02:00
|
|
|
Notifications.majorTomToGroundControl([], [], [txid]);
|
2020-04-28 18:27:35 +02:00
|
|
|
} else {
|
|
|
|
setBroadcastResult(BROADCAST_RESULT.error);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
setBroadcastResult(BROADCAST_RESULT.error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 style={styles.blueArea}>
|
|
|
|
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'position' : null} keyboardShouldPersistTaps="handled">
|
|
|
|
<View style={styles.wrapper}>
|
|
|
|
{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>
|
2020-12-13 02:51:39 +01:00
|
|
|
|
|
|
|
<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}
|
|
|
|
/>
|
|
|
|
</View>
|
2020-04-28 18:27:35 +02:00
|
|
|
|
|
|
|
<BlueSpacing10 />
|
2020-07-20 15:38:46 +02:00
|
|
|
<BlueButton
|
|
|
|
title={loc.send.broadcastButton}
|
|
|
|
onPress={handleBroadcast}
|
|
|
|
disabled={broadcastResult === BROADCAST_RESULT.pending}
|
|
|
|
/>
|
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;
|
2020-12-25 17:09:53 +01:00
|
|
|
Broadcast.navigationOptions = navigationStyle({
|
2020-07-20 15:38:46 +02:00
|
|
|
title: loc.send.create_broadcast,
|
2020-07-15 19:32:59 +02:00
|
|
|
});
|
2020-04-28 18:27:35 +02:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
wrapper: {
|
|
|
|
marginTop: 16,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
},
|
|
|
|
blueArea: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 19,
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
},
|
2020-12-13 02:51:39 +01:00
|
|
|
input: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
borderWidth: 1,
|
|
|
|
borderBottomWidth: 0.5,
|
|
|
|
alignItems: 'center',
|
2020-05-24 11:17:26 +02:00
|
|
|
borderRadius: 4,
|
2020-12-13 02:51:39 +01:00
|
|
|
},
|
|
|
|
text: {
|
|
|
|
padding: 8,
|
|
|
|
minHeight: 33,
|
|
|
|
color: '#81868e',
|
2020-05-24 11:17:26 +02:00
|
|
|
},
|
2020-04-28 18:27:35 +02:00
|
|
|
});
|
|
|
|
|
2020-12-13 02:51:39 +01:00
|
|
|
const SuccessScreen = ({ tx }) => {
|
2020-04-28 18:27:35 +02:00
|
|
|
if (!tx) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-13 02:51:39 +01:00
|
|
|
|
2020-04-28 18:27:35 +02:00
|
|
|
return (
|
|
|
|
<View style={styles.wrapper}>
|
|
|
|
<BlueCard>
|
|
|
|
<View style={styles.broadcastResultWrapper}>
|
|
|
|
<BlueBigCheckmark />
|
|
|
|
<BlueSpacing20 />
|
2020-12-13 02:51:39 +01:00
|
|
|
<BlueTextCentered>{loc.settings.success_transaction_broadcasted}</BlueTextCentered>
|
2020-04-28 18:27:35 +02:00
|
|
|
<BlueSpacing10 />
|
2020-12-13 02:51:39 +01:00
|
|
|
<BlueButtonLink title={loc.settings.open_link_in_explorer} onPress={() => Linking.openURL(`https://blockstream.info/tx/${tx}`)} />
|
2020-04-28 18:27:35 +02:00
|
|
|
</View>
|
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
);
|
2020-12-13 02:51:39 +01:00
|
|
|
};
|
2020-04-28 18:27:35 +02:00
|
|
|
|
|
|
|
SuccessScreen.propTypes = {
|
|
|
|
tx: PropTypes.string.isRequired,
|
|
|
|
};
|