|
| 1 | +import { development, FrameVerifySignatureResult, LensClient, production } from '@lens-protocol/client'; |
| 2 | + |
| 3 | +import { LensFrameOptions, LensFrameRequest, LensFrameResponse } from '@/types/lens'; |
| 4 | + |
| 5 | +type FrameRequest = LensFrameRequest & { |
| 6 | + untrustedData: { |
| 7 | + idToken?: string; |
| 8 | + }; |
| 9 | +}; |
| 10 | + |
| 11 | +export async function getLensFrameMessage( |
| 12 | + frameActionPayload: FrameRequest, |
| 13 | + options?: LensFrameOptions, |
| 14 | +): Promise< |
| 15 | + LensFrameResponse & { |
| 16 | + walletAddress(): Promise<string | undefined>; |
| 17 | + } |
| 18 | +> { |
| 19 | + const lensClientEnvironment = options?.environment === 'development' ? development : production; |
| 20 | + |
| 21 | + const lensClient = new LensClient({ |
| 22 | + environment: lensClientEnvironment, |
| 23 | + }); |
| 24 | + |
| 25 | + const { |
| 26 | + url, |
| 27 | + inputText, |
| 28 | + state, |
| 29 | + buttonIndex, |
| 30 | + actionResponse, |
| 31 | + profileId, |
| 32 | + pubId, |
| 33 | + specVersion, |
| 34 | + deadline, |
| 35 | + identityToken, |
| 36 | + idToken, |
| 37 | + } = frameActionPayload.untrustedData; |
| 38 | + |
| 39 | + const typedData = await lensClient.frames.createFrameTypedData({ |
| 40 | + url, |
| 41 | + inputText, |
| 42 | + state, |
| 43 | + buttonIndex, |
| 44 | + actionResponse, |
| 45 | + profileId, |
| 46 | + pubId, |
| 47 | + specVersion, |
| 48 | + deadline, |
| 49 | + }); |
| 50 | + |
| 51 | + const response = await lensClient.frames.verifyFrameSignature({ |
| 52 | + identityToken: identityToken || idToken || '', |
| 53 | + signature: frameActionPayload.trustedData.messageBytes, |
| 54 | + signedTypedData: typedData, |
| 55 | + }); |
| 56 | + |
| 57 | + return { |
| 58 | + ...typedData.value, |
| 59 | + isValid: response === FrameVerifySignatureResult.Verified, |
| 60 | + async walletAddress() { |
| 61 | + const profile = await lensClient.profile.fetch({ |
| 62 | + forProfileId: typedData.value.profileId, |
| 63 | + }); |
| 64 | + |
| 65 | + return profile?.ownedBy.address; |
| 66 | + }, |
| 67 | + }; |
| 68 | +} |
0 commit comments