# 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).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.opinion.trade/developer-guide/opinion-clob-typescript-sdk/builder-mode/userclient.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
