2019-09-27 16:49:56 +02:00
|
|
|
/* global alert */
|
2020-11-20 02:16:11 +01:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from 'react';
|
2020-01-01 04:31:04 +01:00
|
|
|
import {
|
|
|
|
ActivityIndicator,
|
|
|
|
TouchableOpacity,
|
|
|
|
ScrollView,
|
|
|
|
View,
|
|
|
|
TextInput,
|
|
|
|
Linking,
|
|
|
|
Platform,
|
2020-08-16 04:43:17 +02:00
|
|
|
Text,
|
2020-05-24 11:17:26 +02:00
|
|
|
StyleSheet,
|
2021-02-27 01:23:59 +01:00
|
|
|
findNodeHandle,
|
2020-01-01 04:31:04 +01:00
|
|
|
} from 'react-native';
|
2021-02-28 06:13:37 +01:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2020-12-25 17:09:53 +01:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
|
|
|
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
|
2021-07-10 04:38:09 +02:00
|
|
|
import { isMacCatalina } from '../../blue_modules/environment';
|
2020-12-25 17:09:53 +01:00
|
|
|
import RNFS from 'react-native-fs';
|
2021-01-20 03:13:01 +01:00
|
|
|
import Biometric from '../../class/biometrics';
|
2020-12-25 17:09:53 +01:00
|
|
|
|
2019-09-27 16:49:56 +02:00
|
|
|
import {
|
2020-07-15 19:32:59 +02:00
|
|
|
SecondButton,
|
2019-09-27 16:49:56 +02:00
|
|
|
BlueText,
|
|
|
|
SafeBlueArea,
|
|
|
|
BlueCard,
|
|
|
|
BlueSpacing20,
|
|
|
|
BlueCopyToClipboardButton,
|
2020-06-24 13:56:35 +02:00
|
|
|
DynamicQRCode,
|
2019-09-27 16:49:56 +02:00
|
|
|
} from '../../BlueComponents';
|
2020-12-25 17:09:53 +01:00
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
2020-07-20 15:38:46 +02:00
|
|
|
import loc from '../../loc';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
|
|
|
import Notifications from '../../blue_modules/notifications';
|
2020-07-01 13:56:52 +02:00
|
|
|
const BlueElectrum = require('../../blue_modules/BlueElectrum');
|
2019-09-27 16:49:56 +02:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2020-12-20 08:01:39 +01:00
|
|
|
const fs = require('../../blue_modules/fs');
|
2020-05-24 11:17:26 +02:00
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const PsbtWithHardwareWallet = () => {
|
|
|
|
const { txMetadata, fetchAndSaveWalletTransactions } = useContext(BlueStorageContext);
|
|
|
|
const navigation = useNavigation();
|
|
|
|
const route = useRoute();
|
|
|
|
const { fromWallet, memo, psbt, deepLinkPSBT } = route.params;
|
|
|
|
const routeParamsPSBT = useRef(route.params.psbt);
|
|
|
|
const routeParamsTXHex = route.params.txhex;
|
|
|
|
const { colors } = useTheme();
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2021-08-24 06:33:32 +02:00
|
|
|
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
|
2020-11-20 02:16:11 +01:00
|
|
|
const [txHex, setTxHex] = useState(route.params.txhex);
|
2021-02-27 01:23:59 +01:00
|
|
|
const openScannerButton = useRef();
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const stylesHook = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
rootPadding: {
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
hexWrap: {
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
hexLabel: {
|
|
|
|
color: colors.foregroundColor,
|
|
|
|
},
|
|
|
|
hexInput: {
|
|
|
|
borderColor: colors.formBorder,
|
|
|
|
backgroundColor: colors.inputBackgroundColor,
|
|
|
|
color: colors.foregroundColor,
|
|
|
|
},
|
|
|
|
hexText: {
|
|
|
|
color: colors.foregroundColor,
|
|
|
|
},
|
|
|
|
});
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const _combinePSBT = receivedPSBT => {
|
|
|
|
return fromWallet.combinePsbt(psbt, receivedPSBT);
|
2020-06-24 13:56:35 +02:00
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const onBarScanned = ret => {
|
2020-08-24 13:53:07 +02:00
|
|
|
if (ret && !ret.data) ret = { data: ret };
|
2020-06-24 13:56:35 +02:00
|
|
|
if (ret.data.toUpperCase().startsWith('UR')) {
|
2020-10-08 17:39:31 +02:00
|
|
|
alert('BC-UR not decoded. This should never happen');
|
2020-06-24 13:56:35 +02:00
|
|
|
}
|
2020-02-24 22:45:14 +01:00
|
|
|
if (ret.data.indexOf('+') === -1 && ret.data.indexOf('=') === -1 && ret.data.indexOf('=') === -1) {
|
|
|
|
// this looks like NOT base64, so maybe its transaction's hex
|
2020-11-20 02:16:11 +01:00
|
|
|
setTxHex(ret.data);
|
2020-02-24 22:45:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-08-16 04:43:17 +02:00
|
|
|
try {
|
2020-11-20 02:16:11 +01:00
|
|
|
const Tx = _combinePSBT(ret.data);
|
|
|
|
setTxHex(Tx.toHex());
|
2020-08-16 04:43:17 +02:00
|
|
|
} catch (Err) {
|
|
|
|
alert(Err);
|
|
|
|
}
|
2019-09-27 16:49:56 +02:00
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!psbt && !route.params.txhex) {
|
|
|
|
alert(loc.send.no_tx_signing_in_progress);
|
2020-09-16 18:52:10 +02:00
|
|
|
}
|
|
|
|
|
2020-01-03 05:02:41 +01:00
|
|
|
if (deepLinkPSBT) {
|
2020-09-16 18:52:10 +02:00
|
|
|
const psbt = bitcoin.Psbt.fromBase64(deepLinkPSBT);
|
2020-01-03 05:02:41 +01:00
|
|
|
try {
|
2020-11-20 02:16:11 +01:00
|
|
|
const Tx = fromWallet.combinePsbt(routeParamsPSBT.current, psbt);
|
|
|
|
setTxHex(Tx.toHex());
|
2020-01-03 05:02:41 +01:00
|
|
|
} catch (Err) {
|
|
|
|
alert(Err);
|
|
|
|
}
|
2020-11-20 02:16:11 +01:00
|
|
|
} else if (routeParamsTXHex) {
|
|
|
|
setTxHex(routeParamsTXHex);
|
2020-01-03 05:02:41 +01:00
|
|
|
}
|
2020-11-20 02:16:11 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [deepLinkPSBT, routeParamsTXHex]);
|
2020-01-03 05:02:41 +01:00
|
|
|
|
2021-08-24 06:33:32 +02:00
|
|
|
useEffect(() => {
|
|
|
|
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
|
|
|
|
}, []);
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const broadcast = async () => {
|
|
|
|
setIsLoading(true);
|
2021-01-20 03:13:01 +01:00
|
|
|
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
|
|
|
|
|
|
|
|
if (isBiometricsEnabled) {
|
|
|
|
if (!(await Biometric.unlockWithBiometrics())) {
|
|
|
|
setIsLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 02:16:11 +01:00
|
|
|
try {
|
|
|
|
await BlueElectrum.ping();
|
|
|
|
await BlueElectrum.waitTillConnected();
|
|
|
|
const result = await fromWallet.broadcastTx(txHex);
|
|
|
|
if (result) {
|
|
|
|
setIsLoading(false);
|
|
|
|
const txDecoded = bitcoin.Transaction.fromHex(txHex);
|
|
|
|
const txid = txDecoded.getId();
|
|
|
|
Notifications.majorTomToGroundControl([], [], [txid]);
|
|
|
|
if (memo) {
|
|
|
|
txMetadata[txid] = { memo };
|
2019-09-27 16:49:56 +02:00
|
|
|
}
|
2020-11-20 02:16:11 +01:00
|
|
|
navigation.navigate('Success', { amount: undefined });
|
2020-11-27 17:35:14 +01:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep to make sure network propagates
|
|
|
|
fetchAndSaveWalletTransactions(fromWallet.getID());
|
2020-11-20 02:16:11 +01:00
|
|
|
} else {
|
2019-09-27 16:49:56 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2020-11-20 02:16:11 +01:00
|
|
|
setIsLoading(false);
|
|
|
|
alert(loc.errors.broadcast);
|
2019-09-27 16:49:56 +02:00
|
|
|
}
|
2020-11-20 02:16:11 +01:00
|
|
|
} catch (error) {
|
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
setIsLoading(false);
|
|
|
|
alert(error.message);
|
|
|
|
}
|
2019-11-02 23:51:33 +01:00
|
|
|
};
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2020-11-20 02:19:04 +01:00
|
|
|
const handleOnVerifyPressed = () => {
|
|
|
|
Linking.openURL('https://coinb.in/?verify=' + txHex);
|
|
|
|
};
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2020-11-20 14:01:49 +01:00
|
|
|
const copyHexToClipboard = () => {
|
|
|
|
Clipboard.setString(txHex);
|
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const _renderBroadcastHex = () => {
|
2019-09-27 16:49:56 +02:00
|
|
|
return (
|
2020-11-20 02:16:11 +01:00
|
|
|
<View style={[styles.rootPadding, stylesHook.rootPadding]}>
|
|
|
|
<BlueCard style={[styles.hexWrap, stylesHook.hexWrap]}>
|
|
|
|
<BlueText style={[styles.hexLabel, stylesHook.hexLabel]}>{loc.send.create_this_is_hex}</BlueText>
|
|
|
|
<TextInput style={[styles.hexInput, stylesHook.hexInput]} height={112} multiline editable value={txHex} />
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2021-06-24 14:50:57 +02:00
|
|
|
<TouchableOpacity accessibilityRole="button" style={styles.hexTouch} onPress={copyHexToClipboard}>
|
2020-11-20 02:16:11 +01:00
|
|
|
<Text style={[styles.hexText, stylesHook.hexText]}>{loc.send.create_copy}</Text>
|
2019-09-27 16:49:56 +02:00
|
|
|
</TouchableOpacity>
|
2021-06-24 14:50:57 +02:00
|
|
|
<TouchableOpacity accessibilityRole="button" style={styles.hexTouch} onPress={handleOnVerifyPressed}>
|
2020-11-20 02:16:11 +01:00
|
|
|
<Text style={[styles.hexText, stylesHook.hexText]}>{loc.send.create_verify}</Text>
|
2019-09-27 16:49:56 +02:00
|
|
|
</TouchableOpacity>
|
|
|
|
<BlueSpacing20 />
|
2021-08-24 06:33:32 +02:00
|
|
|
<SecondButton
|
|
|
|
disabled={isElectrumDisabled}
|
|
|
|
onPress={broadcast}
|
|
|
|
title={loc.send.confirm_sendNow}
|
|
|
|
testID="PsbtWithHardwareWalletBroadcastTransactionButton"
|
|
|
|
/>
|
2019-09-27 16:49:56 +02:00
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
);
|
2020-11-20 02:16:11 +01:00
|
|
|
};
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2021-07-10 04:38:09 +02:00
|
|
|
const exportPSBT = () => {
|
2020-11-20 02:16:11 +01:00
|
|
|
const fileName = `${Date.now()}.psbt`;
|
2021-07-10 04:38:09 +02:00
|
|
|
fs.writeFileAndExport(fileName, typeof psbt === 'string' ? psbt : psbt.toBase64());
|
2020-01-01 04:31:04 +01:00
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const openSignedTransaction = async () => {
|
2020-01-01 04:31:04 +01:00
|
|
|
try {
|
2020-02-24 22:45:14 +01:00
|
|
|
const res = await DocumentPicker.pick({
|
|
|
|
type: Platform.OS === 'ios' ? ['io.bluewallet.psbt', 'io.bluewallt.psbt.txn'] : [DocumentPicker.types.allFiles],
|
|
|
|
});
|
2020-01-03 05:02:41 +01:00
|
|
|
const file = await RNFS.readFile(res.uri);
|
|
|
|
if (file) {
|
2020-11-20 02:16:11 +01:00
|
|
|
onBarScanned({ data: file });
|
2020-01-01 04:31:04 +01:00
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (!DocumentPicker.isCancel(err)) {
|
2020-07-20 15:38:46 +02:00
|
|
|
alert(loc.send.details_no_signed_tx);
|
2020-01-01 04:31:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
const openScanner = () => {
|
2021-01-20 03:13:01 +01:00
|
|
|
if (isMacCatalina) {
|
2021-02-27 01:23:59 +01:00
|
|
|
fs.showActionSheet({ anchor: findNodeHandle(openScannerButton.current) }).then(data => onBarScanned({ data }));
|
2020-08-16 04:43:17 +02:00
|
|
|
} else {
|
2020-11-20 02:16:11 +01:00
|
|
|
navigation.navigate('ScanQRCodeRoot', {
|
2020-08-16 04:43:17 +02:00
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params: {
|
2020-11-20 02:16:11 +01:00
|
|
|
launchedBy: route.name,
|
2020-11-19 15:33:18 +01:00
|
|
|
showFileImportButton: false,
|
2020-11-20 02:16:11 +01:00
|
|
|
onBarScanned,
|
2020-08-16 04:43:17 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
if (txHex) return _renderBroadcastHex();
|
2019-09-27 16:49:56 +02:00
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
return isLoading ? (
|
|
|
|
<View style={[styles.rootPadding, stylesHook.rootPadding]}>
|
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
) : (
|
2021-03-22 12:54:17 +01:00
|
|
|
<SafeBlueArea style={stylesHook.root}>
|
2020-11-20 02:16:11 +01:00
|
|
|
<ScrollView centerContent contentContainerStyle={styles.scrollViewContent} testID="PsbtWithHardwareScrollView">
|
|
|
|
<View style={styles.container}>
|
|
|
|
<BlueCard>
|
|
|
|
<BlueText testID="TextHelperForPSBT">{loc.send.psbt_this_is_psbt}</BlueText>
|
|
|
|
<BlueSpacing20 />
|
2020-11-22 08:07:27 +01:00
|
|
|
<Text testID="PSBTHex" style={styles.hidden}>
|
|
|
|
{psbt.toHex()}
|
|
|
|
</Text>
|
2021-03-16 19:03:01 +01:00
|
|
|
<DynamicQRCode value={psbt.toHex()} />
|
2020-11-20 02:16:11 +01:00
|
|
|
<BlueSpacing20 />
|
|
|
|
<SecondButton
|
|
|
|
testID="PsbtTxScanButton"
|
|
|
|
icon={{
|
|
|
|
name: 'qrcode',
|
|
|
|
type: 'font-awesome',
|
|
|
|
color: colors.buttonTextColor,
|
|
|
|
}}
|
|
|
|
onPress={openScanner}
|
2021-02-27 01:23:59 +01:00
|
|
|
ref={openScannerButton}
|
2020-11-20 02:16:11 +01:00
|
|
|
title={loc.send.psbt_tx_scan}
|
|
|
|
/>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<SecondButton
|
|
|
|
icon={{
|
|
|
|
name: 'file-import',
|
|
|
|
type: 'material-community',
|
|
|
|
color: colors.buttonTextColor,
|
|
|
|
}}
|
|
|
|
onPress={openSignedTransaction}
|
|
|
|
title={loc.send.psbt_tx_open}
|
|
|
|
/>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<SecondButton
|
|
|
|
icon={{
|
|
|
|
name: 'share-alternative',
|
|
|
|
type: 'entypo',
|
|
|
|
color: colors.buttonTextColor,
|
|
|
|
}}
|
|
|
|
onPress={exportPSBT}
|
|
|
|
title={loc.send.psbt_tx_export}
|
|
|
|
/>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<View style={styles.copyToClipboard}>
|
|
|
|
<BlueCopyToClipboardButton
|
|
|
|
stringToCopy={typeof psbt === 'string' ? psbt : psbt.toBase64()}
|
|
|
|
displayText={loc.send.psbt_clipboard}
|
2020-01-01 04:31:04 +01:00
|
|
|
/>
|
2020-11-20 02:16:11 +01:00
|
|
|
</View>
|
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
2019-09-27 16:49:56 +02:00
|
|
|
};
|
2020-07-15 19:32:59 +02:00
|
|
|
|
2020-11-20 02:16:11 +01:00
|
|
|
export default PsbtWithHardwareWallet;
|
|
|
|
|
2021-02-15 09:03:54 +01:00
|
|
|
PsbtWithHardwareWallet.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.send.header }));
|
2020-11-20 02:16:11 +01:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
scrollViewContent: {
|
|
|
|
flexGrow: 1,
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingTop: 16,
|
|
|
|
paddingBottom: 16,
|
|
|
|
},
|
|
|
|
rootPadding: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 20,
|
|
|
|
},
|
|
|
|
hexWrap: {
|
|
|
|
alignItems: 'center',
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
hexLabel: {
|
|
|
|
fontWeight: '500',
|
|
|
|
},
|
|
|
|
hexInput: {
|
|
|
|
borderRadius: 4,
|
|
|
|
marginTop: 20,
|
|
|
|
fontWeight: '500',
|
|
|
|
fontSize: 14,
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
paddingBottom: 16,
|
|
|
|
paddingTop: 16,
|
|
|
|
},
|
|
|
|
hexTouch: {
|
|
|
|
marginVertical: 24,
|
|
|
|
},
|
|
|
|
hexText: {
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: '500',
|
|
|
|
alignSelf: 'center',
|
|
|
|
},
|
|
|
|
copyToClipboard: {
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
2020-11-22 08:07:27 +01:00
|
|
|
hidden: {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
},
|
2020-11-20 02:16:11 +01:00
|
|
|
});
|