# Keypair

The `Keypair` class represents an Ed25519 keypair for signing SOMA transactions.

```python
from soma_sdk import Keypair
```

## Constructors

### generate

Generate a new random Ed25519 keypair.

```python
kp = Keypair.generate()
```

**Returns**: [`Keypair`](https://docs.soma.org/reference/sdk/keypair/)

#### Example

```python
kp = Keypair.generate()
print(kp.address())  # 0x...
```

### from_secret_key

Import a keypair from a secret key.

```python
kp = Keypair.from_secret_key(secret)
```

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `secret` | `bytes \| str` | Yes | - | 32-byte secret key (bytes or hex string) |

**Returns**: [`Keypair`](https://docs.soma.org/reference/sdk/keypair/)

### from_mnemonic

Derive a keypair from a BIP-39 mnemonic phrase.

```python
kp = Keypair.from_mnemonic("word1 word2 ... word12")
```

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `mnemonic` | `str` | Yes | - | BIP-39 mnemonic phrase (12, 15, 18, 21, or 24 words) |

**Returns**: [`Keypair`](https://docs.soma.org/reference/sdk/keypair/)

## Methods

### address

Get the SOMA address derived from this keypair's public key.

```python
addr = kp.address()  # "0x..."
```

**Returns**: `str`. Hex-encoded SOMA address.

### sign

Sign transaction data bytes.

```python
signature = kp.sign(tx_data_bytes)
```

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `tx_data_bytes` | `bytes` | Yes | - | BCS-serialized transaction data |

**Returns**: `bytes`. Ed25519 signature.

### to_secret_key

Export the secret key as a hex string.
**Danger:** This exposes the raw private key. Never log, share, or store the result in plaintext.

```python
secret = kp.to_secret_key()  # Bech32 string
```

**Returns**: `str`. Bech32-encoded secret key.