BlueWallet/screen/send/broadcast.js

197 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-04-28 18:27:35 +02:00
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { ActivityIndicator, Linking, StyleSheet, View, KeyboardAvoidingView, Platform, Text, TextInput } from 'react-native';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
2020-04-28 18:27:35 +02:00
import { HDSegwitBech32Wallet } from '../../class';
import {
SafeBlueArea,
BlueCard,
BlueButton,
BlueSpacing10,
BlueSpacing20,
BlueFormLabel,
BlueTextCentered,
BlueBigCheckmark,
2020-12-04 14:39:47 +01:00
BlueNavigationStyle,
2020-04-28 18:27:35 +02:00
} from '../../BlueComponents';
2020-07-15 19:32:59 +02:00
import { BlueCurrentTheme } from '../../components/themes';
import BlueElectrum from '../../blue_modules/BlueElectrum';
import Notifications from '../../blue_modules/notifications';
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-04-28 18:27:35 +02:00
const [tx, setTx] = useState('');
const [txHex, setTxHex] = useState('');
const [broadcastResult, setBroadcastResult] = useState(BROADCAST_RESULT.none);
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) {
const tx = bitcoin.Transaction.fromHex(txHex);
2020-04-28 18:27:35 +02:00
const txid = tx.getId();
setTx(txid);
setBroadcastResult(BROADCAST_RESULT.success);
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>
<TextInput
style={styles.text}
2020-04-28 18:27:35 +02:00
maxHeight={100}
minHeight={100}
maxWidth="100%"
minWidth="100%"
2020-04-28 18:27:35 +02:00
multiline
editable
value={txHex}
onChangeText={handleUpdateTxHex}
/>
<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-04 14:39:47 +01:00
Broadcast.navigationOptions = () => ({
...BlueNavigationStyle(),
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%',
},
link: {
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.foregroundColor,
2020-04-28 18:27:35 +02:00
},
mainCard: {
padding: 0,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-start',
},
topFormRow: {
flex: 0.1,
flexBasis: 0.1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingBottom: 10,
paddingTop: 0,
paddingRight: 100,
height: 30,
maxHeight: 30,
},
text: {
flex: 1,
borderColor: '#ebebeb',
backgroundColor: '#d2f8d6',
borderRadius: 4,
marginTop: 20,
2020-07-15 19:32:59 +02:00
color: BlueCurrentTheme.colors.foregroundColor,
fontWeight: '500',
fontSize: 14,
paddingHorizontal: 16,
paddingBottom: 16,
paddingTop: 16,
},
2020-04-28 18:27:35 +02:00
});
function SuccessScreen({ tx }) {
if (!tx) {
return null;
}
return (
<View style={styles.wrapper}>
<BlueCard>
<View style={styles.broadcastResultWrapper}>
<BlueBigCheckmark />
<BlueSpacing20 />
2020-12-04 14:39:47 +01:00
<BlueTextCentered>Success! You transaction has been broadcasted!</BlueTextCentered>
2020-04-28 18:27:35 +02:00
<BlueSpacing10 />
<Text style={styles.link} onPress={() => Linking.openURL(`https://blockstream.info/tx/${tx}`)}>
2020-12-04 14:39:47 +01:00
Open link in explorer
2020-04-28 18:27:35 +02:00
</Text>
</View>
</BlueCard>
</View>
);
}
SuccessScreen.propTypes = {
tx: PropTypes.string.isRequired,
};