Skip to content

Commit 85a79b4

Browse files
committed
feat: add getAuthContext component
1 parent dd1166d commit 85a79b4

File tree

3 files changed

+159
-56
lines changed

3 files changed

+159
-56
lines changed

packages/auth/src/lib/AuthManager/authContexts/getEoaAuthContext.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
createSiweMessageWithRecaps,
3-
generateAuthSig,
4-
ResourceAbilityRequestBuilder,
3+
generateAuthSig
54
} from '@lit-protocol/auth-helpers';
65
import { LitNodeClient } from '@lit-protocol/lit-node-client';
76
import {
@@ -20,24 +19,15 @@ interface GetEoaAuthContextParams {
2019
};
2120
signerAddress: `0x${string}`;
2221
};
23-
resources?: LitResourceAbilityRequest[];
22+
resources: LitResourceAbilityRequest[];
2423
capabilityAuthSigs?: AuthSig[];
2524
}
2625

27-
export const getEoaAuthContext = ({
28-
litNodeClient,
29-
identity: { pkpPublicKey, signer, signerAddress },
30-
resources,
31-
capabilityAuthSigs,
32-
}: GetEoaAuthContextParams) => {
33-
const resourceBuilder = new ResourceAbilityRequestBuilder();
34-
resourceBuilder.addPKPSigningRequest('*');
35-
const resourceRequests = resourceBuilder.build();
36-
26+
export const getEoaAuthContext = (params: GetEoaAuthContextParams) => {
3727
return {
38-
pkpPublicKey,
28+
pkpPublicKey: params.identity.pkpPublicKey,
3929
chain: 'ethereum',
40-
resourceAbilityRequests: resources || resourceRequests,
30+
resourceAbilityRequests: params.resources,
4131
authNeededCallback: async ({
4232
uri,
4333
expiration,
@@ -59,20 +49,20 @@ export const getEoaAuthContext = ({
5949
uri: uri,
6050
expiration: expiration,
6151
resources: resourceAbilityRequests,
62-
walletAddress: signerAddress,
63-
nonce: await litNodeClient.getLatestBlockhash(),
64-
litNodeClient: litNodeClient,
52+
walletAddress: params.identity.signerAddress,
53+
nonce: await params.litNodeClient.getLatestBlockhash(),
54+
litNodeClient: params.litNodeClient,
6555
});
6656

6757
const authSig = await generateAuthSig({
68-
signer: signer,
58+
signer: params.identity.signer,
6959
toSign,
7060
});
7161

7262
return authSig;
7363
},
74-
...(capabilityAuthSigs && {
75-
capabilityAuthSigs: [...capabilityAuthSigs],
64+
...(params.capabilityAuthSigs && {
65+
capabilityAuthSigs: [...params.capabilityAuthSigs],
7666
}),
7767
};
7868
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { LitNodeClient } from '@lit-protocol/lit-node-client';
2+
import {
3+
AuthMethod,
4+
AuthSig,
5+
AuthenticationContext,
6+
LitResourceAbilityRequest,
7+
} from '@lit-protocol/types';
8+
import { Hex } from 'viem';
9+
10+
/**
11+
* Interface for parameters required to get the native auth context.
12+
*/
13+
interface GetNativeAuthContextParams {
14+
litNodeClient: LitNodeClient;
15+
identity: {
16+
pkpPublicKey: Hex;
17+
authMethods: AuthMethod[];
18+
};
19+
resources: LitResourceAbilityRequest[];
20+
capabilityAuthSigs?: AuthSig[];
21+
expiration?: string;
22+
}
23+
24+
/**
25+
* Get the auth context for a Lit supported native auth method (eg. WebAuthn, Discord, Google).
26+
* This context is needed for requesting session signatures with PKP-based authentication.
27+
*
28+
* @param {GetNativeAuthContextParams} params - Parameters for getting the native auth context.
29+
* @returns {AuthenticationContext} The authentication context object.
30+
*/
31+
export const getPkpAuthContext = (
32+
params: GetNativeAuthContextParams
33+
): AuthenticationContext => {
34+
const authContext = params.litNodeClient.getPkpAuthContext({
35+
pkpPublicKey: params.identity.pkpPublicKey,
36+
authMethods: params.identity.authMethods,
37+
expiration: params.expiration,
38+
resourceAbilityRequests: params.resources,
39+
capabilityAuthSigs: params.capabilityAuthSigs,
40+
});
41+
42+
return authContext;
43+
};
Lines changed: 105 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,132 @@
1-
import { AUTH_METHOD_TYPE_TYPE } from '@lit-protocol/constants';
1+
import { ResourceAbilityRequestBuilder } from '@lit-protocol/auth-helpers';
2+
import { LitNodeClient } from '@lit-protocol/lit-node-client';
23
import {
3-
AuthCallbackParams,
4+
AuthenticationContext,
5+
AuthMethod,
46
AuthSig,
57
LitResourceAbilityRequest,
68
} from '@lit-protocol/types';
9+
import { Hex } from 'viem';
710
import { getEoaAuthContext } from './authContexts/getEoaAuthContext';
8-
import { LitNodeClient } from '@lit-protocol/lit-node-client';
11+
import { getPkpAuthContext } from './authContexts/getPkpAuthContext';
912

10-
export interface AuthenticationContext {
11-
pkpPublicKey: string;
12-
chain: string;
13-
resourceAbilityRequests: LitResourceAbilityRequest[];
14-
authNeededCallback: (params: AuthCallbackParams) => Promise<AuthSig>;
13+
interface BaseAuthContextParams {
14+
pkpPublicKey: Hex;
15+
litNodeClient: LitNodeClient;
1516
capabilityAuthSigs?: AuthSig[];
17+
resources?: LitResourceAbilityRequest[];
1618
}
1719

18-
export interface GetAuthContextParams {
19-
authMethodType: AUTH_METHOD_TYPE_TYPE;
20-
pkpAddress: `0x${string}`;
21-
litNodeClient: LitNodeClient;
20+
interface EthWalletAuthParams extends BaseAuthContextParams {
21+
authMethodType: 'EthWallet';
2222
signer: {
2323
signMessage: (message: any) => Promise<string>;
24-
getAddress?: () => Promise<string>;
24+
address: string | Hex;
2525
};
2626
}
2727

28-
export const getAuthContext = (
28+
interface PkpAuthParams extends BaseAuthContextParams {
29+
authMethodType:
30+
| 'Google'
31+
| 'Discord'
32+
| 'WebAuthn'
33+
| 'StytchEmailFactorOtp'
34+
| 'StytchSmsFactorOtp';
35+
authMethods: AuthMethod[];
36+
}
37+
38+
export type GetAuthContextParams = EthWalletAuthParams | PkpAuthParams;
39+
40+
/**
41+
* Get Auth Context prepares the ingredients for the Auth Context
42+
*/
43+
export const getAuthContext = async (
2944
params: GetAuthContextParams
30-
): AuthenticationContext => {
45+
): Promise<AuthenticationContext> => {
46+
let finalResources = params.resources;
47+
if (!finalResources) {
48+
const resourceBuilder = new ResourceAbilityRequestBuilder();
49+
resourceBuilder.addPKPSigningRequest('*');
50+
finalResources = resourceBuilder.build();
51+
}
52+
53+
const finalCapabilityAuthSigs = [...(params.capabilityAuthSigs ?? [])];
54+
3155
switch (params.authMethodType) {
32-
case 'EthWallet':
56+
case 'EthWallet': {
57+
const { signer } = params;
58+
59+
if (!signer.address) {
60+
throw new Error(
61+
'For EthWallet auth method, signer object must have an `address` property.'
62+
);
63+
}
64+
65+
const signerAddress = signer.address;
66+
const finalSignerAddress = (
67+
signerAddress?.startsWith('0x') ? signerAddress : `0x${signerAddress}`
68+
) as Hex;
69+
3370
return getEoaAuthContext({
3471
litNodeClient: params.litNodeClient,
3572
identity: {
36-
pkpPublicKey: params.pkpAddress,
37-
signer: params.signer,
38-
signerAddress: params.pkpAddress,
73+
signer: signer,
74+
signerAddress: finalSignerAddress,
75+
pkpPublicKey: params.pkpPublicKey,
76+
},
77+
resources: finalResources,
78+
capabilityAuthSigs: finalCapabilityAuthSigs,
79+
});
80+
}
81+
case 'Google':
82+
case 'Discord':
83+
case 'WebAuthn':
84+
case 'StytchEmailFactorOtp':
85+
case 'StytchSmsFactorOtp': {
86+
const { authMethods } = params;
87+
return getPkpAuthContext({
88+
litNodeClient: params.litNodeClient,
89+
identity: {
90+
pkpPublicKey: params.pkpPublicKey,
91+
authMethods: authMethods,
3992
},
93+
resources: finalResources,
94+
capabilityAuthSigs: finalCapabilityAuthSigs,
4095
});
96+
}
4197
default:
42-
throw new Error(`Unsupported auth method type: ${params.authMethodType}`);
98+
throw new Error(
99+
`Unsupported or unhandled auth method type: ${
100+
(params as any).authMethodType
101+
}`
102+
);
43103
}
44104
};
45105

46106
// if (import.meta.main) {
47-
// const litNodeClient = new LitNodeClient({
48-
// litNetwork: 'naga-dev',
49-
// });
50-
// const authContext = getAuthContext({
51-
// authMethodType: 'EthWallet',
52-
// pkpAddress: '0x0000000000000000000000000000000000000000',
53-
// litNodeClient: litNodeClient,
54-
// signer: {
55-
// signMessage: async (message: any) => {
56-
// return '0x0000000000000000000000000000000000000000';
57-
// },
58-
// },
59-
// });
60-
61-
// console.log('authContext', authContext);
107+
// (async () => {
108+
// const litNodeClient = new LitNodeClient({
109+
// litNetwork: 'naga-dev',
110+
// debug: false,
111+
// });
112+
113+
// await litNodeClient.connect();
114+
115+
// const anvilPrivateKey =
116+
// '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d';
117+
118+
// const ethersWallet = new ethers.Wallet(anvilPrivateKey);
119+
// const viemAccount = privateKeyToAccount(anvilPrivateKey);
120+
121+
// const authContext = await getAuthContext({
122+
// authMethodType: 'EthWallet',
123+
// litNodeClient: litNodeClient,
124+
// signer: ethersWallet,
125+
// pkpPublicKey:
126+
// '0x04e5603fe1cc5ce207c12950939738583b599f22a152c3672a4c0eee887d75dd405246ac3ed2430283935a99733eac9520581af9923c0fc04fad1d67d60908ce18',
127+
// });
128+
129+
// console.log('authContext', authContext);
130+
// process.exit();
131+
// })();
62132
// }

0 commit comments

Comments
 (0)