> ## Documentation Index
> Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Wallet Management Functions

## Wallet Access

### userWallets

Get the current list of user wallets (synchronous).

```kotlin theme={"system"}
val wallets: List<BaseWallet> = sdk.wallets.userWallets
```

#### Example

```kotlin theme={"system"}
val wallets = sdk.wallets.userWallets
wallets.forEach { wallet ->
    println("${wallet.address} (${wallet.chain})")
}
```

### userWalletsChanges

Observe wallet list changes (reactive flow).

```kotlin theme={"system"}
val userWalletsChanges: StateFlow<List<BaseWallet>>
```

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    sdk.wallets.userWalletsChanges.collect { wallets ->
        // Handle wallet changes
        _wallets.value = wallets
    }
}
```

## Balance Operations

### getBalance

Get the balance for a specific wallet.

```kotlin theme={"system"}
suspend fun getBalance(wallet: BaseWallet): String
```

#### Parameters

* **wallet** (BaseWallet) - The wallet to check balance for

#### Returns

* **String** - Balance as a string (in native token units)

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val balance = sdk.wallets.getBalance(wallet)
        println("Balance: $balance")
    } catch (e: Exception) {
        println("Failed to get balance: ${e.message}")
    }
}
```

## Network Operations

### getNetwork

Get the current network for a wallet.

```kotlin theme={"system"}
suspend fun getNetwork(wallet: BaseWallet): GenericNetwork
```

#### Parameters

* **wallet** (BaseWallet) - The wallet to get network for

#### Returns

* **GenericNetwork** - The current network

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val network = sdk.wallets.getNetwork(wallet)
        println("Network: ${network.name}, Chain ID: ${network.chainId}")
    } catch (e: Exception) {
        println("Failed to get network: ${e.message}")
    }
}
```

### switchNetwork

Switch a wallet to a different network.

```kotlin theme={"system"}
suspend fun switchNetwork(wallet: BaseWallet, network: GenericNetwork)
```

#### Parameters

* **wallet** (BaseWallet) - The wallet to switch networks for
* **network** (GenericNetwork) - The target network

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val polygon = sdk.networks.evm.first { it.chainId == 137 }
        sdk.wallets.switchNetwork(wallet, polygon)
        println("Switched to ${polygon.name}")
    } catch (e: Exception) {
        println("Failed to switch network: ${e.message}")
    }
}
```

## Message Signing

### signMessage

Sign a message with a wallet.

```kotlin theme={"system"}
suspend fun signMessage(wallet: BaseWallet, message: String): String
```

#### Parameters

* **wallet** (BaseWallet) - The wallet to sign with
* **message** (String) - The message to sign

#### Returns

* **String** - The signature

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val signature = sdk.wallets.signMessage(wallet, "Hello!")
        println("Signature: $signature")
    } catch (e: Exception) {
        println("Failed to sign: ${e.message}")
    }
}
```

## Typed Data Signing (EIP-712)

### signTypedData

Sign typed data (EIP-712) with a wallet.

```kotlin theme={"system"}
suspend fun signTypedData(wallet: BaseWallet, typedDataJson: String): String
```

#### Parameters

* **wallet** (BaseWallet) - The wallet to sign with
* **typedDataJson** (String) - The typed data as a JSON string

#### Returns

* **String** - The signature

#### Example

```kotlin theme={"system"}
val typedData = """
{
  "types": {
    "EIP712Domain": [
      {"name": "name", "type": "string"},
      {"name": "chainId", "type": "uint256"}
    ],
    "Person": [
      {"name": "name", "type": "string"}
    ]
  },
  "primaryType": "Person",
  "domain": {
    "name": "My App",
    "chainId": 1
  },
  "message": {
    "name": "Alice"
  }
}
""".trimIndent()

viewModelScope.launch {
    try {
        val signature = sdk.wallets.signTypedData(wallet, typedData)
        println("Signature: $signature")
    } catch (e: Exception) {
        println("Failed to sign: ${e.message}")
    }
}
```

## Signature Verification

### verifySignature

Verify a signature against a message and wallet.

```kotlin theme={"system"}
suspend fun verifySignature(
    wallet: BaseWallet,
    message: String,
    signature: String
): Boolean
```

#### Parameters

* **wallet** (BaseWallet) - The wallet that signed the message
* **message** (String) - The original message
* **signature** (String) - The signature to verify

#### Returns

* **Boolean** - `true` if signature is valid, `false` otherwise

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val message = "Hello!"
        val signature = sdk.wallets.signMessage(wallet, message)
        val isValid = sdk.wallets.verifySignature(wallet, message, signature)
        println("Signature is ${if (isValid) "valid" else "invalid"}")
    } catch (e: Exception) {
        println("Verification failed: ${e.message}")
    }
}
```

## Primary Wallet Management

### setPrimary

Set a wallet as the primary wallet for the user.

```kotlin theme={"system"}
suspend fun setPrimary(walletId: String)
```

#### Parameters

* **walletId** (String) - The ID of the wallet to set as primary

#### Example

```kotlin theme={"system"}
viewModelScope.launch {
    try {
        val walletId = wallet.id ?: throw Exception("Wallet ID is null")
        sdk.wallets.setPrimary(walletId)
        println("Set wallet as primary")
    } catch (e: Exception) {
        println("Failed to set primary: ${e.message}")
    }
}
```
