# @universal-everything/sdk-core-viem

Drop-in [viem](https://viem.sh) `LocalAccount` adapter for a
WalletKit-managed wallet. The server-held private key appears as an
ordinary viem account — every `viem` helper that takes an account
works unchanged.

## Install

```bash
pnpm add @universal-everything/sdk-core @universal-everything/sdk-core-viem viem
```

## Quick start

```ts
import { WalletKitClient } from "@universal-everything/sdk-core";
import { createWalletKitAccount } from "@universal-everything/sdk-core-viem";
import { createWalletClient, http } from "viem";
import { lukso, luksoTestnet } from "viem/chains";

const client = new WalletKitClient({ baseUrl, getAccessToken });
const [wallet] = await client.listWallets();

const account = createWalletKitAccount(client, wallet);

const walletClient = createWalletClient({
  account,
  chain: luksoTestnet,
  transport: http(),
});

const hash = await walletClient.sendTransaction({
  to: "0x…",
  value: 1_000_000_000_000_000n,
});
```

## Method routing

| viem call                 | Server endpoint                 |
| ------------------------- | ------------------------------- |
| `account.signMessage`     | `POST /v1/sign/message`         |
| `account.signTransaction` | `POST /v1/sign/transaction`     |
| `account.signTypedData`   | *(not supported yet — throws)*  |

`signMessage` handles both forms of viem's `SignableMessage`:

- `{ message: "hello" }` → sent as `utf-8` encoding.
- `{ message: { raw: Uint8Array } }` → hex-encoded.
- `{ message: { raw: "0xdead…" } }` → passed through as hex.

`signTransaction` maps viem's bigint fields to decimal strings on the
wire, then returns the RLP-serialized signed tx verbatim so
`viem`'s `sendRawTransaction` / `recoverTransactionAddress` work
unchanged.

`signTypedData` throws because the relayer has no
`/v1/sign/typed-data` endpoint yet. Track in M11.

## Onboarding helpers (A1 external-controller deploy)

For extensionless onboarding, this package also exports pure,
chain-free helpers to predict a Universal Profile's CREATE2 address and
prove control of an external controller — the client side of the
relayer's A1 deploy path. No account adapter needed; they take a
caller-supplied `signMessage`.

```ts
import {
  predictProfileAddress,
  deriveLsp23ProxyAddress,
  encodeDeploymentCalldata,
  buildControlProof,
} from "@universal-everything/sdk-core-viem";

// 1. Vary `salt` to offer the user a choice of profile addresses
//    (identicons) — each derivation is offline, no RPC.
const { deploymentCalldata, predictedAddress } = predictProfileAddress(
  factoryAddress,
  { salt, primaryImplementation, secondaryImplementation,
    postDeploymentModule, postDeploymentModuleCalldata },
);

// 2. Prove the external controller authorized THIS deploy. The message
//    is byte-identical to the relayer's `buildControlProofMessage`, so
//    the recovered signer must match or the deploy is rejected.
const nonce = await client.getNonce();
const controlProof = await buildControlProof(
  { subject, controllerAddress, predictedAddress, chainId, deploymentCalldata },
  nonce,
  Math.floor(Date.now() / 1000),
  (message) => wallet.signMessage({ message }), // wagmi / viem / Privy
);

// 3. Deploy via the external-controller variant of `client.deployProfile`.
await client.deployProfile({
  chainId, controllerAddress, controlProof, predictedAddress, deploymentCalldata,
});
```

`deriveLsp23ProxyAddress({ factoryAddress, deploymentCalldata })` is the
standalone predictor if you already have calldata;
`encodeDeploymentCalldata` builds it from `Lsp23DeploymentParams`.

## Privy per-op authorization

For user-owned Privy wallets, each sign call needs a fresh per-op
authorization signature. Pass `getOpContext` — it's invoked
immediately before each call:

```ts
const account = createWalletKitAccount(client, wallet, {
  getOpContext: async () => {
    const sig = await privy.generateAuthorizationSignature(payload);
    return {
      authorizationSignature: sig,
      requestExpiry: Date.now() + 60_000,
    };
  },
});
```

Return `null` from `getOpContext` (or omit it entirely) for
unauthenticated / non-Privy signing contexts — typically
`LocalKeyCustody` in tests and local dev.
