Test vectors
This commit is contained in:
parent
99190e910b
commit
561e03ab3c
|
@ -26,6 +26,7 @@ from .pbc_types import (
|
||||||
SignedTransaction,
|
SignedTransaction,
|
||||||
)
|
)
|
||||||
from .crypto import sign_transaction, SenderAuthentication
|
from .crypto import sign_transaction, SenderAuthentication
|
||||||
|
import time
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -42,6 +43,8 @@ URL_NONCE = 'https://{hostname}/{shard}blockchain/account/{address}'
|
||||||
|
|
||||||
MPC_DECIMALS = 10000
|
MPC_DECIMALS = 10000
|
||||||
|
|
||||||
|
TRANSACTION_VALIDITY_DURATION = 60
|
||||||
|
|
||||||
|
|
||||||
def shard_id_for_address(address: str | Address) -> str:
|
def shard_id_for_address(address: str | Address) -> str:
|
||||||
address = str(address)
|
address = str(address)
|
||||||
|
@ -81,9 +84,11 @@ class PbcClient:
|
||||||
msg = 'PbcClient.sender_authentication required for send_transaction'
|
msg = 'PbcClient.sender_authentication required for send_transaction'
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
|
|
||||||
|
valid_to_time: int = int(time.time() + TRANSACTION_VALIDITY_DURATION) * 1000
|
||||||
signed_transaction = sign_transaction(
|
signed_transaction = sign_transaction(
|
||||||
self.sender_authentication,
|
self.sender_authentication,
|
||||||
self.get_sender_authentication_nonce(),
|
self.get_sender_authentication_nonce(),
|
||||||
|
valid_to_time,
|
||||||
gas_cost,
|
gas_cost,
|
||||||
self.get_chain_id(),
|
self.get_chain_id(),
|
||||||
contract_address,
|
contract_address,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
|
|
||||||
import coincurve
|
import coincurve
|
||||||
import hashlib
|
import hashlib
|
||||||
import time
|
|
||||||
from .pbc_types import (
|
from .pbc_types import (
|
||||||
Address,
|
Address,
|
||||||
HashSha256,
|
HashSha256,
|
||||||
|
@ -12,8 +11,6 @@ from .pbc_types import (
|
||||||
)
|
)
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
|
||||||
TRANSACTION_VALIDITY_DURATION = 60
|
|
||||||
|
|
||||||
def find_recovery_id(der_sig: bytes, message_hash: HashSha256, expected_public_key: coincurve.PublicKey) -> int:
|
def find_recovery_id(der_sig: bytes, message_hash: HashSha256, expected_public_key: coincurve.PublicKey) -> int:
|
||||||
r, s = coincurve.der.parse_signature(der_sig)
|
r, s = coincurve.der.parse_signature(der_sig)
|
||||||
|
|
||||||
|
@ -63,6 +60,7 @@ class SenderAuthentication:
|
||||||
def sign_transaction(
|
def sign_transaction(
|
||||||
sender_authentication: SenderAuthentication,
|
sender_authentication: SenderAuthentication,
|
||||||
nonce: int,
|
nonce: int,
|
||||||
|
valid_to_time: int,
|
||||||
gas_cost: int,
|
gas_cost: int,
|
||||||
chain_id: str,
|
chain_id: str,
|
||||||
contract_address: Address | str,
|
contract_address: Address | str,
|
||||||
|
@ -70,7 +68,6 @@ def sign_transaction(
|
||||||
) -> SignedTransaction:
|
) -> SignedTransaction:
|
||||||
print(contract_address)
|
print(contract_address)
|
||||||
sender = sender_authentication.sender_address
|
sender = sender_authentication.sender_address
|
||||||
valid_to_time: int = int(time.time() + TRANSACTION_VALIDITY_DURATION) * 1000
|
|
||||||
|
|
||||||
inner: SignedTransactionInnerPart = SignedTransactionInnerPart(
|
inner: SignedTransactionInnerPart = SignedTransactionInnerPart(
|
||||||
nonce,
|
nonce,
|
||||||
|
|
|
@ -89,7 +89,7 @@ class SignedTransactionInnerPart:
|
||||||
)
|
)
|
||||||
|
|
||||||
def hash(self, chain_id: str) -> HashSha256:
|
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)
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
|
24
test/test_crypto.py
Normal file
24
test/test_crypto.py
Normal file
|
@ -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'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user