BitcoinURI: Make aware of native segwit addresses.

This commit is contained in:
Andreas Schildbach 2018-02-23 18:10:33 +01:00
parent d2caf699e1
commit 2e8a7474b2
2 changed files with 24 additions and 7 deletions

View file

@ -16,7 +16,7 @@
package org.bitcoinj.uri;
import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
@ -175,7 +175,7 @@ public class BitcoinURI {
if (!addressToken.isEmpty()) {
// Attempt to parse the addressToken as a Bitcoin address for this network
try {
LegacyAddress address = LegacyAddress.fromBase58(params, addressToken);
Address address = Address.fromString(params, addressToken);
putWithValidation(FIELD_ADDRESS, address);
} catch (final AddressFormatException e) {
throw new BitcoinURIParseException("Bad address", e);
@ -260,8 +260,8 @@ public class BitcoinURI {
* it.
*/
@Nullable
public LegacyAddress getAddress() {
return (LegacyAddress) parameterMap.get(FIELD_ADDRESS);
public Address getAddress() {
return (Address) parameterMap.get(FIELD_ADDRESS);
}
/**
@ -345,7 +345,7 @@ public class BitcoinURI {
* @param message A message
* @return A String containing the Bitcoin URI
*/
public static String convertToBitcoinURI(LegacyAddress address, Coin amount,
public static String convertToBitcoinURI(Address address, Coin amount,
String label, String message) {
return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message);
}

View file

@ -24,6 +24,8 @@ import org.junit.Test;
import static org.bitcoinj.core.Coin.*;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.SegwitAddress;
import static org.junit.Assert.*;
public class BitcoinURITest {
@ -32,6 +34,7 @@ public class BitcoinURITest {
private static final NetworkParameters MAINNET = MainNetParams.get();
private static final NetworkParameters TESTNET = TestNet3Params.get();
private static final String MAINNET_GOOD_ADDRESS = "1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH";
private static final String MAINNET_GOOD_SEGWIT_ADDRESS = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
private static final String BITCOIN_SCHEME = MAINNET.getUriScheme();
@Test
@ -83,14 +86,28 @@ public class BitcoinURITest {
}
@Test
public void testGood_Simple() throws BitcoinURIParseException {
public void testConvertToBitcoinURI_segwit() throws Exception {
assertEquals("bitcoin:" + MAINNET_GOOD_SEGWIT_ADDRESS + "?message=segwit%20rules", BitcoinURI.convertToBitcoinURI(
SegwitAddress.fromBech32(MAINNET, MAINNET_GOOD_SEGWIT_ADDRESS), null, null, "segwit rules"));
}
@Test
public void testGood_legacy() throws BitcoinURIParseException {
testObject = new BitcoinURI(MAINNET, BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS);
assertNotNull(testObject);
assertEquals(MAINNET_GOOD_ADDRESS, testObject.getAddress().toString());
assertNull("Unexpected amount", testObject.getAmount());
assertNull("Unexpected label", testObject.getLabel());
assertEquals("Unexpected label", 20, testObject.getAddress().getHash().length);
}
@Test
public void testGood_segwit() throws BitcoinURIParseException {
testObject = new BitcoinURI(MAINNET, BITCOIN_SCHEME + ":" + MAINNET_GOOD_SEGWIT_ADDRESS);
assertEquals(MAINNET_GOOD_SEGWIT_ADDRESS, testObject.getAddress().toString());
assertNull("Unexpected amount", testObject.getAmount());
assertNull("Unexpected label", testObject.getLabel());
}
/**
* Test a broken URI (bad scheme)
*/