Class BitcoinURI


  • public class BitcoinURI
    extends java.lang.Object

    Provides a standard implementation of a Bitcoin URI with support for the following:

    • URLEncoded URIs (as passed in by IE on the command line)
    • BIP21 names (including the "req-" prefix handling requirements)

    Accepted formats

    The following input forms are accepted:

    • bitcoin:<address>
    • bitcoin:<address>?<name1>=<value1>&<name2>=<value2> with multiple additional name/value pairs

    The name/value pairs are processed as follows.

    1. URL encoding is stripped and treated as UTF-8
    2. names prefixed with req- are treated as required and if unknown or conflicting cause a parse exception
    3. Unknown names not prefixed with req- are added to a Map, accessible by parameter name
    4. Known names not prefixed with req- are processed unless they are malformed

    The following names are known and have the following formats:

    • amount decimal value to 8 dp (e.g. 0.12345678) Note that the exponent notation is not supported any more
    • label any URL encoded alphanumeric
    • message any URL encoded alphanumeric
    See Also:
    BIP 0021
    • Constructor Detail

      • BitcoinURI

        @Deprecated
        public BitcoinURI​(@Nullable
                          NetworkParameters params,
                          java.lang.String input)
                   throws BitcoinURIParseException
        Deprecated.
        Constructs a new object by trying to parse the input as a valid Bitcoin URI.
        Parameters:
        params - The network parameters that determine which network the URI is from, or null if you don't have any expectation about what network the URI is for and wish to check yourself.
        input - The raw URI data to be parsed (see class comments for accepted formats)
        Throws:
        BitcoinURIParseException - If the input fails Bitcoin URI syntax and semantic checks.
    • Method Detail

      • of

        public static BitcoinURI of​(java.lang.String uri)
                             throws BitcoinURIParseException
        Constructs a new BitcoinURI from the given string. Can be for any network.
        Parameters:
        uri - The raw URI data to be parsed (see class comments for accepted formats)
        Throws:
        BitcoinURIParseException - if the URI is not syntactically or semantically valid.
      • of

        public static BitcoinURI of​(java.lang.String uri,
                                    @Nonnull
                                    Network network)
                             throws BitcoinURIParseException
        Constructs a new object by trying to parse the input as a valid Bitcoin URI.
        Parameters:
        uri - The raw URI data to be parsed (see class comments for accepted formats)
        network - The network the URI is from
        Throws:
        BitcoinURIParseException - If the input fails Bitcoin URI syntax and semantic checks.
      • getAddress

        @Nullable
        public Address getAddress()
        The Bitcoin address from the URI, if one was present. It's possible to have Bitcoin URI's with no address if a r= payment protocol parameter is specified, though this form is not recommended as older wallets can't understand it.
      • getAmount

        public Coin getAmount()
        Returns:
        The amount name encoded using a pure integer value based at 10,000,000 units is 1 BTC. May be null if no amount is specified
      • getLabel

        public java.lang.String getLabel()
        Returns:
        The label from the URI.
      • getMessage

        public java.lang.String getMessage()
        Returns:
        The message from the URI.
      • getPaymentRequestUrl

        public final java.lang.String getPaymentRequestUrl()
        Returns:
        The URL where a payment request (as specified in BIP 70) may be fetched.
      • getPaymentRequestUrls

        public java.util.List<java.lang.String> getPaymentRequestUrls()
        Returns the URLs where a payment request (as specified in BIP 70) may be fetched. The first URL is the main URL, all subsequent URLs are fallbacks.
      • getParameterByName

        public java.lang.Object getParameterByName​(java.lang.String name)
        Parameters:
        name - The name of the parameter
        Returns:
        The parameter value, or null if not present
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • convertToBitcoinURI

        public static java.lang.String convertToBitcoinURI​(Address address,
                                                           Coin amount,
                                                           java.lang.String label,
                                                           java.lang.String message)
        Simple Bitcoin URI builder using known good fields.
        Parameters:
        address - The Bitcoin address
        amount - The amount
        label - A label
        message - A message
        Returns:
        A String containing the Bitcoin URI
      • convertToBitcoinURI

        @Deprecated
        public static java.lang.String convertToBitcoinURI​(NetworkParameters params,
                                                           java.lang.String address,
                                                           @Nullable
                                                           Coin amount,
                                                           @Nullable
                                                           java.lang.String label,
                                                           @Nullable
                                                           java.lang.String message)
        Simple Bitcoin URI builder using known good fields.
        Parameters:
        params - The network parameters that determine which network the URI is for.
        address - The Bitcoin address
        amount - The amount
        label - A label
        message - A message
        Returns:
        A String containing the Bitcoin URI
      • convertToBitcoinURI

        public static java.lang.String convertToBitcoinURI​(Network network,
                                                           java.lang.String address,
                                                           @Nullable
                                                           Coin amount,
                                                           @Nullable
                                                           java.lang.String label,
                                                           @Nullable
                                                           java.lang.String message)
        Simple Bitcoin URI builder using known good fields.
        Parameters:
        network - The network the URI is for.
        address - The Bitcoin address
        amount - The amount
        label - A label
        message - A message
        Returns:
        A String containing the Bitcoin URI