From 14767c3250281d52c1b7da21c1ab0dc83076fda8 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 10 Apr 2020 16:59:23 +0200 Subject: [PATCH] helper methods for working with addresses --- .../sparrowwallet/drongo/address/Address.java | 21 +++++++++++++------ .../drongo/address/P2PKAddress.java | 10 +++++++++ .../drongo/address/P2PKHAddress.java | 12 ++++++++++- .../drongo/address/P2SHAddress.java | 16 +++++++++++--- .../drongo/address/P2WPKHAddress.java | 14 +++++++++++-- .../drongo/address/P2WSHAddress.java | 18 ++++++++++++---- 6 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/address/Address.java b/src/main/java/com/sparrowwallet/drongo/address/Address.java index 01a7a4c..0a6d5f6 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/Address.java +++ b/src/main/java/com/sparrowwallet/drongo/address/Address.java @@ -4,18 +4,18 @@ import com.sparrowwallet.drongo.protocol.Base58; import com.sparrowwallet.drongo.protocol.Script; public abstract class Address { - protected final byte[] pubKeyHash; + protected final byte[] hash; - public Address(byte[] pubKeyHash) { - this.pubKeyHash = pubKeyHash; + public Address(byte[] hash) { + this.hash = hash; } - public byte[] getPubKeyHash() { - return pubKeyHash; + public byte[] getHash() { + return hash; } public String getAddress() { - return Base58.encodeChecked(getVersion(), pubKeyHash); + return Base58.encodeChecked(getVersion(), hash); } public String toString() { @@ -26,6 +26,10 @@ public abstract class Address { public abstract Script getOutputScript(); + public abstract byte[] getOutputScriptData(); + + public abstract String getOutputScriptDataType(); + public boolean equals(Object obj) { if(!(obj instanceof Address)) { return false; @@ -38,4 +42,9 @@ public abstract class Address { public int hashCode() { return getAddress().hashCode(); } + + public String getScriptType() { + String className = this.getClass().getSimpleName(); + return className.replace("Address", ""); + } } diff --git a/src/main/java/com/sparrowwallet/drongo/address/P2PKAddress.java b/src/main/java/com/sparrowwallet/drongo/address/P2PKAddress.java index 4d5f97f..3033f09 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/P2PKAddress.java +++ b/src/main/java/com/sparrowwallet/drongo/address/P2PKAddress.java @@ -27,4 +27,14 @@ public class P2PKAddress extends Address { return new Script(chunks); } + + @Override + public byte[] getOutputScriptData() { + return pubKey; + } + + @Override + public String getOutputScriptDataType() { + return "Public Key"; + } } diff --git a/src/main/java/com/sparrowwallet/drongo/address/P2PKHAddress.java b/src/main/java/com/sparrowwallet/drongo/address/P2PKHAddress.java index cdd7c31..bc5bdea 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/P2PKHAddress.java +++ b/src/main/java/com/sparrowwallet/drongo/address/P2PKHAddress.java @@ -20,10 +20,20 @@ public class P2PKHAddress extends Address { List chunks = new ArrayList<>(); chunks.add(new ScriptChunk(ScriptOpCodes.OP_DUP, 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_CHECKSIG, null)); return new Script(chunks); } + + @Override + public byte[] getOutputScriptData() { + return hash; + } + + @Override + public String getOutputScriptDataType() { + return "Public Key Hash"; + } } diff --git a/src/main/java/com/sparrowwallet/drongo/address/P2SHAddress.java b/src/main/java/com/sparrowwallet/drongo/address/P2SHAddress.java index 0d981b9..f754820 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/P2SHAddress.java +++ b/src/main/java/com/sparrowwallet/drongo/address/P2SHAddress.java @@ -9,8 +9,8 @@ import java.util.ArrayList; import java.util.List; public class P2SHAddress extends Address { - public P2SHAddress(byte[] pubKeyHash) { - super(pubKeyHash); + public P2SHAddress(byte[] scriptHash) { + super(scriptHash); } public int getVersion() { @@ -20,12 +20,22 @@ public class P2SHAddress extends Address { public Script getOutputScript() { List chunks = new ArrayList<>(); 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)); return new Script(chunks); } + @Override + public byte[] getOutputScriptData() { + return hash; + } + + @Override + public String getOutputScriptDataType() { + return "Script Hash"; + } + public static P2SHAddress fromProgram(byte[] program) { return new P2SHAddress(Utils.sha256hash160(program)); } diff --git a/src/main/java/com/sparrowwallet/drongo/address/P2WPKHAddress.java b/src/main/java/com/sparrowwallet/drongo/address/P2WPKHAddress.java index b3dec77..3d92dcd 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/P2WPKHAddress.java +++ b/src/main/java/com/sparrowwallet/drongo/address/P2WPKHAddress.java @@ -19,14 +19,24 @@ public class P2WPKHAddress extends Address { } public String getAddress() { - return Bech32.encode(HRP, getVersion(), pubKeyHash); + return Bech32.encode(HRP, getVersion(), hash); } public Script getOutputScript() { List chunks = new ArrayList<>(); 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); } + + @Override + public byte[] getOutputScriptData() { + return hash; + } + + @Override + public String getOutputScriptDataType() { + return "Witness Public Key Hash"; + } } diff --git a/src/main/java/com/sparrowwallet/drongo/address/P2WSHAddress.java b/src/main/java/com/sparrowwallet/drongo/address/P2WSHAddress.java index c1de40e..0c176f6 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/P2WSHAddress.java +++ b/src/main/java/com/sparrowwallet/drongo/address/P2WSHAddress.java @@ -8,8 +8,8 @@ import java.util.List; import static com.sparrowwallet.drongo.address.P2WPKHAddress.HRP; public class P2WSHAddress extends Address { - public P2WSHAddress(byte[] pubKeyHash) { - super(pubKeyHash); + public P2WSHAddress(byte[] scriptHash) { + super(scriptHash); } public int getVersion() { @@ -17,17 +17,27 @@ public class P2WSHAddress extends Address { } public String getAddress() { - return Bech32.encode(HRP, getVersion(), pubKeyHash); + return Bech32.encode(HRP, getVersion(), hash); } public Script getOutputScript() { List chunks = new ArrayList<>(); 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); } + @Override + public byte[] getOutputScriptData() { + return hash; + } + + @Override + public String getOutputScriptDataType() { + return "Witness Script Hash"; + } + public static P2WSHAddress fromProgram(byte[] program) { return new P2WSHAddress(Sha256Hash.hash(program)); }