mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-24 14:50:57 +01:00
BitcoinURI: Make aware of native segwit addresses.
This commit is contained in:
parent
d2caf699e1
commit
2e8a7474b2
2 changed files with 24 additions and 7 deletions
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
package org.bitcoinj.uri;
|
package org.bitcoinj.uri;
|
||||||
|
|
||||||
import org.bitcoinj.core.LegacyAddress;
|
import org.bitcoinj.core.Address;
|
||||||
import org.bitcoinj.core.AddressFormatException;
|
import org.bitcoinj.core.AddressFormatException;
|
||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
import org.bitcoinj.core.NetworkParameters;
|
import org.bitcoinj.core.NetworkParameters;
|
||||||
|
@ -175,7 +175,7 @@ public class BitcoinURI {
|
||||||
if (!addressToken.isEmpty()) {
|
if (!addressToken.isEmpty()) {
|
||||||
// Attempt to parse the addressToken as a Bitcoin address for this network
|
// Attempt to parse the addressToken as a Bitcoin address for this network
|
||||||
try {
|
try {
|
||||||
LegacyAddress address = LegacyAddress.fromBase58(params, addressToken);
|
Address address = Address.fromString(params, addressToken);
|
||||||
putWithValidation(FIELD_ADDRESS, address);
|
putWithValidation(FIELD_ADDRESS, address);
|
||||||
} catch (final AddressFormatException e) {
|
} catch (final AddressFormatException e) {
|
||||||
throw new BitcoinURIParseException("Bad address", e);
|
throw new BitcoinURIParseException("Bad address", e);
|
||||||
|
@ -260,8 +260,8 @@ public class BitcoinURI {
|
||||||
* it.
|
* it.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public LegacyAddress getAddress() {
|
public Address getAddress() {
|
||||||
return (LegacyAddress) parameterMap.get(FIELD_ADDRESS);
|
return (Address) parameterMap.get(FIELD_ADDRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -345,7 +345,7 @@ public class BitcoinURI {
|
||||||
* @param message A message
|
* @param message A message
|
||||||
* @return A String containing the Bitcoin URI
|
* @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) {
|
String label, String message) {
|
||||||
return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message);
|
return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ import org.junit.Test;
|
||||||
|
|
||||||
import static org.bitcoinj.core.Coin.*;
|
import static org.bitcoinj.core.Coin.*;
|
||||||
import org.bitcoinj.core.NetworkParameters;
|
import org.bitcoinj.core.NetworkParameters;
|
||||||
|
import org.bitcoinj.core.SegwitAddress;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class BitcoinURITest {
|
public class BitcoinURITest {
|
||||||
|
@ -32,6 +34,7 @@ public class BitcoinURITest {
|
||||||
private static final NetworkParameters MAINNET = MainNetParams.get();
|
private static final NetworkParameters MAINNET = MainNetParams.get();
|
||||||
private static final NetworkParameters TESTNET = TestNet3Params.get();
|
private static final NetworkParameters TESTNET = TestNet3Params.get();
|
||||||
private static final String MAINNET_GOOD_ADDRESS = "1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH";
|
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();
|
private static final String BITCOIN_SCHEME = MAINNET.getUriScheme();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -83,14 +86,28 @@ public class BitcoinURITest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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);
|
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 amount", testObject.getAmount());
|
||||||
assertNull("Unexpected label", testObject.getLabel());
|
assertNull("Unexpected label", testObject.getLabel());
|
||||||
assertEquals("Unexpected label", 20, testObject.getAddress().getHash().length);
|
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)
|
* Test a broken URI (bad scheme)
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue