Skip to content

Commit 683709b

Browse files
committed
refactor: tighten auth typed data workflow
1 parent c267429 commit 683709b

4 files changed

Lines changed: 27 additions & 26 deletions

File tree

packages/client/src/authentication.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { TypedDataPayload } from './types';
55
export type AuthenticationWorkflowRequest =
66
| { kind: 'requestAddress' }
77
| {
8-
kind: 'signTypedData';
8+
kind: 'signAuthMessage';
99
payload: TypedDataPayload;
1010
};
1111

packages/client/src/clients.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class PublicClient {
5353
const timestamp = Math.floor(Date.now() / 1000);
5454
const address = expectEvmAddress(yield { kind: 'requestAddress' });
5555
const signature = yield {
56-
kind: 'signTypedData',
56+
kind: 'signAuthMessage',
5757
payload: createL2AuthTypedDataPayload({
5858
address,
5959
chainId: this.environment.chainId,

packages/client/src/types.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { EvmAddress, HexString } from '@polymarket/types';
2+
13
export type TypedDataField = {
24
name: string;
35
type: string;
@@ -8,8 +10,8 @@ export type TypedData = Record<string, readonly TypedDataField[]>;
810
export type TypedDataDomain = {
911
chainId?: number;
1012
name?: string;
11-
salt?: `0x${string}`;
12-
verifyingContract?: string;
13+
salt?: HexString;
14+
verifyingContract?: EvmAddress;
1315
version?: string;
1416
};
1517

@@ -19,8 +21,3 @@ export type TypedDataPayload = {
1921
primaryType: string;
2022
types: TypedData;
2123
};
22-
23-
export type TypedDataSigner = {
24-
getAddress(): Promise<string>;
25-
signTypedData(payload: TypedDataPayload): Promise<string>;
26-
};

packages/client/src/viem/index.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { expectEvmAddress, expectSignature } from '@polymarket/types';
2-
import type { Account, WalletClient } from 'viem';
1+
import {
2+
expectEvmAddress,
3+
expectSignature,
4+
invariant,
5+
} from '@polymarket/types';
6+
import type { Account, Chain, Transport, WalletClient } from 'viem';
37
import type { AuthenticationWorkflow } from '../authentication';
48
import type { SecureClient } from '../clients';
59

6-
function getAccount(walletClient: WalletClient): Account | `0x${string}` {
7-
const account = walletClient.account;
8-
9-
if (account) {
10-
return account;
11-
}
12-
13-
throw new Error('Expected a WalletClient with a hoisted account.');
10+
function isWalletClientWithAccount(
11+
walletClient: WalletClient,
12+
): walletClient is WalletClient<Transport, Chain, Account> {
13+
return walletClient.account !== undefined;
1414
}
1515

1616
export function authenticateWith(walletClient: WalletClient) {
17-
const account = getAccount(walletClient);
17+
invariant(
18+
isWalletClientWithAccount(walletClient),
19+
'Wallet client with account is required',
20+
);
21+
22+
const account = walletClient.account;
1823
const address = expectEvmAddress(
19-
typeof account === 'string' ? account : account.address,
24+
typeof walletClient.account === 'string'
25+
? walletClient.account
26+
: walletClient.account.address,
2027
);
2128

2229
return async function authenticate(
@@ -30,15 +37,12 @@ export function authenticateWith(walletClient: WalletClient) {
3037
case 'requestAddress':
3138
result = await workflow.next(address);
3239
break;
33-
case 'signTypedData':
40+
case 'signAuthMessage':
3441
result = await workflow.next(
3542
expectSignature(
3643
await walletClient.signTypedData({
3744
account,
38-
...(result.value.payload as Omit<
39-
Parameters<WalletClient['signTypedData']>[0],
40-
'account'
41-
>),
45+
...result.value.payload,
4246
}),
4347
),
4448
);

0 commit comments

Comments
 (0)