mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-27 02:26:44 +00:00
helper methods for working with addresses
This commit is contained in:
parent
2f5655708d
commit
14767c3250
6 changed files with 75 additions and 16 deletions
|
@ -4,18 +4,18 @@ import com.sparrowwallet.drongo.protocol.Base58;
|
||||||
import com.sparrowwallet.drongo.protocol.Script;
|
import com.sparrowwallet.drongo.protocol.Script;
|
||||||
|
|
||||||
public abstract class Address {
|
public abstract class Address {
|
||||||
protected final byte[] pubKeyHash;
|
protected final byte[] hash;
|
||||||
|
|
||||||
public Address(byte[] pubKeyHash) {
|
public Address(byte[] hash) {
|
||||||
this.pubKeyHash = pubKeyHash;
|
this.hash = hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getPubKeyHash() {
|
public byte[] getHash() {
|
||||||
return pubKeyHash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return Base58.encodeChecked(getVersion(), pubKeyHash);
|
return Base58.encodeChecked(getVersion(), hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -26,6 +26,10 @@ public abstract class Address {
|
||||||
|
|
||||||
public abstract Script getOutputScript();
|
public abstract Script getOutputScript();
|
||||||
|
|
||||||
|
public abstract byte[] getOutputScriptData();
|
||||||
|
|
||||||
|
public abstract String getOutputScriptDataType();
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(!(obj instanceof Address)) {
|
if(!(obj instanceof Address)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -38,4 +42,9 @@ public abstract class Address {
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return getAddress().hashCode();
|
return getAddress().hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getScriptType() {
|
||||||
|
String className = this.getClass().getSimpleName();
|
||||||
|
return className.replace("Address", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,4 +27,14 @@ public class P2PKAddress extends Address {
|
||||||
|
|
||||||
return new Script(chunks);
|
return new Script(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getOutputScriptData() {
|
||||||
|
return pubKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOutputScriptDataType() {
|
||||||
|
return "Public Key";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,20 @@ public class P2PKHAddress extends Address {
|
||||||
List<ScriptChunk> chunks = new ArrayList<>();
|
List<ScriptChunk> chunks = new ArrayList<>();
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_DUP, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_DUP, null));
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_HASH160, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_HASH160, null));
|
||||||
chunks.add(new ScriptChunk(pubKeyHash.length, pubKeyHash));
|
chunks.add(new ScriptChunk(hash.length, hash));
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_EQUALVERIFY, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_EQUALVERIFY, null));
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_CHECKSIG, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_CHECKSIG, null));
|
||||||
|
|
||||||
return new Script(chunks);
|
return new Script(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getOutputScriptData() {
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOutputScriptDataType() {
|
||||||
|
return "Public Key Hash";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,8 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class P2SHAddress extends Address {
|
public class P2SHAddress extends Address {
|
||||||
public P2SHAddress(byte[] pubKeyHash) {
|
public P2SHAddress(byte[] scriptHash) {
|
||||||
super(pubKeyHash);
|
super(scriptHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVersion() {
|
public int getVersion() {
|
||||||
|
@ -20,12 +20,22 @@ public class P2SHAddress extends Address {
|
||||||
public Script getOutputScript() {
|
public Script getOutputScript() {
|
||||||
List<ScriptChunk> chunks = new ArrayList<>();
|
List<ScriptChunk> chunks = new ArrayList<>();
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_HASH160, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_HASH160, null));
|
||||||
chunks.add(new ScriptChunk(pubKeyHash.length, pubKeyHash));
|
chunks.add(new ScriptChunk(hash.length, hash));
|
||||||
chunks.add(new ScriptChunk(ScriptOpCodes.OP_EQUAL, null));
|
chunks.add(new ScriptChunk(ScriptOpCodes.OP_EQUAL, null));
|
||||||
|
|
||||||
return new Script(chunks);
|
return new Script(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getOutputScriptData() {
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOutputScriptDataType() {
|
||||||
|
return "Script Hash";
|
||||||
|
}
|
||||||
|
|
||||||
public static P2SHAddress fromProgram(byte[] program) {
|
public static P2SHAddress fromProgram(byte[] program) {
|
||||||
return new P2SHAddress(Utils.sha256hash160(program));
|
return new P2SHAddress(Utils.sha256hash160(program));
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,24 @@ public class P2WPKHAddress extends Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return Bech32.encode(HRP, getVersion(), pubKeyHash);
|
return Bech32.encode(HRP, getVersion(), hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Script getOutputScript() {
|
public Script getOutputScript() {
|
||||||
List<ScriptChunk> chunks = new ArrayList<>();
|
List<ScriptChunk> chunks = new ArrayList<>();
|
||||||
chunks.add(new ScriptChunk(Script.encodeToOpN(getVersion()), null));
|
chunks.add(new ScriptChunk(Script.encodeToOpN(getVersion()), null));
|
||||||
chunks.add(new ScriptChunk(pubKeyHash.length, pubKeyHash));
|
chunks.add(new ScriptChunk(hash.length, hash));
|
||||||
|
|
||||||
return new Script(chunks);
|
return new Script(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getOutputScriptData() {
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOutputScriptDataType() {
|
||||||
|
return "Witness Public Key Hash";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import java.util.List;
|
||||||
import static com.sparrowwallet.drongo.address.P2WPKHAddress.HRP;
|
import static com.sparrowwallet.drongo.address.P2WPKHAddress.HRP;
|
||||||
|
|
||||||
public class P2WSHAddress extends Address {
|
public class P2WSHAddress extends Address {
|
||||||
public P2WSHAddress(byte[] pubKeyHash) {
|
public P2WSHAddress(byte[] scriptHash) {
|
||||||
super(pubKeyHash);
|
super(scriptHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVersion() {
|
public int getVersion() {
|
||||||
|
@ -17,17 +17,27 @@ public class P2WSHAddress extends Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return Bech32.encode(HRP, getVersion(), pubKeyHash);
|
return Bech32.encode(HRP, getVersion(), hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Script getOutputScript() {
|
public Script getOutputScript() {
|
||||||
List<ScriptChunk> chunks = new ArrayList<>();
|
List<ScriptChunk> chunks = new ArrayList<>();
|
||||||
chunks.add(new ScriptChunk(Script.encodeToOpN(getVersion()), null));
|
chunks.add(new ScriptChunk(Script.encodeToOpN(getVersion()), null));
|
||||||
chunks.add(new ScriptChunk(pubKeyHash.length, pubKeyHash));
|
chunks.add(new ScriptChunk(hash.length, hash));
|
||||||
|
|
||||||
return new Script(chunks);
|
return new Script(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getOutputScriptData() {
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOutputScriptDataType() {
|
||||||
|
return "Witness Script Hash";
|
||||||
|
}
|
||||||
|
|
||||||
public static P2WSHAddress fromProgram(byte[] program) {
|
public static P2WSHAddress fromProgram(byte[] program) {
|
||||||
return new P2WSHAddress(Sha256Hash.hash(program));
|
return new P2WSHAddress(Sha256Hash.hash(program));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue