Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Sep 19, 2023
1 parent 1523764 commit 464e4e7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
28 changes: 21 additions & 7 deletions examples/soroban_auth_with_stellar_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
)
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions stellar_sdk/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions stellar_sdk/soroban_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.",
Expand Down

0 comments on commit 464e4e7

Please sign in to comment.