Skip to content

Commit a7d6805

Browse files
authored
Merge pull request #959 from LIT-Protocol/feature/jss-99-bug-walletclientauthenticator-needs-to-provide-a-way-to
fix(auth): allow overriding siwe message fields
2 parents 70feead + 6795202 commit a7d6805

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

.changeset/cuddly-rats-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-protocol/auth': patch
3+
---
4+
5+
Allows `WalletClientAuthenticator.authenticate` to build SIWE messages with user-specified fields (`domain`, `uri`, `statement`, etc.) while still managing the nonce internally.

docs/sdk/auth/pkp-native-auth/pkp-eoa-auth.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,21 @@ description: "Externally Owned Account (EOA) authentication uses your existing E
4141
Use the WalletClientAuthenticator/ViemAccountAuthenticator to authenticate your connected wallet and generate auth data.
4242

4343
<CodeGroup>
44-
```ts wagmi
44+
```ts wagmi
4545
import { WalletClientAuthenticator } from '@lit-protocol/auth';
4646

4747
const authData = await WalletClientAuthenticator.authenticate(walletClient);
48-
```
48+
49+
// Override SIWE fields (e.g., when running on production domains)
50+
const authDataForProd = await WalletClientAuthenticator.authenticate(
51+
walletClient,
52+
undefined,
53+
{
54+
domain: 'example.com',
55+
uri: 'https://example.com/login',
56+
}
57+
);
58+
```
4959

5060
```ts viem/accounts
5161
import { ViemAccountAuthenticator } from '@lit-protocol/auth';
@@ -111,4 +121,4 @@ const authContext = await authManager.createPkpAuthContext({
111121
});
112122
```
113123
</Step>
114-
</Steps>
124+
</Steps>

packages/auth/src/lib/authenticators/WalletClientAuthenticator.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@lit-protocol/constants';
1010
import { getChildLogger } from '@lit-protocol/logger';
1111
import { AuthData } from '@lit-protocol/schemas';
12-
import { AuthMethod, AuthSig } from '@lit-protocol/types';
12+
import { AuthMethod, AuthSig, BaseSiweMessage } from '@lit-protocol/types';
1313
import { GetWalletClientReturnType } from '@wagmi/core';
1414
import { getAddress, Hex, keccak256, stringToBytes, WalletClient } from 'viem';
1515
import { fetchBlockchainData } from './helper/fetchBlockchainData';
@@ -18,6 +18,10 @@ const _logger = getChildLogger({
1818
module: 'WalletClientAuthenticator',
1919
});
2020

21+
export type WalletClientAuthenticateOverrides = Partial<
22+
Omit<BaseSiweMessage, 'walletAddress' | 'nonce'>
23+
>;
24+
2125
export class WalletClientAuthenticator {
2226
public readonly type = 'walletClient';
2327

@@ -42,16 +46,30 @@ export class WalletClientAuthenticator {
4246
});
4347
}
4448

49+
/**
50+
* Generate an AuthSig for the connected wallet. Provide a full message to sign via `messageToSign`,
51+
* or let the helper build one while overriding specific SIWE fields with `siweMessageOverrides`.
52+
*/
4553
static async authenticate(
4654
account: GetWalletClientReturnType | WalletClient,
47-
messageToSign?: string
55+
messageToSign?: string,
56+
siweMessageOverrides?: WalletClientAuthenticateOverrides
4857
): Promise<AuthData> {
4958
let _toSign = messageToSign;
5059

5160
if (!_toSign) {
52-
_toSign = await createSiweMessage({
61+
const restOverrides = siweMessageOverrides ?? {};
62+
63+
const nonce = await fetchBlockchainData();
64+
65+
const siweParams: BaseSiweMessage = {
5366
walletAddress: account.account!.address,
54-
nonce: await fetchBlockchainData(),
67+
nonce,
68+
...restOverrides,
69+
};
70+
71+
_toSign = await createSiweMessage({
72+
...siweParams,
5573
});
5674
}
5775

0 commit comments

Comments
 (0)