Skip to main content

UCLCoin: A naive blockchain/cryptocurrency implementation

Project description

https://img.shields.io/pypi/v/uclcoin.svg?style=flat-square https://img.shields.io/pypi/pyversions/uclcoin.svg?style=flat-square https://img.shields.io/pypi/l/uclcoin.svg?style=flat-square

A naive blockchain/cryptocurrency implementation for educational purposes.

Installation

UCLCoin is distributed on PyPI and is available on Linux/macOS and Windows and supports Python 3.6+.

$ pip install -U uclcoin

Examples

For the following code examples it is assumed that the necessary dependecies were imported.

>>> from uclcoin import KeyPair, Transaction, Block, BlockChain

Generating your key pair

The private key is used to sign transactions and the public key is your UCLCoin address. It is used to receive coins from transactions and rewards from mining.

Use the KeyPair class to generate your keys

>>> wallet = KeyPair()
>>> address = wallet.public_key
>>> address
'03d70f9a58c9bc6d8fdc47f96d6931f14a7abb0d72cd76886ee05047023fd49471'

To reuse your key pair instantiate the class using your private key wallet.private_key

>>> wallet = KeyPair('your-private-key')

BlockChain

Create a new BlockChain for doing your tests:

>>> blockchain = BlockChain()

Your blockchain will contain only the Genesis block. It is ready to accept transactions, but you can’t send any coins if your balance is zero.

>>> blockchain.get_balance(wallet.public_key)
0

Mining a block

Get a new minable block from the blockchain:

>>> new_block = blockchain.get_minable_block(wallet.public_key)

The blockchain returns a new block with the next valid index, any pending transactions and a coinbase (reward) transaction to your public key (wallet.public_key)

UCLCoin the proof of work consists on manipulating the nonce field and recalculating the block hash until it starts with N zeros. The current difficulty can be obtained with the calculate_hash_difficulty method.

>>> N = blockchain.calculate_hash_difficulty()

A simple mining method is to increment the nonce until you get a valid hash:

>>> while new_block.current_hash[:N].count('0') < N:
...     new_block.nonce +=1
...     new_block.recalculate_hash()

The operation will block while the hash is calculated. After finishing just submit the new block to the blockchain. If it is accepted your balance will be updated.

>>> blockchain.add_block(new_block)
True
>>> blockchain.get_balance(wallet.public_key)
10

Sending a transaction

You can now spent your new coins.

>>> destination = 'public_key_of_the_receiver'
>>> value = wallet.create_transaction(destination, 2)
>>> blockchain.add_transaction(value)
True

Your transaction will be added to the pending transactions queue. It will only be confirmed after being included in a mined block.

>>> blockchain.get_balance(wallet.public_key)
10

You can check your balance including the pending transactions

>>> blockchain.get_balance_pending(wallet.public_key)
8

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uclcoin-0.3.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

uclcoin-0.3.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file uclcoin-0.3.2.tar.gz.

File metadata

  • Download URL: uclcoin-0.3.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for uclcoin-0.3.2.tar.gz
Algorithm Hash digest
SHA256 87ca5a2c8f5139d2b60f687e16e56d1a644c68a7f75cd7ab1c043855bcb5e315
MD5 887b6ead49f9e602d48de858d8ece0eb
BLAKE2b-256 081d3d2ed3186cd59632c76695ecc31595edada3e7d8ddb3d544b54f50c9d96d

See more details on using hashes here.

File details

Details for the file uclcoin-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for uclcoin-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 718aa1d14bbdea42fce7e668b0548d65e436bbde432526df91cbe90dc9d123c6
MD5 d2a90c50bfea0cabbcbb7721c1c1956c
BLAKE2b-256 b9d241896f28f01072044d229a39392f4c0dc4207d2d525b8a591c9afdbb2932

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page