diff --git a/pbc_client/__init__.py b/pbc_client/__init__.py index 6f7f4fa..4a9da2e 100644 --- a/pbc_client/__init__.py +++ b/pbc_client/__init__.py @@ -26,6 +26,7 @@ from .pbc_types import ( SignedTransaction, ) from .crypto import sign_transaction, SenderAuthentication +import time logger = logging.getLogger(__name__) @@ -42,6 +43,8 @@ URL_NONCE = 'https://{hostname}/{shard}blockchain/account/{address}' MPC_DECIMALS = 10000 +TRANSACTION_VALIDITY_DURATION = 60 + def shard_id_for_address(address: str | Address) -> str: address = str(address) @@ -81,9 +84,11 @@ class PbcClient: msg = 'PbcClient.sender_authentication required for send_transaction' raise Exception(msg) + valid_to_time: int = int(time.time() + TRANSACTION_VALIDITY_DURATION) * 1000 signed_transaction = sign_transaction( self.sender_authentication, self.get_sender_authentication_nonce(), + valid_to_time, gas_cost, self.get_chain_id(), contract_address, diff --git a/pbc_client/crypto.py b/pbc_client/crypto.py index be141ba..bb3506d 100644 --- a/pbc_client/crypto.py +++ b/pbc_client/crypto.py @@ -1,7 +1,6 @@ import coincurve import hashlib -import time from .pbc_types import ( Address, HashSha256, @@ -12,8 +11,6 @@ from .pbc_types import ( ) import dataclasses -TRANSACTION_VALIDITY_DURATION = 60 - def find_recovery_id(der_sig: bytes, message_hash: HashSha256, expected_public_key: coincurve.PublicKey) -> int: r, s = coincurve.der.parse_signature(der_sig) @@ -63,6 +60,7 @@ class SenderAuthentication: def sign_transaction( sender_authentication: SenderAuthentication, nonce: int, + valid_to_time: int, gas_cost: int, chain_id: str, contract_address: Address | str, @@ -70,7 +68,6 @@ def sign_transaction( ) -> SignedTransaction: print(contract_address) sender = sender_authentication.sender_address - valid_to_time: int = int(time.time() + TRANSACTION_VALIDITY_DURATION) * 1000 inner: SignedTransactionInnerPart = SignedTransactionInnerPart( nonce, diff --git a/pbc_client/pbc_types.py b/pbc_client/pbc_types.py index d0da924..84afa81 100644 --- a/pbc_client/pbc_types.py +++ b/pbc_client/pbc_types.py @@ -89,7 +89,7 @@ class SignedTransactionInnerPart: ) def hash(self, chain_id: str) -> HashSha256: - return HashSha256.of_bytes(self.rpc_serialize() + chain_id.encode('utf8')) + return HashSha256.of_bytes(self.rpc_serialize() + size_prefixed(chain_id.encode('utf8'))) @dataclasses.dataclass(frozen=True) diff --git a/test/test_crypto.py b/test/test_crypto.py new file mode 100644 index 0000000..4533486 --- /dev/null +++ b/test/test_crypto.py @@ -0,0 +1,24 @@ + +from pbc_client.crypto import sign_transaction, SenderAuthentication + +def test_sign(): + sender_authentication = SenderAuthentication("01") + chain_id = "SpecificChainIDLocatable" + contract_address = '000000000000000000000000000000000000000001' + signed = sign_transaction( + sender_authentication = sender_authentication, + nonce = 2, + valid_to_time = 3, + gas_cost = 2, + chain_id = chain_id, + contract_address = contract_address, + transaction_rpc = bytes([0 for i in range(99)]), + ) + print(signed) + assert str(signed.inner.rpc_serialize().hex()) == '00000000000000020000000000000003000000000000000200000000000000000000000000000000000000000100000063000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + assert str(signed.hash(chain_id)) == '1be2895a85862e8dd3cc75b290cc28a22e960ae02dd5c07a73b93716f9adbee8' + + + assert str(signed.signature) == '01f0f7d8f44919466eedbebc76bcb369bbb4c6f4f076e25c0ffe8ec9285890e53b4e39098540d088878a019c345ad73963543ce813fb9ccf4b84b0c25770452bd1' + +