From d9831bdcd0b7c24e7f3ee59c146cf6d9873b42a1 Mon Sep 17 00:00:00 2001 From: satsen <95986745+satsen@users.noreply.github.com> Date: Wed, 13 Apr 2022 13:38:59 +0100 Subject: [PATCH] Use URI instead of manual parsing --- .../sparrowwallet/drongo/uri/BitcoinURI.java | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java b/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java index 055ece0..9ab8e57 100644 --- a/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java +++ b/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java @@ -87,9 +87,7 @@ public class BitcoinURI { * @throws BitcoinURIParseException if the URI is not syntactically or semantically valid. */ public BitcoinURI(String input) throws BitcoinURIParseException { - String scheme = BITCOIN_SCHEME; - - // Attempt to form the URI (fail fast syntax checking to official standards). + // Attempt to parse the URI URI uri; try { uri = new URI(input); @@ -100,23 +98,14 @@ public class BitcoinURI { // URI is formed as bitcoin:
? // blockchain.info generates URIs of non-BIP compliant form bitcoin://address?.... - // Remove the bitcoin scheme. - // (Note: getSchemeSpecificPart() is not used as it unescapes the label and parse then fails. - // For instance with : bitcoin:129mVqKUmJ9uwPxKJBnNdABbuaaNfho4Ha?amount=0.06&label=Tom%20%26%20Jerry - // the & (%26) in Tom and Jerry gets interpreted as a separator and the label then gets parsed - // as 'Tom ' instead of 'Tom & Jerry') - String blockchainInfoScheme = scheme + "://"; - String correctScheme = scheme + ":"; - String schemeSpecificPart; - final String inputLc = input.toLowerCase(Locale.US); - if(inputLc.startsWith(blockchainInfoScheme)) { - schemeSpecificPart = input.substring(blockchainInfoScheme.length()); - } else if(inputLc.startsWith(correctScheme)) { - schemeSpecificPart = input.substring(correctScheme.length()); - } else { + if (!BITCOIN_SCHEME.equalsIgnoreCase(uri.getScheme())) { throw new BitcoinURIParseException("Unsupported URI scheme: " + uri.getScheme()); } + String schemeSpecificPart = uri.getRawSchemeSpecificPart().startsWith("//") + ? uri.getRawSchemeSpecificPart().substring(2) + : uri.getRawSchemeSpecificPart(); + // Split off the address from the rest of the query parameters. String[] addressSplitTokens = schemeSpecificPart.split("\\?", 2); if(addressSplitTokens.length == 0) {