Skip to content
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
6 changes: 3 additions & 3 deletions crypto/configuration/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class NetworkType(TypedDict):
epoch: datetime
wif: int
wif: str
chain_id: int

network: NetworkType = {
Expand Down Expand Up @@ -36,12 +36,12 @@ def get_network() -> NetworkType:
"""
return network

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

Args:
epoch (datetime): chains epoch time
wif (int): chains wif
wif (str): chains wif
chain_id (int): chain id
"""
global network
Expand Down
23 changes: 23 additions & 0 deletions crypto/identity/private_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from hashlib import sha256
from coincurve import PrivateKey as PvtKey
from Cryptodome.Hash import keccak
from base58 import b58decode

from crypto.configuration.network import get_network
from crypto.enums.constants import Constants

def keccak256(data: bytes) -> bytes:
Expand Down Expand Up @@ -74,3 +76,24 @@ def from_hex(cls, private_key: str):
PrivateKey: Private key object
"""
return cls(private_key)

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

Args:
wif (str):

Returns:
PrivateKey: Private key object
"""

wif = b58decode(wif).hex()

version = wif[0:2]
if version != get_network()['wif']:
raise ValueError(f"Invalid network version: {version}")

private_key = wif[2:66]

return cls(private_key)
6 changes: 3 additions & 3 deletions crypto/identity/wif.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

from crypto.configuration.network import get_network

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

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

Returns:
string: wif
Expand All @@ -23,6 +23,6 @@ def wif_from_passphrase(passphrase: str, network_wif: Optional[int] = None):
network_wif = network['wif']

private_key = hashlib.sha256(passphrase.encode())
seed = write_bit8(network_wif) + private_key.digest() + write_bit8(0x01)
seed = write_bit8(int(network_wif, 16)) + private_key.digest() + write_bit8(0x01)

return b58encode_check(seed).decode()
2 changes: 1 addition & 1 deletion crypto/networks/mainnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class Mainnet(object):
epoch = datetime(2017, 3, 21, 13, 00, 00)
wif = 186
wif = 'ba'
chain_id = 10000
2 changes: 1 addition & 1 deletion crypto/networks/testnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class Testnet(object):
epoch = datetime(2017, 3, 21, 13, 00, 00)
wif = 186
wif = 'ba'
chain_id = 10000
9 changes: 4 additions & 5 deletions tests/configuration/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from crypto.configuration.network import get_network, set_custom_network, set_network
from crypto.networks.testnet import Testnet
from crypto.networks.mainnet import Mainnet
from crypto.networks.testnet import Testnet


def test_get_network():
Expand All @@ -14,22 +13,22 @@ def test_set_network():
# mainnet
set_network(Mainnet)
result = get_network()
assert result['wif'] == 186
assert result['wif'] == 'ba'
assert result['chain_id'] == 10000

# testnet
set_network(Testnet)
result = get_network()
assert result['wif'] == 186
assert result['wif'] == 'ba'
assert result['chain_id'] == 10000

set_network(Testnet) # set back to Testnet so other tests don't fail

def test_set_custom_network():
epoch_time = datetime(2017, 1, 1, 13, 00, 00)
set_custom_network(epoch_time, 130, 10000)
set_custom_network(epoch_time, '82', 10000)
result = get_network()
assert result['wif'] == 130
assert result['wif'] == '82'
assert result['epoch'] == epoch_time
assert result['chain_id'] == 10000

Expand Down
5 changes: 5 additions & 0 deletions tests/identity/test_private_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ def test_sign_compact(sign_compact):
assert signature[1:33] == bytes.fromhex(sign_compact['data']['r'])
assert signature[33:] == bytes.fromhex(sign_compact['data']['s'])
assert signature.hex() == sign_compact['data']['serialized']

def test_it_should_parse_the_private_key_from_wif(identity):
private_key = PrivateKey.from_wif(identity['data']['wif'])
assert isinstance(private_key, PrivateKey)
assert private_key.to_hex() == identity['data']['private_key']