add block header serialization and output descriptor normalize functions

This commit is contained in:
Craig Raw 2022-12-08 08:40:49 +02:00
parent fa18ec9d45
commit 692f23e026
2 changed files with 51 additions and 0 deletions

View file

@ -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;

View file

@ -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);
}
}