BlueWallet/screen/send/Broadcast.tsx

230 lines
6.9 KiB
TypeScript
Raw Normal View History

2024-05-20 10:54:13 +01:00
import { useNavigation, useRoute } from '@react-navigation/native';
import * as bitcoin from 'bitcoinjs-lib';
2024-05-20 10:54:13 +01:00
import React, { useState } from 'react';
import { ActivityIndicator, Keyboard, KeyboardAvoidingView, Linking, Platform, StyleSheet, TextInput, View } from 'react-native';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
import { isTablet } from '../../blue_modules/environment';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import Notifications from '../../blue_modules/notifications';
2020-04-28 17:27:35 +01:00
import {
2020-12-25 19:09:53 +03:00
BlueBigCheckmark,
BlueButtonLink,
BlueCard,
BlueFormLabel,
2020-04-28 17:27:35 +01:00
BlueSpacing10,
BlueSpacing20,
BlueTextCentered,
} from '../../BlueComponents';
2024-05-20 10:54:13 +01:00
import { HDSegwitBech32Wallet } from '../../class';
import presentAlert from '../../components/Alert';
2023-11-15 04:40:22 -04:00
import Button from '../../components/Button';
import SafeArea from '../../components/SafeArea';
2024-05-20 10:54:13 +01:00
import { useTheme } from '../../components/themes';
import { scanQrHelper } from '../../helpers/scan-qr';
2024-05-20 10:54:13 +01:00
import loc from '../../loc';
2020-04-28 17:27:35 +01:00
const BROADCAST_RESULT = Object.freeze({
2020-12-08 17:49:26 +03:30
none: 'Input transaction hex',
2020-04-28 17:27:35 +01:00
pending: 'pending',
success: 'success',
error: 'error',
});
2024-04-17 21:33:13 -04:00
interface SuccessScreenProps {
tx: string;
}
2024-04-18 00:40:47 -04:00
const Broadcast: React.FC = () => {
const { name } = useRoute();
const { navigate } = useNavigation();
2024-04-17 21:33:13 -04:00
const [tx, setTx] = useState<string | undefined>();
const [txHex, setTxHex] = useState<string | undefined>();
const { colors } = useTheme();
2024-04-17 21:33:13 -04:00
const [broadcastResult, setBroadcastResult] = useState<string>(BROADCAST_RESULT.none);
const stylesHooks = StyleSheet.create({
input: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
});
2024-04-17 21:33:13 -04:00
const handleUpdateTxHex = (nextValue: string) => setTxHex(nextValue.trim());
2020-04-28 17:27:35 +01:00
const handleBroadcast = async () => {
2021-03-10 22:27:51 -05:00
Keyboard.dismiss();
2020-04-28 17:27:35 +01:00
setBroadcastResult(BROADCAST_RESULT.pending);
try {
await BlueElectrum.ping();
await BlueElectrum.waitTillConnected();
const walletObj = new HDSegwitBech32Wallet();
2024-04-17 21:33:13 -04:00
if (txHex) {
const result = await walletObj.broadcastTx(txHex);
if (result) {
const newTx = bitcoin.Transaction.fromHex(txHex);
const txid = newTx.getId();
setTx(txid);
setBroadcastResult(BROADCAST_RESULT.success);
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
// @ts-ignore: fix later
Notifications.majorTomToGroundControl([], [], [txid]);
} else {
setBroadcastResult(BROADCAST_RESULT.error);
}
2020-04-28 17:27:35 +01:00
}
2024-04-17 21:33:13 -04:00
} catch (error: any) {
presentAlert({ title: loc.errors.error, message: error.message });
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
2020-04-28 17:27:35 +01:00
setBroadcastResult(BROADCAST_RESULT.error);
}
};
const handleQRScan = async () => {
const scannedData = await scanQrHelper(navigate, name);
if (!scannedData) return;
2024-04-18 00:40:47 -04:00
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 {
2024-04-18 00:40:47 -04:00
// should be base64 encoded PSBT
const validTx = bitcoin.Psbt.fromBase64(scannedData).extractTransaction();
return handleUpdateTxHex(validTx.toHex());
} catch (e) {}
};
2020-07-20 16:38:46 +03: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 17:27:35 +01:00
return (
<SafeArea>
2024-05-15 08:05:42 -04:00
<KeyboardAvoidingView enabled={!isTablet} behavior={Platform.OS === 'ios' ? 'position' : undefined}>
2021-03-02 16:38:02 +03:00
<View style={styles.wrapper} testID="BroadcastView">
2020-04-28 17:27:35 +01:00
{BROADCAST_RESULT.success !== broadcastResult && (
<BlueCard style={styles.mainCard}>
<View style={styles.topFormRow}>
2020-07-20 16:38:46 +03:00
<BlueFormLabel>{status}</BlueFormLabel>
2020-04-28 17:27:35 +01:00
{BROADCAST_RESULT.pending === broadcastResult && <ActivityIndicator size="small" />}
</View>
<View style={[styles.input, stylesHooks.input]}>
<TextInput
style={styles.text}
multiline
editable
placeholderTextColor="#81868e"
value={txHex}
onChangeText={handleUpdateTxHex}
2021-03-10 22:27:51 -05:00
onSubmitEditing={Keyboard.dismiss}
2021-03-01 13:20:01 +03:00
testID="TxHex"
/>
</View>
<BlueSpacing20 />
2023-11-15 04:40:22 -04:00
<Button title={loc.multisig.scan_or_open_file} onPress={handleQRScan} />
<BlueSpacing20 />
2020-04-28 17:27:35 +01:00
2023-11-15 04:40:22 -04:00
<Button
2020-07-20 16:38:46 +03:00
title={loc.send.broadcastButton}
onPress={handleBroadcast}
2024-04-18 00:40:47 -04:00
disabled={broadcastResult === BROADCAST_RESULT.pending || txHex?.length === 0 || txHex === undefined}
2021-03-01 13:20:01 +03:00
testID="BroadcastButton"
2020-07-20 16:38:46 +03:00
/>
<BlueSpacing20 />
2020-04-28 17:27:35 +01:00
</BlueCard>
)}
2024-04-17 21:33:13 -04:00
{BROADCAST_RESULT.success === broadcastResult && tx && <SuccessScreen tx={tx} />}
2020-04-28 17:27:35 +01:00
</View>
</KeyboardAvoidingView>
</SafeArea>
2020-04-28 17:27:35 +01:00
);
2020-07-15 13:32:59 -04:00
};
2024-04-17 21:33:13 -04:00
const SuccessScreen: React.FC<SuccessScreenProps> = ({ tx }) => {
if (!tx) {
return null;
}
return (
<View style={styles.wrapper}>
<BlueCard>
<View style={styles.broadcastResultWrapper}>
<BlueBigCheckmark />
<BlueSpacing20 />
<BlueTextCentered>{loc.settings.success_transaction_broadcasted}</BlueTextCentered>
<BlueSpacing10 />
<BlueButtonLink title={loc.settings.open_link_in_explorer} onPress={() => Linking.openURL(`https://mempool.space/tx/${tx}`)} />
</View>
</BlueCard>
</View>
);
};
2020-07-15 13:32:59 -04:00
export default Broadcast;
2020-04-28 17:27:35 +01: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,
color: '#81868e',
2024-04-17 21:33:13 -04:00
maxHeight: 100,
minHeight: 100,
maxWidth: '100%',
minWidth: '100%',
},
2020-04-28 17:27:35 +01:00
});