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,42 +84,46 @@ 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) {
try { address = address.toLowerCase();
byte[] decodedBytes = Base58.decodeChecked(address);
if(decodedBytes.length == 21) {
int version = Byte.toUnsignedInt(decodedBytes[0]);
byte[] hash = Arrays.copyOfRange(decodedBytes, 1, 21);
if(version == network.getP2PKHAddressHeader()) {
return new P2PKHAddress(hash);
}
if(version == network.getP2SHAddressHeader()) {
return new P2SHAddress(hash);
}
}
} catch (Exception e) {
nested = e;
}
}
if(address != null && address.startsWith(network.getBech32AddressHRP())) { if(network.hasP2PKHAddressPrefix(address) || network.hasP2SHAddressPrefix(address)) {
try { try {
Bech32.Bech32Data data = Bech32.decode(address); byte[] decodedBytes = Base58.decodeChecked(address);
if(data.hrp.equals(network.getBech32AddressHRP())) { if(decodedBytes.length == 21) {
int witnessVersion = data.data[0]; int version = Byte.toUnsignedInt(decodedBytes[0]);
if (witnessVersion == 0) { byte[] hash = Arrays.copyOfRange(decodedBytes, 1, 21);
byte[] convertedProgram = Arrays.copyOfRange(data.data, 1, data.data.length); if(version == network.getP2PKHAddressHeader()) {
byte[] witnessProgram = Bech32.convertBits(convertedProgram, 0, convertedProgram.length, 5, 8, false); return new P2PKHAddress(hash);
if (witnessProgram.length == 20) {
return new P2WPKHAddress(witnessProgram);
} }
if (witnessProgram.length == 32) { if(version == network.getP2SHAddressHeader()) {
return new P2WSHAddress(witnessProgram); return new P2SHAddress(hash);
} }
} }
} catch (Exception e) {
nested = e;
}
}
if(address.startsWith(network.getBech32AddressHRP())) {
try {
Bech32.Bech32Data data = Bech32.decode(address);
if(data.hrp.equals(network.getBech32AddressHRP())) {
int witnessVersion = data.data[0];
if (witnessVersion == 0) {
byte[] convertedProgram = Arrays.copyOfRange(data.data, 1, data.data.length);
byte[] witnessProgram = Bech32.convertBits(convertedProgram, 0, convertedProgram.length, 5, 8, false);
if (witnessProgram.length == 20) {
return new P2WPKHAddress(witnessProgram);
}
if (witnessProgram.length == 32) {
return new P2WSHAddress(witnessProgram);
}
}
}
} catch (Exception e) {
nested = e;
} }
} catch (Exception e) {
nested = e;
} }
} }

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());
}
}