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: {
memo?: string;
fromWallet: TWallet;
walletID?: string;
launchedBy?: string;
psbt?: Psbt;
txhex?: string;

View file

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

View file

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