Support generating URIs for non-Bitcoin networks

This commit is contained in:
Ross Nicoll
2015-12-22 21:14:44 +00:00
committed by Andreas Schildbach
parent 1dd64534d0
commit 159a6de24f
2 changed files with 35 additions and 17 deletions

View File

@@ -22,9 +22,6 @@ import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.AbstractBitcoinNetParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.UnsupportedEncodingException;
@@ -80,11 +77,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
* @see <a href="https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki">BIP 0021</a>
*/
public class BitcoinURI {
/**
* Provides logging for this class
*/
private static final Logger log = LoggerFactory.getLogger(BitcoinURI.class);
// Not worth turning into an enum
public static final String FIELD_MESSAGE = "message";
public static final String FIELD_LABEL = "label";
@@ -129,7 +121,6 @@ public class BitcoinURI {
*/
public BitcoinURI(@Nullable NetworkParameters params, String input) throws BitcoinURIParseException {
checkNotNull(input);
log.debug("Attempting to parse '{}' for {}", input, params == null ? "any" : params.getId());
String scheme = null == params
? AbstractBitcoinNetParams.BITCOIN_SCHEME
@@ -345,28 +336,43 @@ public class BitcoinURI {
return builder.toString();
}
public static String convertToBitcoinURI(Address address, Coin amount, String label, String message) {
return convertToBitcoinURI(address.toString(), amount, label, message);
}
/**
* Simple Bitcoin URI builder using known good fields.
*
*
* @param address The Bitcoin address
* @param amount The amount
* @param label A label
* @param message A message
* @return A String containing the Bitcoin URI
*/
public static String convertToBitcoinURI(String address, @Nullable Coin amount, @Nullable String label,
@Nullable String message) {
public static String convertToBitcoinURI(Address address, Coin amount,
String label, String message) {
return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message);
}
/**
* Simple Bitcoin URI builder using known good fields.
*
* @param params The network parameters that determine which network the URI
* is for.
* @param address The Bitcoin address
* @param amount The amount
* @param label A label
* @param message A message
* @return A String containing the Bitcoin URI
*/
public static String convertToBitcoinURI(NetworkParameters params,
String address, @Nullable Coin amount,
@Nullable String label, @Nullable String message) {
checkNotNull(params);
checkNotNull(address);
if (amount != null && amount.signum() < 0) {
throw new IllegalArgumentException("Coin must be positive");
}
StringBuilder builder = new StringBuilder();
builder.append(BITCOIN_SCHEME).append(":").append(address);
String scheme = params.getUriScheme();
builder.append(scheme).append(":").append(address);
boolean questionMarkHasBeenOutput = false;

View File

@@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList;
import org.junit.Test;
import static org.bitcoinj.core.Coin.*;
import org.bitcoinj.core.NetworkParameters;
import static org.junit.Assert.*;
public class BitcoinURITest {
@@ -66,6 +67,17 @@ public class BitcoinURITest {
// no amount, no label, no message
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));
// different scheme
final NetworkParameters alternativeParameters = new MainNetParams() {
@Override
public String getUriScheme() {
return "test";
}
};
assertEquals("test:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage",
BitcoinURI.convertToBitcoinURI(Address.fromBase58(alternativeParameters, MAINNET_GOOD_ADDRESS), parseCoin("12.34"), "Hello", "AMessage"));
}
@Test