# UserClient

> Test wallet helper for simulating user signing in Builder mode.

`UserClient` holds a private key and can sign EIP-712 typed data and order hashes. It is a **testing utility** -- in production, users sign with their own wallets (MetaMask, WalletConnect, etc.).

## Create a Random Test Wallet

```typescript
import { UserClient } from '@opinion-labs/opinion-clob-sdk';

const user = UserClient.createRandom();
console.log(user.address);  // Random EOA address
```

## Create from Private Key

```typescript
const user = new UserClient('0xYourPrivateKey...');
console.log(user.address);  // Derived EOA address
```

## Sign Safe Transactions

Use `signTypedData()` for Safe transaction signing (enable trading, split, merge, redeem, withdraw):

```typescript
const txResult = await builder.buildEnableTradingTx(safeAddress);
const signature = await user.signTypedData(txResult.eip712Data);
await builder.submitSafeTx(userAddress, txResult, signature);
```

### Parameters

| Parameter   | Type      | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| `typedData` | TypedData | EIP-712 typed data with `domain`, `types`, `primaryType`, and `message` |

Returns a hex signature string (`0x`-prefixed).

## Sign Order Hashes

Use `signHash()` for order signing (place order):

```typescript
const orderData = await builder.buildOrderForSigning({ ... });
const signature = await user.signHash(orderData.structHash);
await builder.placeOrderForUserFromBuildResult(orderData, signature, safeAddress);
```

### Parameters

| Parameter    | Type   | Description                                            |
| ------------ | ------ | ------------------------------------------------------ |
| `hashToSign` | string | Hash to sign (hex string, with or without `0x` prefix) |

Returns a hex signature string (`0x`-prefixed).

## File Persistence

For demo scripts, `UserClient` supports saving and loading from JSON files:

```typescript
// Save to file
user.saveToFile('.test_user.json');

// Load from file
const user = UserClient.loadFromFile('.test_user.json');

// Load or create (creates new if file doesn't exist)
const user = UserClient.loadOrCreate('.test_user.json');
```

## Notes

* **Order signing** uses `signHash(structHash)` -- signs the hash directly.
* **Safe TX signing** uses `signTypedData(eip712Data)` -- signs EIP-712 typed data.
* These are two distinct signing patterns. Do not mix them up.
* In production, replace `UserClient` with wallet integration (MetaMask `eth_signTypedData_v4` for Safe TXs, `personal_sign` for order hashes).
