diff --git a/core/src/main/java/org/bitcoinj/base/Sha256Hash.java b/core/src/main/java/org/bitcoinj/base/Sha256Hash.java index a5aef7d6a..e81617d54 100644 --- a/core/src/main/java/org/bitcoinj/base/Sha256Hash.java +++ b/core/src/main/java/org/bitcoinj/base/Sha256Hash.java @@ -269,9 +269,11 @@ public class Sha256Hash implements Comparable { } /** - * Returns a reversed copy of the internal byte array. + * Allocates a byte array and writes the hash into it, in reversed order. + * + * @return byte array containing the hash */ - public byte[] getReversedBytes() { + public byte[] serialize() { return ByteUtils.reverseBytes(bytes); } @@ -284,7 +286,7 @@ public class Sha256Hash implements Comparable { */ public ByteBuffer write(ByteBuffer buf) throws BufferOverflowException { // we have to flip it around, as on the wire it's in little endian - buf.put(getReversedBytes()); + buf.put(serialize()); return buf; } diff --git a/core/src/main/java/org/bitcoinj/core/Block.java b/core/src/main/java/org/bitcoinj/core/Block.java index 000208b37..f5f59380e 100644 --- a/core/src/main/java/org/bitcoinj/core/Block.java +++ b/core/src/main/java/org/bitcoinj/core/Block.java @@ -269,8 +269,8 @@ public class Block extends Message { // default for testing void writeHeader(OutputStream stream) throws IOException { ByteUtils.writeInt32LE(version, stream); - stream.write(prevBlockHash.getReversedBytes()); - stream.write(getMerkleRoot().getReversedBytes()); + stream.write(prevBlockHash.serialize()); + stream.write(getMerkleRoot().serialize()); ByteUtils.writeInt32LE(time.getEpochSecond(), stream); ByteUtils.writeInt32LE(difficultyTarget, stream); ByteUtils.writeInt32LE(nonce, stream); @@ -520,7 +520,7 @@ public class Block extends Message { if (witnessReserved.length != 32) throw new VerificationException("Coinbase witness reserved invalid: length"); - Sha256Hash witnessRootHash = Sha256Hash.twiceOf(getWitnessRoot().getReversedBytes(), witnessReserved); + Sha256Hash witnessRootHash = Sha256Hash.twiceOf(getWitnessRoot().serialize(), witnessReserved); if (!witnessRootHash.equals(witnessCommitment)) throw new VerificationException("Witness merkle root invalid. Expected " + witnessCommitment.toString() + " but got " + witnessRootHash.toString()); @@ -594,8 +594,8 @@ public class Block extends Message { Sha256Hash leftHash = tree.get(levelOffset + left); Sha256Hash rightHash = tree.get(levelOffset + right); tree.add(Sha256Hash.wrapReversed(hashTwice( - leftHash.getReversedBytes(), - rightHash.getReversedBytes()))); + leftHash.serialize(), + rightHash.serialize()))); } // Move to the next level. levelOffset += levelSize; diff --git a/core/src/main/java/org/bitcoinj/core/GetBlocksMessage.java b/core/src/main/java/org/bitcoinj/core/GetBlocksMessage.java index e06ab93db..c6494439c 100644 --- a/core/src/main/java/org/bitcoinj/core/GetBlocksMessage.java +++ b/core/src/main/java/org/bitcoinj/core/GetBlocksMessage.java @@ -84,10 +84,10 @@ public class GetBlocksMessage extends Message { stream.write(VarInt.of(locator.size()).serialize()); for (Sha256Hash hash : locator.getHashes()) { // Have to reverse as wire format is little endian. - stream.write(hash.getReversedBytes()); + stream.write(hash.serialize()); } // Next, a block ID to stop at. - stream.write(stopHash.getReversedBytes()); + stream.write(stopHash.serialize()); } @Override diff --git a/core/src/main/java/org/bitcoinj/core/ListMessage.java b/core/src/main/java/org/bitcoinj/core/ListMessage.java index afd6137c6..05fece6ef 100644 --- a/core/src/main/java/org/bitcoinj/core/ListMessage.java +++ b/core/src/main/java/org/bitcoinj/core/ListMessage.java @@ -91,7 +91,7 @@ public abstract class ListMessage extends Message { // Write out the type code. ByteUtils.writeInt32LE(i.type.code, stream); // And now the hash. - stream.write(i.hash.getReversedBytes()); + stream.write(i.hash.serialize()); } } diff --git a/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java b/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java index dfaa5979c..69dada611 100644 --- a/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java +++ b/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java @@ -116,7 +116,7 @@ public class PartialMerkleTree extends Message { stream.write(VarInt.of(hashes.size()).serialize()); for (Sha256Hash hash : hashes) - stream.write(hash.getReversedBytes()); + stream.write(hash.serialize()); stream.write(VarInt.of(matchedChildBits.length).serialize()); stream.write(matchedChildBits); diff --git a/core/src/main/java/org/bitcoinj/core/RejectMessage.java b/core/src/main/java/org/bitcoinj/core/RejectMessage.java index 988baa594..caf2636b4 100644 --- a/core/src/main/java/org/bitcoinj/core/RejectMessage.java +++ b/core/src/main/java/org/bitcoinj/core/RejectMessage.java @@ -113,7 +113,7 @@ public class RejectMessage extends Message { stream.write(VarInt.of(reasonBytes.length).serialize()); stream.write(reasonBytes); if ("block".equals(message) || "tx".equals(message)) - stream.write(messageHash.getReversedBytes()); + stream.write(messageHash.serialize()); } /** diff --git a/core/src/main/java/org/bitcoinj/core/Transaction.java b/core/src/main/java/org/bitcoinj/core/Transaction.java index bc9460d5a..1b815ded4 100644 --- a/core/src/main/java/org/bitcoinj/core/Transaction.java +++ b/core/src/main/java/org/bitcoinj/core/Transaction.java @@ -1430,7 +1430,7 @@ public class Transaction extends Message { if (!anyoneCanPay) { ByteArrayOutputStream bosHashPrevouts = new ByteArrayOutputStream(256); for (TransactionInput input : this.inputs) { - bosHashPrevouts.write(input.getOutpoint().getHash().getReversedBytes()); + bosHashPrevouts.write(input.getOutpoint().getHash().serialize()); writeInt32LE(input.getOutpoint().getIndex(), bosHashPrevouts); } hashPrevouts = Sha256Hash.hashTwice(bosHashPrevouts.toByteArray()); @@ -1468,7 +1468,7 @@ public class Transaction extends Message { writeInt32LE(version, bos); bos.write(hashPrevouts); bos.write(hashSequence); - bos.write(inputs.get(inputIndex).getOutpoint().getHash().getReversedBytes()); + bos.write(inputs.get(inputIndex).getOutpoint().getHash().serialize()); writeInt32LE(inputs.get(inputIndex).getOutpoint().getIndex(), bos); bos.write(VarInt.of(scriptCode.length).serialize()); bos.write(scriptCode); diff --git a/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java b/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java index bd56798cb..8e40ba8c2 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java @@ -105,7 +105,7 @@ public class TransactionOutPoint extends Message { @Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { - stream.write(hash.getReversedBytes()); + stream.write(hash.serialize()); ByteUtils.writeInt32LE(index, stream); } diff --git a/core/src/test/java/org/bitcoinj/core/BlockTest.java b/core/src/test/java/org/bitcoinj/core/BlockTest.java index d7137a908..c2b5f2952 100644 --- a/core/src/test/java/org/bitcoinj/core/BlockTest.java +++ b/core/src/test/java/org/bitcoinj/core/BlockTest.java @@ -315,8 +315,8 @@ public class BlockTest { @Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { ByteUtils.writeInt32LE(getVersion(), stream); - stream.write(getPrevBlockHash().getReversedBytes()); - stream.write(getMerkleRoot().getReversedBytes()); + stream.write(getPrevBlockHash().serialize()); + stream.write(getMerkleRoot().serialize()); ByteUtils.writeInt32LE(getTimeSeconds(), stream); ByteUtils.writeInt32LE(getDifficultyTarget(), stream); ByteUtils.writeInt32LE(getNonce(), stream); diff --git a/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java b/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java index 61faad8f8..f6aa196d4 100644 --- a/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java +++ b/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java @@ -241,7 +241,7 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup { stream.write(VarInt.of(Integer.MAX_VALUE).serialize()); //stream.write(VarInt.of(hashes.size()).encode()); for (Sha256Hash hash : hashes) - stream.write(hash.getReversedBytes()); + stream.write(hash.serialize()); stream.write(VarInt.of(bits.length).serialize()); stream.write(bits);