Docs

How do you use this?

Intro

To create a new BlockChain object, you only need a genesis string and a HashType.

BlockChain chain = new BlockChain("genesis string", Util.HashType.SHA3_256)

If your blockchain will only store string data, it is preferred to use a StringChain.

StringChain chain = new StringChain("genesis", Util.HashType.SHA3_256)

The genesis string is what will be used to generate the previous hash for the first block. The hash type just specifies whether the SHA 256 or the SHA-3 hashing algorithm will be used. The difference comes down to preformance vs security.

At their essence, blockchains look something like this:

If any data in any block is altered, so would its hash, invalidating the condition that the next block hold the previous block's hash. This process can be explained more in depth, but what it means for us now is that no modifications can happen on any block except the last.

Because of this, adding data is simple.

chain.add("party X transferred $20 to party A");
chain.add("party M transferred $90 to party D");
chain.add("party D transferred $30 to party F");

Typically every x entries, or every predetermined time period, the head block is sealed and data begins being stored in a new head block. Every time this happens, the hash of the previous head is established, stored in the new head, and the data is recorded forever. Commanding this is also a simple process.

chain.nextBlock();

Now all new data added will go in a new block. Run nextBlock() again and further data will go in a third block.

To see your blockchain at this point, a simple toString() will do.

The full list of what you can do with your blockchain is as follows:

Methods

Next Block

Seals current head block and creates new one. Returns false if failed
boolean nextBlock();

Add single object

Adds something to the head block. Returns false if failed
boolean add(Object item);

Add multiple objects

Adds every parameter to the head block. Returns false if failed
boolean add(Object... items);

size

Returns amount of blocks in chain including unsealed head
int size();

Total items

Returns total number of items in every block of chain
int totalItems();

Discard head

Discards current head and everything in it. Creates a new one to replace it.
void discardBlock();

find

Returns id of the first block that contains a given object. Returns -1 if it cannot be found.
int find(Object item);

soonest to

Returns id of the block with a timestamp nearest to the one provided, or -1 if chain is empty.
int soonestTo(Date timestamp);

get contents

Returns an array holding everything in a specific block of given id.
Object[] getContents(int blockID);

to array

Converts entire chain to a 2D array
Object[][] toArray();

is current empty

Checks if current head block is empty.
boolean isCurrentEmpty();

is everything empty

Checks if every block in chain is empty.
boolean isCurrentEmpty();

verify

Attempts to recreate hashes of every block to see if any data has been modified. Returns true if chain is intact, or false if any sealed blocks in chain have been tampered with in the slightest.
boolean verify();

to string

Returns a visual representation of all the data in the chain, like the image in the section above
String toString();

serialize

Returns a tokenized version of toString() that is used internally to save chain to files. 
String serialize();

Save & Load

Blockchains work best decentralized, with various sources having a copy of the chain and saving to a file when changes are made.

To save a blockchain to a file, simply do

Util.save("filename.txt", chain);

Internally, this runs serialize() on the chain and prints it to a specified file. It should look something like this:

To load this file onto a blockchain object, do:

StringChain chain = Util.load("filename.txt")

As of now, only StringChains can be loaded from files. This is because blockchains can only be saved in text files. Database support will be added in the future.