def generate_test_keys(): private_key = Ed25519PrivateKey.generate() public_key = private_key.public_key() public_key_bytes = public_key.public_bytes( encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw) public_address = remove_0x_prefix(to_hex(public_key_bytes)) return private_key, public_address
def create_account(convex_url, public_address): account_data = { 'accountKey': remove_0x_prefix(to_public_key_checksum(public_address)), } url = f'{convex_url}/api/v1/createAccount' print('create_account send to ', url, 'data:', account_data) response = requests.post(url, data=json.dumps(account_data)) assert (response.status_code == 200) result = response.json() return result['address']
def get_test_account(): private_key = Ed25519PrivateKey.from_private_bytes( to_bytes(PRIVATE_TEST_KEY)) public_key = private_key.public_key() public_key_bytes = public_key.public_bytes( encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw) public_address = remove_0x_prefix(to_hex(public_key_bytes)) assert (public_address == PUBLIC_ADDRESS) return private_key, public_address
def is_equal(self, public_key_pair): """ Compare the value to see if it is the same as this key_pair :param: str, KeyPair public_key_pair: This can be a string ( public key) or a KeyPair object :returns: True if the public_key_pair str or KeyPair have the same public key as this object. """ public_key = None if isinstance(public_key_pair, KeyPair): public_key = public_key_pair.public_key elif isinstance(public_key_pair, str): public_key = public_key_pair else: raise TypeError('invalid key_pair or public_key') return remove_0x_prefix( self.public_key_checksum).lower() == remove_0x_prefix( public_key).lower()
def _transaction_submit(self, address, public_key, hash_data, signed_data): """ """ submit_url = urljoin(self._url, '/api/v1/transaction/submit') data = { 'address': f'#{to_address(address)}', 'accountKey': public_key, 'hash': hash_data, 'sig': remove_0x_prefix(signed_data) } logger.debug(f'_transaction_submit {submit_url} {data}') result = self._transaction_post(submit_url, data) logger.debug(f'_transaction_submit response {result}') if 'errorCode' in result: raise ConvexAPIError('_transaction_submit', result['errorCode'], result['value']) return result
def public_key_api(self): """ Return the public key of the KeyPair without the leading '0x' :returns: public_key without the leading '0x' :rtype: str .. code-block:: python >>> # create a random KeyPair >>> key_pair = KeyPair() >>> # show the public key as a hex string with the leading '0x' removed >>> print(key_pair.public_key_api) 36d8c5c40dbe2d1b0131acf41c38b9d37ebe04d85... """ return remove_0x_prefix(self.public_key_checksum)
def test_submit_transaction(convex_url): private_key, public_address = generate_test_keys() address = create_account(convex_url, public_address) request_funds(convex_url, address) # faucet request amount # prepare prepare_data = {'address': f'#{address}', 'source': '(map inc [1 2 3])'} url = f'{convex_url}/api/v1/transaction/prepare' print('prepare send', prepare_data) response = requests.post(url, data=json.dumps(prepare_data)) if response.status_code != 200: print('prepare error', response.text) assert (response.status_code == 200) result = response.json() #submit print(result) hash_data = to_bytes(hexstr=result['hash']) signed_hash_bytes = private_key.sign(hash_data) signed_hash = to_hex(signed_hash_bytes) submit_data = { 'address': f'#{address}', 'accountKey': public_address, 'hash': result['hash'], 'sig': remove_0x_prefix(signed_hash) } url = f'{convex_url}/api/v1/transaction/submit' print('submit send', submit_data) response = requests.post(url, data=json.dumps(submit_data)) if response.status_code != 200: print('submit error', response.text, response.status_code) assert (response.status_code == 200) print(response.json())
def test_key_pair_address_api(test_key_pair_info): key_pair = KeyPair.import_from_bytes(test_key_pair_info['private_bytes']) assert (key_pair) assert (key_pair.public_key_api == remove_0x_prefix( test_key_pair_info['public_key']))