diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java index 01fa265..fd75f10 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java @@ -1,4 +1,60 @@ package com.sparrowwallet.drongo.protocol; -public class BlockHeader { +import java.util.Date; + +public class BlockHeader extends Message { + private long version; + private Sha256Hash prevBlockHash; + private Sha256Hash merkleRoot, witnessRoot; + private long time; + private long difficultyTarget; // "nBits" + private long nonce; + + public BlockHeader(byte[] rawheader) { + super(rawheader, 0); + } + + @Override + protected void parse() throws ProtocolException { + version = readUint32(); + prevBlockHash = readHash(); + merkleRoot = readHash(); + time = readUint32(); + difficultyTarget = readUint32(); + nonce = readUint32(); + + length = cursor - offset; + } + + public long getVersion() { + return version; + } + + public Sha256Hash getPrevBlockHash() { + return prevBlockHash; + } + + public Sha256Hash getMerkleRoot() { + return merkleRoot; + } + + public Sha256Hash getWitnessRoot() { + return witnessRoot; + } + + public long getTime() { + return time; + } + + public Date getTimeAsDate() { + return new Date(time); + } + + public long getDifficultyTarget() { + return difficultyTarget; + } + + public long getNonce() { + return nonce; + } } diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Message.java b/src/main/java/com/sparrowwallet/drongo/protocol/Message.java index f00bfd1..cd575a5 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Message.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Message.java @@ -13,7 +13,7 @@ public abstract class Message { public static final int MAX_SIZE = 0x02000000; // 32MB public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE; - protected byte[] rawtx; + protected byte[] payload; // The offset is how many bytes into the provided byte array this message payload starts at. protected int offset; @@ -23,8 +23,8 @@ public abstract class Message { protected int length = UNKNOWN_LENGTH; - public Message(byte[] rawtx, int offset) { - this.rawtx = rawtx; + public Message(byte[] payload, int offset) { + this.payload = payload; this.cursor = this.offset = offset; parse(); @@ -53,7 +53,7 @@ public abstract class Message { protected long readUint32() throws ProtocolException { try { - long u = Utils.readUint32(rawtx, cursor); + long u = Utils.readUint32(payload, cursor); cursor += 4; return u; } catch (ArrayIndexOutOfBoundsException e) { @@ -63,7 +63,7 @@ public abstract class Message { protected long readInt64() throws ProtocolException { try { - long u = Utils.readInt64(rawtx, cursor); + long u = Utils.readInt64(payload, cursor); cursor += 8; return u; } catch (ArrayIndexOutOfBoundsException e) { @@ -72,12 +72,12 @@ public abstract class Message { } protected byte[] readBytes(int length) throws ProtocolException { - if ((length > MAX_SIZE) || (cursor + length > rawtx.length)) { + if ((length > MAX_SIZE) || (cursor + length > payload.length)) { throw new ProtocolException("Claimed value length too large: " + length); } try { byte[] b = new byte[length]; - System.arraycopy(rawtx, cursor, b, 0, length); + System.arraycopy(payload, cursor, b, 0, length); cursor += length; return b; } catch (IndexOutOfBoundsException e) { @@ -91,7 +91,7 @@ public abstract class Message { protected long readVarInt(int offset) throws ProtocolException { try { - VarInt varint = new VarInt(rawtx, cursor + offset); + VarInt varint = new VarInt(payload, cursor + offset); cursor += offset + varint.getOriginalSizeInBytes(); return varint.value; } catch (ArrayIndexOutOfBoundsException e) { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java index e4d8748..6fff614 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java @@ -182,7 +182,7 @@ public class Transaction extends ChildMessage { // version version = readUint32(); // peek at marker - byte marker = rawtx[cursor]; + byte marker = payload[cursor]; segwit = (marker == 0); // marker, flag if (segwit) { @@ -206,7 +206,7 @@ public class Transaction extends ChildMessage { long numInputs = readVarInt(); inputs = new ArrayList<>(Math.min((int) numInputs, Utils.MAX_INITIAL_ARRAY_LENGTH)); for (long i = 0; i < numInputs; i++) { - TransactionInput input = new TransactionInput(this, rawtx, cursor); + TransactionInput input = new TransactionInput(this, payload, cursor); inputs.add(input); long scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH); cursor += scriptLen + 4; @@ -217,7 +217,7 @@ public class Transaction extends ChildMessage { long numOutputs = readVarInt(); outputs = new ArrayList<>(Math.min((int) numOutputs, Utils.MAX_INITIAL_ARRAY_LENGTH)); for (long i = 0; i < numOutputs; i++) { - TransactionOutput output = new TransactionOutput(this, rawtx, cursor); + TransactionOutput output = new TransactionOutput(this, payload, cursor); outputs.add(output); long scriptLen = readVarInt(8); cursor += scriptLen; @@ -227,7 +227,7 @@ public class Transaction extends ChildMessage { private void parseWitnesses() { int numWitnesses = inputs.size(); for (int i = 0; i < numWitnesses; i++) { - TransactionWitness witness = new TransactionWitness(this, rawtx, cursor); + TransactionWitness witness = new TransactionWitness(this, payload, cursor); inputs.get(i).setWitness(witness); cursor += witness.getLength(); } diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java index 450ef79..a531aef 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java @@ -31,7 +31,7 @@ public class TransactionInput extends ChildMessage { } protected void parse() throws ProtocolException { - outpoint = new TransactionOutPoint(rawtx, cursor, this); + outpoint = new TransactionOutPoint(payload, cursor, this); cursor += outpoint.getMessageSize(); int scriptLen = (int) readVarInt(); length = cursor - offset + scriptLen + 4; @@ -62,7 +62,7 @@ public class TransactionInput extends ChildMessage { } void setScriptBytes(byte[] scriptBytes) { - super.rawtx = null; + super.payload = null; this.scriptSig = null; int oldLength = length; this.scriptBytes = scriptBytes; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java index c9d44a1..209c489 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java @@ -34,7 +34,7 @@ public class TransactionOutput extends ChildMessage { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitcoinSerializeToStream(baos); - rawtx = baos.toByteArray(); + payload = baos.toByteArray(); } catch(IOException e) { //ignore }