> ## Documentation Index
> Fetch the complete documentation index at: https://developers.stellar.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Stellar Network

> In the previous section we learned how to create a wallet and a Stellar object that provides a connection to Horizon.

> [!NOTE]
>
> This guide is available on four different programming languages: Typescript, Kotlin, Flutter (Dart) and Swift. You can change the shown version on each page via the buttons above.

> [!CAUTION]
>
> **Important**
>
> Documentation for this language is currently work in progress. Some of information may not be applicable for this language, or missing. Code samples may be incomplete.

In the previous section we learned how to create a wallet and a `Stellar` object that provides a connection to Horizon. In this section, we will look at the usages of this class.

## Accounts

The most basic entity on the Stellar network is an account. Let's look into AccountService that provides the capability to work with accounts:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val account = wallet.stellar().account()
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let account = wal.stellar().account();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var account = wallet.stellar().account();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let account = wallet.stellar.account
> ```

Now we can create a keypair:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val accountKeyPair = account.createKeyPair()
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let accountKeyPair = account.createKeypair();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var accountKeyPair = account.createKeyPair();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let accountKeyPair = account.createKeyPair()
> ```

> [!NOTE]
>
> If using react-native, `createKeypair` won't work. Instead use the helper method `createKeypairFromRandom` like this:
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import * as Random from "expo-crypto";
> const rand = Random.randomBytes(32);
> const kp = account.createKeypairFromRandom(Buffer.from(rand));
> ```

## Build Transaction

The transaction builder allows you to create various transactions that can be signed and submitted to the Stellar network. Some transactions can be sponsored.

### Building Basic Transactions

First, let's look into building basic transactions.

#### Create Account

The create account transaction activates/creates an account with a starting balance of XLM (1 XLM by default).

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun createAccount(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).createAccount(destinationAccountKeyPair).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sourceAccountKeyPair,
> });
> const tx = txBuilder.createAccount(destinationAccountKeyPair).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.createAccount(destinationAccountKeyPair).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.createAccount(newAccount: destinationAccountKeyPair).build()
> ```

#### Modify Account

You can lock the master key of the account by setting its weight to 0. Use caution when locking the account's master key. Make sure you have set the correct signers and weights. Otherwise, you will lock the account irreversibly.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun lockMasterKey(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).lockAccountMasterKey().build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sourceAccountKeyPair,
> });
>
> const tx = txBuilder.lockAccountMasterKey().build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.lockAccountMasterKey().build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.lockAccountMasterKey().build()
> ```

Add a new signer to the account. Use caution when adding new signers and make sure you set the correct signer weight. Otherwise, you will lock the account irreversibly.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val newSignerKeyPair = account.createKeyPair()
>
> suspend fun addSigner(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).addAccountSigner(newSignerKeyPair, 10).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const newSignerKeyPair = account.createKeypair();
>
> const tx = txBuilder.addAccountSigner(newSignerKeyPair, 10).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var newSignerKeyPair = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.addAccountSigner(newSignerKeyPair, 10).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let newSignerKeyPair = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.addAccountSigner(signerAddress: newSignerKeyPair, signerWeight: 10).build()
> ```

Remove a signer from the account.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun removeSigner(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).removeAccountSigner(newSignerKeyPair).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const tx = txBuilder.removeAccountSigner(newSignerKeyPair).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.removeAccountSigner(newSignerKeyPair).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.removeAccountSigner(signerAddress: newSignerKeyPair).build()
> ```

Modify account thresholds (useful when multiple signers are assigned to the account). This allows you to restrict access to certain operations when the limit is not reached.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun setThreshold(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).setThreshold(low = 1, medium = 10, high = 30).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const tx = txBuilder.setThreshold({ low: 1, medium: 10, high: 30 }).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.setThreshold(low: 1, medium: 10, high: 20).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.setThreshold(low: 1, medium: 10, high: 20).build()
> ```

#### Modify Assets (Trustlines)

Add an asset (trustline) to the account. This allows the account to receive transfers of the asset.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val asset = IssuedAssetId("USDC", "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5")
>
> suspend fun addAsset(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).addAssetSupport(asset).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const asset = new IssuedAssetId(
>   "USDC",
>   "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
> );
>
> const tx = txBuilder.addAssetSupport(asset).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var asset = IssuedAssetId(
>         code: "USDC",
>         issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5");
>
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.addAssetSupport(asset).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let asset = try IssuedAssetId(
>     code: "USDC",
>     issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5")
>
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.addAssetSupport(asset: asset).build()
> ```

Remove an asset from the account (the asset's balance must be 0).

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun removeAsset(): Transaction {
>   return stellar.transaction(sourceAccountKeyPair).removeAssetSupport(asset).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const tx = txBuilder.removeAssetSupport(asset).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
> var tx = txBuilder.removeAssetSupport(asset).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let tx = try txBuilder.removeAssetSupport(asset: asset).build()
> ```

#### Swap

Exchange an account's asset for a different asset. The account must have a trustline for the destination asset.

A swap is filled at whatever rate the network can find when the transaction is applied, which may differ from the rate you quoted to the user. The minimum destination amount (`destMin`) is the slippage limit that protects against that: it is the least amount of the destination asset the account is willing to receive for the amount it sends. If the swap cannot deliver at least that much, the transaction fails instead of executing at a worse rate. Derive it from a current quote minus the slippage tolerance you are willing to accept.

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sourceKp,
> });
> const usdcAsset = new IssuedAssetId(
>   "USDC",
>   "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
> );
> // Send 0.1 XLM and accept no less than 0.025 USDC back
> const txn = txBuilder
>   .swap(new NativeAssetId(), usdcAsset, ".1", ".025")
>   .build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceKp);
>
> final usdcAsset = IssuedAssetId(
>     code: 'USDC',
>     issuer: 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',
> );
>
> // Send 0.1 XLM and accept no less than 0.025 USDC back
> var txn = txBuilder.swap(
>     fromAsset: NativeAssetId(),
>     toAsset: usdcAsset,
>     amount: "0.1",
>     destMin: "0.025",
>   ).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceKp)
>
> let usdcAsset = try IssuedAssetId(
>     code: "USDC",
>     issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5")
>
> // Send 0.1 XLM and accept no less than 0.025 USDC back
> let txn = try txBuilder.swap(fromAsset: NativeAssetId(),
>                              toAsset: usdcAsset,
>                              amount: 0.1,
>                              destMin: 0.025).build()
> ```

#### Path Pay

Send one asset from the source account and receive a different asset in the destination account.

Like a swap, a path payment is filled at the rate available when the transaction is applied, so it takes a slippage limit. Which limit depends on which side of the payment is fixed:

* When you fix the amount to send (`sendAmount`), pass `destMin`: the least amount of the destination asset the recipient must receive.
* When you fix the amount to deliver (`destAmount`), pass `sendMax`: the maximum amount of the send asset the source account is willing to spend.

If the network cannot fill the payment within that limit, the transaction fails instead of executing at a worse rate. Derive the limit from a current quote plus or minus the slippage tolerance you are willing to accept.

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sourceKp,
> });
> const usdcAsset = new IssuedAssetId(
>   "USDC",
>   "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
> );
> // Send 5 XLM; the recipient must receive at least 1.25 USDC
> const txn = txBuilder
>   .pathPay({
>     destinationAddress: receivingKp.publicKey,
>     sendAsset: new NativeAssetId(),
>     destAsset: usdcAsset,
>     sendAmount: "5",
>     destMin: "1.25",
>   })
>   .build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sourceKp);
>
> final usdcAsset = IssuedAssetId(
>     code: 'USDC',
>     issuer: 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',
> );
>
> // Send 5 XLM; the recipient must receive at least 1.25 USDC
> var txn = txBuilder.pathPay(
>     destinationAddress: receivingKp.address,
>     sendAsset: NativeAssetId(),
>     destinationAsset: usdcAsset,
>     sendAmount: "5",
>     destMin: "1.25",
>   ).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sourceKp)
>
> let usdcAsset = try IssuedAssetId(
>     code: "USDC",
>     issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5")
>
> // Send 5 XLM; the recipient must receive at least 1.25 USDC
> let txn = try txBuilder.pathPay(destinationAddress: receivingKp.address,
>                                 sendAsset: NativeAssetId(),
>                                 destinationAsset: usdcAsset,
>                                 sendAmount: 5,
>                                 destMin: 1.25).build()
> ```

To deliver an exact amount of the destination asset instead, pass `destAmount` together with `sendMax`:

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Deliver exactly 5 USDC; spend no more than 21 XLM to do it
> const txn = txBuilder
>   .pathPay({
>     destinationAddress: receivingKp.publicKey,
>     sendAsset: new NativeAssetId(),
>     destAsset: usdcAsset,
>     destAmount: "5",
>     sendMax: "21",
>   })
>   .build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Deliver exactly 5 USDC; spend no more than 21 XLM to do it
> var txn = txBuilder.pathPay(
>     destinationAddress: receivingKp.address,
>     sendAsset: NativeAssetId(),
>     destinationAsset: usdcAsset,
>     destAmount: "5",
>     sendMax: "21",
>   ).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Deliver exactly 5 USDC; spend no more than 21 XLM to do it
> let txn = try txBuilder.pathPay(destinationAddress: receivingKp.address,
>                                 sendAsset: NativeAssetId(),
>                                 destinationAsset: usdcAsset,
>                                 destAmount: 5,
>                                 sendMax: 21).build()
> ```

#### Set Memo

Set a memo on the transaction. The memo object can be imported from ["@stellar/stellar-sdk"](https://www.npmjs.com/package/@stellar/stellar-sdk).

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import { Memo } from "@stellar/stellar-sdk";
>
> const tx = txBuilder.setMemo(new Memo("text", "Memo string")).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart' as flutter_sdk;
>
> var memo = flutter_sdk.MemoText("Memo string");
> var tx = txBuilder.setMemo(memo).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import stellarsdk
>
> let memo = Memo.text("Memo string")
> let tx = try txBuilder.setMemo(memo: memo).build()
> ```

#### Account Merge

Merges account into a destination account.

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: accountKp,
>   baseFee: 1000,
> });
> const mergeTxn = txBuilder
>   .accountMerge(accountKp.publicKey, sourceKp.publicKey)
>   .build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(accountKp, baseFee: 1000);
>
> var mergeTxn = txBuilder.accountMerge(
>     destinationAddress: accountKp.address,
>     sourceAddress: sourceKp.address,
>   ).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: accountKp,
>                                               baseFee: 1000)
>
> let mergeTxn = try txBuilder.accountMerge(destinationAddress: accountKp.address,
>                                           sourceAddress: sourceKp.address).build()
> ```

#### Fund Testnet Account

Fund an account on the Stellar test network

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> wallet.stellar().fundTestnetAccount(accountKp.publicKey);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> await wallet.stellar().fundTestNetAccount(accountKp.address);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> try await wallet.stellar.fundTestNetAccount(address: accountKp.address)
> ```

### Building Advanced Transactions

In some cases a private key may not be known prior to forming a transaction. For example, a new account must be funded to exist and the wallet may not have the key for the account so may request the create account transaction to be sponsored by a third party.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Third-party key that will sponsor creating new account
> val externalKeyPair = "MySponsorAddress".toPublicKeyPair()
> val newKeyPair = account.createKeyPair()
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Third-party key that will sponsor creating new account
> const externalKeyPair = new PublicKeypair.fromPublicKey("GC5GD...");
> const newKeyPair = account.createKeypair();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Third-party key that will sponsor creating new account
> var externalKeyPair = PublicKeyPair.fromAccountId("GC5GD...");
> var newKeyPair = account.createKeyPair();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // Third-party key that will sponsor creating new account
> let externalKeyPair = try PublicKeyPair(accountId: "GC5GD...")
> let newKeyPair = account.createKeyPair()
> ```

First, the account must be created.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun makeCreateTx(): Transaction {
>   return stellar.transaction(externalKeyPair).createAccount(newKeyPair).build()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const createTxn = txBuilder.createAccount(newKeyPair).build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(externalKeyPair);
> var createTxn = txBuilder.createAccount(newKeyPair).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: externalKeyPair)
> let createTxn = try txBuilder.createAccount(newAccount: newKeyPair).build()
> ```

This transaction must be sent to external signer (holder of `externalKeyPair`) to be signed.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun remoteSignTransaction(transaction: Transaction) {
>   val xdrString = transaction.toEnvelopeXdrBase64()
>   // Send xdr encoded transaction to your backend server to sign
>   val xdrStringFromBackend = sendTransactionToBackend(xdrString)
>
>   // Decode xdr to get the signed transaction
>   val signedTransaction = stellar.decodeTransaction(xdrStringFromBackend)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const xdrString = createTxn.toXDR();
> // Send xdr encoded transaction to your backend server to sign
> const xdrStringFromBackend = await sendTransactionToBackend(xdrString);
>
> // Decode xdr to get the signed transaction
> const signedTransaction = stellar.decodeTransaction(xdrStringFromBackend);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var xdrString = createTxn.toEnvelopeXdrBase64();
>
> // Send xdr encoded transaction to your backend server to sign
> var xdrStringFromBackend = await sendTransactionToBackend(xdrString);
>
> // Decode xdr to get the signed transaction
> var signedTransaction = stellar.decodeTransaction(xdrStringFromBackend);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let xdrString = createTxn.toEnvelopeXdrBase64()
>
> // Send xdr encoded transaction to your backend server to sign
> let xdrStringFromBackend = await sendTransactionToBackend(xdrString)
>
> // Decode xdr to get the signed transaction
> let signedTransaction = stellar.decodeTransaction(xdr: xdrStringFromBackend)
> ```

> [!NOTE]
>
> You can read more about passing XDR transaction to the server in the [chapter below](#using-xdr-to-send-transaction-data).

Signed transaction can be submitted by the wallet.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun submitCreateTx(signedCreateTx: Transaction) {
>   wallet.stellar().submitTransaction(signedCreateTx)
> }
>
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> await wallet.stellar().submitTransaction(signedTransaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bool success = await stellar.submitTransaction(signedTransaction);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> try await stellar.submitTransaction(signedTransaction: signedTransaction)
> ```

Now, after the account is created, it can perform operations. For example, we can disable the master keypair and replace it with a new one (let's call it the device keypair) atomically in one transaction:

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun addDeviceKeyPair() {
>   val deviceKeyPair = account.createKeyPair()
>
>   val modifyAccountTransaction =
>     stellar
>       .transaction(newKeyPair)
>       .addAccountSigner(
>         deviceKeyPair,
>         signerWeight = 1,
>       )
>       .lockAccountMasterKey()
>       .build()
>       .sign(newKeyPair)
>
>   wallet.stellar().submitTransaction(modifyAccountTransaction)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const deviceKeyPair = account.createKeypair();
>
> const txBuilder = await stellar.transaction({ sourceAddress: newKeyPair });
> const modifyAccountTransaction = txBuilder
>   .addAccountSigner(deviceKeyPair, 1)
>   .lockAccountMasterKey()
>   .build();
> newKeyPair.sign(modifyAccountTransaction);
>
> await wallet.stellar().submitTransaction(modifyAccountTransaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var deviceKeyPair = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(newKeyPair);
> var modifyAccountTransaction = txBuilder
>     .addAccountSigner(deviceKeyPair, 1)
>     .lockAccountMasterKey()
>     .build();
>
> stellar.sign(modifyAccountTransaction, newKeyPair);
>
> bool success = await stellar.submitTransaction(modifyAccountTransaction);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let deviceKeyPair = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: newKeyPair)
>
> let modifyAccountTransaction = try txBuilder
>     .addAccountSigner(signerAddress: deviceKeyPair, signerWeight: 1)
>     .lockAccountMasterKey()
>     .build()
>
> stellar.sign(tx: modifyAccountTransaction,
>              keyPair: newKeyPair)
>
> try await stellar.submitTransaction(signedTransaction: modifyAccountTransaction)
> ```

#### Adding an Operation

Add a custom Operation to a transaction. This can be any [Operation](https://developers.stellar.org/docs/learn/fundamentals/transactions/list-of-operations.md) supported by the Stellar network. The Operation object can be imported from ["@stellar/stellar-sdk"](https://www.npmjs.com/package/@stellar/stellar-sdk).

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import { Operation } from "@stellar/stellar-sdk";
>
> const txBuilder = await stellar.transaction({
>   sourceAddress: sourceAccountKeyPair,
> });
> const tx = txBuilder.addOperation(
>   Operation.manageData({
>     name: "web_auth_domain",
>     value: new URL(authServer).hostname,
>     source: sourceAccountKeyPair,
>   }),
> );
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart' as flutter_sdk;
>
> var txBuilder = await stellar.transaction(sourceAccountKeyPair);
>
> var key = "web_auth_domain";
> var value = "https://testanchor.stellar.org";
> var valueBytes = Uint8List.fromList(value.codeUnits);
>
> var manageDataOperation = flutter_sdk.ManageDataOperationBuilder(
>     key,
>     valueBytes,
>   ).build();
>
> var tx = txBuilder.addOperation(
>     manageDataOperation,
>   ).build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import stellarsdk
>
> let txBuilder = try await stellar.transaction(sourceAddress: sourceAccountKeyPair)
> let key = "web_auth_domain"
> let value = "testanchor.stellar.org"
> let manageDataOperation = ManageDataOperation(sourceAccountId: sourceAccountKeyPair.address,
>                                               name: key,
>                                               data: value.data(using: .utf8))
>
> let tx = try txBuilder.addOperation(operation: manageDataOperation).build()
> ```

### Sponsoring Transactions

#### Sponsor Operations

Some operations, that modify account reserves can be [sponsored](https://developers.stellar.org/docs/build/guides/transactions/sponsored-reserves.md). For sponsored operations, the sponsoring account will be paying for the reserves instead of the account that being sponsored. This allows you to do some operations, even if account doesn't have enough funds to perform such operations. To sponsor a transaction, simply start a `sponsoring` block (Kotlin), or simply create a building function (describing which operations are to be sponsored) and pass it to the `sponsoring` method (TypeScript):

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun sponsorOperation() {
>   val transaction =
>     stellar
>       .transaction(sponsoredKeyPair)
>       .sponsoring(sponsorKeyPair) { addAssetSupport(asset) }
>       .build()
>
>   transaction.sign(sponsorKeyPair).sign(sponsoredKeyPair)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sponsoredKeyPair,
> });
>
> const buildingFunction = (bldr) => bldr.addAssetSupport(asset);
> const transaction = txBuilder
>   .sponsoring(sponsorKeyPair, buildingFunction)
>   .build();
>
> sponsoredKeyPair.sign(transaction);
> sponsorKeyPair.sign(transaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var txBuilder = await stellar.transaction(sponsoredKeyPair);
> var transaction = txBuilder
>     .sponsoring(sponsorKeyPair, (builder) => builder.addAssetSupport(asset))
>     .build();
>
> stellar.sign(transaction, sponsorKeyPair);
> stellar.sign(transaction, sponsoredKeyPair);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let txBuilder = try await stellar.transaction(sourceAddress: sponsoredKeyPair)
> let transaction = try txBuilder.sponsoring(
>     sponsorAccount: sponsorKeyPair,
>     buildingFunction:{ (builder) in builder.addAssetSupport(asset: asset)}).build()
>
> stellar.sign(tx: transaction, keyPair: sponsorKeyPair)
> stellar.sign(tx: transaction, keyPair: sponsoredKeyPair)
> ```

> [!NOTE]
>
> Only some operations can be sponsored, and a sponsoring block (Kotlin) / builder (TypeScript) has a slightly different set of functions available compared to the regular `TransactionBuilder`. Note, that a transaction must be signed by both the sponsor account (`sponsoringKeyPair`) and the account being sponsored (`sponsoredKeyPair`).

#### Sponsoring Account Creation

One of the things that can be done via sponsoring is to create an account with a 0 starting balance. This account creation can be created by simply writing:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun sponsorAccountCreation() {
>   val newKeyPair = account.createKeyPair()
>
>   val transaction =
>     stellar
>       .transaction(sponsorKeyPair)
>       .sponsoring(sponsorKeyPair, sponsoredAccount = newKeyPair) { createAccount(newKeyPair) }
>       .build()
>
>   transaction.sign(sponsorKeyPair).sign(newKeyPair)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({ sourceAddress: sponsorKeyPair });
>
> const newKeyPair = account.createKeypair();
>
> const buildingFunction = (bldr) => bldr.createAccount(newKeyPair);
> const transaction = txBuilder
>   .sponsoring(sponsorKeyPair, buildingFunction, newKeyPair)
>   .build();
>
> newKeyPair.sign(transaction);
> sponsorKeyPair.sign(transaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var newKeyPair = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(sponsorKeyPair);
> var transaction = txBuilder
>     .sponsoring(sponsorKeyPair, sponsoredAccount: newKeyPair,
>         (builder) => builder.createAccount(newKeyPair))
>     .build();
>
> stellar.sign(transaction, sponsorKeyPair);
> stellar.sign(transaction, newKeyPair);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let newKeyPair = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: sponsorKeyPair)
> let transaction = try txBuilder.sponsoring(
>     sponsorAccount: sponsorKeyPair,
>     buildingFunction:{ (builder) in builder.createAccount(newAccount: newKeyPair)},
>     sponsoredAccount: newKeyPair).build()
>
> stellar.sign(tx: transaction, keyPair: sponsorKeyPair)
> stellar.sign(tx: transaction, keyPair: newKeyPair)
> ```

Note how in the first example the transaction source account is set to `sponsoredKeyPair`. Due to this, we did not need to pass a sponsored account value to the `sponsoring` block (Kotlin) / method (TypeScript). Since when ommitted, the sponsored account defaults to the transaction source account (`sponsoredKeyPair`).

However, this time, the sponsored account (freshly created `newKeyPair`) is different from the transaction source account. Therefore, it's necessary to specify it. Otherwise, the transaction will contain a malformed operation. As before, the transaction must be signed by both keys.

#### Sponsoring Account Creation and Modification

If you want to create an account and modify it in one transaction, it's possible to do so with passing a `sponsoredAccount` optional argument to the sponsored block (Kotlin) / sponsoring method (TypeScript) (`newKeyPair` below). If this argument is present, all operations inside the sponsored block will be sourced by this `sponsoredAccount`. (Except account creation, which is always sourced by the sponsor).

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun sponsorAccountCreationAndModification() {
>   val newKeyPair = account.createKeyPair()
>   val replaceWith = account.createKeyPair()
>
>   val transaction =
>     stellar
>         .transaction(sponsorKeyPair)
>         .sponsoring(sponsorKeyPair, newKeyPair) {
>           createAccount(newKeyPair)
>           // source account for below operations will be newKeyPair
>           addAccountSigner(replaceWith, 1)
>           lockAccountMasterKey()
>         }
>         .build()
>
>   transaction.sign(sponsorKeyPair).sign(newKeyPair)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({ sourceAddress: sponsorKeyPair });
>
> const newKeyPair = account.createKeypair();
> const replaceWith = account.createKeypair();
>
> const buildingFunction = (bldr) =>
>   bldr
>     .createAccount(newKeyPair)
>     // source account for below operations will be newKeyPair
>     .addAccountSigner(replaceWith, 1)
>     .lockAccountMasterKey();
>
> const transaction = txBuilder
>   .sponsoring(sponsorKeyPair, buildingFunction, newKeyPair)
>   .build();
>
> newKeyPair.sign(transaction);
> sponsorKeyPair.sign(transaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var newKeyPair = account.createKeyPair();
> var replaceWith = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(sponsorKeyPair);
> var transaction = txBuilder
>     .sponsoring(
>     sponsorKeyPair,
>     sponsoredAccount: newKeyPair,
>         (builder) => builder
>         .createAccount(newKeyPair)
>         .addAccountSigner(replaceWith, 1)
>         .lockAccountMasterKey())
>     .build();
>
> stellar.sign(transaction, sponsorKeyPair);
> stellar.sign(transaction, newKeyPair);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let newKeyPair = account.createKeyPair()
> let replaceWith = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: sponsorKeyPair)
> let transaction = try txBuilder.sponsoring(
>     sponsorAccount: sponsorKeyPair,
>     buildingFunction:{ (builder) in builder
>             .createAccount(newAccount: newKeyPair)
>             .addAccountSigner(signerAddress: replaceWith, signerWeight: 1)
>             .lockAccountMasterKey()
>     },
>     sponsoredAccount: newKeyPair).build()
>
> stellar.sign(tx: transaction, keyPair: sponsorKeyPair)
> stellar.sign(tx: transaction, keyPair: newKeyPair)
> ```

### Fee-Bump Transaction

If you wish to modify a newly created account with a 0 balance, it's also possible to do so via `FeeBump`. It can be combined with a sponsoring block (Kotlin) / method (TypeScript) to achieve the same result as in the example above. However, with `FeeBump` it's also possible to add more operations (that don't require sponsoring), such as a transfer.

First, let's create a transaction that will replace the master key of an account with a new keypair.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val replaceWith = account.createKeyPair()
>
> val transaction =
>   stellar
>     .transaction(sponsoredKeyPair)
>     .sponsoring(sponsorKeyPair) {
>       lockAccountMasterKey()
>       addAccountSigner(replaceWith, signerWeight = 1)
>     }
>     .build()
>
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({
>   sourceAddress: sponsoredKeyPair,
> });
>
> const replaceWith = account.createKeypair();
>
> const buildingFunction = (bldr) =>
>   bldr.lockAccountMasterKey().addAccountSigner(replaceWith, 1);
> const transaction = txBuilder
>   .sponsoring(sponsorKeyPair, buildingFunction)
>   .build();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var replaceWith = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(sponsoredKeyPair);
> var transaction = txBuilder
>     .sponsoring(sponsorKeyPair,
>         (builder) => builder.lockAccountMasterKey().addAccountSigner(replaceWith, 1))
>     .build();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let replaceWith = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: sponsoredKeyPair)
> let transaction = try txBuilder.sponsoring(
>     sponsorAccount: sponsorKeyPair,
>     buildingFunction:{ (builder) in builder
>             .lockAccountMasterKey()
>             .addAccountSigner(signerAddress: replaceWith, signerWeight: 1)
>     }).build()
> ```

Second, sign transaction with both keys.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> transaction.sign(sponsoredKeyPair).sign(sponsorKeyPair)
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> sponsorKeyPair.sign(transaction);
> sponsoredKeyPair.sign(transaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> stellar.sign(transaction, sponsorKeyPair);
> stellar.sign(transaction, sponsoredKeyPair);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> stellar.sign(tx: transaction, keyPair: sponsorKeyPair)
> stellar.sign(tx: transaction, keyPair: sponsoredKeyPair)
> ```

Next, create a fee bump, targeting the transaction.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val feeBump = stellar.makeFeeBump(sponsorKeyPair, transaction)
> feeBump.sign(sponsorKeyPair)
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const feeBump = stellar.makeFeeBump({
>   feeAddress: sponsorKeyPair,
>   transaction,
> });
> sponsorKeyPair.sign(feeBump);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var feeBump = stellar.makeFeeBump(sponsorKeyPair, transaction);
> stellar.sign(feeBump, sponsorKeyPair);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let feeBump = try stellar.makeFeeBump(feeAddress: sponsorKeyPair,
>                                       transaction: transaction)
> stellar.sign(tx: transaction, keyPair: sponsorKeyPair)
> ```

Finally, submit a fee-bump transaction. Executing this transaction will be fully covered by the `sponsorKeyPair` and `sponsoredKeyPair` and may not even have any XLM funds on its account.

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> wallet.stellar().submitTransaction(feeBump)
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> await wallet.stellar().submitTransaction(feeBump);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bool success = await stellar.submitTransaction(feeBump);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> try await stellar.submitTransaction(signedFeeBumpTransaction: feeBump)
> ```

### Using XDR to Send Transaction Data

Note, that a wallet may not have a signing key for `sponsorKeyPair`. In that case, it's necessary to convert the transaction to XDR, send it to the server, containing `sponsorKey` and return the signed transaction back to the wallet. Let's use the previous example of sponsoring account creation, but this time with the sponsor key being unknown to the wallet. The first step is to define the public key of the sponsor keypair:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val sponsorKeyPair = "SponsorAddress".toPublicKeyPair()
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const sponsorKeyPair = new PublicKeypair.fromPublicKey("GC5GD...");
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var sponsorKeyPair = PublicKeyPair.fromAccountId("GC5GD...");
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let sponsorKeyPair = try PublicKeyPair(accountId: "GC5GD...")
> ```

Next, create an account in the same manner as before and sign it with `newKeyPair`. This time, convert the transaction to XDR:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun sponsorAccountCreation(): String {
>   val newKeyPair = account.createKeyPair()
>
>   return stellar
>     .transaction(sponsorKeyPair)
>     .sponsoring(sponsorKeyPair) { createAccount(newKeyPair) }
>     .build()
>     .sign(newKeyPair)
>     .toEnvelopeXdrBase64()
> }
>
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const txBuilder = await stellar.transaction({ sourceAddress: sponsorKeyPair });
>
> const newKeyPair = account.createKeypair();
>
> const transaction = txBuilder
>   .sponsoring(sponsorKeyPair, (bldr) => bldr.createAccount(newKeyPair))
>   .build();
> const xdrString = newKeyPair.sign(transaction).toXDR();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var newKeyPair = account.createKeyPair();
>
> var txBuilder = await stellar.transaction(sponsorKeyPair);
> var transaction = txBuilder
>     .sponsoring(
>         sponsorKeyPair, (builder) => builder.createAccount(newKeyPair),
>         sponsoredAccount: newKeyPair)
>     .build();
>
> stellar.sign(transaction, newKeyPair);
>
> var xdrString = transaction.toEnvelopeXdrBase64();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let newKeyPair = account.createKeyPair()
>
> let txBuilder = try await stellar.transaction(sourceAddress: sponsorKeyPair)
> let transaction = try txBuilder.sponsoring(
>             sponsorAccount: sponsorKeyPair,
>             buildingFunction:{ (builder) in builder.createAccount(newAccount: newKeyPair)},
>             sponsoredAccount: newKeyPair).build()
>
> stellar.sign(tx: transaction, keyPair: newKeyPair)
>
> let xdrString = transaction.toEnvelopeXdrBase64()
> ```

It can now be sent to the server. On the server, sign it with a private key for the sponsor address:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // On the server
> fun signTransaction(xdrString: String): String {
>   val sponsorPrivateKey = SigningKeyPair.fromSecret("MySecret")
>
>   val signedTransaction = stellar.decodeTransaction(xdrString).sign(sponsorPrivateKey)
>
>   return signedTransaction.toEnvelopeXdrBase64()
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> // On the server
> const sponsorPrivateKey = SigningKeyPair.fromSecret("SD3LH4...");
>
> const signedTransaction = sponsorPrivateKey.sign(
>   stellar.decodeTransaction(xdrString),
> );
>
> return signedTransaction.toXDR();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> String signTransaction(String xdrString) {
>   var sponsorPrivateKey = SigningKeyPair.fromSecret("SD3LH4...");
>
>   var transaction = stellar.decodeTransaction(xdrString);
>   stellar.sign(transaction, sponsorPrivateKey);
>
>   return transaction.toEnvelopeXdrBase64();
> }
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> internal func signTransaction(xdr:String) throws -> String {
>     let sponsorPrivateKey = try SigningKeyPair(secretKey: "SD3LH4...")
>
>     let transactionEnum = stellar.decodeTransaction(xdr: xdr)
>     switch transactionEnum {
>     case .transaction(let tx):
>         stellar.sign(tx: tx, keyPair: sponsorPrivateKey)
>         return tx.toEnvelopeXdrBase64()
>     case .feeBumpTransaction(let feeBumpTx):
>         stellar.sign(feeBumpTx: feeBumpTx, keyPair: sponsorPrivateKey)
>         return feeBumpTx.toEnvelopeXdrBase64()
>     case .invalidXdrErr:
>         throw ValidationError.invalidArgument(message: "invalid xdr")
>     }
> }
> ```

When the client receives the fully signed transaction, it can be decoded and sent to the Stellar network:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun recoverSigned(xdrString: String) {
>   val signedTransaction = stellar.decodeTransaction(xdrString)
>
>   stellar.submitTransaction(signedTransaction)
> }
>
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const signedTransaction = stellar.decodeTransaction(xdrString);
>
> await wallet.stellar().submitTransaction(signedTransaction);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var signedTransaction = stellar.decodeTransaction(xdrStringFromBackend);
>
> bool success = await stellar.submitTransaction(signedTransaction);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let signedTransactionEnum = stellar.decodeTransaction(xdr: xdrStringFromBackend)
>
> switch signedTransactionEnum {
> case .transaction(let tx):
>     try await stellar.submitTransaction(signedTransaction: tx)
> case .feeBumpTransaction(let feeBumpTx):
>     try await stellar.submitTransaction(signedFeeBumpTransaction: feeBumpTx)
> case .invalidXdrErr:
>     throw ValidationError.invalidArgument(message: "invalid xdr")
> }
> ```

## Submit Transaction

> [!NOTE]
>
> It's strongly recommended to use the wallet SDK transaction submission functions instead of Horizon alternatives. The wallet SDK gracefully handles timeout and out-of-fee exceptions.

Finally, let's submit a signed transaction to the Stellar network. Note that a sponsored transaction must be signed by both the account and the sponsor.

The transaction is automatically re-submitted on the Horizon 504 error (timeout), which indicates a sudden network activity increase.

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun signAndSubmit() {
>   val signedTxn = createAccount().sign(sourceAccountKeyPair)
>   wallet.stellar().submitTransaction(signedTxn)
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const signedTxn = transaction.sign(sourceAccountKeyPair);
> await wallet.stellar().submitTransaction(signedTxn);
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> stellar.sign(transaction, sourceAccountKeyPair);
> bool success = await stellar.submitTransaction(transaction);
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> stellar.sign(tx: transaction, keyPair: sourceAccountKeyPair)
> try await stellar.submitTransaction(signedTransaction: transaction)
> ```

However, the method above doesn't handle fee surge pricing in the network gracefully. If the required fee for a transaction to be included in the ledger becomes too high and transaction expires before making it into the ledger, this method will throw an exception.

So, instead, the alternative approach is to:

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> suspend fun submitWithFeeIncrease() {
>   wallet.stellar().submitWithFeeIncrease(sourceAccountKeyPair, Duration.ofSeconds(30), 100u) {
>     this.createAccount(destinationAccountKeyPair)
>   }
> }
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const buildingFunction = (builder) =>
>   builder.transfer(kp.publicKey, new NativeAssetId(), "2");
>
> await stellar.submitWithFeeIncrease({
>   sourceAddress: kp,
>   timeout: 30,
>   baseFeeIncrease: 100,
>   buildingFunction,
> });
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bool success = await stellar.submitWithFeeIncrease(
>     sourceAddress: sourceAccountKeyPair,
>     timeout: const Duration(seconds: 30),
>     baseFeeIncrease: 100,
>     maxBaseFee: 2000,
>     buildingFunction: (builder) =>
>     builder.transfer(destinationAccountKeyPair.address, NativeAssetId(), "10.0"));
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> try await stellar.submitWithFeeIncrease(
>   sourceAddress: sourceAccountKeyPair,
>   timeout: 30,
>   baseFeeIncrease: 100,
>   maxBaseFee: 2000,
>   buildingFunction: {
>       (builder) in try! builder
>           .transfer(destinationAddress: destinationAccountKeyPair.address,
>                     assetId: NativeAssetId(),
>                     amount: 10.0)
>   })
> ```

This will create and sign the transaction that originated from the `sourceAccountKeyPair`. Every 30 seconds this function will re-construct this transaction with a new fee (increased by 100 stroops), repeating signing and submitting. Once the transaction is successful, the function will return the transaction body. Note, that any other error will terminate the retry cycle and an exception will be thrown.

## Accessing Horizon SDK

It's very simple to use the Horizon SDK connecting to the same Horizon instance as a `Wallet` class. To do so, simply call:

> [tab] Kotlin
>
> ```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val server = wallet.stellar().server
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const server = wallet.stellar().server;
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var server = wallet.stellar().server;
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let server = wallet.stellar.server
> ```

And you can work with Horizon Server instance:

> [tab] Kotlin
>
> ```kt theme={"theme":{"light":"github-light","dark":"github-dark"}}
> val stellarTransaction = server.transactions().transaction("transaction_id")
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const stellarTransaction = server
>   .transactions()
>   .forAccount("account_id")
>   .call();
> ```

> [tab] Flutter
>
> ```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
> var transactions = await server.transactions.forAccount("accountId").execute();
> ```

> [tab] Swift
>
> ```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
> let transactionsEnum = await server.transactions.getTransactions(forAccount: "accountId")
> switch transactionsEnum {
> case .success(let page):
>     let transactions = page.records
> case .failure(let error):
>     throw error
> }
> ```
