|
| 1 | +import { recoverMessageAddress } from 'viem'; |
| 2 | +import { User } from '.prisma/client'; |
| 3 | +import { |
| 4 | + getBuilderPlaceByCollaboratorAddressAndId, |
| 5 | + getBuilderPlaceByOwnerTlIdAndId, |
| 6 | +} from '../../modules/BuilderPlace/actions/builderPlace'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Checks if the signature is from a BuilderPlace collaborator by getting the BuilderPlace |
| 10 | + * by the collaborator address & BuilderPlace Database id |
| 11 | + * @param id: Database BuilderPlace id |
| 12 | + * @param signature: Signature to verify |
| 13 | + */ |
| 14 | +export const checkSignature = async (id: string, signature: `0x${string}` | Uint8Array) => { |
| 15 | + try { |
| 16 | + const address = await recoverMessageAddress({ |
| 17 | + message: id, |
| 18 | + signature: signature, |
| 19 | + }); |
| 20 | + |
| 21 | + const builderPlace = await getBuilderPlaceByCollaboratorAddressAndId( |
| 22 | + address.toLocaleLowerCase(), |
| 23 | + id, |
| 24 | + ); |
| 25 | + |
| 26 | + if (!builderPlace) { |
| 27 | + return Response.json({ error: 'No BuilderPlace found' }, { status: 400 }); |
| 28 | + } |
| 29 | + |
| 30 | + return { builderPlace, address }; |
| 31 | + } catch (error) { |
| 32 | + console.error('Error in checkSignature:', error); |
| 33 | + |
| 34 | + return Response.json( |
| 35 | + { error: 'An error occurred while verifying the signature' }, |
| 36 | + { status: 500 }, |
| 37 | + ); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Checks if the signature is from the BuilderPlace owner by getting the BuilderPlace |
| 43 | + * by the owner DataBase id & BuilderPlace Database id |
| 44 | + * @param builderPlaceId: Database BuilderPlace id |
| 45 | + * @param ownerId: Database BuilderPlace owner id |
| 46 | + * @param signature: Signature to verify |
| 47 | + * @param address: Address to verify |
| 48 | + * @throws 401 if the signature is not from the BuilderPlace owner |
| 49 | + */ |
| 50 | +export const checkOwnerSignature = async ( |
| 51 | + builderPlaceId: string, |
| 52 | + ownerId: string, |
| 53 | + signature: `0x${string}` | Uint8Array, |
| 54 | + address: `0x${string}`, |
| 55 | +) => { |
| 56 | + try { |
| 57 | + const signatureAddress = await recoverMessageAddress({ |
| 58 | + message: `connect with ${address}`, |
| 59 | + signature: signature, |
| 60 | + }); |
| 61 | + |
| 62 | + const builderPlace = await getBuilderPlaceByOwnerTlIdAndId(ownerId, builderPlaceId); |
| 63 | + |
| 64 | + /** |
| 65 | + * Check whether the signature is from the BuilderPlace owner |
| 66 | + */ |
| 67 | + if ( |
| 68 | + builderPlace && |
| 69 | + signatureAddress.toLocaleLowerCase() !== builderPlace?.owner?.address?.toLocaleLowerCase() |
| 70 | + ) { |
| 71 | + return Response.json({ error: 'Not BuilderPlace owner' }, { status: 401 }); |
| 72 | + } |
| 73 | + |
| 74 | + return { builderPlace, address: signatureAddress }; |
| 75 | + } catch (error) { |
| 76 | + console.error('Error in checkSignature:', error); |
| 77 | + |
| 78 | + return Response.json( |
| 79 | + { error: 'An error occurred while verifying the signature' }, |
| 80 | + { status: 500 }, |
| 81 | + ); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +export const isCollaboratorExists = ( |
| 86 | + collaborators: User[] = [], |
| 87 | + newCollaboratorAddress: string, |
| 88 | +): boolean => { |
| 89 | + console.log( |
| 90 | + 'collaborators', |
| 91 | + collaborators.map(collaborator => { |
| 92 | + console.log(collaborator.address); |
| 93 | + }), |
| 94 | + ); |
| 95 | + console.log('newCollaboratorAddress', newCollaboratorAddress); |
| 96 | + return collaborators.some( |
| 97 | + collaborator => |
| 98 | + collaborator?.address?.toLocaleLowerCase() === newCollaboratorAddress.toLocaleLowerCase(), |
| 99 | + ); |
| 100 | +}; |
0 commit comments