diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ea1fac --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# BitcoinPaymentURI +[![Release](https://jitpack.io/v/SandroMachado/BitcoinPaymentURI.svg)](https://jitpack.io/#SandroMachado/BitcoinPaymentURI) + +BitcoinPaymentURI is an open source library to handle the Bitcoin payment URI based on the [BIT 21](https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki). The purpose of this library is to provide a simplier way to the developers to integrate in their applications support for this URI Scheme to easily make payments. + +# Gradle Dependency + +## Repository + +First, add the following to your app's `build.gradle` file: + +```Gradle +repositories { + maven { url "https://jitpack.io" } +} +``` + +Them include the openalpr-android dependency: + +```gradle +dependencies { + + // ... other dependencies here. + compile 'com.github.SandroMachado:BitcoinPaymentURI:1.0.0' +} +``` + +# Usage + +## Code + +Parse the URI `bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=50&label=Luke-Jr&message=Donation%20for%20project%20xyz`. + +```Java +BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=50&label=Luke-Jr&message=Donation%20for%20project%20xyz"); + +bitcoinPaymentURI.getAddress(); \\175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W +bitcoinPaymentURI.getAmount(); \\50 +bitcoinPaymentURI.getLabel(); \\ "Luke-Jr" +bitcoinPaymentURI.getMessage(); \\ "Donation for project xyz" +bitcoinPaymentURI.getParameters().size(); \\0 +``` + +Generate the following URI `bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?message=Donation%20for%20project%20xyz&amount=50.0&req-fiz=biz&foo=bar&label=Luke-Jr` + +```Java +BitcoinPaymentURI bitcoinPaymentURI = new BitcoinPaymentURI.Builder() + .address("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W") + .amount(50.0) + .label("Luke-Jr") + .message("Donation for project xyz") + .parameter("foo", "bar") + .requiredParameter("fiz", "biz") + .build(); +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..3d670be --- /dev/null +++ b/build.gradle @@ -0,0 +1,27 @@ +apply plugin: 'java' +apply plugin: 'maven' + +group = 'com.github.SandroMachado' + +repositories { + jcenter() +} + +dependencies { + testCompile 'junit:junit:4.12' +} + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +artifacts { + archives sourcesJar + archives javadocJar +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..359af33 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Feb 14 00:40:08 WET 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..a255339 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,19 @@ +/* + * This settings file was auto generated by the Gradle buildInit task + * by 'sandromachado' at '2/14/16 12:40 AM' with Gradle 2.6 + * + * The settings file is used to specify which projects to include in your build. + * In a single project build this file can be empty or even removed. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/2.6/userguide/multi_project_builds.html + */ + +/* +// To declare projects as part of a multi-project build use the 'include' method +include 'shared' +include 'api' +include 'services:webservice' +*/ + +rootProject.name = 'BPURI' diff --git a/src/main/java/BitcoinPaymentURI.java b/src/main/java/BitcoinPaymentURI.java new file mode 100644 index 0000000..0b8c8ab --- /dev/null +++ b/src/main/java/BitcoinPaymentURI.java @@ -0,0 +1,377 @@ +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import model.Parameter; + +/** + * Java library to handle Bitcoin payment URI. + * This library is based on the specification at the BIP 21. + * + * The BIT is available at: https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki + */ + +public class BitcoinPaymentURI { + + private static final String SCHEME = "bitcoin:"; + private static final String PARAMETER_AMOUNT = "amount"; + private static final String PARAMETER_LABEL = "label"; + private static final String PARAMETER_MESSAGE = "message"; + + private final String address; + private final HashMap parameters; + + private BitcoinPaymentURI(Builder builder) { + this.address = builder.address; + + parameters = new HashMap(); + + if (builder.amount != null) { + parameters.put(PARAMETER_AMOUNT, new Parameter(String.valueOf(builder.amount), false)); + } + + if (builder.label != null) { + parameters.put(PARAMETER_LABEL, new Parameter(builder.label, false)); + } + + if (builder.message != null) { + parameters.put(PARAMETER_MESSAGE, new Parameter(builder.message, false)); + } + + if (builder.otherParameters != null) { + parameters.putAll(builder.otherParameters); + } + } + + /** + * Gets the URI Bitcoin address. + * + * @return the URI Bitcoin address. + */ + + public String getAddress() { + return address; + } + + /** + * Gets the URI amount. + * + * @return the URI amount. + */ + + public Double getAmount() { + if (parameters.get(PARAMETER_AMOUNT) == null) { + return null; + } + + return Double.valueOf(parameters.get(PARAMETER_AMOUNT).getValue()); + } + + /** + * Gets the URI label. + * + * @return the URI label. + */ + + public String getLabel() { + if (parameters.get(PARAMETER_LABEL) == null) { + return null; + } + + return parameters.get(PARAMETER_LABEL).getValue(); + } + + /** + * Gets the URI message. + * + * @return the URI message. + */ + + public String getMessage() { + if (parameters.get(PARAMETER_MESSAGE) == null) { + return null; + } + + return parameters.get(PARAMETER_MESSAGE).getValue(); + } + + /** + * Gets the URI parameters. + * + * @return the URI parameters. + */ + + public HashMap getParameters() { + HashMap filteredParameters = new HashMap(parameters); + + filteredParameters.remove(PARAMETER_AMOUNT); + filteredParameters.remove(PARAMETER_LABEL); + filteredParameters.remove(PARAMETER_MESSAGE); + + return filteredParameters; + } + + /** + * Gets the URI. + * + * @return a string with the URI. This string can be used to make a Bitcoin payment. + */ + + public String getURI() { + String queryParameters = null; + try { + for (Map.Entry entry : parameters.entrySet()) { + if (queryParameters == null) { + if (entry.getValue().isRequired()) { + queryParameters = String.format("req-%s=%s", URLEncoder.encode(entry.getKey(), "UTF-8").replace("+", "%20"), URLEncoder.encode(entry.getValue().getValue(), "UTF-8").replace("+", "%20")); + + continue; + } + + queryParameters = String.format("%s=%s", URLEncoder.encode(entry.getKey(), "UTF-8").replace("+", "%20"), URLEncoder.encode(entry.getValue().getValue(), "UTF-8").replace("+", "%20")); + + continue; + } + + if (entry.getValue().isRequired()) { + queryParameters = String.format("%s&req-%s=%s", queryParameters, URLEncoder.encode(entry.getKey(), "UTF-8").replace("+", "%20"), URLEncoder.encode(entry.getValue().getValue(), "UTF-8").replace("+", "%20")); + + continue; + } + + queryParameters = String.format("%s&%s=%s", queryParameters, URLEncoder.encode(entry.getKey(), "UTF-8").replace("+", "%20"), URLEncoder.encode(entry.getValue().getValue(), "UTF-8").replace("+", "%20")); + } + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + + return null; + } + + return String.format("%s%s%s", SCHEME, getAddress(), queryParameters == null ? "" : String.format("?%s", queryParameters)); + } + + /** + * Parses a string to a Bitcoin payment URI. + * + * @param string The string to be parsed. + * + * @return a Bitcoin payment URI if the URI is valid, or null for an invalid string. + */ + + public static BitcoinPaymentURI parse(String string) { + try { + string = URLDecoder.decode(string, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + + return null; + } + + if (string == null) { + return null; + } + + if (string.isEmpty()) { + return null; + } + + if (!string.toLowerCase().startsWith(SCHEME)) { + return null; + } + + String bitcoinPaymentURIWithoutScheme = string.replaceFirst(".*:", ""); + ArrayList bitcoinPaymentURIElements = new ArrayList<>(Arrays.asList(bitcoinPaymentURIWithoutScheme.split("\\?"))); + + if (bitcoinPaymentURIElements.size() != 1 && bitcoinPaymentURIElements.size() != 2) { + return null; + } + + if (bitcoinPaymentURIElements.get(0).length() == 0) { + return null; + } + + if (bitcoinPaymentURIElements.size() == 1) { + return new Builder().address(bitcoinPaymentURIElements.get(0)).build(); + } + + List queryParametersList = Arrays.asList(bitcoinPaymentURIElements.get(1).split("&")); + + if (queryParametersList.isEmpty()) { + return new Builder().address(bitcoinPaymentURIElements.get(0)).build(); + } + + HashMap queryParametersFiltered = new HashMap(); + + for (String query : queryParametersList) { + String[] queryParameter = query.split("="); + + try { + queryParametersFiltered.put(queryParameter[0], queryParameter[1]); + }catch(ArrayIndexOutOfBoundsException exception) { + exception.printStackTrace(); + + return null; + } + } + + Builder bitcoinPaymentURIBuilder = new Builder().address(bitcoinPaymentURIElements.get(0)); + + if (queryParametersFiltered.containsKey(PARAMETER_AMOUNT)) { + bitcoinPaymentURIBuilder.amount(Double.valueOf(queryParametersFiltered.get(PARAMETER_AMOUNT))); + + queryParametersFiltered.remove(PARAMETER_AMOUNT); + } + + if (queryParametersFiltered.containsKey(PARAMETER_LABEL)) { + bitcoinPaymentURIBuilder.label(queryParametersFiltered.get(PARAMETER_LABEL)); + + queryParametersFiltered.remove(PARAMETER_LABEL); + } + + if (queryParametersFiltered.containsKey(PARAMETER_MESSAGE)) { + bitcoinPaymentURIBuilder.message(queryParametersFiltered.get(PARAMETER_MESSAGE)); + + queryParametersFiltered.remove(PARAMETER_MESSAGE); + } + + for (Map.Entry entry : queryParametersFiltered.entrySet()) { + bitcoinPaymentURIBuilder.parameter(entry.getKey(), entry.getValue()); + } + + return bitcoinPaymentURIBuilder.build(); + } + + public static class Builder{ + + private String address; + private Double amount; + private String label; + private String message; + private HashMap otherParameters; + + /** + * Returns a builder for the Bitcoin payment URI. + */ + + public Builder() { + } + + /** + * Adds the address to the builder. + * + * @param address The address. + * + * @return the builder with the address. + */ + + public Builder address(String address) { + this.address = address; + + return this; + } + + /** + * Adds the amount to the builder. + * + * @param amount The amount. + * + * @return the builder with the amount. + */ + + public Builder amount(Double amount) { + this.amount = amount; + + return this; + } + + /** + * Adds the label to the builder. + * + * @param label The label. + * + * @return the builder with the label. + */ + + public Builder label(String label) { + this.label = label; + + return this; + } + + /** + * Adds the message to the builder. + * + * @param message The message. + * + * @return the builder with the message. + */ + + public Builder message(String message) { + this.message = message; + + return this; + } + + /** + * Adds a parameter to the builder. + * + * @param key The parameter. + * @param value The value. + * + * @return the builder with the parameter. + */ + + public Builder parameter(String key, String value) { + if (otherParameters == null) { + otherParameters = new HashMap(); + } + + if (key.startsWith("req-")) { + otherParameters.put(key.replace("req-", ""), new Parameter(value, true)); + + return this; + } + + otherParameters.put(key, new Parameter(value, false)); + + return this; + } + + /** + * Adds a required to the builder. + * + * @param key The key. + * @param value The value. + * + * @return the builder with the parameter. + */ + + public Builder requiredParameter(String key, String value) { + if (otherParameters == null) { + otherParameters = new HashMap(); + } + + otherParameters.put(key, new Parameter(value, true)); + + return this; + } + + /** + * Builds a Bitcoin payment URI. + * + * @return a Bitcoin payment URI. + */ + + public BitcoinPaymentURI build() { + return new BitcoinPaymentURI(this); + } + + } + +} diff --git a/src/main/java/model/Parameter.java b/src/main/java/model/Parameter.java new file mode 100644 index 0000000..9b09121 --- /dev/null +++ b/src/main/java/model/Parameter.java @@ -0,0 +1,41 @@ +package model; + +public class Parameter { + + private final String value; + private final Boolean required; + + /** + * Constructor. + * + * @param value The value. + * @param required A boolean indicating if the parameter is required. + */ + + public Parameter(String value, Boolean required) { + super(); + this.value = value; + this.required = required; + } + + /** + * Gets the parameter value. + * + * @return the parameter value. + */ + + public String getValue() { + return value; + } + + /** + * Gets a boolean indicating if the parameter is required. + * + * @return a boolean indicating if the parameter is required + */ + + public Boolean isRequired() { + return required; + } + +} diff --git a/src/test/java/BitcoinPaymentURITest.java b/src/test/java/BitcoinPaymentURITest.java new file mode 100644 index 0000000..03427fc --- /dev/null +++ b/src/test/java/BitcoinPaymentURITest.java @@ -0,0 +1,104 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BitcoinPaymentURITest { + + @Test + public void testParseForAddressMethod() { + BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertNull(bitcoinPaymentURI.getAmount()); + assertNull(bitcoinPaymentURI.getLabel()); + assertNull(bitcoinPaymentURI.getMessage()); + assertEquals(bitcoinPaymentURI.getParameters().size(), 0); + } + + @Test + public void testParseForAddressWithNameMethod() { + BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label=Luke-Jr"); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertNull(bitcoinPaymentURI.getAmount()); + assertEquals(bitcoinPaymentURI.getLabel(), "Luke-Jr"); + assertNull(bitcoinPaymentURI.getMessage()); + assertEquals(bitcoinPaymentURI.getParameters().size(), 0); + } + + @Test + public void testParseForAddressWithAmountAndNameMethod() { + BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=20.3&label=Luke-Jr"); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertEquals(bitcoinPaymentURI.getAmount(), 20,3); + assertEquals(bitcoinPaymentURI.getLabel(), "Luke-Jr"); + assertNull(bitcoinPaymentURI.getMessage()); + assertEquals(bitcoinPaymentURI.getParameters().size(), 0); + } + + @Test + public void testParseForAddressWithAmountAndNameAndMessageAndRequiredParameterMethod() { + BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=50&label=Luke-Jr&message=Donation%20for%20project%20xyz"); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertEquals(bitcoinPaymentURI.getAmount(), Double.valueOf(50)); + assertEquals(bitcoinPaymentURI.getLabel(), "Luke-Jr"); + assertEquals(bitcoinPaymentURI.getMessage(), "Donation for project xyz"); + assertEquals(bitcoinPaymentURI.getParameters().size(), 0); + } + + @Test + public void testParseForAddressWithAmountAndNameAndMessageAndParametersMethod() { + BitcoinPaymentURI bitcoinPaymentURI = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50&somethingelseyoudontget=999&req-app=appname"); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertNull(bitcoinPaymentURI.getAmount()); + assertNull(bitcoinPaymentURI.getLabel()); + assertNull(bitcoinPaymentURI.getMessage()); + assertEquals(bitcoinPaymentURI.getParameters().size(), 3); + assertEquals(bitcoinPaymentURI.getParameters().get("somethingyoudontunderstand").getValue(), "50"); + assertEquals(bitcoinPaymentURI.getParameters().get("somethingelseyoudontget").getValue(), "999"); + assertEquals(bitcoinPaymentURI.getParameters().get("app").getValue(), "appname"); + assertTrue(bitcoinPaymentURI.getParameters().get("app").isRequired()); + } + + @Test + public void testParseForInvalidAddressesMethod() { + BitcoinPaymentURI bitcoinPaymentURI1 = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50&somethingelseyoudontget"); + BitcoinPaymentURI bitcoinPaymentURI2 = BitcoinPaymentURI.parse("bitcoinX:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50"); + BitcoinPaymentURI bitcoinPaymentURI3 = BitcoinPaymentURI.parse("bitcoin175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50"); + BitcoinPaymentURI bitcoinPaymentURI4 = BitcoinPaymentURI.parse("bitcoin:?somethingyoudontunderstand=50"); + BitcoinPaymentURI bitcoinPaymentURI5 = BitcoinPaymentURI.parse("bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label"); + + assertNull(bitcoinPaymentURI1); + assertNull(bitcoinPaymentURI2); + assertNull(bitcoinPaymentURI3); + assertNull(bitcoinPaymentURI4); + assertNull(bitcoinPaymentURI5); + } + + @Test + public void testBuilder() { + BitcoinPaymentURI bitcoinPaymentURI = new BitcoinPaymentURI.Builder() + .address("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W") + .amount(50.0) + .label("Luke-Jr") + .message("Donation for project xyz") + .parameter("foo", "bar") + .requiredParameter("fiz", "biz") + .build(); + + assertEquals(bitcoinPaymentURI.getAddress(), "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"); + assertEquals(bitcoinPaymentURI.getAmount(), Double.valueOf(50)); + assertEquals(bitcoinPaymentURI.getLabel(), "Luke-Jr"); + assertEquals(bitcoinPaymentURI.getMessage(), "Donation for project xyz"); + assertEquals(bitcoinPaymentURI.getParameters().size(), 2); + assertEquals(bitcoinPaymentURI.getParameters().get("foo").getValue(), "bar"); + assertFalse(bitcoinPaymentURI.getParameters().get("foo").isRequired()); + assertEquals(bitcoinPaymentURI.getParameters().get("fiz").getValue(), "biz"); + assertTrue(bitcoinPaymentURI.getParameters().get("fiz").isRequired()); + assertEquals(bitcoinPaymentURI.getURI(), "bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?message=Donation%20for%20project%20xyz&amount=50.0&req-fiz=biz&foo=bar&label=Luke-Jr"); + } + +}