From 464e4e711db175477cca6f0f3320ad95de5c4274 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Tue, 19 Sep 2023 17:47:26 +0800 Subject: [PATCH] WIP --- examples/soroban_auth_with_stellar_account.py | 28 ++++++++++++++----- stellar_sdk/auth.py | 18 +++++++----- stellar_sdk/soroban_server.py | 6 ++-- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/examples/soroban_auth_with_stellar_account.py b/examples/soroban_auth_with_stellar_account.py index c50fa20a..a821cfb8 100644 --- a/examples/soroban_auth_with_stellar_account.py +++ b/examples/soroban_auth_with_stellar_account.py @@ -5,18 +5,25 @@ """ import time -from stellar_sdk import Keypair, Network, SorobanServer, TransactionBuilder, scval, InvokeHostFunction +from stellar_sdk import ( + InvokeHostFunction, + Keypair, + Network, + SorobanServer, + TransactionBuilder, + scval, +) from stellar_sdk import xdr as stellar_xdr from stellar_sdk.auth import authorize_entry from stellar_sdk.exceptions import PrepareTransactionException from stellar_sdk.soroban_rpc import GetTransactionStatus, SendTransactionStatus -rpc_server_url = "http://100.116.189.108:8000/soroban/rpc" +rpc_server_url = "http://100.83.15.43:8000/soroban/rpc" network_passphrase = Network.STANDALONE_NETWORK_PASSPHRASE soroban_server = SorobanServer(rpc_server_url) # https://github.com/stellar/soroban-examples/tree/v0.6.0/auth -contract_id = "CBMNHHCVF75POVMU7GZ3N5FQLZTMKRU52TLGXNL3DYP3L4ELCLTIMCWD" +contract_id = "CDCYWK73YTYFJZZSJ5V7EDFNHYBG4QN3VUNG2IGD27KJDDPNCZKBCBXK" tx_submitter_kp = Keypair.from_secret( "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV" ) @@ -29,12 +36,13 @@ source = soroban_server.load_account(tx_submitter_kp.public_key) tx = ( - TransactionBuilder(source, network_passphrase, base_fee=50000) + TransactionBuilder(source, network_passphrase, base_fee=60000) .add_time_bounds(0, 0) .append_invoke_contract_function_op( contract_id=contract_id, function_name=func_name, parameters=args, + # source=op_invoker_kp.public_key ) .build() ) @@ -43,15 +51,21 @@ simulate_resp = soroban_server.simulate_transaction(tx) op = tx.transaction.operations[0] assert isinstance(op, InvokeHostFunction) - op.auth = [authorize_entry( - simulate_resp.results[0].auth[0], op_invoker_kp, simulate_resp.latest_ledger + 50, network_passphrase - )] + op.auth = [ + authorize_entry( + simulate_resp.results[0].auth[0], + op_invoker_kp, + simulate_resp.latest_ledger + 3, + network_passphrase, + ) + ] tx = soroban_server.prepare_transaction(tx, simulate_resp) except PrepareTransactionException as e: print(f"Got exception: {e.simulate_transaction_response}") raise e tx.sign(tx_submitter_kp) +# tx.sign(op_invoker_kp) print(f"Signed XDR:\n{tx.to_xdr()}") send_transaction_data = soroban_server.send_transaction(tx) diff --git a/stellar_sdk/auth.py b/stellar_sdk/auth.py index f201cb95..670b94c4 100644 --- a/stellar_sdk/auth.py +++ b/stellar_sdk/auth.py @@ -82,12 +82,16 @@ def authorize_entry( # This structure is defined here: # https://soroban.stellar.org/docs/fundamentals-and-concepts/invoking-contracts-with-transactions#stellar-account-signatures - addr_auth.signature = scval.to_vec([scval.to_map( - { - scval.to_symbol("public_key"): scval.to_bytes(public_key), - scval.to_symbol("signature"): scval.to_bytes(signature), - } - )]) + addr_auth.signature = scval.to_vec( + [ + scval.to_map( + { + scval.to_symbol("public_key"): scval.to_bytes(public_key), + scval.to_symbol("signature"): scval.to_bytes(signature), + } + ) + ] + ) return entry @@ -113,7 +117,7 @@ def authorize_invocation( :param valid_until_ledger_sequence: the (exclusive) future ledger sequence number until which this authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) :param invocation: invocation the invocation tree that we're authorizing (likely, this comes from transaction simulation) - :public_key: the public identity of the signer (when providing a :class:`Keypair` to `signer`, + :param public_key: the public identity of the signer (when providing a :class:`Keypair` to `signer`, this can be omitted, as it just uses the public key of the keypair) :param network_passphrase: the network passphrase is incorporated into the signature (see :class:`stellar_sdk.Network` for options) :return: a signed Soroban authorization entry. diff --git a/stellar_sdk/soroban_server.py b/stellar_sdk/soroban_server.py index 08a683e9..c01379e9 100644 --- a/stellar_sdk/soroban_server.py +++ b/stellar_sdk/soroban_server.py @@ -271,7 +271,7 @@ def get_contract_data( def prepare_transaction( self, transaction_envelope: TransactionEnvelope, - simulate_transaction_response: SimulateTransactionResponse = None + simulate_transaction_response: SimulateTransactionResponse = None, ) -> TransactionEnvelope: """Submit a trial contract invocation, first run a simulation of the contract invocation as defined on the incoming transaction, and apply the results to @@ -306,7 +306,9 @@ def prepare_transaction( discovered from the simulation. """ if not simulate_transaction_response: - simulate_transaction_response = self.simulate_transaction(transaction_envelope) + simulate_transaction_response = self.simulate_transaction( + transaction_envelope + ) if simulate_transaction_response.error: raise PrepareTransactionException( "Simulation transaction failed, the response contains error information.",