Skip to content

Commit 8ba009a

Browse files
committed
chore(docs): improve docs on how payment works in v8
1 parent 5035dd9 commit 8ba009a

File tree

2 files changed

+110
-25
lines changed

2 files changed

+110
-25
lines changed

docs/sdk/getting-started/auth-services.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ Lit hosts default Auth Service instances so you can build without deploying infr
3232
configuration.
3333
</Note>
3434

35+
## Payment Delegation APIs
36+
37+
The Auth Service also exposes lightweight endpoints that sponsor user requests on the network. These are the same APIs the Lit SDK calls when you use `litClient.authService.registerPayer` / `delegateUsers` from the Payment Manager guide.
38+
39+
- `POST /register-payer`
40+
- Headers: `x-api-key`.
41+
- Behaviour: generates a random `payerSecretKey`, hashes it with the API key, and derives a child wallet from `LIT_DELEGATION_ROOT_MNEMONIC`.
42+
- Response: `{ success, payerWalletAddress, payerSecretKey }`. The service **does not** persist the secret—you must store it securely (KMS, vault, etc.).
43+
- Rotation: call the endpoint again with the same API key to rotate the secret and derive a new child wallet.
44+
- `POST /add-users`
45+
- Headers: `x-api-key`, `payer-secret-key`; body is a JSON array of user addresses.
46+
- Behaviour: recomputes the same child wallet on the fly and calls `PaymentManager.delegatePaymentsBatch` so the payer sponsors those users.
47+
48+
<Tip>
49+
Running the Auth Service yourself keeps the derivation mnemonic and payer secrets inside your infrastructure. The Lit-hosted instance is great for quick starts, but you remain responsible for storing the returned `payerSecretKey`.
50+
</Tip>
51+
52+
If you prefer not to run an Auth Service at all, you can still sponsor users manually: create a payer wallet, fund it, and call `paymentManager.delegatePayments*` directly from your backend. See [Payment Manager Setup](/sdk/getting-started/payment-manager-setup#sponsoring-your-users-capacity-delegation-replacement) for sample code.
53+
3554

3655
### Install the SDK
3756

docs/sdk/getting-started/payment-manager-setup.mdx

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Payment Manager Setup"
3-
description: "Configure payment system for the Lit JS SDK"
2+
title: 'Payment Manager Setup'
3+
description: 'Configure payment system for the Lit JS SDK'
44
---
55

66
<Warning>
@@ -26,7 +26,7 @@ The Payment Manager demonstrates Lit Protocol's payment system - a billing syste
2626
- Encryption/Decryption - Secure data with programmable access control
2727
- PKP Signing - Cryptographic keys that can sign transactions based on conditions
2828
- Lit Actions - Serverless functions with cryptographic capabilities
29-
Similar to how you pay AWS for cloud computing, this\system ensures the decentralised network can sustain itself and pay node operators. You can deposit funds, request withdrawals with security delays, and manage balances for yourself or other users (enabling applications to sponsor their users' costs for better UX).
29+
Similar to how you pay AWS for cloud computing, this\system ensures the decentralised network can sustain itself and pay node operators. You can deposit funds, request withdrawals with security delays, and manage balances for yourself or other users (enabling applications to sponsor their users' costs for better UX).
3030

3131
<Steps>
3232
<Step title="Payment Manager Setup">
@@ -42,6 +42,7 @@ const litClient = await createLitClient({ network: nagaTest });
4242
const paymentManager = await litClient.getPaymentManager({
4343
account: yourAccount // viem account instance
4444
});
45+
4546
````
4647

4748
</Step>
@@ -59,7 +60,7 @@ const { data: myAccount } = useWalletClient();
5960
6061
```typescript viem/accounts
6162
// 1. import the privateKeyToAccount function from viem/accounts
62-
import { privateKeyToAccount } from "viem/accounts";
63+
import { privateKeyToAccount } from 'viem/accounts';
6364

6465
// 2. Convert your private key to a viem account object that can be used for payment operations.
6566
const myAccount = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
@@ -74,7 +75,7 @@ const myAccount = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
7475
```typescript Deposit funds to your own account
7576
// 1. Deposit funds to your own account
7677
const result = await paymentManager.deposit({
77-
amountInEth: "0.1",
78+
amountInEth: '0.1',
7879
});
7980

8081
console.log(`Deposit successful: ${result.hash}`);
@@ -84,8 +85,8 @@ console.log(`Deposit successful: ${result.hash}`);
8485
```typescript Deposit for another user
8586
// 1. Deposit funds for another user
8687
const result = await paymentManager.depositForUser({
87-
userAddress: "0x742d35Cc6638Cb49f4E7c9ce71E02ef18C53E1d5",
88-
amountInEth: "0.05",
88+
userAddress: '0x742d35Cc6638Cb49f4E7c9ce71E02ef18C53E1d5',
89+
amountInEth: '0.05',
8990
});
9091

9192
console.log(`Deposit successful: ${result.hash}`);
@@ -97,12 +98,84 @@ console.log(`Deposit successful: ${result.hash}`);
9798

9899
</Steps>
99100

101+
## Sponsoring Your Users (Capacity Delegation Replacement)
102+
103+
If your app previously minted Capacity Credits and handed users a `CapacityDelegationAuthSig`, the equivalent Naga flow is:
104+
105+
1. **Fund a payer wallet** – call `litClient.getPaymentManager({ account })` and deposit $LITKEY ($tstLPX in testnet) into the ledger contract (see examples above).
106+
2. **Register the payer** – decide how you want to provision and operate the sponsor wallet:
107+
- **Lit-hosted Auth Service** – call `authService.registerPayer`; Lit derives the wallet + `payerSecretKey` for you (ideal for prototypes).
108+
- **Self-hosted Auth Service** – deploy the open-source service (see [Auth Services setup](/sdk/getting-started/auth-services)) so derivation stays within your infrastructure. You’ll supply your own `LIT_DELEGATION_ROOT_MNEMONIC`.
109+
- **Manual (no Auth Service)** – generate a wallet yourself (e.g. `privateKeyToAccount`) and store the private key securely; you’ll call the Payment Manager directly in the next step.
110+
3. **Delegate end-user addresses** – map users to the payer:
111+
- Using an Auth Service: POST to `authService.add-users`.
112+
- Manual route: call `paymentManager.delegatePayments*` with your server-side account; this writes to the same Payment Delegation contract.
113+
114+
<Note>
115+
For production environments we recommend running your own Auth Service (self-hosted) so you retain full custody over the `LIT_DELEGATION_ROOT_MNEMONIC` and derivation secrets. The Lit-hosted Auth Service is handy for prototypes and quick starts, but you still remain responsible for storing the returned `payerSecretKey` securely.
116+
</Note>
117+
4. **Users decrypt as normal** – the browser still calls `litClient.decrypt` with an auth context; the network draws fees from the delegated payer instead of the user’s wallet.
118+
119+
```typescript server
120+
// Manual delegation without Auth Service
121+
import { createLitClient } from '@lit-protocol/lit-client';
122+
import { privateKeyToAccount } from 'viem/accounts';
123+
import { nagaTest } from '@lit-protocol/networks';
124+
125+
const payerAccount = privateKeyToAccount(
126+
process.env.PAYER_PRIVATE_KEY as `0x${string}`
127+
);
128+
const litClient = await createLitClient({ network: nagaTest });
129+
130+
const paymentManager = await litClient.getPaymentManager({
131+
account: payerAccount,
132+
});
133+
134+
// Delegate a single user
135+
await paymentManager.delegatePayments({ userAddress: '0xUser...' });
136+
137+
// Or delegate multiple users in one transaction
138+
await paymentManager.delegatePaymentsBatch({
139+
userAddresses: ['0xAlice...', '0xBob...'],
140+
});
141+
```
142+
143+
```typescript
144+
// Client-side decrypt with sponsored payments
145+
const authContext = await authManager.createEoaAuthContext({
146+
litClient,
147+
config: { account: walletClient },
148+
authConfig: {
149+
resources: [
150+
['access-control-condition-decryption', '*'],
151+
['lit-action-execution', '*'],
152+
],
153+
},
154+
});
155+
156+
const response = await litClient.decrypt({
157+
data: encryptedData,
158+
unifiedAccessControlConditions: accs,
159+
authContext,
160+
chain: 'ethereum',
161+
userMaxPrice: BigInt('200000000000000000'), // optional guardrail
162+
});
163+
```
164+
165+
Behind the scenes the SDK:
166+
167+
- Builds a pricing context during handshake (product, per-node prices, threshold).
168+
- Encrypts requests per node using the JIT keyset.
169+
- Emits per-node max price in the session signature.
170+
- Lets the validators call `Ledger.chargeUser` against the delegated payer wallet.
171+
100172
## Auth Service API Endpoints
101173

102-
Leverage the hosted Auth Service to manage delegation without exposing private keys in your application:
174+
Leverage the hosted Auth Service to manage delegation without exposing private keys in your application. Full request/response details live in the [Auth Services setup guide](/sdk/getting-started/auth-services#payment-delegation-apis). In practice you will:
103175

104-
- `POST /register-payer` - Send your `x-api-key` header to receive a delegated payer address and `payerSecretKey`. Persist this secret securely; it is required for all future delegation calls.
105-
- `POST /add-users` - Provide headers `x-api-key` and `payer-secret-key` plus a JSON body containing an array of user addresses. The Auth Service uses the Payment Manager internally to delegate payments to each address in a single transaction.
176+
- Call `authService.registerPayer` (hosted or self-hosted) to derive a payer wallet + `payerSecretKey`.
177+
- Call `authService.delegateUsers` (`/add-users`) to sponsor a list of user addresses.
178+
- Or skip the service entirely and use the Payment Manager methods directly (example below).
106179

107180
> The legacy capacity-credit minting flow has been removed. Payment delegation now interacts directly with the Payment Manager contracts.
108181
@@ -118,35 +191,28 @@ const apiKey = process.env.LIT_API_KEY!;
118191

119192
// 3. Register a payer wallet (store the secret securely server-side)
120193
const registerResponse = await litClient.authService.registerPayer({
121-
authServiceBaseUrl,
122-
apiKey,
194+
authServiceBaseUrl,
195+
apiKey,
123196
});
124197

125198
console.log('Payer wallet:', registerResponse.payerWalletAddress);
126199
console.log('Payer secret (store securely!):', registerResponse.payerSecretKey);
127200

128201
// 4. Later on, delegate payments for multiple users using the saved secret
129202
const delegateResponse = await litClient.authService.delegateUsers({
130-
authServiceBaseUrl,
131-
apiKey,
132-
payerSecretKey: registerResponse.payerSecretKey,
133-
userAddresses: [
134-
'0x1234...abcd',
135-
'0xabcd...1234',
136-
],
203+
authServiceBaseUrl,
204+
apiKey,
205+
payerSecretKey: registerResponse.payerSecretKey,
206+
userAddresses: ['0x1234...abcd', '0xabcd...1234'],
137207
});
138208

139209
console.log('Delegation submitted with tx hash:', delegateResponse.txHash);
140210

141211
// 5. Continue to use the same payer secret for future delegation calls
142-
````
212+
```
143213

144214
### How the Auth Service derives payer wallets
145215

146-
- The service holds a single root mnemonic (`LIT_DELEGATION_ROOT_MNEMONIC`).
147-
- `/register-payer` combines the `x-api-key` header with a freshly generated `payerSecretKey`. That pair is hashed into a deterministic derivation index, which is then used with the root mnemonic to derive a unique child wallet.
148-
- The response includes the derived wallet address and the random `payerSecretKey`. The server does not store this secret; you must persist it securely on the client side.
149-
- Later, `/add-users` expects both headers (`x-api-key` and `payer-secret-key`). The service recomputes the same derivation index and wallet on the fly, so the same header pair always maps to the same child wallet.
150-
- Calling `/register-payer` again with the same API key issues a new random `payerSecretKey`, which leads to a different child wallet. Choose whether to rotate secrets or keep the original one depending on your application needs.
216+
- For hosted or self-hosted deployments, see the derivation and rotation notes in the [Auth Services guide](/sdk/getting-started/auth-services#payment-delegation-apis). Manual deployments can always provision and delegate entirely via the `PaymentManager` helper without touching these endpoints.
151217

152218
![](https://www.plantuml.com/plantuml/png/XPAn3jCm48PtFyMfKw8IiKSAQcab1a1K58c1CXpEaLWaJcHVIlFs6DkKcBHYYy-Vl_Floyuo6fxwJh3YZc0_SGjdCbSb2KuuzwGPZjHHWwm6BSJeS2NLYAw-ENJAxMy0BOJFT7ifyz3lGbodv3l5qG3lKMD3nlFn-ndh6RTyr3lSFTN5MYo96Xc_eINOh8F2OT1iKFeUzuKGLGKVgL6MoS28CnceAX5lNhnQ1YpXzE7y2LwQY1SUl-ZiLk2eYXyqvsA1hqw_8Kq6cKARCqb3VD57CkfAy1ExZXY-cw67NbC_Q2LX2quCJfnwdJXSi0ogp_xilguDMNlH2_rRcdt2-0m4aoLZ_viGwxhmv3BSYz2iiDuSAXxwydMLEmwaX8RYBBDSnABR_plY4fmCcToZEbUgMM1Ub0uxGoc7INCk0XNJf509Ibj6pGfvPVyNhUCZnRfzZIpRp4VCHGgxu_TVo1zSlAxuim75WoPy0qEIrCWhPJeBZxPeswUpjvEKP2rix-IET3trtIy0)

0 commit comments

Comments
 (0)