项目作者: ahmed-BH

项目描述 :
Blockchain-demo is an implemtation of the blockchain-data-structure and a small simulation how the blockchain works.
高级语言: Python
项目地址: git://github.com/ahmed-BH/Blockchain-demo.git
创建时间: 2018-11-11T12:53:49Z
项目社区:https://github.com/ahmed-BH/Blockchain-demo

开源协议:GNU General Public License v3.0

下载


Blockchain-demo

Blockchain-demo is an implemtation of the blockchain-data-structure and a small simulation how the blockchain
works.See wiki section for more details

Prerequisites

  • python3 should be installed.

  • Installing needed python modules:

  1. pip install -r requirements.txt

Running

  1. cd Blockchain-demo/
  • Generating asymmetric cryptography to be used in transactions:
    ```python
    from utils.encryption import *
    person_1 = generate_encryption_keys()
    person_2 = generate_encryption_keys()
  1. **generate_encryption_keys()** return a dictionnary object with *public_key* and *private_key* as keys.
  2. Note that the dictionnary values are respectively **RSAPublicKey** and **RSAPrivateKey** objects from the **cryptography** module.
  3. * Making a transaction : *person_1* sends something to *person_2*:
  4. ```python
  5. from transaction import Transaction
  6. tr_1 = Transaction(sender=person_1["public_key"], receiver=person_2["public_key"], assets="SOMETHING")
  7. tr_1.sign(person_1["private_key"])

A transaction must be signed with sign() methods in order to be valid.
The is_valid() method can be used to verify wether a transaction object is valid or not.

  1. tr_1.is_valid()
  2. True

Now, we will create a block that holds the transaction.

  1. from block import Block
  2. bl_1 = Block(tr_1)

To be a valid block (also to be added to the blockchain), a proof of work is required.
mine() method will find a solution to the hash puzzle according to a difficulty level.

  1. bl_1.mine()

Creating a Blockchain and adding a block..

  1. from blockchain import Blockchain
  2. blc = Blockchain()
  3. blc.add_block(bl_1)

show_blocks() can be used to print all blocks in the blockchain.

  1. from blockchain import Blockchain
  2. blc.show_blocks()