|
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'; |
2 | 3 | import { |
3 | | - AuthCallbackParams, |
| 4 | + AuthenticationContext, |
| 5 | + AuthMethod, |
4 | 6 | AuthSig, |
5 | 7 | LitResourceAbilityRequest, |
6 | 8 | } from '@lit-protocol/types'; |
| 9 | +import { Hex } from 'viem'; |
7 | 10 | import { getEoaAuthContext } from './authContexts/getEoaAuthContext'; |
8 | | -import { LitNodeClient } from '@lit-protocol/lit-node-client'; |
| 11 | +import { getPkpAuthContext } from './authContexts/getPkpAuthContext'; |
9 | 12 |
|
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; |
15 | 16 | capabilityAuthSigs?: AuthSig[]; |
| 17 | + resources?: LitResourceAbilityRequest[]; |
16 | 18 | } |
17 | 19 |
|
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'; |
22 | 22 | signer: { |
23 | 23 | signMessage: (message: any) => Promise<string>; |
24 | | - getAddress?: () => Promise<string>; |
| 24 | + address: string | Hex; |
25 | 25 | }; |
26 | 26 | } |
27 | 27 |
|
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 ( |
29 | 44 | 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 | + |
31 | 55 | 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 | + |
33 | 70 | return getEoaAuthContext({ |
34 | 71 | litNodeClient: params.litNodeClient, |
35 | 72 | 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, |
39 | 92 | }, |
| 93 | + resources: finalResources, |
| 94 | + capabilityAuthSigs: finalCapabilityAuthSigs, |
40 | 95 | }); |
| 96 | + } |
41 | 97 | 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 | + ); |
43 | 103 | } |
44 | 104 | }; |
45 | 105 |
|
46 | 106 | // 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 | +// })(); |
62 | 132 | // } |
0 commit comments