reduxu-chain
yeah, I am yet another blockchain implementation in javascript.
See example on https://codesandbox.io/s/34wm9xq8m5
Simple state container like implementation of the blockchain driven flowtype checked, this is in an super early stage and can fail...
Key concepts of reduxu-chain
- A tiny redux like implementation of the blockchain.
- Typed with flowtypes, styled by prettier.
- Allows minning the block or build some kind of smart contract.
- A consensus algorithm, simple proof-of-work or proof-of-stake implementation using nonce
- Built by rollup, it should work on web and node, but tried it for
react-native
Table of Contents
Installation
Using yarn:
$ yarn add reduxu-chain
Dependencies
- mitt an awesome tiny library to broadcast a block notification through a middleware
- crypto-js is here to generate sha256
hash
for each block
Usage
; ; { thisstore = ; ; } { return thisstore; } { return thisstore; } { return thisstore; } { // add transaction or define any smart contract const tx = timestamp: / 1000 ; thisstore; } // Add a block into the local BlockChain { // this one is the last as of now but might not be after minning const lastBlock = thisstore; const block = previousBlock: lastBlock data ; // consensus algorithm return ; }Component { thisstore1; this; } { return <Card> <CardContent> <Title>Store 1 Local Block Info</Title> <Caption> thisstateblock1transactions open transactions </Caption> <Paragraph>JSON</Paragraph> </CardContent> </Card> ; }
Simple middleware to handle side effects using eventEmitterMiddleware ( or build yours )
let emitter = ; { emitter; return { return { if actiontype === "@Blockchain/NEW_BLOCK" // fire an event when accepted a new block emitter; ; }; };}
Definitions
The block
// @flow interface IBlock hash: string; // define your state, any kind of transaction state can be here +data: State; // serialized data, this might go through p2p, but I am not trackling that as of now _data: string; // optional props previousBlock?: IBlock; // integrity to the chain is validated in block each time. previousHash: string; // privates // timestamp to create the hash +timestamp: number; // validates the block after minning nonce: number;
Action creators
// @flow | // action dispatched when the store is created, and it is not handled by the middlewares type: "@Blockchain/INIT" | // action dispatched when new block is added to the local chain type: "@Blockchain/NEW_BLOCK" newBlock: IBlock difficulty: number ;
The blockchain
// @flow interface IBlockChain // the entire blockchain chain: Array<IBlock>; // amount of zeros before the hash, increments the nonce till get it right, this brings security to the transaction and it is kind of useless :) difficulty: number; // works along with getLastBlock() if this chain is last one, you did it. // emmits Promise<*>; IBlock; // returns a block that might not be the last one :( // replacing the local chain and trying to emit your change void; // minning a just a block Promise<*>;
Related projects
Awesome inspiring projects I looked into!!!
Contribute
Thanks for taking the time to contribute!
Credits
This project was originally forked and inspired from blockchain-redux
and the awesome work of Swizec Teller 👍
TODO
- Allows implementing a P2P communication through a redux
applyMiddleware(somethingMiddlewareP2PLike)
- Allows designing your reducers to develop a transaction handler or your smart contract and
data: State
should be serialized.