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

The following examples show how to use net.bither.bitherj.utils.Utils#uint32ToByteStreamLE() . 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: ScriptChunk.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void write(OutputStream stream) throws IOException {
    if (isOpCode()) {
        checkState(data == null);
        stream.write(opcode);
    } else if (data != null) {
        checkNotNull(data);
        if (opcode < OP_PUSHDATA1) {
            checkState(data.length == opcode);
            stream.write(opcode);
        } else if (opcode == OP_PUSHDATA1) {
            checkState(data.length <= 0xFF);
            stream.write(OP_PUSHDATA1);
            stream.write(data.length);
        } else if (opcode == OP_PUSHDATA2) {
            checkState(data.length <= 0xFFFF);
            stream.write(OP_PUSHDATA2);
            stream.write(0xFF & data.length);
            stream.write(0xFF & (data.length >> 8));
        } else if (opcode == OP_PUSHDATA4) {
            checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
            stream.write(OP_PUSHDATA4);
            Utils.uint32ToByteStreamLE(data.length, stream);
        } else {
            throw new RuntimeException("Unimplemented");
        }
        stream.write(data);
    } else {
        stream.write(opcode); // smallNum
    }
}
 
Example 2
Source File: PartialMerkleTree.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
    Utils.uint32ToByteStreamLE(transactionCount, stream);

    stream.write(new VarInt(hashes.size()).encode());
    for (byte[] hash : hashes)
        stream.write(hash);

    stream.write(new VarInt(matchedChildBits.length).encode());
    stream.write(matchedChildBits);
}
 
Example 3
Source File: GetBlocksMessage.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
    // Version, for some reason.
    Utils.uint32ToByteStreamLE(BitherjSettings.PROTOCOL_VERSION, stream);
    // Then a vector of block hashes. This is actually a "block locator", a set of block
    // identifiers that spans the entire chain with exponentially increasing gaps between
    // them, until we end up at the genesis block. See CBlockLocator::Set()
    stream.write(new VarInt(locator.size()).encode());
    for (byte[] hash : locator) {
        // Have to reverse as wire format is little endian.
        stream.write(hash);
    }
    // Next, a block ID to stop at.
    stream.write(stopHash);
}
 
Example 4
Source File: ListMessage.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
    stream.write(new VarInt(items.size()).encode());
    for (InventoryItem i : items) {
        // Write out the type code.
        Utils.uint32ToByteStreamLE(i.type.ordinal(), stream);
        // And now the hash.
        stream.write(i.hash);
    }
}
 
Example 5
Source File: Block.java    From bitherj with Apache License 2.0 5 votes vote down vote up
void writeHeader(OutputStream stream) throws IOException {
    Utils.uint32ToByteStreamLE(this.blockVer, stream);
    stream.write(this.blockPrev);
    stream.write(this.blockRoot);
    Utils.uint32ToByteStreamLE(this.blockTime, stream);
    Utils.uint32ToByteStreamLE(this.blockBits, stream);
    Utils.uint32ToByteStreamLE(this.blockNonce, stream);
}
 
Example 6
Source File: In.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
    stream.write(prevTxHash);
    Utils.uint32ToByteStreamLE(prevOutSn, stream);
    stream.write(new VarInt(inSignature.length).encode());
    stream.write(inSignature);
    Utils.uint32ToByteStreamLE(inSequence, stream);
}
 
Example 7
Source File: BloomFilter.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes this message to the provided stream. If you just want the raw bytes use
 * bitcoinSerialize().
 */
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
    stream.write(new VarInt(data.length).encode());
    stream.write(data);
    Utils.uint32ToByteStreamLE(hashFuncs, stream);
    Utils.uint32ToByteStreamLE(nTweak, stream);
    stream.write(nFlags);
}
 
Example 8
Source File: Out.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public byte[] getOutpointData() {
    ByteArrayOutputStream stream = new UnsafeByteArrayOutputStream(36 + 32);
    try {
        stream.write(getTxHash());
        Utils.uint32ToByteStreamLE(getOutSn(), stream);
    } catch (IOException e) {
        return null;
    }
    return stream.toByteArray();
}