Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add type hints & general tidy up #135

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ jobs:
. venv/bin/activate
mkdir test-results
pytest -v -s --junitxml=test-reports/junit.xml --cov=crypto --cov-config=.coveragerc --cov-report xml
- name: Codecov
run: bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }}
30 changes: 0 additions & 30 deletions CHANGELOG.md

This file was deleted.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
> A simple Cryptography Implementation in Python for the Ark Blockchain.

[![Build Status](https://badgen.now.sh/circleci/github/ArkEcosystem/python-crypto)](https://circleci.com/gh/ArkEcosystem/python-crypto)
[![Codecov](https://badgen.now.sh/codecov/c/github/arkecosystem/python-crypto)](https://codecov.io/gh/arkecosystem/python-crypto)
[![Latest Version](https://badgen.now.sh/github/release/ArkEcosystem/python-crypto)](https://github.com/ArkEcosystem/python-crypto/releases/latest)
[![License: MIT](https://badgen.now.sh/badge/license/MIT/green)](https://opensource.org/licenses/MIT)

Expand Down
11 changes: 5 additions & 6 deletions crypto/configuration/fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

fees = TRANSACTION_FEES.copy()


def get_fee(transaction_type):
def get_fee(transaction_type: int, *, default: int = 0) -> int:
"""Get a fee for a given transaction type

Args:
transaction_type (int): transaction type for which we wish to get a fee

Returns:
int: transaction fee
int | None: transaction fee, or None if the transaction type is not found
"""
return fees.get(transaction_type)

return fees.get(transaction_type, default)

def set_fee(transaction_type, value):
def set_fee(transaction_type: int, value: int) -> None:
"""Set a fee

Args:
transaction_type (int): transaction_type for which we wish to set a fee
value (int): fee for a given transaction type
"""
global fees

fees[transaction_type] = value
28 changes: 19 additions & 9 deletions crypto/configuration/network.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
from datetime import datetime
from typing import Type, TypedDict, Union
from crypto.networks.devnet import Devnet
from crypto.networks.mainnet import Mainnet
from crypto.networks.testnet import Testnet

network = {}
class NetworkType(TypedDict):
epoch: datetime
version: int
wif: int

network: NetworkType = {
'epoch': Devnet.epoch,
'version': Devnet.version,
'wif': Devnet.wif,
}

def set_network(network_object):
def set_network(network_object: Union[Type[Mainnet], Type[Devnet], Type[Testnet]]) -> None:
"""Set what network you want to use in the crypto library

Args:
network_object (Network object): Testnet, Devnet, Mainnet
network_object: Testnet, Devnet, Mainnet
"""
global network

network = {
'epoch': network_object.epoch,
'version': network_object.version,
'wif': network_object.wif,
}


def get_network():
def get_network() -> NetworkType:
"""Get settings for a selected network, default network is devnet

Returns:
dict: network settings (default network is devnet)
"""
if not network:
set_network(Devnet)
return network


def set_custom_network(epoch, version, wif):
def set_custom_network(epoch: datetime, version: int, wif: int) -> None:
"""Set custom network

Args:
Expand All @@ -37,6 +46,7 @@ def set_custom_network(epoch, version, wif):
wif (int): chains wif
"""
global network

network = {
'epoch': epoch,
'version': version,
Expand Down
35 changes: 16 additions & 19 deletions crypto/identity/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from crypto.identity.private_key import PrivateKey

from Cryptodome.Hash import RIPEMD160, keccak
from Cryptodome.Hash import keccak
from coincurve import PrivateKey, PublicKey

def get_checksum_address(address: bytes) -> str:
def get_checksum_address(address: str) -> str:
"""Get checksum address

Args:
Expand All @@ -33,50 +33,47 @@ def get_checksum_address(address: bytes) -> str:

return "0x" + ''.join(chars)

def address_from_public_key(public_key):
def address_from_public_key(public_key: str) -> str:
"""Get an address from a public key

Args:
public_key (str):
public_key (str): public key to get address

Returns:
str: address
"""

public_key = PublicKey(bytes.fromhex(public_key)).format(compressed=False)[1:]
public_key_bytes = PublicKey(bytes.fromhex(public_key)).format(compressed=False)[1:]

keccak_hash = keccak.new(
data=bytearray.fromhex(public_key.hex()),
data=bytearray.fromhex(public_key_bytes.hex()),
digest_bits=256,
)

return get_checksum_address(unhexlify(keccak_hash.hexdigest()[22:]).hex())


def address_from_private_key(private_key):
def address_from_private_key(private_key: str) -> str:
"""Get an address from private key

Args:
private_key (string):
private_key (string): private key to get address

Returns:
TYPE: Description
str: address
"""
private_key = PrivateKey.from_hex(private_key)

return address_from_public_key(private_key.public_key.format(compressed=False).hex())
private_key_object = PrivateKey.from_hex(private_key)

return address_from_public_key(private_key_object.public_key.format(compressed=False).hex())

def address_from_passphrase(passphrase):
def address_from_passphrase(passphrase: str) -> str:
"""Get an address from passphrase

Args:
passphrase (str):
network_version (int, optional):
passphrase (str): passphrase to get address

Returns:
string: address
str: address
"""
private_key = hashlib.sha256(passphrase.encode()).hexdigest()
address = address_from_private_key(private_key)
return address

return address_from_private_key(private_key)
14 changes: 7 additions & 7 deletions crypto/identity/private_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@

from coincurve import PrivateKey as PvtKey


class PrivateKey(object):
def __init__(self, private_key):
def __init__(self, private_key: str):
self.private_key = PvtKey.from_hex(private_key)
self.public_key = hexlify(self.private_key.public_key.format()).decode()

def sign(self, message):
def sign(self, message: bytes) -> bytes:
"""Sign a message with this private key object

Args:
message (bytes): bytes data you want to sign

Returns:
str: signature of the signed message
bytes: signature of the signed message
"""
from crypto.transactions.signature import Signature

signature = Signature.sign(
hexlify(message),
self.private_key.to_hex()
self
)

return signature.encode()
Expand All @@ -36,7 +35,7 @@ def to_hex(self):
return self.private_key.to_hex()

@classmethod
def from_passphrase(cls, passphrase):
def from_passphrase(cls, passphrase: str):
"""Create PrivateKey object from a given passphrase

Args:
Expand All @@ -46,10 +45,11 @@ def from_passphrase(cls, passphrase):
PrivateKey: Private key object
"""
private_key = sha256(passphrase.encode()).hexdigest()

return cls(private_key)

@classmethod
def from_hex(cls, private_key):
def from_hex(cls, private_key: str):
"""Create PrivateKey object from a given hex private key

Args:
Expand Down
10 changes: 5 additions & 5 deletions crypto/identity/public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from crypto.identity.private_key import PrivateKey

class PublicKey(object):
def __init__(self, public_key):
public_key = unhexlify(public_key.encode())
self.public_key = PubKey(public_key)
def __init__(self, public_key: str):
self.public_key = PubKey(unhexlify(public_key.encode()))

def to_hex(self):
def to_hex(self) -> str:
return hexlify(self.public_key.format()).decode()

@classmethod
def from_passphrase(cls, passphrase):
def from_passphrase(cls, passphrase: str) -> str:
private_key = PrivateKey.from_passphrase(passphrase)

return private_key.public_key

@classmethod
Expand Down
7 changes: 4 additions & 3 deletions crypto/identity/wif.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import hashlib
from typing import Optional

from base58 import b58encode_check

from binary.unsigned_integer.writer import write_bit8

from crypto.configuration.network import get_network


def wif_from_passphrase(passphrase, network_wif=None):
def wif_from_passphrase(passphrase: str, network_wif: Optional[int] = None):
"""Get wif from passphrase

Args:
passphrase (bytes):
passphrase (str):
network_wif (int, optional):

Returns:
string: wif
"""
if not network_wif:
network = get_network()

network_wif = network['wif']

private_key = hashlib.sha256(passphrase.encode())
Expand Down
1 change: 0 additions & 1 deletion crypto/networks/devnet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime


class Devnet(object):
epoch = datetime(2017, 3, 21, 13, 00, 00)
version = 30
Expand Down
1 change: 0 additions & 1 deletion crypto/networks/mainnet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime


class Mainnet(object):
epoch = datetime(2017, 3, 21, 13, 00, 00)
version = 23
Expand Down
1 change: 0 additions & 1 deletion crypto/networks/testnet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime


class Testnet(object):
epoch = datetime(2017, 3, 21, 13, 00, 00)
version = 23
Expand Down
1 change: 0 additions & 1 deletion crypto/schnorr/__init__.py

This file was deleted.

Loading
Loading