diff --git a/web/cashtab/src/components/Common/Ticker.js b/web/cashtab/src/components/Common/Ticker.js --- a/web/cashtab/src/components/Common/Ticker.js +++ b/web/cashtab/src/components/Common/Ticker.js @@ -47,14 +47,24 @@ } export function toLegacy(address) { + let testedAddress; let legacyAddress; + let hasPrefix = address.includes(':'); + + if (!hasPrefix) { + testedAddress = `bitcoincash:` + address; + } else { + testedAddress = address; + } try { - if (isCash(address)) { - const { type, hash } = cashaddr.decode(address); + if (isCash(testedAddress)) { + const { type, hash } = cashaddr.decode(testedAddress); legacyAddress = cashaddr.encode('bitcoincash', type, hash); - console.log(`legacyAddress`); } else { - throw new Error('Address prefix is not in Ticker.prefixes array'); + console.log(`Error: ${address} is not a cash address`); + throw new Error( + 'Address prefix is not a valid cash address with a prefix from the Ticker.prefixes array', + ); } } catch (err) { return err; diff --git a/web/cashtab/src/components/Common/__tests__/Ticker.test.js b/web/cashtab/src/components/Common/__tests__/Ticker.test.js --- a/web/cashtab/src/components/Common/__tests__/Ticker.test.js +++ b/web/cashtab/src/components/Common/__tests__/Ticker.test.js @@ -51,6 +51,13 @@ ); }); +test('toLegacy() accepts a valid BCH address with no prefix and returns with prefix', async () => { + const result = toLegacy('qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0'); + expect(result).toStrictEqual( + 'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0', + ); +}); + test('toLegacy() returns a valid bitcoincash: prefix address unchanged', async () => { const result = toLegacy( 'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0', @@ -76,6 +83,8 @@ ); expect(result).toStrictEqual( - new Error('Address prefix is not in Ticker.prefixes array'), + new Error( + 'Address prefix is not a valid cash address with a prefix from the Ticker.prefixes array', + ), ); }); diff --git a/web/cashtab/src/components/Send/Send.js b/web/cashtab/src/components/Send/Send.js --- a/web/cashtab/src/components/Send/Send.js +++ b/web/cashtab/src/components/Send/Send.js @@ -183,11 +183,22 @@ // Ensure address has bitcoincash: prefix and checksum cleanAddress = toLegacy(cleanAddress); - // If there was an error converting the address - if (!cleanAddress.startsWith('bitcoincash:')) { - // return as above with other errors + let hasValidCashPrefix; + try { + hasValidCashPrefix = cleanAddress.startsWith('bitcoincash:'); + } catch (err) { + hasValidCashPrefix = false; console.log(`toLegacy() returned an error:`, cleanAddress); - // Note: the address must be valid to get to this point, so unsure if this can be produced + } + + if (!hasValidCashPrefix) { + // set loading to false and set address validation to false + // Now that the no-prefix case is handled, this happens when user tries to send + // BCHA to an SLPA address + setLoading(false); + setSendBchAddressError( + `Destination is not a valid ${currency.ticker} address`, + ); return; }