Skip to content

Instantly share code, notes, and snippets.

@shekohex
Created December 21, 2017 00:06
Show Gist options
  • Save shekohex/3aef759074ad9fcb31fe0f772c8f8fb4 to your computer and use it in GitHub Desktop.
Save shekohex/3aef759074ad9fcb31fe0f772c8f8fb4 to your computer and use it in GitHub Desktop.
Simple Block Chain Mechanism in Dart for Learning Purpose
library block;
import 'dart:async';
import 'package:crypto/crypto.dart' show sha256;
class Block {
int nonce = 0;
String _hash;
final int index;
final int ts;
final List<int> data;
String pHash;
Block({this.index, this.ts, this.data, this.pHash}) {
_hash = calculateHash();
}
get hash => _hash;
set hash(String hash) => _hash = hash;
set previousHash(String hash) => pHash = hash;
get previousHash => pHash;
Future<bool> mine(int difficulty) async {
while (_hash.substring(0, difficulty) != "0" * difficulty) {
nonce++;
print('\u2717: \x1b[31m$hash\x1b[0m');
hash = await calculateHash();
}
print('\u2713: \x1b[32m$hash\x1b[0m');
return true;
}
String calculateHash() {
if (pHash == null) pHash = "0";
data.addAll([index, ts, int.parse(pHash, radix: 35), nonce]);
return sha256.convert(data).toString();
}
}
import 'dart:async';
import 'block.dart';
class BlockChain {
final int difficulty = 2; // don't make it harder :D
List<Block> chain = [];
BlockChain.createChain() {
chain = [
new Block(
index: 0,
data: "1".codeUnits.toList(growable: true),
ts: new DateTime.now().millisecondsSinceEpoch)
];
}
Future<bool> add(Block block) async {
block.previousHash = chain.last.hash;
await block.mine(difficulty);
chain.add(block);
if (!_isChainValid()) {
chain.remove(block);
return false;
}
return true;
}
Future<bool> addAll(Iterable<Block> blocks) async {
for (var block in blocks) {
await add(block);
}
return true;
}
bool _isChainValid() {
for (var i = 1; i < chain.length; i++) {
var currentBlock = chain[i];
var previousBlock = chain[i - 1];
if (currentBlock.previousHash != previousBlock.hash) {
return false;
}
}
return true;
}
}
library main;
import 'block.dart';
import 'block_chain.dart';
main() async {
BlockChain chain = new BlockChain.createChain();
Block block = new Block(
index: 1,
ts: new DateTime.now().millisecondsSinceEpoch,
data: "124".codeUnits.toList(growable: true));
Block block2 = new Block(
index: 2,
ts: new DateTime.now().millisecondsSinceEpoch,
data: "20".codeUnits.toList(growable: true));
Block block3 = new Block(
index: 3,
ts: new DateTime.now().millisecondsSinceEpoch,
data: "10".codeUnits.toList(growable: true));
Block block4 = new Block(
index: 4,
ts: new DateTime.now().millisecondsSinceEpoch,
data: "30".codeUnits.toList(growable: true));
Block block5 = new Block(
index: 5,
ts: new DateTime.now().millisecondsSinceEpoch,
data: "44".codeUnits.toList(growable: true));
try {
await chain.add(block);
await chain.addAll([block2, block3, block4, block5]);
} catch (e) {
print(e);
}
print('\x1b[42m\x1b[34m Done added ${chain.chain.length} blocks \x1b[0m');
}
name: blockchain
version: 1.0.0
description: >
Simple Block Chain Mechanism in Dart for Learning Purpose
author: Shady Khalifa <shekohex@gmail.com>
dependencies:
crypto: "^2.0.2+1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment