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 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 sign(self, hash_text): """ Sign a hash text using the private key. :param str hash_text: Hex string of the hash to sign :returns: Hex string of the signed text .. code-block:: python >>> sig = key_pair.sign('7e2f1062f5fc51ed65a28b5945b49425aa42df6b7e67107efec357794096e05e') >>> print(sig) '5d41b964c63d1087ad66e58f4f9d3fe2b7bd0560b..' """ hash_data = to_bytes(hexstr=hash_text) signed_hash_bytes = self._private_key.sign(hash_data) return to_hex(signed_hash_bytes)
def public_key(self): """ Return the public key of the KeyPair in the format '0x....' :returns: public_key with leading '0x' :rtype: str .. code-block:: python >>> # create a random KeyPair >>> key_pair = KeyPair() >>> # show the public key as a hex string >>> print(key_pair.public_key) 0x36d8c5c40dbe2d1b0131acf41c38b9d37ebe04d85... """ return to_hex(self.public_key_bytes)
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())