mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-22 22:25:41 +01:00
BitcoinURI: migrate to static from native constructors
This commit is contained in:
parent
429dfedce9
commit
5db73f2d7a
4 changed files with 52 additions and 48 deletions
|
@ -106,9 +106,20 @@ public class BitcoinURI {
|
||||||
* @param uri The raw URI data to be parsed (see class comments for accepted formats)
|
* @param 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.
|
* @throws BitcoinURIParseException if the URI is not syntactically or semantically valid.
|
||||||
*/
|
*/
|
||||||
public BitcoinURI(String uri) throws BitcoinURIParseException {
|
public static BitcoinURI of(String uri) throws BitcoinURIParseException {
|
||||||
// TODO: Discover (via Service Loader mechanism) the correct Network from the URI string
|
// TODO: Discover (via Service Loader mechanism) the correct Network from the URI string
|
||||||
this(uri, BitcoinNetwork.MAINNET);
|
return new BitcoinURI(uri, BitcoinNetwork.MAINNET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new object by trying to parse the input as a valid Bitcoin URI.
|
||||||
|
*
|
||||||
|
* @param uri The raw URI data to be parsed (see class comments for accepted formats)
|
||||||
|
* @param network The network the URI is from
|
||||||
|
* @throws BitcoinURIParseException If the input fails Bitcoin URI syntax and semantic checks.
|
||||||
|
*/
|
||||||
|
public static BitcoinURI of(String uri, @Nonnull Network network) throws BitcoinURIParseException {
|
||||||
|
return new BitcoinURI(uri, network);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,21 +130,14 @@ public class BitcoinURI {
|
||||||
* @param input The raw URI data to be parsed (see class comments for accepted formats)
|
* @param 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.
|
* @throws BitcoinURIParseException If the input fails Bitcoin URI syntax and semantic checks.
|
||||||
* @deprecated Use {@link BitcoinURI#BitcoinURI(String, Network)} or {@link BitcoinURI#BitcoinURI(String)}
|
* @deprecated Use {@link BitcoinURI#of(String, Network)} or {@link BitcoinURI#of(String)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public BitcoinURI(@Nullable NetworkParameters params, String input) throws BitcoinURIParseException {
|
public BitcoinURI(@Nullable NetworkParameters params, String input) throws BitcoinURIParseException {
|
||||||
this(input, params != null ? params.network() : BitcoinNetwork.MAINNET);
|
this(input, params != null ? params.network() : BitcoinNetwork.MAINNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private BitcoinURI(String input, @Nonnull Network network) throws BitcoinURIParseException {
|
||||||
* Constructs a new object by trying to parse the input as a valid Bitcoin URI.
|
|
||||||
*
|
|
||||||
* @param input The raw URI data to be parsed (see class comments for accepted formats)
|
|
||||||
* @param network The network the URI is from
|
|
||||||
* @throws BitcoinURIParseException If the input fails Bitcoin URI syntax and semantic checks.
|
|
||||||
*/
|
|
||||||
public BitcoinURI(String input, @Nonnull Network network) throws BitcoinURIParseException {
|
|
||||||
Objects.requireNonNull(network);
|
Objects.requireNonNull(network);
|
||||||
Objects.requireNonNull(input);
|
Objects.requireNonNull(input);
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGood_legacy() throws BitcoinURIParseException {
|
public void testGood_legacy() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS, MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS, MAINNET);
|
||||||
assertEquals(MAINNET_GOOD_ADDRESS, testObject.getAddress().toString());
|
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());
|
||||||
|
@ -114,7 +114,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGood_uppercaseScheme() throws BitcoinURIParseException {
|
public void testGood_uppercaseScheme() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME.toUpperCase(Locale.US) + ":" + MAINNET_GOOD_ADDRESS, MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME.toUpperCase(Locale.US) + ":" + MAINNET_GOOD_ADDRESS, MAINNET);
|
||||||
assertEquals(MAINNET_GOOD_ADDRESS, testObject.getAddress().toString());
|
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());
|
||||||
|
@ -123,7 +123,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGood_segwit() throws BitcoinURIParseException {
|
public void testGood_segwit() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_SEGWIT_ADDRESS, MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_SEGWIT_ADDRESS, MAINNET);
|
||||||
assertEquals(MAINNET_GOOD_SEGWIT_ADDRESS, testObject.getAddress().toString());
|
assertEquals(MAINNET_GOOD_SEGWIT_ADDRESS, testObject.getAddress().toString());
|
||||||
assertNull("Unexpected amount", testObject.getAmount());
|
assertNull("Unexpected amount", testObject.getAmount());
|
||||||
assertNull("Unexpected label", testObject.getLabel());
|
assertNull("Unexpected label", testObject.getLabel());
|
||||||
|
@ -135,7 +135,7 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testBad_Scheme() {
|
public void testBad_Scheme() {
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI("blimpcoin:" + MAINNET_GOOD_ADDRESS, MAINNET);
|
testObject = BitcoinURI.of("blimpcoin:" + MAINNET_GOOD_ADDRESS, MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
}
|
}
|
||||||
|
@ -148,14 +148,14 @@ public class BitcoinURITest {
|
||||||
public void testBad_BadSyntax() {
|
public void testBad_BadSyntax() {
|
||||||
// Various illegal characters
|
// Various illegal characters
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + "|" + MAINNET_GOOD_ADDRESS, MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + "|" + MAINNET_GOOD_ADDRESS, MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "\\", MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "\\", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
||||||
|
@ -163,7 +163,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
// Separator without field
|
// Separator without field
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":", MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
assertTrue(e.getMessage().contains("Bad URI syntax"));
|
||||||
|
@ -176,7 +176,7 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testBad_Address() {
|
public void testBad_Address() {
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME, MAINNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME, MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testBad_IncorrectAddressType() {
|
public void testBad_IncorrectAddressType() {
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS, TESTNET);
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS, TESTNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
assertTrue(e.getMessage().contains("Bad address"));
|
assertTrue(e.getMessage().contains("Bad address"));
|
||||||
|
@ -204,22 +204,22 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testGood_Amount() throws BitcoinURIParseException {
|
public void testGood_Amount() throws BitcoinURIParseException {
|
||||||
// Test the decimal parsing
|
// Test the decimal parsing
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=6543210.12345678", MAINNET);
|
+ "?amount=6543210.12345678", MAINNET);
|
||||||
assertEquals("654321012345678", testObject.getAmount().toString());
|
assertEquals("654321012345678", testObject.getAmount().toString());
|
||||||
|
|
||||||
// Test the decimal parsing
|
// Test the decimal parsing
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=.12345678", MAINNET);
|
+ "?amount=.12345678", MAINNET);
|
||||||
assertEquals("12345678", testObject.getAmount().toString());
|
assertEquals("12345678", testObject.getAmount().toString());
|
||||||
|
|
||||||
// Test the integer parsing
|
// Test the integer parsing
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=6543210", MAINNET);
|
+ "?amount=6543210", MAINNET);
|
||||||
assertEquals("654321000000000", testObject.getAmount().toString());
|
assertEquals("654321000000000", testObject.getAmount().toString());
|
||||||
|
|
||||||
// the maximum amount
|
// the maximum amount
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=" + new BigDecimal(Long.MAX_VALUE).movePointLeft(8), MAINNET);
|
+ "?amount=" + new BigDecimal(Long.MAX_VALUE).movePointLeft(8), MAINNET);
|
||||||
assertEquals(Long.MAX_VALUE, testObject.getAmount().longValue());
|
assertEquals(Long.MAX_VALUE, testObject.getAmount().longValue());
|
||||||
}
|
}
|
||||||
|
@ -232,7 +232,7 @@ public class BitcoinURITest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGood_Label() throws BitcoinURIParseException {
|
public void testGood_Label() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?label=Hello%20World", MAINNET);
|
+ "?label=Hello%20World", MAINNET);
|
||||||
assertEquals("Hello World", testObject.getLabel());
|
assertEquals("Hello World", testObject.getLabel());
|
||||||
}
|
}
|
||||||
|
@ -247,7 +247,7 @@ public class BitcoinURITest {
|
||||||
public void testGood_LabelWithAmpersandAndPlus() throws BitcoinURIParseException {
|
public void testGood_LabelWithAmpersandAndPlus() throws BitcoinURIParseException {
|
||||||
String testString = "Hello Earth & Mars + Venus";
|
String testString = "Hello Earth & Mars + Venus";
|
||||||
String encodedLabel = BitcoinURI.encodeURLString(testString);
|
String encodedLabel = BitcoinURI.encodeURLString(testString);
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
|
||||||
+ encodedLabel, MAINNET);
|
+ encodedLabel, MAINNET);
|
||||||
assertEquals(testString, testObject.getLabel());
|
assertEquals(testString, testObject.getLabel());
|
||||||
}
|
}
|
||||||
|
@ -263,7 +263,7 @@ public class BitcoinURITest {
|
||||||
// Moscow in Russian in Cyrillic
|
// Moscow in Russian in Cyrillic
|
||||||
String moscowString = "\u041c\u043e\u0441\u043a\u0432\u0430";
|
String moscowString = "\u041c\u043e\u0441\u043a\u0432\u0430";
|
||||||
String encodedLabel = BitcoinURI.encodeURLString(moscowString);
|
String encodedLabel = BitcoinURI.encodeURLString(moscowString);
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
|
||||||
+ encodedLabel, MAINNET);
|
+ encodedLabel, MAINNET);
|
||||||
assertEquals(moscowString, testObject.getLabel());
|
assertEquals(moscowString, testObject.getLabel());
|
||||||
}
|
}
|
||||||
|
@ -276,7 +276,7 @@ public class BitcoinURITest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGood_Message() throws BitcoinURIParseException {
|
public void testGood_Message() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?message=Hello%20World", MAINNET);
|
+ "?message=Hello%20World", MAINNET);
|
||||||
assertEquals("Hello World", testObject.getMessage());
|
assertEquals("Hello World", testObject.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@ public class BitcoinURITest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGood_Combinations() throws BitcoinURIParseException {
|
public void testGood_Combinations() throws BitcoinURIParseException {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=6543210&label=Hello%20World&message=Be%20well", MAINNET);
|
+ "?amount=6543210&label=Hello%20World&message=Be%20well", MAINNET);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"BitcoinURI['amount'='654321000000000','label'='Hello World','message'='Be well','address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH']",
|
"BitcoinURI['amount'='654321000000000','label'='Hello World','message'='Be well','address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH']",
|
||||||
|
@ -303,7 +303,7 @@ public class BitcoinURITest {
|
||||||
public void testBad_Amount() {
|
public void testBad_Amount() {
|
||||||
// Missing
|
// Missing
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=", MAINNET);
|
+ "?amount=", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
|
@ -312,7 +312,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
// Non-decimal (BIP 21)
|
// Non-decimal (BIP 21)
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=12X4", MAINNET);
|
+ "?amount=12X4", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
|
@ -322,13 +322,13 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmpty_Label() throws BitcoinURIParseException {
|
public void testEmpty_Label() throws BitcoinURIParseException {
|
||||||
assertNull(new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
assertNull(BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?label=", MAINNET).getLabel());
|
+ "?label=", MAINNET).getLabel());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmpty_Message() throws BitcoinURIParseException {
|
public void testEmpty_Message() throws BitcoinURIParseException {
|
||||||
assertNull(new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
assertNull(BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?message=", MAINNET).getMessage());
|
+ "?message=", MAINNET).getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,7 +338,7 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testBad_Duplicated() {
|
public void testBad_Duplicated() {
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?address=aardvark", MAINNET);
|
+ "?address=aardvark", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
|
@ -348,7 +348,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGood_ManyEquals() throws BitcoinURIParseException {
|
public void testGood_ManyEquals() throws BitcoinURIParseException {
|
||||||
assertEquals("aardvark=zebra", new BitcoinURI(BITCOIN_SCHEME + ":"
|
assertEquals("aardvark=zebra", BitcoinURI.of(BITCOIN_SCHEME + ":"
|
||||||
+ MAINNET_GOOD_ADDRESS + "?label=aardvark=zebra", MAINNET).getLabel());
|
+ MAINNET_GOOD_ADDRESS + "?label=aardvark=zebra", MAINNET).getLabel());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,7 +361,7 @@ public class BitcoinURITest {
|
||||||
@Test
|
@Test
|
||||||
public void testUnknown() throws BitcoinURIParseException {
|
public void testUnknown() throws BitcoinURIParseException {
|
||||||
// Unknown not required field
|
// Unknown not required field
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?aardvark=true", MAINNET);
|
+ "?aardvark=true", MAINNET);
|
||||||
assertEquals("BitcoinURI['aardvark'='true','address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH']", testObject.toString());
|
assertEquals("BitcoinURI['aardvark'='true','address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH']", testObject.toString());
|
||||||
|
|
||||||
|
@ -369,7 +369,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
// Unknown not required field (isolated)
|
// Unknown not required field (isolated)
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?aardvark", MAINNET);
|
+ "?aardvark", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
|
@ -378,7 +378,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
// Unknown and required field
|
// Unknown and required field
|
||||||
try {
|
try {
|
||||||
testObject = new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
testObject = BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?req-aardvark=true", MAINNET);
|
+ "?req-aardvark=true", MAINNET);
|
||||||
fail("Expecting BitcoinURIParseException");
|
fail("Expecting BitcoinURIParseException");
|
||||||
} catch (BitcoinURIParseException e) {
|
} catch (BitcoinURIParseException e) {
|
||||||
|
@ -390,34 +390,34 @@ public class BitcoinURITest {
|
||||||
public void brokenURIs() throws BitcoinURIParseException {
|
public void brokenURIs() throws BitcoinURIParseException {
|
||||||
// Check we can parse the incorrectly formatted URIs produced by blockchain.info and its iPhone app.
|
// Check we can parse the incorrectly formatted URIs produced by blockchain.info and its iPhone app.
|
||||||
String str = "bitcoin://1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH?amount=0.01000000";
|
String str = "bitcoin://1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH?amount=0.01000000";
|
||||||
BitcoinURI uri = new BitcoinURI(str);
|
BitcoinURI uri = BitcoinURI.of(str);
|
||||||
assertEquals("1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH", uri.getAddress().toString());
|
assertEquals("1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH", uri.getAddress().toString());
|
||||||
assertEquals(CENT, uri.getAmount());
|
assertEquals(CENT, uri.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BitcoinURIParseException.class)
|
@Test(expected = BitcoinURIParseException.class)
|
||||||
public void testBad_AmountTooPrecise() throws BitcoinURIParseException {
|
public void testBad_AmountTooPrecise() throws BitcoinURIParseException {
|
||||||
new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=0.123456789", MAINNET);
|
+ "?amount=0.123456789", MAINNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BitcoinURIParseException.class)
|
@Test(expected = BitcoinURIParseException.class)
|
||||||
public void testBad_NegativeAmount() throws BitcoinURIParseException {
|
public void testBad_NegativeAmount() throws BitcoinURIParseException {
|
||||||
new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=-1", MAINNET);
|
+ "?amount=-1", MAINNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BitcoinURIParseException.class)
|
@Test(expected = BitcoinURIParseException.class)
|
||||||
public void testBad_TooLargeAmount() throws BitcoinURIParseException {
|
public void testBad_TooLargeAmount() throws BitcoinURIParseException {
|
||||||
BigDecimal tooLargeByOne = new BigDecimal(Long.MAX_VALUE).add(BigDecimal.ONE);
|
BigDecimal tooLargeByOne = new BigDecimal(Long.MAX_VALUE).add(BigDecimal.ONE);
|
||||||
new BitcoinURI(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
BitcoinURI.of(BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
|
||||||
+ "?amount=" + tooLargeByOne.movePointLeft(8), MAINNET);
|
+ "?amount=" + tooLargeByOne.movePointLeft(8), MAINNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPaymentProtocolReq() throws Exception {
|
public void testPaymentProtocolReq() throws Exception {
|
||||||
// Non-backwards compatible form ...
|
// Non-backwards compatible form ...
|
||||||
BitcoinURI uri = new BitcoinURI("bitcoin:?r=https%3A%2F%2Fbitcoincore.org%2F%7Egavin%2Ff.php%3Fh%3Db0f02e7cea67f168e25ec9b9f9d584f9", TESTNET);
|
BitcoinURI uri = BitcoinURI.of("bitcoin:?r=https%3A%2F%2Fbitcoincore.org%2F%7Egavin%2Ff.php%3Fh%3Db0f02e7cea67f168e25ec9b9f9d584f9", TESTNET);
|
||||||
assertEquals("https://bitcoincore.org/~gavin/f.php?h=b0f02e7cea67f168e25ec9b9f9d584f9", uri.getPaymentRequestUrl());
|
assertEquals("https://bitcoincore.org/~gavin/f.php?h=b0f02e7cea67f168e25ec9b9f9d584f9", uri.getPaymentRequestUrl());
|
||||||
assertEquals(Collections.singletonList("https://bitcoincore.org/~gavin/f.php?h=b0f02e7cea67f168e25ec9b9f9d584f9"),
|
assertEquals(Collections.singletonList("https://bitcoincore.org/~gavin/f.php?h=b0f02e7cea67f168e25ec9b9f9d584f9"),
|
||||||
uri.getPaymentRequestUrls());
|
uri.getPaymentRequestUrls());
|
||||||
|
@ -426,14 +426,14 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiplePaymentProtocolReq() throws Exception {
|
public void testMultiplePaymentProtocolReq() throws Exception {
|
||||||
BitcoinURI uri = new BitcoinURI("bitcoin:?r=https%3A%2F%2Fbitcoincore.org%2F%7Egavin&r1=bt:112233445566", MAINNET);
|
BitcoinURI uri = BitcoinURI.of("bitcoin:?r=https%3A%2F%2Fbitcoincore.org%2F%7Egavin&r1=bt:112233445566", MAINNET);
|
||||||
assertEquals(Arrays.asList("bt:112233445566", "https://bitcoincore.org/~gavin"), uri.getPaymentRequestUrls());
|
assertEquals(Arrays.asList("bt:112233445566", "https://bitcoincore.org/~gavin"), uri.getPaymentRequestUrls());
|
||||||
assertEquals("https://bitcoincore.org/~gavin", uri.getPaymentRequestUrl());
|
assertEquals("https://bitcoincore.org/~gavin", uri.getPaymentRequestUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoPaymentProtocolReq() throws Exception {
|
public void testNoPaymentProtocolReq() throws Exception {
|
||||||
BitcoinURI uri = new BitcoinURI("bitcoin:" + MAINNET_GOOD_ADDRESS, MAINNET);
|
BitcoinURI uri = BitcoinURI.of("bitcoin:" + MAINNET_GOOD_ADDRESS, MAINNET);
|
||||||
assertNull(uri.getPaymentRequestUrl());
|
assertNull(uri.getPaymentRequestUrl());
|
||||||
assertEquals(Collections.emptyList(), uri.getPaymentRequestUrls());
|
assertEquals(Collections.emptyList(), uri.getPaymentRequestUrls());
|
||||||
assertNotNull(uri.getAddress());
|
assertNotNull(uri.getAddress());
|
||||||
|
@ -441,7 +441,7 @@ public class BitcoinURITest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnescapedPaymentProtocolReq() throws Exception {
|
public void testUnescapedPaymentProtocolReq() throws Exception {
|
||||||
BitcoinURI uri = new BitcoinURI("bitcoin:?r=https://merchant.com/pay.php?h%3D2a8628fc2fbe", TESTNET);
|
BitcoinURI uri = BitcoinURI.of("bitcoin:?r=https://merchant.com/pay.php?h%3D2a8628fc2fbe", TESTNET);
|
||||||
assertEquals("https://merchant.com/pay.php?h=2a8628fc2fbe", uri.getPaymentRequestUrl());
|
assertEquals("https://merchant.com/pay.php?h=2a8628fc2fbe", uri.getPaymentRequestUrl());
|
||||||
assertEquals(Collections.singletonList("https://merchant.com/pay.php?h=2a8628fc2fbe"), uri.getPaymentRequestUrls());
|
assertEquals(Collections.singletonList("https://merchant.com/pay.php?h=2a8628fc2fbe"), uri.getPaymentRequestUrls());
|
||||||
assertNull(uri.getAddress());
|
assertNull(uri.getAddress());
|
||||||
|
|
|
@ -60,7 +60,7 @@ public class PaymentProtocolTool {
|
||||||
} else if ("http".equals(uri.getScheme())) {
|
} else if ("http".equals(uri.getScheme())) {
|
||||||
session = PaymentSession.createFromUrl(arg).get();
|
session = PaymentSession.createFromUrl(arg).get();
|
||||||
} else if ("bitcoin".equals(uri.getScheme())) {
|
} else if ("bitcoin".equals(uri.getScheme())) {
|
||||||
BitcoinURI bcuri = new BitcoinURI(arg);
|
BitcoinURI bcuri = BitcoinURI.of(arg);
|
||||||
final String paymentRequestUrl = bcuri.getPaymentRequestUrl();
|
final String paymentRequestUrl = bcuri.getPaymentRequestUrl();
|
||||||
if (paymentRequestUrl == null) {
|
if (paymentRequestUrl == null) {
|
||||||
System.err.println("No r= param in bitcoin URI");
|
System.err.println("No r= param in bitcoin URI");
|
||||||
|
|
|
@ -796,7 +796,7 @@ public class WalletTool implements Callable<Integer> {
|
||||||
if (location.startsWith("http")) {
|
if (location.startsWith("http")) {
|
||||||
future = PaymentSession.createFromUrl(location, verifyPki);
|
future = PaymentSession.createFromUrl(location, verifyPki);
|
||||||
} else {
|
} else {
|
||||||
BitcoinURI paymentRequestURI = new BitcoinURI(location);
|
BitcoinURI paymentRequestURI = BitcoinURI.of(location);
|
||||||
future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
|
future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
|
||||||
}
|
}
|
||||||
PaymentSession session = future.get();
|
PaymentSession session = future.get();
|
||||||
|
|
Loading…
Add table
Reference in a new issue