DEV Community

darkvallen
darkvallen

Posted on

Deploying and Invoking An Auth Contract with Stellar Account Authorization using Soroban Python SDK

In my previous post, I showed you how to deploy a wrapped token contract using Soroban Python SDK. In this post, we'll use Soroban Python SDK to deploy and invoke an auth contract with stellar account authorization.

Preparation

First, create a Futurenet account, this account will be used to deploy the auth contract and invoke the contract using Soroban Python SDK. Create account at Stellar Laboratory and make sure to fund them. The account that i will be using :

Public Key  GATQFKA7BV7LYYE6UJ2LRL56W3BZRRSWTINA6TG2EM5CCDORFIERAKWV
Secret Key  SCHKLGAGY7QGWUKUFDGHEQMSCPFZNMLEHQE6Y6WPGYGHQIM23T3NJE3C
Enter fullscreen mode Exit fullscreen mode

The files that we're gonna use :

soroban_auth_contract.wasm - The Auth Contract
soroban_deploy_contract.py - Python Script to Deploy the contract.
soroban_auth_with_stellar_account - Python script to invoke the auth contract with Stellar Account authorization.

Deploying The Contract

To deploy a contract, open the soroban_deploy_contract.py file and adjust the following parameters:

secret = "SCHKLGAGY7QGWUKUFDGHEQMSCPFZNMLEHQE6Y6WPGYGHQIM23T3NJE3C" #Put your Account Secret Key here
rpc_server_url = "https://horizon-futurenet.stellar.cash:443/soroban/rpc"
network_passphrase = Network.FUTURENET_NETWORK_PASSPHRASE
contract_file_path = "./soroban_auth_contract.wasm" 
Enter fullscreen mode Exit fullscreen mode

secret: the Secret Key of the Account that we're gonna use to deploy the auth contract. Replace the value with your own account secret key.
rpc_server_url: the URL of the RPC server you'll use to interact with the Soroban Futurenet network.
network_passphrase: the passphrase you'll use to select the network. This can be changed depending on the network you're interacting with; in this case, we'll use Futurenet.
contract_file_path: the path to the auth contract file.

Save the soroban_deploy_contract.py file with your changes and then run the script in a command prompt using this command:

python `soroban_deploy_contract.py`
Enter fullscreen mode Exit fullscreen mode

Once the script has finished running, you'll receive a contract ID. Be sure to save this for later.

contract id: af0aadafacc6bcb73b6ec9aeee03350ca0c6a3430e099d4b5dd371c6e07cc214
Enter fullscreen mode Exit fullscreen mode

Invoking the Contract

To invoke the auth contract, open the soroban_auth_with_stellar_account.py file and adjust the following parameters:

contract_id = "af0aadafacc6bcb73b6ec9aeee03350ca0c6a3430e099d4b5dd371c6e07cc214"
tx_submitter_kp = Keypair.from_secret(
    "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV"
)
# If tx_submitter_kp and op_invoker_kp use the same account, the submission will fail, a bug?
op_invoker_kp = Keypair.from_secret(
    "SCHKLGAGY7QGWUKUFDGHEQMSCPFZNMLEHQE6Y6WPGYGHQIM23T3NJE3C"
)
Enter fullscreen mode Exit fullscreen mode

tx_submitter_kp: the secret key of the account you'll use to submit the transaction.
op_invoker_kp: the secret key of the account you'll use to invoke the contract function. Replace the value with your own account secret key.
contract_id: the contract ID of the auth contract you want to invoke. Replace the value with the contract ID you received from deploying contract before.

Save the soroban_auth_with_account file with your changes. Note : somehow using the same keypair for tx_submitter_kp and op_invoker_kp makes the transaction failed to be sumbited, maybe this is a bug. If it is a bug, this is expected since Soroban is still in development.

Before running the script, i want to explain this specific codes :

nonce = soroban_server.get_nonce(contract_id, op_invoker_kp.public_key)
func_name = "increment"
args = [Address(op_invoker_kp.public_key), Uint32(10)]

invocation = AuthorizedInvocation(
    contract_id=contract_id,
    function_name=func_name,
    args=args,
    sub_invocations=[],
)

contract_auth = ContractAuth(
    address=Address(op_invoker_kp.public_key),
    nonce=nonce,
    root_invocation=invocation,
)

contract_auth.sign(op_invoker_kp, network_passphrase)
Enter fullscreen mode Exit fullscreen mode

From the script above the function of the contract that we're going to invoke is increment function with Stellar Account keypair as the argument, and uses it to get a nonce and set the address and sign the ContractAuth. This is for account authorization, where a specific account address authorizes the invocation.

Now we will run the script using this command :

python soroban_auth_with_account.py
Enter fullscreen mode Exit fullscreen mode

We will get result:

transaction result: <SCVal [type=1, u32=<Uint32 [uint32=10]>]>
Enter fullscreen mode Exit fullscreen mode

run the script once more, we will get result :

transaction result: <SCVal [type=1, u32=<Uint32 [uint32=20]>]>
Enter fullscreen mode Exit fullscreen mode

The increment function that we called worked as intended.

Closing

In this blog post, we have gone over the steps to Deploy and Invoke Auth Contract with Account Authorization using Soroban Python SDK. From the script you will get an insight how basic Soroban Account Authorization implemented in Soroban Python SDK.

Top comments (0)