|
1 | | -import { ethers } from 'ethers'; |
| 1 | +import { AUTH_METHOD_TYPE_VALUES, PRODUCT_IDS } from '@lit-protocol/constants'; |
2 | 2 | import { |
3 | | - AuthConfig, |
4 | | - AuthManagerParams, |
5 | | - BaseAuthContext, |
6 | | -} from '../auth-manager'; |
7 | | -import { PkpAuthDepsSchema } from './getPkpAuthContextAdapter'; |
8 | | -import { AUTH_METHOD_TYPE } from '@lit-protocol/constants'; |
| 3 | + AuthData, |
| 4 | + HexPrefixedSchema, |
| 5 | + NodeUrlsSchema, |
| 6 | +} from '@lit-protocol/schemas'; |
| 7 | +import { ethers } from 'ethers'; |
| 8 | +import { z } from 'zod'; |
| 9 | +import { AuthConfigV2 } from '../../authenticators/types'; |
| 10 | +import { AuthManagerParams } from '../auth-manager'; |
| 11 | +import { getCustomAuthContext } from '../authContexts/getCustomAuthContext'; |
| 12 | +import { processResources } from '../utils/processResources'; |
9 | 13 | import { tryGetCachedAuthData } from '../try-getters/tryGetCachedAuthData'; |
10 | 14 |
|
11 | | -export interface ICustomAuthenticator { |
12 | | - new (settings: any): ICustomAuthenticatorInstance; |
13 | | - LIT_ACTION_CODE_BASE64?: string; |
14 | | - LIT_ACTION_IPFS_ID?: string; |
15 | | -} |
16 | | - |
17 | | -interface ICustomAuthenticatorInstance { |
18 | | - // Method to perform external auth and return jsParams for the Lit Action |
19 | | - // Accepts the config object which includes pkpPublicKey and other needed params |
20 | | - authenticate(config: { |
21 | | - pkpPublicKey: string; |
22 | | - [key: string]: any; |
23 | | - }): Promise<Record<string, any> | null>; |
24 | | -} |
| 15 | +export const CustomAuthDepsSchema = z.object({ |
| 16 | + nonce: z.any(), |
| 17 | + currentEpoch: z.any(), |
| 18 | + getSignSessionKey: z.any(), |
| 19 | + nodeUrls: NodeUrlsSchema, |
| 20 | +}); |
25 | 21 |
|
26 | 22 | export async function getCustomAuthContextAdapter( |
27 | 23 | upstreamParams: AuthManagerParams, |
28 | 24 | params: { |
29 | | - authenticator: ICustomAuthenticator; // Use the interface type |
30 | | - settings: Record<string, any>; // For constructor |
31 | | - config: { pkpPublicKey: string; [key: string]: any }; // For authenticate method |
32 | | - authConfig: AuthConfig; // For SIWE/session |
33 | | - litClient: BaseAuthContext<any>['litClient']; |
| 25 | + // authData: AuthData; |
| 26 | + pkpPublicKey: z.infer<typeof HexPrefixedSchema>; |
| 27 | + authConfig: AuthConfigV2; |
| 28 | + litClient: { |
| 29 | + getContext: () => Promise<any>; |
| 30 | + }; |
| 31 | + customAuthParams: { |
| 32 | + litActionCode?: string; |
| 33 | + litActionIpfsId?: string; |
| 34 | + jsParams?: Record<string, any>; |
| 35 | + }; |
34 | 36 | } |
35 | 37 | ) { |
36 | | - // 1. Instantiate the custom authenticator helper using 'settings' |
37 | | - const customAuthHelper = new params.authenticator(params.settings); |
| 38 | + const _resources = processResources(params.authConfig.resources); |
38 | 39 |
|
39 | | - // 2. Call the helper's authenticate method using 'config' |
40 | | - if (!customAuthHelper.authenticate) { |
41 | | - throw new Error("Custom authenticator is missing 'authenticate' method."); |
42 | | - } |
43 | | - // Pass the entire config object to the authenticator's authenticate method |
44 | | - const jsParams = await customAuthHelper.authenticate(params.config); |
45 | | - if (!jsParams) { |
46 | | - throw new Error('Custom authenticator failed to produce jsParams.'); |
47 | | - } |
| 40 | + // TODO: 👇 The plan is to identify if the certain operations could be wrapped inside a single function |
| 41 | + // where different network modules can provide their own implementations. |
48 | 42 |
|
49 | | - // 3. Get the static Lit Action code/ID from the authenticator class |
50 | | - const litActionCode = params.authenticator.LIT_ACTION_CODE_BASE64; |
51 | | - const litActionIpfsId = params.authenticator.LIT_ACTION_IPFS_ID; // Optional |
52 | | - if (!litActionCode && !litActionIpfsId) { |
53 | | - throw new Error( |
54 | | - 'Custom authenticator is missing static LIT_ACTION_CODE_BASE64 or LIT_ACTION_IPFS_ID.' |
55 | | - ); |
56 | | - } |
| 43 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! |
| 44 | + const litClientCtx = await params.litClient.getContext(); |
| 45 | + |
| 46 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! (This can be in both Naga and Datil) |
| 47 | + const latestConnectionInfo = litClientCtx.latestConnectionInfo; |
57 | 48 |
|
58 | | - // 4. Extract pkpPublicKey (already available in params.config) |
59 | | - const pkpPublicKey = params.config.pkpPublicKey; |
| 49 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! (This can only be in Naga) |
| 50 | + const nodePrices = latestConnectionInfo.priceFeedInfo.networkPrices; |
60 | 51 |
|
61 | | - // 5. Get node dependencies, session key etc. |
62 | | - const litClientConfig = PkpAuthDepsSchema.parse({ |
63 | | - nonce: await params.litClient.getLatestBlockhash(), |
64 | | - currentEpoch: await params.litClient.getCurrentEpoch(), |
65 | | - getSignSessionKey: params.litClient.getSignSessionKey, |
66 | | - nodeUrls: await params.litClient.getMaxPricesForNodeProduct({ |
67 | | - product: 'LIT_ACTION', // Or appropriate product |
| 52 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! (This can be in both Naga and Datil) |
| 53 | + const handshakeResult = litClientCtx.handshakeResult; |
| 54 | + |
| 55 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! (This can be in both Naga and Datil) |
| 56 | + const threshold = handshakeResult.threshold; |
| 57 | + |
| 58 | + // TODO: ❗️THIS IS NOT TYPED - we have to fix this! (This can only be in Naga) |
| 59 | + const nodeUrls = litClientCtx.getMaxPricesForNodeProduct({ |
| 60 | + nodePrices: nodePrices, |
| 61 | + userMaxPrice: litClientCtx.getUserMaxPrice({ |
| 62 | + product: 'LIT_ACTION', |
68 | 63 | }), |
| 64 | + productId: PRODUCT_IDS['LIT_ACTION'], |
| 65 | + numRequiredNodes: threshold, |
69 | 66 | }); |
70 | | - const pkpAddress = ethers.utils.computeAddress(pkpPublicKey); |
71 | | - const authData = await tryGetCachedAuthData({ |
| 67 | + |
| 68 | + const pkpAddress = ethers.utils.computeAddress(params.pkpPublicKey); |
| 69 | + |
| 70 | + const litAuthData = await tryGetCachedAuthData({ |
72 | 71 | storage: upstreamParams.storage, |
73 | 72 | address: pkpAddress, |
74 | | - expiration: params.authConfig.expiration, |
75 | | - type: AUTH_METHOD_TYPE.LitAction, // Session type remains LitAction |
| 73 | + expiration: params.authConfig.expiration!, |
| 74 | + type: 'custom' as unknown as AUTH_METHOD_TYPE_VALUES, |
76 | 75 | }); |
77 | 76 |
|
78 | | - // 6. Prepare the request body for the node signing function |
79 | | - const requestBodyForCustomAuth = { |
80 | | - sessionKey: authData.sessionKey.keyPair.publicKey, |
81 | | - pkpPublicKey: pkpPublicKey, |
82 | | - statement: params.authConfig.statement, |
83 | | - domain: params.authConfig.domain, |
84 | | - expiration: params.authConfig.expiration, |
85 | | - resources: params.authConfig.resources, |
86 | | - uri: authData.sessionKey.keyPair.publicKey, |
87 | | - nonce: litClientConfig.nonce, |
88 | | - ...(litActionCode && { code: litActionCode }), |
89 | | - ...(litActionIpfsId && { litActionIpfsId: litActionIpfsId }), |
90 | | - jsParams: jsParams, // Use the result from customAuthHelper.authenticate |
91 | | - authMethods: [], |
92 | | - epoch: litClientConfig.currentEpoch, |
93 | | - // ... other fields like curveType, signingScheme ... |
94 | | - }; |
95 | | - |
96 | | - // 7. Return the auth context object |
97 | | - return { |
98 | | - chain: 'ethereum', |
99 | | - pkpPublicKey: pkpPublicKey, |
100 | | - resources: params.authConfig.resources, |
101 | | - capabilityAuthSigs: params.authConfig.capabilityAuthSigs, |
102 | | - expiration: params.authConfig.expiration, |
103 | | - authNeededCallback: async () => { |
104 | | - const authSig = await litClientConfig.getSignSessionKey({ |
105 | | - requestBody: requestBodyForCustomAuth, |
106 | | - nodeUrls: litClientConfig.nodeUrls.map((node: any) => node.url), |
107 | | - }); |
108 | | - return authSig; |
| 77 | + return getCustomAuthContext({ |
| 78 | + authentication: { |
| 79 | + pkpPublicKey: params.pkpPublicKey, |
| 80 | + // authData: {} as any, |
109 | 81 | }, |
110 | | - }; |
| 82 | + authConfig: { |
| 83 | + domain: params.authConfig.domain!, |
| 84 | + resources: _resources, |
| 85 | + capabilityAuthSigs: params.authConfig.capabilityAuthSigs!, |
| 86 | + expiration: params.authConfig.expiration!, |
| 87 | + statement: params.authConfig.statement!, |
| 88 | + }, |
| 89 | + customParams: { |
| 90 | + litActionCode: params.customAuthParams.litActionCode, |
| 91 | + litActionIpfsId: params.customAuthParams.litActionIpfsId, |
| 92 | + jsParams: params.customAuthParams.jsParams, |
| 93 | + }, |
| 94 | + deps: { |
| 95 | + litAuthData: litAuthData, |
| 96 | + connection: { |
| 97 | + nonce: litClientCtx.latestBlockhash, |
| 98 | + currentEpoch: |
| 99 | + litClientCtx.latestConnectionInfo.epochState.currentNumber, |
| 100 | + nodeUrls: nodeUrls, |
| 101 | + }, |
| 102 | + signCustomSessionKey: litClientCtx.signCustomSessionKey, |
| 103 | + storage: upstreamParams.storage, |
| 104 | + pkpAddress: pkpAddress, |
| 105 | + }, |
| 106 | + }); |
111 | 107 | } |
0 commit comments