|
| 1 | +import { prisma } from '../prisma.js'; |
| 2 | + |
| 3 | +type Params = |
| 4 | + | { |
| 5 | + accountAddress: string; |
| 6 | + signaturePublicKey: string; |
| 7 | + spaceId?: string; |
| 8 | + } |
| 9 | + | { |
| 10 | + accountAddress: string; |
| 11 | + appId: string; |
| 12 | + spaceId?: string; |
| 13 | + }; |
| 14 | + |
| 15 | +export type GetIdentityResult = { |
| 16 | + accountAddress: string; |
| 17 | + ciphertext: string; |
| 18 | + nonce: string; |
| 19 | + signaturePublicKey: string; |
| 20 | + encryptionPublicKey: string; |
| 21 | + accountProof: string; |
| 22 | + keyProof: string; |
| 23 | + appId: string | null; |
| 24 | +}; |
| 25 | + |
| 26 | +export const getAppOrConnectIdentity = async (params: Params): Promise<GetIdentityResult> => { |
| 27 | + if (!('appId' in params)) { |
| 28 | + const where: { address: string; connectSignaturePublicKey?: string } = { address: params.accountAddress }; |
| 29 | + if ('signaturePublicKey' in params) { |
| 30 | + where.connectSignaturePublicKey = params.signaturePublicKey; |
| 31 | + } |
| 32 | + const account = await prisma.account.findFirst({ |
| 33 | + where, |
| 34 | + }); |
| 35 | + if (account) { |
| 36 | + return { |
| 37 | + accountAddress: account.address, |
| 38 | + ciphertext: account.connectCiphertext, |
| 39 | + nonce: account.connectNonce, |
| 40 | + signaturePublicKey: account.connectSignaturePublicKey, |
| 41 | + encryptionPublicKey: account.connectEncryptionPublicKey, |
| 42 | + accountProof: account.connectAccountProof, |
| 43 | + keyProof: account.connectKeyProof, |
| 44 | + appId: null, |
| 45 | + }; |
| 46 | + } |
| 47 | + } |
| 48 | + const appWhere: { |
| 49 | + accountAddress: string; |
| 50 | + appId?: string; |
| 51 | + signaturePublicKey?: string; |
| 52 | + spaces?: { some: { id: string } }; |
| 53 | + } = { |
| 54 | + accountAddress: params.accountAddress, |
| 55 | + }; |
| 56 | + if ('signaturePublicKey' in params) { |
| 57 | + appWhere.signaturePublicKey = params.signaturePublicKey; |
| 58 | + } |
| 59 | + if ('appId' in params) { |
| 60 | + appWhere.appId = params.appId; |
| 61 | + } |
| 62 | + if (params.spaceId) { |
| 63 | + appWhere.spaces = { some: { id: params.spaceId } }; |
| 64 | + } |
| 65 | + |
| 66 | + const appIdentity = await prisma.appIdentity.findFirst({ |
| 67 | + where: appWhere, |
| 68 | + }); |
| 69 | + if (appIdentity) { |
| 70 | + return { |
| 71 | + accountAddress: appIdentity.accountAddress, |
| 72 | + ciphertext: appIdentity.ciphertext, |
| 73 | + nonce: appIdentity.nonce, |
| 74 | + signaturePublicKey: appIdentity.signaturePublicKey, |
| 75 | + encryptionPublicKey: appIdentity.encryptionPublicKey, |
| 76 | + accountProof: appIdentity.accountProof, |
| 77 | + keyProof: appIdentity.keyProof, |
| 78 | + appId: appIdentity.appId, |
| 79 | + }; |
| 80 | + } |
| 81 | + throw new Error('Identity not found'); |
| 82 | +}; |
0 commit comments