samourai bitcoin uri compatibility

This commit is contained in:
Craig Raw 2020-10-02 10:51:50 +02:00
parent 9c6d3ec94b
commit 32b0af7381
3 changed files with 51 additions and 32 deletions

View file

@ -84,7 +84,10 @@ public abstract class Address {
public static Address fromString(Network network, String address) throws InvalidAddressException { public static Address fromString(Network network, String address) throws InvalidAddressException {
Exception nested = null; Exception nested = null;
if(address != null && (network.hasP2PKHAddressPrefix(address) || network.hasP2SHAddressPrefix(address))) { if(address != null) {
address = address.toLowerCase();
if(network.hasP2PKHAddressPrefix(address) || network.hasP2SHAddressPrefix(address)) {
try { try {
byte[] decodedBytes = Base58.decodeChecked(address); byte[] decodedBytes = Base58.decodeChecked(address);
if(decodedBytes.length == 21) { if(decodedBytes.length == 21) {
@ -102,7 +105,7 @@ public abstract class Address {
} }
} }
if(address != null && address.startsWith(network.getBech32AddressHRP())) { if(address.startsWith(network.getBech32AddressHRP())) {
try { try {
Bech32.Bech32Data data = Bech32.decode(address); Bech32.Bech32Data data = Bech32.decode(address);
if(data.hrp.equals(network.getBech32AddressHRP())) { if(data.hrp.equals(network.getBech32AddressHRP())) {
@ -122,6 +125,7 @@ public abstract class Address {
nested = e; nested = e;
} }
} }
}
if(nested != null) { if(nested != null) {
throw new InvalidAddressException("Could not parse invalid address " + address, nested); throw new InvalidAddressException("Could not parse invalid address " + address, nested);

View file

@ -165,7 +165,7 @@ public class BitcoinURI {
if(FIELD_AMOUNT.equals(nameToken) && !valueToken.isEmpty()) { if(FIELD_AMOUNT.equals(nameToken) && !valueToken.isEmpty()) {
// Decode the amount (contains an optional decimal component to 8dp). // Decode the amount (contains an optional decimal component to 8dp).
try { try {
long amount = new BigDecimal(valueToken).movePointRight(SMALLEST_UNIT_EXPONENT).longValueExact(); long amount = new BigDecimal(valueToken.replace(',', '.')).movePointRight(SMALLEST_UNIT_EXPONENT).longValueExact();
if(amount > MAX_BITCOIN * SATOSHIS_PER_BITCOIN) { if(amount > MAX_BITCOIN * SATOSHIS_PER_BITCOIN) {
throw new BitcoinURIParseException("Maximum amount exceeded"); throw new BitcoinURIParseException("Maximum amount exceeded");
} }

View file

@ -0,0 +1,15 @@
package com.sparrowwallet.drongo.uri;
import org.junit.Assert;
import org.junit.Test;
public class BitcoinUriTest {
@Test
public void testSamourai() throws BitcoinURIParseException {
String uri = "bitcoin:BC1QT4NRM47695YWDG9N30N68JARMXRJNKFMR36994?amount=0,001";
BitcoinURI bitcoinURI = new BitcoinURI(uri);
Assert.assertEquals("BC1QT4NRM47695YWDG9N30N68JARMXRJNKFMR36994".toLowerCase(), bitcoinURI.getAddress().toString());
Assert.assertEquals(Long.valueOf(100000), bitcoinURI.getAmount());
}
}