This commit is contained in:
Marcos Rodriguez Velez 2024-10-02 20:25:54 -04:00
parent b80324a43e
commit 9080d50240
3 changed files with 26 additions and 34 deletions

View file

@ -35,7 +35,7 @@ export type SendDetailsStackParamList = {
}; };
PsbtWithHardwareWallet: { PsbtWithHardwareWallet: {
memo?: string; memo?: string;
fromWallet: TWallet; walletID?: string;
launchedBy?: string; launchedBy?: string;
psbt?: Psbt; psbt?: Psbt;
txhex?: string; txhex?: string;

View file

@ -612,7 +612,7 @@ const SendDetails = () => {
// user whether he wants to broadcast it // user whether he wants to broadcast it
navigation.navigate('PsbtWithHardwareWallet', { navigation.navigate('PsbtWithHardwareWallet', {
memo: transactionMemo, memo: transactionMemo,
fromWallet: wallet, walletID: wallet.getID(),
psbt, psbt,
launchedBy: routeParams.launchedBy, launchedBy: routeParams.launchedBy,
}); });
@ -700,7 +700,7 @@ const SendDetails = () => {
navigation.navigate('PsbtWithHardwareWallet', { navigation.navigate('PsbtWithHardwareWallet', {
memo: transactionMemo, memo: transactionMemo,
fromWallet: wallet, walletID: wallet.getID(),
psbt, psbt,
}); });
setIsLoading(false); setIsLoading(false);
@ -734,7 +734,7 @@ const SendDetails = () => {
const file = await RNFS.readFile(res.uri, 'ascii'); const file = await RNFS.readFile(res.uri, 'ascii');
const psbt = bitcoin.Psbt.fromBase64(file); const psbt = bitcoin.Psbt.fromBase64(file);
const txhex = psbt.extractTransaction().toHex(); const txhex = psbt.extractTransaction().toHex();
navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, fromWallet: wallet, txhex }); navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, walletID: wallet.getID(), txhex });
setIsLoading(false); setIsLoading(false);
return; return;
@ -745,7 +745,7 @@ const SendDetails = () => {
// so user can do smth with it: // so user can do smth with it:
const file = await RNFS.readFile(res.uri, 'ascii'); const file = await RNFS.readFile(res.uri, 'ascii');
const psbt = bitcoin.Psbt.fromBase64(file); const psbt = bitcoin.Psbt.fromBase64(file);
navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, fromWallet: wallet, psbt }); navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, walletID: wallet.getID(), psbt });
setIsLoading(false); setIsLoading(false);
return; return;
@ -754,7 +754,7 @@ const SendDetails = () => {
if (DeeplinkSchemaMatch.isTXNFile(res.uri)) { if (DeeplinkSchemaMatch.isTXNFile(res.uri)) {
// plain text file with txhex ready to broadcast // plain text file with txhex ready to broadcast
const file = (await RNFS.readFile(res.uri, 'ascii')).replace('\n', '').replace('\r', ''); const file = (await RNFS.readFile(res.uri, 'ascii')).replace('\n', '').replace('\r', '');
navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, fromWallet: wallet, txhex: file }); navigation.navigate('PsbtWithHardwareWallet', { memo: transactionMemo, walletID: wallet.getID(), txhex: file });
setIsLoading(false); setIsLoading(false);
return; return;

View file

@ -22,19 +22,18 @@ import { useStorage } from '../../hooks/context/useStorage';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
const PsbtWithHardwareWallet = () => { const PsbtWithHardwareWallet = () => {
const { txMetadata, fetchAndSaveWalletTransactions, isElectrumDisabled } = useStorage(); const { txMetadata, fetchAndSaveWalletTransactions, isElectrumDisabled, wallets } = useStorage();
const { isBiometricUseCapableAndEnabled } = useBiometrics(); const { isBiometricUseCapableAndEnabled } = useBiometrics();
const navigation = useExtendedNavigation(); const navigation = useExtendedNavigation();
const route = useRoute(); const route = useRoute();
const { fromWallet, memo, psbt, deepLinkPSBT, launchedBy } = route.params; const { walletID, memo, deepLinkPSBT, launchedBy } = route.params;
const routeParamsPSBT = useRef(route.params.psbt); const wallet = wallets.find(w => w.getID() === walletID);
const routeParamsTXHex = route.params.txhex; const { psbt, txhex } = route.params;
const { colors } = useTheme();
const [isLoading, setIsLoading] = useState(false);
const [txHex, setTxHex] = useState(route.params.txhex);
const openScannerButton = useRef(); const openScannerButton = useRef();
const dynamicQRCode = useRef(); const dynamicQRCode = useRef();
const isFocused = useIsFocused(); const isFocused = useIsFocused();
const { colors } = useTheme();
const [isLoading, setIsLoading] = useState(false);
const stylesHook = StyleSheet.create({ const stylesHook = StyleSheet.create({
scrollViewContent: { scrollViewContent: {
@ -60,7 +59,7 @@ const PsbtWithHardwareWallet = () => {
}); });
const _combinePSBT = receivedPSBT => { const _combinePSBT = receivedPSBT => {
return fromWallet.combinePsbt(psbt, receivedPSBT); return wallet.combinePsbt(psbt, receivedPSBT);
}; };
const onBarScanned = ret => { const onBarScanned = ret => {
@ -71,18 +70,14 @@ const PsbtWithHardwareWallet = () => {
} }
if (ret.data.indexOf('+') === -1 && ret.data.indexOf('=') === -1 && ret.data.indexOf('=') === -1) { if (ret.data.indexOf('+') === -1 && ret.data.indexOf('=') === -1 && ret.data.indexOf('=') === -1) {
// this looks like NOT base64, so maybe its transaction's hex // this looks like NOT base64, so maybe its transaction's hex
setTxHex(ret.data); navigation.setParams({ txhex: ret.data });
return; return;
} }
try { try {
const Tx = _combinePSBT(ret.data); const Tx = _combinePSBT(ret.data);
setTxHex(Tx.toHex()); navigation.setParams({ txhex: Tx.toHex() });
if (launchedBy) { if (launchedBy) {
// we must navigate back to the screen who requested psbt (instead of broadcasting it ourselves)
// most likely for LN channel opening
navigation.navigate({ name: launchedBy, params: { psbt }, merge: true }); navigation.navigate({ name: launchedBy, params: { psbt }, merge: true });
// ^^^ we just use `psbt` variable sinse it was finalized in the above _combinePSBT()
// (passed by reference)
} }
} catch (Err) { } catch (Err) {
presentAlert({ message: Err.message }); presentAlert({ message: Err.message });
@ -98,23 +93,20 @@ const PsbtWithHardwareWallet = () => {
}, [isFocused]); }, [isFocused]);
useEffect(() => { useEffect(() => {
if (!psbt && !route.params.txhex) { if (!psbt && !txhex) {
presentAlert({ message: loc.send.no_tx_signing_in_progress }); presentAlert({ message: loc.send.no_tx_signing_in_progress });
} }
if (deepLinkPSBT) { if (deepLinkPSBT) {
const newPsbt = bitcoin.Psbt.fromBase64(deepLinkPSBT); const newPsbt = bitcoin.Psbt.fromBase64(deepLinkPSBT);
try { try {
const Tx = fromWallet.combinePsbt(routeParamsPSBT.current, newPsbt); const Tx = wallet.combinePsbt(psbt, newPsbt);
setTxHex(Tx.toHex()); navigation.setParams({ txhex: Tx.toHex() });
} catch (Err) { } catch (Err) {
presentAlert({ message: Err }); presentAlert({ message: Err });
} }
} else if (routeParamsTXHex) {
setTxHex(routeParamsTXHex);
} }
// eslint-disable-next-line react-hooks/exhaustive-deps }, [deepLinkPSBT, wallet, navigation, psbt, txhex]);
}, [deepLinkPSBT, routeParamsTXHex]);
const broadcast = async () => { const broadcast = async () => {
setIsLoading(true); setIsLoading(true);
@ -129,10 +121,10 @@ const PsbtWithHardwareWallet = () => {
try { try {
await BlueElectrum.ping(); await BlueElectrum.ping();
await BlueElectrum.waitTillConnected(); await BlueElectrum.waitTillConnected();
const result = await fromWallet.broadcastTx(txHex); const result = await wallet.broadcastTx(txhex);
if (result) { if (result) {
setIsLoading(false); setIsLoading(false);
const txDecoded = bitcoin.Transaction.fromHex(txHex); const txDecoded = bitcoin.Transaction.fromHex(txhex);
const txid = txDecoded.getId(); const txid = txDecoded.getId();
Notifications.majorTomToGroundControl([], [], [txid]); Notifications.majorTomToGroundControl([], [], [txid]);
if (memo) { if (memo) {
@ -140,7 +132,7 @@ const PsbtWithHardwareWallet = () => {
} }
navigation.navigate('Success', { amount: undefined }); navigation.navigate('Success', { amount: undefined });
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep to make sure network propagates await new Promise(resolve => setTimeout(resolve, 3000)); // sleep to make sure network propagates
fetchAndSaveWalletTransactions(fromWallet.getID()); fetchAndSaveWalletTransactions(wallet.getID());
} else { } else {
triggerHapticFeedback(HapticFeedbackTypes.NotificationError); triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
setIsLoading(false); setIsLoading(false);
@ -154,11 +146,11 @@ const PsbtWithHardwareWallet = () => {
}; };
const handleOnVerifyPressed = () => { const handleOnVerifyPressed = () => {
Linking.openURL('https://coinb.in/?verify=' + txHex); Linking.openURL('https://coinb.in/?verify=' + txhex);
}; };
const copyHexToClipboard = () => { const copyHexToClipboard = () => {
Clipboard.setString(txHex); Clipboard.setString(txhex);
}; };
const _renderBroadcastHex = () => { const _renderBroadcastHex = () => {
@ -166,7 +158,7 @@ const PsbtWithHardwareWallet = () => {
<View style={[styles.rootPadding, stylesHook.rootPadding]}> <View style={[styles.rootPadding, stylesHook.rootPadding]}>
<BlueCard style={[styles.hexWrap, stylesHook.hexWrap]}> <BlueCard style={[styles.hexWrap, stylesHook.hexWrap]}>
<BlueText style={[styles.hexLabel, stylesHook.hexLabel]}>{loc.send.create_this_is_hex}</BlueText> <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} /> <TextInput style={[styles.hexInput, stylesHook.hexInput]} height={112} multiline editable value={txhex} />
<TouchableOpacity accessibilityRole="button" style={styles.hexTouch} onPress={copyHexToClipboard}> <TouchableOpacity accessibilityRole="button" style={styles.hexTouch} onPress={copyHexToClipboard}>
<Text style={[styles.hexText, stylesHook.hexText]}>{loc.send.create_copy}</Text> <Text style={[styles.hexText, stylesHook.hexText]}>{loc.send.create_copy}</Text>
@ -220,7 +212,7 @@ const PsbtWithHardwareWallet = () => {
onBarScanned({ data: scannedData }); onBarScanned({ data: scannedData });
}; };
if (txHex) return _renderBroadcastHex(); if (txhex) return _renderBroadcastHex();
const renderView = isLoading ? ( const renderView = isLoading ? (
<ActivityIndicator /> <ActivityIndicator />