mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-02 18:26:43 +00:00
address and related optimizations
This commit is contained in:
parent
fefebbabb5
commit
9ae1f68dc4
11 changed files with 48 additions and 50 deletions
|
@ -9,14 +9,14 @@ import com.sparrowwallet.drongo.protocol.ScriptType;
|
|||
import java.util.Arrays;
|
||||
|
||||
public abstract class Address {
|
||||
protected final byte[] hash;
|
||||
protected final byte[] data;
|
||||
|
||||
public Address(byte[] hash) {
|
||||
this.hash = hash;
|
||||
public Address(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public byte[] getHash() {
|
||||
return hash;
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
|
@ -24,7 +24,7 @@ public abstract class Address {
|
|||
}
|
||||
|
||||
public String getAddress(Network network) {
|
||||
return Base58.encodeChecked(getVersion(network), hash);
|
||||
return Base58.encodeChecked(getVersion(network), data);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
@ -50,16 +50,15 @@ public abstract class Address {
|
|||
public abstract String getOutputScriptDataType();
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof Address)) {
|
||||
if(!(obj instanceof Address address)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Address address = (Address)obj;
|
||||
return address.getAddress().equals(this.getAddress());
|
||||
return Arrays.equals(data, address.data) && getVersion(Network.get()) == address.getVersion(Network.get());
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return getAddress().hashCode();
|
||||
return Arrays.hashCode(data) + getVersion(Network.get());
|
||||
}
|
||||
|
||||
public static Address fromString(String address) throws InvalidAddressException {
|
||||
|
|
|
@ -6,11 +6,8 @@ import com.sparrowwallet.drongo.protocol.Script;
|
|||
import com.sparrowwallet.drongo.protocol.ScriptType;
|
||||
|
||||
public class P2PKAddress extends Address {
|
||||
private final byte[] pubKey;
|
||||
|
||||
public P2PKAddress(byte[] pubKey) {
|
||||
super(Utils.sha256hash160(pubKey));
|
||||
this.pubKey = pubKey;
|
||||
super(pubKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,17 +15,22 @@ public class P2PKAddress extends Address {
|
|||
return network.getP2PKHAddressHeader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddress(Network network) {
|
||||
return Utils.bytesToHex(data);
|
||||
}
|
||||
|
||||
public ScriptType getScriptType() {
|
||||
return ScriptType.P2PK;
|
||||
}
|
||||
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(pubKey);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return pubKey;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -21,12 +21,12 @@ public class P2PKHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(hash);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return hash;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,12 +22,12 @@ public class P2SHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(hash);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return hash;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
package com.sparrowwallet.drongo.address;
|
||||
|
||||
import com.sparrowwallet.drongo.Network;
|
||||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.protocol.Bech32;
|
||||
import com.sparrowwallet.drongo.protocol.Script;
|
||||
import com.sparrowwallet.drongo.protocol.ScriptType;
|
||||
|
||||
public class P2TRAddress extends Address {
|
||||
private final byte[] pubKey;
|
||||
|
||||
public P2TRAddress(byte[] pubKey) {
|
||||
super(Utils.sha256hash160(pubKey));
|
||||
this.pubKey = pubKey;
|
||||
super(pubKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,7 +17,7 @@ public class P2TRAddress extends Address {
|
|||
|
||||
@Override
|
||||
public String getAddress(Network network) {
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), pubKey);
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,12 +27,12 @@ public class P2TRAddress extends Address {
|
|||
|
||||
@Override
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(pubKey);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return pubKey;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,7 +17,7 @@ public class P2WPKHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public String getAddress(Network network) {
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), hash);
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,12 +27,12 @@ public class P2WPKHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(hash);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return hash;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -15,7 +15,7 @@ public class P2WSHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public String getAddress(Network network) {
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), hash);
|
||||
return Bech32.encode(network.getBech32AddressHRP(), getVersion(), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -25,12 +25,12 @@ public class P2WSHAddress extends Address {
|
|||
|
||||
@Override
|
||||
public Script getOutputScript() {
|
||||
return getScriptType().getOutputScript(hash);
|
||||
return getScriptType().getOutputScript(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getOutputScriptData() {
|
||||
return hash;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -275,6 +275,10 @@ public class ECKey {
|
|||
* use {@code new BigInteger(1, bytes);}
|
||||
*/
|
||||
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
|
||||
if (privKey.bitLength() > CURVE.getN().bitLength()) {
|
||||
privKey = privKey.mod(CURVE.getN());
|
||||
}
|
||||
|
||||
if(Secp256k1Context.isEnabled()) {
|
||||
try {
|
||||
byte[] pubKeyBytes = NativeSecp256k1.computePubkey(Utils.bigIntegerToBytes(privKey, 32), false);
|
||||
|
@ -285,13 +289,6 @@ public class ECKey {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group order,
|
||||
* but that could change in future versions.
|
||||
*/
|
||||
if (privKey.bitLength() > CURVE.getN().bitLength()) {
|
||||
privKey = privKey.mod(CURVE.getN());
|
||||
}
|
||||
return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
|
||||
}
|
||||
|
||||
|
|
|
@ -134,10 +134,10 @@ public class PSBT {
|
|||
for(TransactionOutput txOutput : transaction.getOutputs()) {
|
||||
try {
|
||||
Address address = txOutput.getScript().getToAddresses()[0];
|
||||
if(walletTransaction.getPayments().stream().anyMatch(payment -> payment.getAddress().equals(address))) {
|
||||
outputNodes.add(wallet.getWalletAddresses().getOrDefault(address, null));
|
||||
if(walletTransaction.getAddressNodeMap().containsKey(address)) {
|
||||
outputNodes.add(walletTransaction.getAddressNodeMap().get(address));
|
||||
} else if(walletTransaction.getChangeMap().keySet().stream().anyMatch(changeNode -> changeNode.getAddress().equals(address))) {
|
||||
outputNodes.add(wallet.getWalletAddresses().getOrDefault(address, null));
|
||||
outputNodes.add(walletTransaction.getChangeMap().keySet().stream().filter(changeNode -> changeNode.getAddress().equals(address)).findFirst().orElse(null));
|
||||
}
|
||||
} catch(NonStandardScriptException e) {
|
||||
//Ignore, likely OP_RETURN output
|
||||
|
|
|
@ -154,8 +154,12 @@ public class WalletTransaction {
|
|||
getAddressNodeMap(wallet);
|
||||
}
|
||||
|
||||
public Map<Address, WalletNode> getAddressNodeMap() {
|
||||
return getAddressNodeMap(getWallet());
|
||||
}
|
||||
|
||||
public Map<Address, WalletNode> getAddressNodeMap(Wallet wallet) {
|
||||
Map<Script, WalletNode> walletOutputScripts = null;
|
||||
Map<Address, WalletNode> walletAddresses = null;
|
||||
|
||||
Map<Address, WalletNode> walletAddressNodeMap = addressNodeMap.computeIfAbsent(wallet, w -> new LinkedHashMap<>());
|
||||
for(Payment payment : payments) {
|
||||
|
@ -164,11 +168,11 @@ public class WalletTransaction {
|
|||
}
|
||||
|
||||
if(payment.getAddress() != null && wallet != null) {
|
||||
if(walletOutputScripts == null) {
|
||||
walletOutputScripts = wallet.getWalletOutputScripts();
|
||||
if(walletAddresses == null) {
|
||||
walletAddresses = wallet.getWalletAddresses();
|
||||
}
|
||||
|
||||
WalletNode walletNode = walletOutputScripts.get(payment.getAddress().getOutputScript());
|
||||
WalletNode walletNode = walletAddresses.get(payment.getAddress());
|
||||
walletAddressNodeMap.put(payment.getAddress(), walletNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ public class AddressTest {
|
|||
Address address = (i % 2 == 0 ? new P2PKHAddress(values) : new P2WPKHAddress(values));
|
||||
String strAddress = address.toString();
|
||||
Address checkAddress = Address.fromString(strAddress);
|
||||
Assert.assertArrayEquals(values, checkAddress.getHash());
|
||||
Assert.assertArrayEquals(values, checkAddress.getData());
|
||||
}
|
||||
|
||||
byte[] values32 = new byte[32];
|
||||
|
@ -107,7 +107,7 @@ public class AddressTest {
|
|||
Address address = new P2WSHAddress(values32);
|
||||
String strAddress = address.toString();
|
||||
Address checkAddress = Address.fromString(strAddress);
|
||||
Assert.assertArrayEquals(values32, checkAddress.getHash());
|
||||
Assert.assertArrayEquals(values32, checkAddress.getData());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue