diff --git a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java index 154bae2..c5d17d7 100644 --- a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java +++ b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java @@ -275,6 +275,10 @@ public class OutputDescriptor { return wallet; } + public static String toDescriptorString(Address address) { + return "addr(" + address + ")"; + } + public static OutputDescriptor getOutputDescriptor(Wallet wallet) { return getOutputDescriptor(wallet, null); } @@ -392,6 +396,17 @@ public class OutputDescriptor { return new OutputDescriptor(scriptType, multisigThreshold, keyDerivationMap, keyChildDerivationMap); } + public static String normalize(String descriptor) { + String normalized = descriptor.replaceAll("'", "h"); + + int checksumHash = normalized.lastIndexOf('#'); + if(checksumHash > -1) { + normalized = normalized.substring(0, checksumHash); + } + + return normalized + "#" + getChecksum(normalized); + } + private static String getChecksum(String descriptor) { BigInteger c = BigInteger.valueOf(1); int cls = 0; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java index f152876..863d3cb 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java @@ -1,7 +1,12 @@ package com.sparrowwallet.drongo.protocol; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.Date; +import static com.sparrowwallet.drongo.Utils.uint32ToByteStreamLE; + public class BlockHeader extends Message { private long version; private Sha256Hash prevBlockHash; @@ -14,6 +19,16 @@ public class BlockHeader extends Message { super(rawheader, 0); } + public BlockHeader(long version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot, Sha256Hash witnessRoot, long time, long difficultyTarget, long nonce) { + this.version = version; + this.prevBlockHash = prevBlockHash; + this.merkleRoot = merkleRoot; + this.witnessRoot = witnessRoot; + this.time = time; + this.difficultyTarget = difficultyTarget; + this.nonce = nonce; + } + @Override protected void parse() throws ProtocolException { version = readUint32(); @@ -57,4 +72,25 @@ public class BlockHeader extends Message { public long getNonce() { return nonce; } + + public byte[] bitcoinSerialize() { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + bitcoinSerializeToStream(outputStream); + return outputStream.toByteArray(); + } catch (IOException e) { + //can't happen + } + + return null; + } + + protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { + uint32ToByteStreamLE(version, stream); + stream.write(prevBlockHash.getReversedBytes()); + stream.write(merkleRoot.getReversedBytes()); + uint32ToByteStreamLE(time, stream); + uint32ToByteStreamLE(difficultyTarget, stream); + uint32ToByteStreamLE(nonce, stream); + } }