From eb5d026df4aa0ca552ba5b953072dcafc2c3f689 Mon Sep 17 00:00:00 2001 From: finned-palmer Date: Mon, 6 Sep 2021 12:59:04 -0500 Subject: [PATCH] Fix issues with bridge and external invoices --- .../src/components/Send/BridgeInvoice.tsx | 35 +++++++++++++++---- .../src/components/Send/ExternalInvoice.tsx | 33 ++++++++++++++--- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/pkg/btc-wallet/src/components/Send/BridgeInvoice.tsx b/pkg/btc-wallet/src/components/Send/BridgeInvoice.tsx index 1753d79be..0186bcc82 100644 --- a/pkg/btc-wallet/src/components/Send/BridgeInvoice.tsx +++ b/pkg/btc-wallet/src/components/Send/BridgeInvoice.tsx @@ -25,10 +25,18 @@ type Props = { }; const BridgeInvoice: React.FC = ({ payee, stopSending, satsAmount }) => { - const { error, currencyRates, fee, broadcastSuccess, denomination, psbt } = - useSettings(); + const { + error, + currencyRates, + fee, + broadcastSuccess, + denomination, + psbt, + history, + } = useSettings(); const [txHex, setTxHex] = useState(''); const [ready, setReady] = useState(false); + const [historyLength, setHistoryLength] = useState(0); const [localError, setLocalError] = useState(''); const [broadcasting, setBroadcasting] = useState(false); const invoiceRef = useRef(); @@ -44,7 +52,17 @@ const BridgeInvoice: React.FC = ({ payee, stopSending, satsAmount }) => { useEffect(() => { window.open('https://bridge.urbit.org/?kind=btc&utx=' + psbt); - }); + }, []); + + useEffect(() => { + if (historyLength === 0) { + setHistoryLength(history.length); + } + if (broadcasting && history.length > historyLength) { + setBroadcasting(false); + stopSending(); + } + }, [history]); const broadCastTx = (hex: string) => { let command = { @@ -65,9 +83,14 @@ const BridgeInvoice: React.FC = ({ payee, stopSending, satsAmount }) => { }; const checkTxHex = (e: React.ChangeEvent) => { - setTxHex(e.target.value); - setReady(txHex.length > 0); - setLocalError(''); + // TODO: validate this hex with something other than length check. + if (e.target.value.length > 0) { + setTxHex(e.target.value); + setReady(true); + setLocalError(''); + } else { + setLocalError('Invalid transaction hex'); + } }; let inputColor = 'black'; diff --git a/pkg/btc-wallet/src/components/Send/ExternalInvoice.tsx b/pkg/btc-wallet/src/components/Send/ExternalInvoice.tsx index 864425c59..e4270579d 100644 --- a/pkg/btc-wallet/src/components/Send/ExternalInvoice.tsx +++ b/pkg/btc-wallet/src/components/Send/ExternalInvoice.tsx @@ -29,10 +29,18 @@ const ExternalInvoice: React.FC = ({ stopSending, satsAmount, }) => { - const { error, currencyRates, fee, broadcastSuccess, denomination, psbt } = - useSettings(); + const { + error, + currencyRates, + fee, + broadcastSuccess, + denomination, + psbt, + history, + } = useSettings(); const [txHex, setTxHex] = useState(''); const [ready, setReady] = useState(false); + const [historyLength, setHistoryLength] = useState(0); const [localError, setLocalError] = useState(''); const [broadcasting, setBroadcasting] = useState(false); const invoiceRef = useRef(); @@ -46,6 +54,16 @@ const ExternalInvoice: React.FC = ({ } }, [error, broadcasting, setBroadcasting]); + useEffect(() => { + if (historyLength === 0) { + setHistoryLength(history.length); + } + if (broadcasting && history.length > historyLength) { + setBroadcasting(false); + stopSending(); + } + }, [history]); + const broadCastTx = (hex: string) => { let command = { 'broadcast-tx': hex, @@ -65,9 +83,14 @@ const ExternalInvoice: React.FC = ({ }; const checkTxHex = (e: React.ChangeEvent) => { - setTxHex(e.target.value); - setReady(txHex.length > 0); - setLocalError(''); + // TODO: validate this hex with something other than length check. + if (e.target.value.length > 0) { + setTxHex(e.target.value); + setReady(true); + setLocalError(''); + } else { + setLocalError('Invalid transaction hex'); + } }; const copyPsbt = () => {