mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-26 01:56:44 +00:00
samourai bitcoin uri compatibility
This commit is contained in:
parent
9c6d3ec94b
commit
32b0af7381
3 changed files with 51 additions and 32 deletions
|
@ -84,42 +84,46 @@ public abstract class Address {
|
|||
public static Address fromString(Network network, String address) throws InvalidAddressException {
|
||||
Exception nested = null;
|
||||
|
||||
if(address != null && (network.hasP2PKHAddressPrefix(address) || network.hasP2SHAddressPrefix(address))) {
|
||||
try {
|
||||
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 = address.toLowerCase();
|
||||
|
||||
if(address != null && 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(network.hasP2PKHAddressPrefix(address) || network.hasP2SHAddressPrefix(address)) {
|
||||
try {
|
||||
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 (witnessProgram.length == 32) {
|
||||
return new P2WSHAddress(witnessProgram);
|
||||
if(version == network.getP2SHAddressHeader()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -165,7 +165,7 @@ public class BitcoinURI {
|
|||
if(FIELD_AMOUNT.equals(nameToken) && !valueToken.isEmpty()) {
|
||||
// Decode the amount (contains an optional decimal component to 8dp).
|
||||
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) {
|
||||
throw new BitcoinURIParseException("Maximum amount exceeded");
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue