def _validate_consensus_config(self) -> None: assert self.consensus is not None expected_fields = [ "validator_keys", "first_round_timeout", "status_timeout", "peers_timeout", "txs_block_limit", "max_message_len", "min_propose_timeout", "max_propose_timeout", "propose_timeout_threshold", ] for field in expected_fields: if field not in self.consensus: raise RuntimeError( f"Invalid consensus config '{self.consensus}', at least the field '{field}' is missing." ) for key_pair in self.consensus["validator_keys"]: if len(key_pair) != 2: raise RuntimeError( "Validator keys should be a list of pairs (consensus_key, service_key) in hexadecimal form" ) # Check that keys can be parsed correctly. _ = PublicKey(bytes.fromhex(key_pair[0])) _ = PublicKey(bytes.fromhex(key_pair[1]))
def test_to_caller_address(self) -> None: """Tests converting PublicKey to caller address.""" public_key = PublicKey(bytes([i for i in range(PUBLIC_KEY_BYTES_LEN)])) hash_address = MessageGenerator.pk_to_hash_address(public_key) self.assertEqual( hash_address.hex(), "fc608d4bd40aee124e73a8036d38db51788b79a18bb51d80ea15ca5fddaace69")
def get_balance(client: ExonumClient, key: PublicKey) -> int: """The example returns the balance of the wallet.""" # Call the /wallets/info endpoint to retrieve the balance: service_public_api = client.service_public_api( CRYPTOCURRENCY_INSTANCE_NAME) wallet_info = service_public_api.get_service( "v1/wallets/info?pub_key={}".format(key.hex())) ensure_status_code(wallet_info) balance = wallet_info.json( )["wallet_proof"]["to_wallet"]["entries"][0]["value"]["balance"] return balance
def test_keys(self) -> None: """Tests the PublicKey and the SecretKey classes.""" data = bytes([i for i in range(_PUBLIC_KEY_BYTES_LEN)]) public_key = PublicKey(data) self.assertTrue(isinstance(public_key, _FixedByteArray)) self.assertEqual(public_key.value, data) data = bytes([i for i in range(_SECRET_KEY_BYTES_LEN)]) secret_key = SecretKey(data) self.assertTrue(isinstance(secret_key, _FixedByteArray)) self.assertEqual(secret_key.value, data)
def get_balance(key: PublicKey) -> int: """The example returns the balance of the wallet.""" python_api_map_endpoint = "http://127.0.0.1:{}/".format( PYTHON_RUNTIME_PORT) api_map = requests.get(python_api_map_endpoint) ensure_status_code(api_map) service_public_port = api_map.json( )["service_api"][CRYPTOCURRENCY_INSTANCE_NAME]["public_port"] # Call the /wallets endpoint to retrieve the balance: endpoint = "http://127.0.0.1:{}/wallets/{}".format(service_public_port, key.hex()) wallet_info = requests.get(endpoint) ensure_status_code(wallet_info) balance = wallet_info.json()["balance"] return balance
def test_signature(self) -> None: """Tests the Signature class.""" keypair = KeyPair.generate() data = bytes([i for i in range(10)]) signature = Signature.sign(data, keypair.secret_key) self.assertTrue(isinstance(signature, Signature)) self.assertTrue(isinstance(signature, _FixedByteArray)) self.assertEqual(signature.value, crypto_sign_detached(data, keypair.secret_key.value)) self.assertTrue(signature.verify(data, keypair.public_key)) wrong_data = bytes([1, 2]) wrong_pk = PublicKey(bytes([i for i in range(_PUBLIC_KEY_BYTES_LEN)])) self.assertFalse(signature.verify(wrong_data, keypair.public_key)) self.assertFalse(signature.verify(data, wrong_pk))
def test_keypair(self) -> None: """Tests the KeyPair class.""" public_key = PublicKey(bytes([i for i in range(PUBLIC_KEY_BYTES_LEN)])) secret_key = SecretKey(bytes([i for i in range(SECRET_KEY_BYTES_LEN)])) # Check that creation with unmatched keys raises an error: with self.assertRaises(ValueError): KeyPair(public_key, secret_key) # Check that generation of the keypair works: keypair = KeyPair.generate() self.assertTrue(isinstance(keypair.public_key, PublicKey)) self.assertTrue(isinstance(keypair.secret_key, SecretKey)) self.assertNotEqual(keypair.public_key, keypair.secret_key) self.assertEqual(keypair.secret_key.value[PUBLIC_KEY_BYTES_LEN:], keypair.public_key.value) # Check that creating a keypair from the matched keys works: _new_keypair = KeyPair(keypair.public_key, keypair.secret_key)