Skip to content

Commit 9691cbb

Browse files
authored
refactor: define HRP for crypto (#47)
1 parent 8b087dd commit 9691cbb

13 files changed

+27
-37
lines changed

examples/example_jsonrpc_get_blockchain_info.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
async def main() -> None:
66
client_url = "https://testnet1.pactus.org/jsonrpc"
7-
client = PactusOpenRPCClient(
8-
headers={},
9-
client_url=client_url)
7+
client = PactusOpenRPCClient(headers={}, client_url=client_url, timeout=300)
108

119
res = await client.pactus.blockchain.get_blockchain_info()
1210

examples/example_jsonrpc_get_node_info.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
async def main() -> None:
66
client_url = "https://testnet1.pactus.org/jsonrpc"
7-
client = PactusOpenRPCClient(
8-
headers={},
9-
client_url=client_url)
7+
client = PactusOpenRPCClient(headers={}, client_url=client_url, timeout=300)
108

119
res = await client.pactus.network.get_node_info()
1210

examples/example_key_generation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22

3-
from pactus.crypto import CryptoConfig
3+
from pactus.crypto.hrp import HRP
44
from pactus.crypto.address import AddressType
55
from pactus.crypto.bls.private_key import PrivateKey as BLSPrivateKey
66
from pactus.crypto.ed25519.private_key import PrivateKey as Ed25519PrivateKey
@@ -28,7 +28,7 @@ def main() -> None:
2828
args = parser.parse_args()
2929

3030
if args.testnet:
31-
CryptoConfig.use_testnet()
31+
HRP.use_testnet()
3232

3333
match AddressType(args.address_type):
3434
case AddressType.VALIDATOR:

examples/example_transfer_transaction_bls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from pactus.crypto import CryptoConfig
1+
from pactus.crypto.hrp import HRP
22
from pactus.crypto.address import Address
33
from pactus.crypto.bls.private_key import PrivateKey
44
from pactus.transaction.transaction import Transaction
55
from pactus.amount import Amount
66

77

88
def main() -> None:
9-
CryptoConfig.use_testnet()
9+
HRP.use_testnet()
1010

1111
lock_time = 1_735_096
1212
memo = "This is a test transaction"

examples/example_transfer_transaction_ed25519.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from pactus.crypto import CryptoConfig
1+
from pactus.crypto.hrp import HRP
22
from pactus.crypto.address import Address
33
from pactus.crypto.ed25519.private_key import PrivateKey
44
from pactus.transaction.transaction import Transaction
55
from pactus.amount import Amount
66

77

88
def main() -> None:
9-
CryptoConfig.use_testnet()
9+
HRP.use_testnet()
1010

1111
lock_time = 1_735_096
1212
memo = "This is a test transaction"

pactus/crypto/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from .crypto import CryptoConfig
2-
3-
__all__ = ["CryptoConfig"]

pactus/crypto/address.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from enum import Enum
44

5-
from pactus.crypto import CryptoConfig
5+
from pactus.crypto.hrp import HRP
66
from pactus.utils import utils
77

88
# Address format: hrp + `1` + type + data + checksum
@@ -34,7 +34,7 @@ def from_string(cls, text: str) -> Address:
3434
return bytes([0])
3535

3636
hrp, typ, data = utils.decode_to_base256_with_type(text)
37-
if hrp != CryptoConfig.ADDRESS_HRP:
37+
if hrp != HRP.ADDRESS_HRP:
3838
msg = f"Invalid HRP: {hrp}"
3939
raise ValueError(msg)
4040

@@ -57,7 +57,7 @@ def string(self) -> str:
5757
return TREASURY_ADDRESS_STRING
5858

5959
return utils.encode_from_base256_with_type(
60-
CryptoConfig.ADDRESS_HRP,
60+
HRP.ADDRESS_HRP,
6161
self.data[0],
6262
self.data[1:],
6363
)

pactus/crypto/bls/private_key.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import secrets
44
from math import ceil, log2
55

6-
from pactus.crypto import CryptoConfig
6+
from pactus.crypto.hrp import HRP
77
from pactus.utils import utils
88

99
from .bls12_381.bls_sig_g1 import sign
@@ -17,12 +17,12 @@
1717

1818

1919
class PrivateKey:
20-
def __init__(self, scalar: any) -> None:
21-
self.scalar = scalar
20+
def __init__(self, scalar: int) -> None:
21+
self.scalar = scalar % curve_order
2222

2323
@classmethod
2424
def from_bytes(cls, buffer: bytes) -> PrivateKey:
25-
return cls(int.from_bytes(buffer, "big") % curve_order)
25+
return cls(int.from_bytes(buffer, "big"))
2626

2727
@classmethod
2828
def key_gen(cls, ikm: bytes = [], key_info: bytes = b"") -> PrivateKey:
@@ -47,7 +47,7 @@ def random(cls) -> PrivateKey:
4747
def from_string(cls, text: str) -> PrivateKey:
4848
hrp, typ, data = utils.decode_to_base256_with_type(text)
4949

50-
if hrp != CryptoConfig.PRIVATE_KEY_HRP:
50+
if hrp != HRP.PRIVATE_KEY_HRP:
5151
msg = f"Invalid hrp: {hrp}"
5252
raise ValueError(msg)
5353

@@ -67,7 +67,7 @@ def raw_bytes(self) -> bytes:
6767

6868
def string(self) -> str:
6969
return utils.encode_from_base256_with_type(
70-
CryptoConfig.PRIVATE_KEY_HRP,
70+
HRP.PRIVATE_KEY_HRP,
7171
SIGNATURE_TYPE_BLS,
7272
self.raw_bytes(),
7373
)

pactus/crypto/bls/public_key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from ripemd.ripemd160 import ripemd160
66

7-
from pactus.crypto import CryptoConfig
87
from pactus.crypto.address import Address, AddressType
8+
from pactus.crypto.hrp import HRP
99
from pactus.utils import utils
1010

1111
from .bls12_381.bls_sig_g1 import aggregate_pubs, verify
@@ -23,7 +23,7 @@ def __init__(self, point_g2: any) -> None:
2323
def from_string(cls, text: str) -> PublicKey:
2424
hrp, typ, data = utils.decode_to_base256_with_type(text)
2525

26-
if hrp != CryptoConfig.PUBLIC_KEY_HRP:
26+
if hrp != HRP.PUBLIC_KEY_HRP:
2727
msg = f"Invalid hrp: {hrp}"
2828
raise ValueError(msg)
2929

@@ -51,7 +51,7 @@ def raw_bytes(self) -> bytes:
5151

5252
def string(self) -> str:
5353
return utils.encode_from_base256_with_type(
54-
CryptoConfig.PUBLIC_KEY_HRP,
54+
HRP.PUBLIC_KEY_HRP,
5555
SIGNATURE_TYPE_BLS,
5656
self.raw_bytes(),
5757
)

pactus/crypto/ed25519/private_key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from cryptography.hazmat.primitives.asymmetric import ed25519
44

5-
from pactus.crypto import CryptoConfig
5+
from pactus.crypto.hrp import HRP
66
from pactus.utils import utils
77

88
from .public_key import PublicKey
@@ -29,7 +29,7 @@ def random(cls) -> PrivateKey:
2929
def from_string(cls, text: str) -> PrivateKey:
3030
hrp, typ, data = utils.decode_to_base256_with_type(text)
3131

32-
if hrp != CryptoConfig.PRIVATE_KEY_HRP:
32+
if hrp != HRP.PRIVATE_KEY_HRP:
3333
msg = f"Invalid hrp: {hrp}"
3434
raise ValueError(msg)
3535

@@ -49,7 +49,7 @@ def raw_bytes(self) -> bytes:
4949

5050
def string(self) -> str:
5151
return utils.encode_from_base256_with_type(
52-
CryptoConfig.PRIVATE_KEY_HRP,
52+
HRP.PRIVATE_KEY_HRP,
5353
SIGNATURE_TYPE_ED25519,
5454
self.raw_bytes(),
5555
)

0 commit comments

Comments
 (0)