Java Code Examples for net.bither.bitherj.utils.Utils#reverseBytes()

The following examples show how to use net.bither.bitherj.utils.Utils#reverseBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Block.java    From bitherj with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the hash of the block is OK (lower than difficulty target).
 */
private boolean checkProofOfWork(boolean throwException) throws VerificationException {
    // This part is key - it is what proves the block was as difficult to make as it claims
    // to be. Note however that in the context of this function, the block can claim to be
    // as difficult as it wants to be .... if somebody was able to take control of our network
    // connection and fork us onto a different chain, they could send us valid blocks with
    // ridiculously easy difficulty and this function would accept them.
    //
    // To prevent this attack from being possible, elsewhere we check that the difficultyTarget
    // field is of the right value. This requires us to have the preceeding blocks.
    BigInteger target = getDifficultyTargetAsInteger();

    BigInteger h = new BigInteger(1, Utils.reverseBytes(getBlockHash()));
    if (h.compareTo(target) > 0) {
        // Proof of work check failed!
        if (throwException)
            throw new VerificationException("Hash is higher than target: " + getHashAsString() + " vs "
                    + target.toString(16));
        else
            return false;
    }
    return true;
}
 
Example 2
Source File: Message.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected BigInteger readUint64() throws ProtocolException {
    try {
        // Java does not have an unsigned 64 bit type. So scrape it off the wire then flip.
        byte[] valbytes = new byte[8];
        System.arraycopy(bytes, cursor, valbytes, 0, 8);
        valbytes = Utils.reverseBytes(valbytes);
        cursor += valbytes.length;
        return new BigInteger(valbytes);
    } catch (IndexOutOfBoundsException e) {
        throw new ProtocolException(e);
    }
}
 
Example 3
Source File: Block.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public Block(long version, String prevBlock, String merkleRoot, int timestamp
        , long target, long nonce, int height) {
    this.blockVer = version;
    this.blockPrev = Utils.reverseBytes(Utils.hexStringToByteArray(prevBlock));
    this.blockRoot = Utils.reverseBytes(Utils.hexStringToByteArray(merkleRoot));
    this.blockTime = timestamp;
    this.blockBits = target;
    this.blockNonce = nonce;
    this.blockNo = height;
    this.blockHash = calculateHash();

}
 
Example 4
Source File: TxTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testDb() {
    Tx tx = new Tx();
    byte[] txHash = Utils.reverseBytes(
            Utils.hexStringToByteArray("f8a8335594d4c883f367e003cb3832015640f24714b48bd21cf6fbe84a617dfe"));
    tx.setTxHash(Utils.reverseBytes(
            Utils.hexStringToByteArray("f8a8335594d4c883f367e003cb3832015640f24714b48bd21cf6fbe84a617dfe")));
    tx.setBlockNo(304942);
    tx.setTxTime((int) new Date().getTime() / 1000);
    tx.setTxVer(1);
    In inPut = new In();
    inPut.setPrevTxHash(Utils.reverseBytes(
            Utils.hexStringToByteArray("d7f4efff7aeaffc1630dd3653e923a233fd463f9dc7dd4f97bb5cbf0cf99e56a")));
    inPut.setInSn(0);
    inPut.setTxHash(txHash);
    inPut.setInSequence(1);
    inPut.setInSignature(txHash);

    tx.addInput(inPut);
    Out out = new Out();
    out.setTxHash(txHash);
    out.setOutSn(0);
    out.setOutValue(3400);
    out.setOutScript(Utils.hexStringToByteArray("76a914abceaddc7d791f749671c17dfa36e9b17a4b055588ac"));
    out.setOutStatus(Out.OutStatus.spent);
    out.setOutAddress("test");
    tx.addOutput(out);
    AbstractDb.txProvider.add(tx);
    Tx testTx = AbstractDb.txProvider.getTxDetailByTxHash(txHash);
    assertEquals(Utils.bytesToHexString(tx.getTxHash()), Utils.bytesToHexString(testTx.getTxHash()));
}
 
Example 5
Source File: TxTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor() {
    byte[] rawTx = Utils.hexStringToByteArray("0100000001bdc0141fe3e5c2223a6d26a95acbf791042d93f9d9b8b38f133bf7adb5c1e293010000006a47304402202214770c0f5a9261190337273219a108132a4bc987c745db8dd6daded34b0dcb0220573de1d973166024b8342d6b6fef2a864a06cceee6aee13a910e5d8df465ed2a01210382b259804ad8d88b96a23222e24dd5a130d39588e78960c9e9b48a5b49943649ffffffff02a0860100000000001976a91479a7bf0bba8359561d4dab457042d7b632d5e64188ac605b0300000000001976a914b036c529faeca8040232cc4bd5918e709e90c4ff88ac00000000");
    Tx tx = new Tx(rawTx);
    byte[] txBytes = tx.bitcoinSerialize();
    assertTrue(Arrays.equals(rawTx, txBytes));
    byte[] exceptTxHash = Utils.reverseBytes(Utils.hexStringToByteArray("584985ca8a9ed57987da36ea3d13fe05a7c498f2098ddeb6c8d0f3214067640c"));
    byte[] txHash = tx.getTxHash();
    for (Out out : tx.getOuts()) {
        String outAddress = out.getOutAddress();
    }
    assertTrue(Arrays.equals(exceptTxHash, txHash));
}
 
Example 6
Source File: BlockTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlockConstructor() {
    Block block = new Block(1, "0000000000000000000000000000000000000000000000000000000000000000"
            , "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", 1231006505
            , 486604799, 2083236893, 0);
    byte[] expectBlockHash = Utils.reverseBytes(Utils.hexStringToByteArray("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
    assertTrue(Arrays.equals(block.getBlockHash(), expectBlockHash));
    Block block1 = new Block(block.bitcoinSerialize());
    assertTrue(Arrays.equals(block1.getBlockHash(), expectBlockHash));
}