diff --git a/crypto/configuration/network.py b/crypto/configuration/network.py index 285fd5d0..c98debc7 100644 --- a/crypto/configuration/network.py +++ b/crypto/configuration/network.py @@ -5,7 +5,7 @@ class NetworkType(TypedDict): epoch: datetime - wif: int + wif: str chain_id: int network: NetworkType = { @@ -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 diff --git a/crypto/identity/private_key.py b/crypto/identity/private_key.py index 647c269b..893aca66 100644 --- a/crypto/identity/private_key.py +++ b/crypto/identity/private_key.py @@ -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: @@ -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) diff --git a/crypto/identity/wif.py b/crypto/identity/wif.py index c541ceea..a8860dba 100644 --- a/crypto/identity/wif.py +++ b/crypto/identity/wif.py @@ -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 @@ -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() diff --git a/crypto/networks/mainnet.py b/crypto/networks/mainnet.py index d9a22977..921d84a7 100644 --- a/crypto/networks/mainnet.py +++ b/crypto/networks/mainnet.py @@ -2,5 +2,5 @@ class Mainnet(object): epoch = datetime(2017, 3, 21, 13, 00, 00) - wif = 186 + wif = 'ba' chain_id = 10000 diff --git a/crypto/networks/testnet.py b/crypto/networks/testnet.py index d879ab8c..9796dbcf 100644 --- a/crypto/networks/testnet.py +++ b/crypto/networks/testnet.py @@ -2,5 +2,5 @@ class Testnet(object): epoch = datetime(2017, 3, 21, 13, 00, 00) - wif = 186 + wif = 'ba' chain_id = 10000 diff --git a/tests/configuration/test_network.py b/tests/configuration/test_network.py index 260b1bef..d16c7847 100644 --- a/tests/configuration/test_network.py +++ b/tests/configuration/test_network.py @@ -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(): @@ -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 diff --git a/tests/identity/test_private_key.py b/tests/identity/test_private_key.py index 26f181a7..e7203e08 100644 --- a/tests/identity/test_private_key.py +++ b/tests/identity/test_private_key.py @@ -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']