162 lines
4.5 KiB
Python
162 lines
4.5 KiB
Python
# -*- 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 click
|
|
import urllib
|
|
import json
|
|
from blockchain.chain import Block, Blockchain
|
|
from colorama import Fore, Back, Style
|
|
|
|
# ==================================================
|
|
# ===== SUPPORTED COMMANDS LIST IN BLOCKSHELL ======
|
|
# ==================================================
|
|
SUPPORTED_COMMANDS = [
|
|
'dotx',
|
|
'allblocks',
|
|
'getblock',
|
|
'help'
|
|
]
|
|
|
|
def printLogo():
|
|
print("""
|
|
____ _ _ _____ _ _ _
|
|
| _ \ | | | | / ____| | | | | | |
|
|
| |_) | | | ___ ___ | | __ | (___ | |__ ___ | | | |
|
|
| _ < | | / _ \ / __| | |/ / \___ \ | '_ \ / _ \ | | | |
|
|
| |_) | | | | (_) | | (__ | < ____) | | | | | | __/ | | | |
|
|
|____/ |_| \___/ \___| |_|\_\ |_____/ |_| |_| \___| |_| |_|
|
|
|
|
> A command line utility for learning Blockchain concepts.
|
|
> Type 'help' to see supported commands.
|
|
> Project by Daxeel Soni - https://daxeel.github.io
|
|
|
|
""")
|
|
|
|
def startShell():
|
|
"""
|
|
Method to start Blockshell CLI
|
|
"""
|
|
printLogo()
|
|
# Start blockshell shell
|
|
while True:
|
|
cmd = input("[BlockShell] $ ")
|
|
processInput(cmd)
|
|
|
|
# Init blockchain
|
|
coin = Blockchain()
|
|
|
|
# Create group of commands
|
|
@click.group()
|
|
def cli():
|
|
"""
|
|
Create a group of commands for CLI
|
|
"""
|
|
pass
|
|
|
|
# ==================================================
|
|
# ============= BLOCKSHELL CLI COMMAND =============
|
|
# ==================================================
|
|
@cli.command()
|
|
@click.option("--difficulty", default=3, help="Define difficulty level of blockchain.")
|
|
def init(difficulty):
|
|
"""Initialize local blockchain"""
|
|
|
|
coin.writeBlocks()
|
|
# Set difficulty of blockchain
|
|
coin.difficulty = difficulty
|
|
|
|
# Start blockshell shell
|
|
startShell()
|
|
|
|
@cli.command()
|
|
@click.argument("filename", type=click.Path())
|
|
@click.option("--difficulty", default=3, help="Define difficulty level of blockchain.")
|
|
def load(filename, difficulty):
|
|
"""Load blockchain from file"""
|
|
|
|
# Load blockchain from file
|
|
coin.loadBlocks(filename)
|
|
|
|
# Set difficulty of blockchain
|
|
coin.difficulty = difficulty
|
|
|
|
# Start blockshell shell
|
|
startShell()
|
|
|
|
# Process input from Blockshell shell
|
|
def processInput(cmd):
|
|
"""
|
|
Method to process user input from Blockshell CLI.
|
|
"""
|
|
splitted = cmd.split(" ")
|
|
userCmd = splitted[0]
|
|
if len(splitted) and len(userCmd) > 0:
|
|
if userCmd in SUPPORTED_COMMANDS:
|
|
globals()[userCmd](splitted[1:])
|
|
else:
|
|
# error
|
|
msg = "Command not found. Try help command for documentation"
|
|
throwError(msg)
|
|
|
|
|
|
# ==================================================
|
|
# =========== BLOCKSHELL COMMAND METHODS ===========
|
|
# ==================================================
|
|
def dotx(args):
|
|
"""
|
|
Do Transaction - Method to perform new transaction on blockchain.
|
|
"""
|
|
print("Doing transaction...")
|
|
coin.addBlock(Block.fromValues(int(args[0]), args[1], args[2], args[3]))
|
|
|
|
def allblocks(cmd):
|
|
"""
|
|
Method to list all mined blocks.
|
|
"""
|
|
print("")
|
|
for eachBlock in coin.chain:
|
|
print(eachBlock.hash)
|
|
print("")
|
|
|
|
def getblock(args):
|
|
"""
|
|
Method to fetch the details of block for given hash.
|
|
"""
|
|
blockHash = args[0]
|
|
for eachBlock in coin.chain:
|
|
if eachBlock.hash == blockHash:
|
|
print("")
|
|
print(eachBlock.__dict__)
|
|
print("")
|
|
|
|
def help(cmd):
|
|
"""
|
|
Method to display supported commands in Blockshell
|
|
"""
|
|
print("Commands:")
|
|
print(" dotx <uid> <mail> <firstname> <lastname> Create new transaction")
|
|
print(" allblocks Fetch all mined blocks in blockchain")
|
|
print(" getblock <block hash> Fetch information about particular block")
|
|
|
|
def throwError(msg):
|
|
"""
|
|
Method to throw an error from Blockshell.
|
|
"""
|
|
print(f"{ Fore.RED }{ msg }{ Fore.RESET }")
|
|
|
|
def printSuccess(msg):
|
|
"""
|
|
Method to print success message from Blockshell.
|
|
"""
|
|
print(f"{ Fore.GREEN }{ msg }{ Fore.RESET }") |