mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-02 18:26:43 +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;
|
||||
|
||||
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", "");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,10 +20,20 @@ public class P2PKHAddress extends Address {
|
|||
List<ScriptChunk> 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";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ScriptChunk> 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));
|
||||
}
|
||||
|
|
|
@ -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<ScriptChunk> 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";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ScriptChunk> 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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue