From 599403ae2493d9fad00b0903d74a3f6f07ffcf58 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Thu, 25 Jan 2018 23:23:50 +0530 Subject: [PATCH 01/17] Updated readme with project details and installation --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 398b024..b26ff94 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,52 @@ -# blockshell +# BlockShell A command line utility for learning Blockchain. + + + +## About +Anyone who wants to understand how blockchain technology works, then BlockShell should be a great start. Because I have created BlockShell keeping blockchain fundamentals in the center of development. With BlockShell you will actually create a tiny blockchain in your system where you can create blocks with data, explore blocks etc. + +So, by using BlockShell anyone can learn following blockchain concepts, +* Block & Chaining +* Hashing +* Mining +* Proof of Work + +## Installation +Step 1 - Create project directory +``` +mkdir && cd project_name +``` + +Step 2 - Create new virtual environment +``` +virtualenv venv +``` + +Step 3 - Activate virtual environment +``` +source venv/bin/activate +``` + +Step 4 - Clone this repo +``` +git clone https://github.com/daxeel/blockshell.git +``` + +Step 5 - Change directory to cloned one +``` +cd blockshell +``` + +Step 6 - Install blockshell +``` +python setup.py install +``` + +Step 7 - Try "blockshell" command and test installation! +``` +blockshell +``` + +Output in terminal after calling BlockShell command + From 843f8023d0d4c470b7dfbd618b811250826b22cf Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Thu, 25 Jan 2018 23:33:06 +0530 Subject: [PATCH 02/17] Added BlockShell Web docs --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index b26ff94..7dfdb89 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,13 @@ So, by using BlockShell anyone can learn following blockchain concepts, * Mining * Proof of Work +## BlockShell Web Explorer +

BlockShell comes with built-in blockchain explorer by which you can actully see how blocks are mined and what is stored and where.

+ +Latest Mined Blocks | Block Details +:------------------------------:|:-------------------------: +![](https://preview.ibb.co/iZa5jG/Screen_Shot_2018_01_25_at_11_25_22_PM.png) | ![](https://preview.ibb.co/cDB0Jb/Screen_Shot_2018_01_25_at_11_25_35_PM.png) + ## Installation Step 1 - Create project directory ``` From 007d569e03e8b67dbbabc3dd7d63bec0cd8eab94 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Thu, 25 Jan 2018 23:35:33 +0530 Subject: [PATCH 03/17] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d5be28f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Daxeel Soni + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From a66eaa951ee2d8c16036b5a4056a28a94333979c Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Thu, 25 Jan 2018 23:54:45 +0530 Subject: [PATCH 04/17] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7dfdb89..d2b05f4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # BlockShell -A command line utility for learning Blockchain. +A command line utility for learning Blockchain technical concepts likechaining, mining, proof of work etc. - +BlockShell Logo | BlockShell CLI +:------------------------------:|:-------------------------: +![](https://preview.ibb.co/dhC7yb/Logomakr_5g_Ei_Dw.png) | ![](https://preview.ibb.co/h8OfJb/Screen_Shot_2018_01_25_at_11_51_03_PM.png) ## About Anyone who wants to understand how blockchain technology works, then BlockShell should be a great start. Because I have created BlockShell keeping blockchain fundamentals in the center of development. With BlockShell you will actually create a tiny blockchain in your system where you can create blocks with data, explore blocks etc. From 6e9a32de3270db2ce74241643d4c404e8e8a3ff5 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:33:46 +0530 Subject: [PATCH 05/17] Updated instructions and metadata --- bls.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/bls.py b/bls.py index 40e70a5..e73fad9 100644 --- a/bls.py +++ b/bls.py @@ -1,11 +1,25 @@ # -*- coding: utf-8 -*- -# import modules +# ================================================== +# ==================== META DATA =================== +# ================================================== +__author__ = "Daxeel Soni" +__url__ = "https://daxeel.github.io" +__email__ = "daxeelsoni44@gmail.com" +__license__ = "MIT" +__version__ = "0.1" +__maintainer__ = "Daxeel Soni" + +# ================================================== +# ================= IMPORT MODULES ================= +# ================================================== import click import urllib import json from blockshell import Block, Blockchain -# Supported commands +# ================================================== +# ===== SUPPORTED COMMANDS LIST IN BLOCKSHELL ====== +# ================================================== SUPPORTED_COMMANDS = [ 'dotx', 'allblocks', @@ -19,9 +33,14 @@ coin = Blockchain() # Create group of commands @click.group() def cli(): + """ + Create a group of commands for CLI + """ pass -# Start lbc cli +# ================================================== +# ============= BLOCKSHELL CLI COMMAND ============= +# ================================================== @cli.command() @click.option("--difficulty", default=3, help="Difine dufficulty level of blockchain.") def init(difficulty): @@ -36,19 +55,23 @@ def init(difficulty): > A command line utility for learning Blockchain concepts. > Type 'help' to see supported commands. + > Project by Daxeel Soni - https://daxeel.github.io """ # Set difficulty of blockchain coin.difficulty = difficulty - # Start lbc chell + # Start blockshell shell while True: - cmd = raw_input("[LBC] $ ") + cmd = raw_input("[BlockShell] $ ") processInput(cmd) -# Process input from LBC shell +# Process input from Blockshell shell def processInput(cmd): + """ + Method to process user input from Blockshell CLI. + """ userCmd = cmd.split(" ")[0] if len(cmd) > 0: if userCmd in SUPPORTED_COMMANDS: @@ -58,10 +81,14 @@ def processInput(cmd): msg = "Command not found. Try help command for documentation" throwError(msg) -# ------------------------------------ -# Supported Commands Methods -# ------------------------------------ + +# ================================================== +# =========== BLOCKSHELL COMMAND METHODS =========== +# ================================================== def dotx(cmd): + """ + Do Transaction - Method to perform new transaction on blockchain. + """ txData = cmd.split("dotx ")[-1] if "{" in txData: txData = json.loads(txData) @@ -69,12 +96,18 @@ def dotx(cmd): coin.addBlock(Block(data=txData)) def allblocks(cmd): + """ + Method to list all mined blocks. + """ print "" for eachBlock in coin.chain: print eachBlock.hash print "" def getblock(cmd): + """ + Method to fetch the details of block for given hash. + """ blockHash = cmd.split(" ")[-1] for eachBlock in coin.chain: if eachBlock.hash == blockHash: @@ -83,9 +116,15 @@ def getblock(cmd): print "" def help(cmd): + """ + Method to display supported commands in Blockshell + """ print "Commands:" print " dotx Create new transaction" print " allblocks Fetch all mined blocks in blockchain" def throwError(msg): + """ + Method to throw an error from Blockshell. + """ print "Error : " + msg From bed0ceac7eff44c821b93476ad06295209a5d1de Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:34:18 +0530 Subject: [PATCH 06/17] Updated metadata --- blockshell.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/blockshell.py b/blockshell.py index e423245..ba1a139 100644 --- a/blockshell.py +++ b/blockshell.py @@ -1,12 +1,30 @@ +# -*- coding: utf-8 -*- +# ================================================== +# ==================== META DATA =================== +# ================================================== +__author__ = "Daxeel Soni" +__url__ = "https://daxeel.github.io" +__email__ = "daxeelsoni44@gmail.com" +__license__ = "MIT" +__version__ = "0.1" +__maintainer__ = "Daxeel Soni" + +# ================================================== +# ================= IMPORT MODULES ================= +# ================================================== import hashlib import datetime import json from colorama import Fore, Back, Style import time -# index, timestamp, previousHash, blockHash, data - +# ================================================== +# ====================BLOCK CLASS ================== +# ================================================== class Block: + """ + Create new block with arguments 'data' + """ def __init__(self, data, index=0): self.index = index self.previousHash = "" @@ -16,10 +34,16 @@ class Block: self.hash = self.calculateHash() def calculateHash(self): + """ + Method to calculate hash for the block. + """ hashData = str(self.index) + str(self.data) + self.timestamp + str(self.nonce) return hashlib.sha256(hashData).hexdigest() def mineBlock(self, difficulty): + """ + Mine block with proof of work by initial zeros algorithm. + """ print Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ..." startTime = time.time() @@ -33,14 +57,23 @@ class Block: print Style.RESET_ALL class Blockchain: + """ + Inititate new blockchain with this class. + """ def __init__(self): self.chain = [self.createGenesisBlock()] self.difficulty = 3 def createGenesisBlock(self): + """ + Method to create genesis block after just creating an object. + """ return Block("Genesis Block") def addBlock(self, newBlock): + """ + Method to mine new block from data to blockchain. + """ newBlock.index = len(self.chain) newBlock.previousHash = self.chain[-1].hash newBlock.mineBlock(self.difficulty) @@ -48,6 +81,9 @@ class Blockchain: self.writeBlocks() def writeBlocks(self): + """ + This method will write newly created block to chain.txt file in json format. + """ dataFile = file("chain.txt", "w") chainData = [] for eachBlock in self.chain: From a5f5322a8411d7b69d258ebafeed66eae3216c68 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:37:46 +0530 Subject: [PATCH 07/17] Changed index endpoint --- web.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/web.py b/web.py index 5af2387..1bf2c48 100644 --- a/web.py +++ b/web.py @@ -1,14 +1,28 @@ +# -*- coding: utf-8 -*- +# ================================================== +# ==================== META DATA =================== +# ================================================== +__author__ = "Daxeel Soni" +__url__ = "https://daxeel.github.io" +__email__ = "daxeelsoni44@gmail.com" +__license__ = "MIT" +__version__ = "0.1" +__maintainer__ = "Daxeel Soni" + +# ================================================== +# ================= IMPORT MODULES ================= +# ================================================== from flask import Flask, render_template, jsonify import json +# Init flask app app = Flask(__name__) -# Index endpoint -@app.route('/') -def hello_world(): - return "Blockshell" -@app.route('/allblocks') -def blocks(): +@app.route('/') +def mined_blocks(): + """ + Endpoint to list all mined blocks. + """ f = open("chain.txt", "r") data = json.loads(f.read()) f.close() @@ -16,6 +30,9 @@ def blocks(): @app.route('/block/') def block(hash): + """ + Endpoint which shows all the data for given block hash. + """ f = open("chain.txt", "r") data = json.loads(f.read()) f.close() @@ -23,6 +40,6 @@ def block(hash): if eachBlock['hash'] == hash: return render_template('blockdata.html', data=eachBlock) - +# Run flask app if __name__ == '__main__': app.run(debug=True) From e7cb8df41dd9780ae4b9f66d99edad5856f9e1ee Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:39:46 +0530 Subject: [PATCH 08/17] Fixed bug for not defined /block/ endpoint --- web.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web.py b/web.py index 1bf2c48..973b554 100644 --- a/web.py +++ b/web.py @@ -39,6 +39,8 @@ def block(hash): for eachBlock in data: if eachBlock['hash'] == hash: return render_template('blockdata.html', data=eachBlock) + else: + return "No block found!" # Run flask app if __name__ == '__main__': From 8f60bbdc60fd3690d183eed3ddd9ad9b3968426a Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:53:37 +0530 Subject: [PATCH 09/17] Added about section and cleaned up code --- templates/blocks.html | 213 +++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 108 deletions(-) diff --git a/templates/blocks.html b/templates/blocks.html index 500c2bb..20bceac 100644 --- a/templates/blocks.html +++ b/templates/blocks.html @@ -1,114 +1,111 @@ - - - - - - - - - - Blockshell | All Blocks - - - - - - - - - - - - - - - -
-
-
- -

Latest Blocks

- {% for item in data -%} - - - - - - - - - - - - + + + + + From 148ac69199066da7110b6b999e74acb160a3f727 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 10:57:23 +0530 Subject: [PATCH 10/17] Update web.py --- web.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/web.py b/web.py index 973b554..1bf2c48 100644 --- a/web.py +++ b/web.py @@ -39,8 +39,6 @@ def block(hash): for eachBlock in data: if eachBlock['hash'] == hash: return render_template('blockdata.html', data=eachBlock) - else: - return "No block found!" # Run flask app if __name__ == '__main__': From b48a728d61422c03e3b5ff9e4592dbc3a8047429 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 11:02:15 +0530 Subject: [PATCH 11/17] Added about and cleaned up code --- templates/blockdata.html | 265 +++++++++++++++++++-------------------- 1 file changed, 128 insertions(+), 137 deletions(-) diff --git a/templates/blockdata.html b/templates/blockdata.html index 2a781d7..037ebb1 100644 --- a/templates/blockdata.html +++ b/templates/blockdata.html @@ -1,142 +1,133 @@ - - - - - - - - - - - - - - - Blockshell | All Blocks - - - - - - - - - - - - - - - -
-
-
- -

{{data['hash']}}

-
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Height{{data['index']}}
Timestamp{{data['timestamp']}}
Data{{data['data']}}
Hash{{data['hash']}}
Previous Block Hash{{data['previousHash']}}
Nonce{{data['nonce']}}
- - - -

- -

+ + - -
-
- Card image cap -
-
About Project
-

Project BlockShell is created by with a vision to teach blockchain concepts to students and programmers.

- View Profile -
-
-
- -
-
- - - - - - - - - - - + + + + + From 367b3c0d5472114f2d187d50b3740710366b93d5 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 11:03:02 +0530 Subject: [PATCH 12/17] View profile button edited --- templates/blocks.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/blocks.html b/templates/blocks.html index 20bceac..d6c327f 100644 --- a/templates/blocks.html +++ b/templates/blocks.html @@ -87,7 +87,7 @@
Card image cap

- + View Profile
Daxeel Soni
From 0ec27d95ce95a312632262a5fcc8aa0f52e14880 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 11:14:57 +0530 Subject: [PATCH 13/17] Added gif --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d2b05f4..82c1965 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # BlockShell A command line utility for learning Blockchain technical concepts likechaining, mining, proof of work etc. -BlockShell Logo | BlockShell CLI -:------------------------------:|:-------------------------: -![](https://preview.ibb.co/dhC7yb/Logomakr_5g_Ei_Dw.png) | ![](https://preview.ibb.co/h8OfJb/Screen_Shot_2018_01_25_at_11_51_03_PM.png) + + ## About Anyone who wants to understand how blockchain technology works, then BlockShell should be a great start. Because I have created BlockShell keeping blockchain fundamentals in the center of development. With BlockShell you will actually create a tiny blockchain in your system where you can create blocks with data, explore blocks etc. From bd57afff08a17b2767601f9a0961e05047ccd221 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 12:22:25 +0530 Subject: [PATCH 14/17] Added Blockshell web instructions --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 82c1965..aecdd9d 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,15 @@ Step 7 - Try "blockshell" command and test installation! blockshell ``` +## Lanuch BlockShell Web +Step 1 - Start new terminal window, go to cloned directory + +Step 2 - Start web.py +``` +python web.py +``` + +Step 3 - Go to 127.0.0.1:5000 address in browser and boom! + Output in terminal after calling BlockShell command From f3ef9e3384a0a9f163f80a9b342b8dbe9a2c3501 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Fri, 26 Jan 2018 12:23:10 +0530 Subject: [PATCH 15/17] Changed demo photo position --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aecdd9d..9ba3058 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ Step 7 - Try "blockshell" command and test installation! blockshell ``` +Output in terminal after calling BlockShell command + + ## Lanuch BlockShell Web Step 1 - Start new terminal window, go to cloned directory @@ -66,5 +69,3 @@ python web.py Step 3 - Go to 127.0.0.1:5000 address in browser and boom! -Output in terminal after calling BlockShell command - From 47dda9fe4c0eead12b65463c8007021054ec01d8 Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Sat, 27 Jan 2018 18:11:09 +0530 Subject: [PATCH 16/17] flask dependecy added --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 33cbef3..3c6e290 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,8 @@ setup( py_modules=['bls'], install_requires=[ 'Click', - 'colorama' + 'colorama', + 'flask' ], entry_points=''' [console_scripts] From 3e1907a9bd6181e82a93802bdb2f43504e47dcee Mon Sep 17 00:00:00 2001 From: Daxeel Soni Date: Sat, 27 Jan 2018 19:07:52 +0530 Subject: [PATCH 17/17] Delete blockshell.py --- blockshell.py | 92 --------------------------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 blockshell.py diff --git a/blockshell.py b/blockshell.py deleted file mode 100644 index ba1a139..0000000 --- a/blockshell.py +++ /dev/null @@ -1,92 +0,0 @@ -# -*- coding: utf-8 -*- -# ================================================== -# ==================== META DATA =================== -# ================================================== -__author__ = "Daxeel Soni" -__url__ = "https://daxeel.github.io" -__email__ = "daxeelsoni44@gmail.com" -__license__ = "MIT" -__version__ = "0.1" -__maintainer__ = "Daxeel Soni" - -# ================================================== -# ================= IMPORT MODULES ================= -# ================================================== -import hashlib -import datetime -import json -from colorama import Fore, Back, Style -import time - -# ================================================== -# ====================BLOCK CLASS ================== -# ================================================== -class Block: - """ - Create new block with arguments 'data' - """ - def __init__(self, data, index=0): - self.index = index - self.previousHash = "" - self.data = data - self.timestamp = str(datetime.datetime.now()) - self.nonce = 0 - self.hash = self.calculateHash() - - def calculateHash(self): - """ - Method to calculate hash for the block. - """ - hashData = str(self.index) + str(self.data) + self.timestamp + str(self.nonce) - return hashlib.sha256(hashData).hexdigest() - - def mineBlock(self, difficulty): - """ - Mine block with proof of work by initial zeros algorithm. - """ - print Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ..." - startTime = time.time() - - while self.hash[:difficulty] != "0"*difficulty: - self.nonce += 1 - self.hash = self.calculateHash() - - endTime = time.time() - print Back.BLUE + "[ Info ] Time Elapsed : " + str(endTime - startTime) + " seconds." - print Back.BLUE + "[ Info ] Mined Hash : " + self.hash - print Style.RESET_ALL - -class Blockchain: - """ - Inititate new blockchain with this class. - """ - def __init__(self): - self.chain = [self.createGenesisBlock()] - self.difficulty = 3 - - def createGenesisBlock(self): - """ - Method to create genesis block after just creating an object. - """ - return Block("Genesis Block") - - def addBlock(self, newBlock): - """ - Method to mine new block from data to blockchain. - """ - newBlock.index = len(self.chain) - newBlock.previousHash = self.chain[-1].hash - newBlock.mineBlock(self.difficulty) - self.chain.append(newBlock) - self.writeBlocks() - - def writeBlocks(self): - """ - This method will write newly created block to chain.txt file in json format. - """ - dataFile = file("chain.txt", "w") - chainData = [] - for eachBlock in self.chain: - chainData.append(eachBlock.__dict__) - dataFile.write(json.dumps(chainData, indent=4)) - dataFile.close()