|
| 1 | +import { createAuthManager, storagePlugins } from '@lit-protocol/auth'; |
| 2 | +import { hexToBigInt, keccak256, toBytes } from 'viem'; |
| 3 | +import { init } from './init'; |
| 4 | +export const customAuthFlow = async () => { |
| 5 | + const { myAccount, litClient } = await init(); |
| 6 | + |
| 7 | + // ========== Providing PKP for your user ========== |
| 8 | + |
| 9 | + // Imagine you are an existing site owner, and you want to provide PKPs for your users. |
| 10 | + class myDappBackend { |
| 11 | + // Create a unique secret name of your dApp (you can share it if you want to share the authMethodType) |
| 12 | + uniqueDappName: string = 'my-supa-dupa-app-name'; |
| 13 | + |
| 14 | + // [❗️REQUIRED] a very big unique number for your unique dApp name |
| 15 | + // This will be used to generate a unique authData for each user |
| 16 | + // and be used to validate inside the Lit Action / immutable javascript. |
| 17 | + uniqueAuthMethodType: bigint = hexToBigInt( |
| 18 | + keccak256(toBytes(this.uniqueDappName)) |
| 19 | + ); |
| 20 | + |
| 21 | + // [❗️REQUIRED] You will need to know the hexed version of the unique authMethodType |
| 22 | + // to be compared against with on Lit Action |
| 23 | + // ❗️❗️You will probably only need to know this value once and hardcode it |
| 24 | + // on the Lit Action validation code. |
| 25 | + // eg. permittedAuthMethod["auth_method_type"] === "0x15f85" |
| 26 | + hexedUniqueAuthMethodType = keccak256(toBytes(this.uniqueDappName)); |
| 27 | + |
| 28 | + // [❗️REQUIRED] Validation IPFS CID |
| 29 | + // https://explorer.litprotocol.com/ipfs/QmYLeVmwJPVs7Uebk85YdVPivMyrvoeKR6X37kyVRZUXW4 |
| 30 | + public static validationIpfsCid = |
| 31 | + 'QmYLeVmwJPVs7Uebk85YdVPivMyrvoeKR6X37kyVRZUXW4'; |
| 32 | + |
| 33 | + // [DEMO] Not a very safe database of registered users |
| 34 | + public registeredUsers: Array<{ |
| 35 | + userId: string; |
| 36 | + password: string; |
| 37 | + pkpPublicKey: string | null; |
| 38 | + }> = [ |
| 39 | + { userId: 'alice', password: 'password-1', pkpPublicKey: null }, |
| 40 | + { userId: 'bob', password: 'password-2', pkpPublicKey: null }, |
| 41 | + ]; |
| 42 | + |
| 43 | + printSiteInfo() { |
| 44 | + console.log( |
| 45 | + `✍️ Unique Auth Method Type: ${this.hexedUniqueAuthMethodType}` |
| 46 | + ); |
| 47 | + console.log('🔐 validationIpfsCid:', myDappBackend.validationIpfsCid); |
| 48 | + } |
| 49 | + |
| 50 | + // [❗️REQUIRED] Generate a unique auth data for each user |
| 51 | + // Customise this to your needs. |
| 52 | + private _generateAuthData(userId: string) { |
| 53 | + const uniqueUserId = `${this.uniqueDappName}-${userId}`; |
| 54 | + |
| 55 | + return { |
| 56 | + authMethodType: this.uniqueAuthMethodType, |
| 57 | + authMethodId: keccak256(toBytes(uniqueUserId)), |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + async mintPKPForUser(userId: string) { |
| 62 | + // 1. Check if the user is registered |
| 63 | + if (!this.registeredUsers.find((user) => user.userId === userId)) { |
| 64 | + throw new Error('User not registered'); |
| 65 | + } |
| 66 | + |
| 67 | + // 2. Generate the auth data from the unique user id |
| 68 | + const uniqueUserAuthData = this._generateAuthData(userId); |
| 69 | + console.log('✅ uniqueUserAuthData:', uniqueUserAuthData); |
| 70 | + |
| 71 | + // 3. Mint a PKP for the user. Then, we will send the PKP to itself, since itself is also |
| 72 | + // a valid ETH Wallet. The owner of the PKP will have NO permissions. To access the PKP, |
| 73 | + // the user will need to pass the immutable validation code which lives inside the Lit Action. |
| 74 | + const { pkpData: mintedPKP, validationIpfsCid } = |
| 75 | + await litClient.mintWithCustomAuth({ |
| 76 | + account: myAccount, |
| 77 | + authData: uniqueUserAuthData, |
| 78 | + scope: 'sign-anything', |
| 79 | + validationIpfsCid: myDappBackend.validationIpfsCid, |
| 80 | + }); |
| 81 | + |
| 82 | + console.log('✅ validationIpfsCid:', validationIpfsCid); |
| 83 | + console.log('✅ mintedPKP:', mintedPKP); |
| 84 | + console.log( |
| 85 | + '✅ hexedUniqueAuthMethodType:', |
| 86 | + this.hexedUniqueAuthMethodType |
| 87 | + ); |
| 88 | + |
| 89 | + // find the user first |
| 90 | + const user = this.registeredUsers.find((user) => user.userId === userId); |
| 91 | + if (!user) { |
| 92 | + throw new Error('User not found'); |
| 93 | + } |
| 94 | + |
| 95 | + // update the user with the PKP public key |
| 96 | + user.pkpPublicKey = mintedPKP.data.pubkey; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + class myDappFrontend { |
| 101 | + constructor(private readonly myDappBackend: myDappBackend) { |
| 102 | + this.myDappBackend = myDappBackend; |
| 103 | + } |
| 104 | + |
| 105 | + userDashboard(userId: string) { |
| 106 | + const user = this.myDappBackend.registeredUsers.find( |
| 107 | + (user) => user.userId === userId |
| 108 | + ); |
| 109 | + const uniqueAuthMethodId = `${this.myDappBackend.uniqueDappName}-${userId}`; |
| 110 | + |
| 111 | + if (!user) { |
| 112 | + throw new Error('User not found'); |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + getMyPkpPublicKey() { |
| 117 | + return user.pkpPublicKey; |
| 118 | + }, |
| 119 | + |
| 120 | + getAuthMethodId() { |
| 121 | + return keccak256(toBytes(uniqueAuthMethodId)); |
| 122 | + }, |
| 123 | + |
| 124 | + // Ideally, as the site owner you will publish this to the public |
| 125 | + // so they can see the validation logic. |
| 126 | + getValidationIpfsCid() { |
| 127 | + return myDappBackend.validationIpfsCid; |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // ========== As the site owner ========== |
| 134 | + const ownerDapp = new myDappBackend(); |
| 135 | + ownerDapp.printSiteInfo(); |
| 136 | + await ownerDapp.mintPKPForUser('alice'); |
| 137 | + |
| 138 | + // ========== As a user ========== |
| 139 | + const frontend = new myDappFrontend(ownerDapp); |
| 140 | + // Then as a user, first i will want to get the PKP public key if i don't have it already. |
| 141 | + // then i will also need to know the validation IPFS CID. |
| 142 | + const userDashboard = frontend.userDashboard('alice'); |
| 143 | + const userPkpPublicKey = userDashboard.getMyPkpPublicKey(); |
| 144 | + const dAppValidationIpfsCid = userDashboard.getValidationIpfsCid(); |
| 145 | + const authMethodId = userDashboard.getAuthMethodId(); |
| 146 | + |
| 147 | + console.log('✅ userPkpPublicKey:', userPkpPublicKey); |
| 148 | + |
| 149 | + // Then, in order to sign with the PKP the site owner minted for you, you will need to get the authContext from the authManager |
| 150 | + const userAuthManager = createAuthManager({ |
| 151 | + // on browser, use browser storage plugin by default |
| 152 | + storage: storagePlugins.localStorageNode({ |
| 153 | + appName: 'my-app', // namespace for isolating auth data |
| 154 | + networkName: 'naga-dev', // useful for distinguishing environments |
| 155 | + storagePath: './lit-auth-storage', // file path for storing session data |
| 156 | + }), |
| 157 | + }); |
| 158 | + const userAuthContext = await userAuthManager.createCustomAuthContext({ |
| 159 | + pkpPublicKey: userPkpPublicKey!, |
| 160 | + authConfig: { |
| 161 | + resources: [ |
| 162 | + ['pkp-signing', '*'], |
| 163 | + ['lit-action-execution', '*'], |
| 164 | + ], |
| 165 | + expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), |
| 166 | + }, |
| 167 | + litClient: litClient, |
| 168 | + customAuthParams: { |
| 169 | + litActionIpfsId: dAppValidationIpfsCid, |
| 170 | + jsParams: { |
| 171 | + pkpPublicKey: userPkpPublicKey, |
| 172 | + username: 'alice', |
| 173 | + password: 'lit', |
| 174 | + authMethodId: authMethodId, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }); |
| 178 | + |
| 179 | + console.log('✅ userAuthContext:', userAuthContext); |
| 180 | + |
| 181 | + // Finally, the user can sign with the PKP |
| 182 | + const userSignRes = await litClient.chain.ethereum.pkpSign({ |
| 183 | + pubKey: userPkpPublicKey!, |
| 184 | + toSign: 'hello', |
| 185 | + authContext: userAuthContext, |
| 186 | + }); |
| 187 | + |
| 188 | + console.log('✅ userSignRes:', userSignRes); |
| 189 | +}; |
0 commit comments