|
| 1 | +import { z } from "zod"; |
| 2 | +import { DEPLOYMENTS } from "../../constants"; |
| 3 | +import { ZodSchemaValidator } from "../base/SchemaValidator"; |
| 4 | +import { isHypercertToken } from "src/utils/tokenIds"; |
| 5 | + |
| 6 | +const AttestationSchema = z |
| 7 | + .object({ |
| 8 | + chain_id: z.coerce.bigint(), |
| 9 | + contract_address: z.string(), |
| 10 | + token_id: z.coerce.bigint(), |
| 11 | + }) |
| 12 | + .passthrough() |
| 13 | + .refine( |
| 14 | + (data) => { |
| 15 | + return Number(data.chain_id) in DEPLOYMENTS; |
| 16 | + }, |
| 17 | + (data) => ({ |
| 18 | + code: "INVALID_CHAIN_ID", |
| 19 | + message: `Chain ID ${data.chain_id.toString()} is not supported`, |
| 20 | + path: ["chain_id"], |
| 21 | + }), |
| 22 | + ) |
| 23 | + .refine( |
| 24 | + (data) => { |
| 25 | + const deployment = DEPLOYMENTS[Number(data.chain_id) as keyof typeof DEPLOYMENTS]; |
| 26 | + if (!deployment?.addresses) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + const knownAddresses = Object.values(deployment.addresses).map((addr) => addr.toLowerCase()); |
| 30 | + return knownAddresses.includes(data.contract_address.toLowerCase()); |
| 31 | + }, |
| 32 | + (data) => ({ |
| 33 | + code: "INVALID_CONTRACT_ADDRESS", |
| 34 | + message: `Contract address ${data.contract_address} is not deployed on chain ${data.chain_id.toString()}`, |
| 35 | + path: ["contract_address"], |
| 36 | + }), |
| 37 | + ) |
| 38 | + .refine( |
| 39 | + (data) => { |
| 40 | + return isHypercertToken(data.token_id); |
| 41 | + }, |
| 42 | + (data) => ({ |
| 43 | + code: "INVALID_TOKEN_ID", |
| 44 | + message: `Token ID ${data.token_id.toString()} is not a valid hypercert token`, |
| 45 | + path: ["token_id"], |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | +type AttestationData = z.infer<typeof AttestationSchema>; |
| 50 | + |
| 51 | +// Example raw attestation |
| 52 | + |
| 53 | +// { |
| 54 | +// "uid": "0x4f923f7485e013d3c64b55268304c0773bb84d150b4289059c77af0e28aea3f6", |
| 55 | +// "data": "0x000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000822f17a9a5eecfd66dbaff7946a8071c265d1d0700000000000000000000000000009c0900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b5a757a616c752032303233000000000000000000000000000000000000000000", |
| 56 | +// "time": 1727969021, |
| 57 | +// "refUID": "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 58 | +// "schema": "0x2f4f575d5df78ac52e8b124c4c900ec4c540f1d44f5b8825fac0af5308c91449", |
| 59 | +// "attester": "0x676703E18b2d03Aa36d6A3124B4F58716dBf61dB", |
| 60 | +// "recipient": "0x0000000000000000000000000000000000000000", |
| 61 | +// "revocable": false, |
| 62 | +// "expirationTime": 0, |
| 63 | +// "revocationTime": 0 |
| 64 | +// } |
| 65 | + |
| 66 | +// Example decoded attestation data |
| 67 | + |
| 68 | +// { |
| 69 | +// "tags": [ |
| 70 | +// "Zuzalu 2023" |
| 71 | +// ], |
| 72 | +// "chain_id": 10, |
| 73 | +// "comments": "", |
| 74 | +// "token_id": 1.3592579146656887e+43, |
| 75 | +// "evaluate_work": 1, |
| 76 | +// "evaluate_basic": 1, |
| 77 | +// "contract_address": "0x822F17A9A5EeCFd66dBAFf7946a8071C265D1d07", |
| 78 | +// "evaluate_properties": 1, |
| 79 | +// "evaluate_contributors": 1 |
| 80 | +// } |
| 81 | + |
| 82 | +// Example raw attestation data |
| 83 | + |
| 84 | +// { |
| 85 | +// "uid": "0xc6b717cfbf9df516c0cbdc670fdd7d098ae0a7d30b2fb2c1ff7bd15a822bf1f4", |
| 86 | +// "data": "0x0000000000000000000000000000000000000000000000000000000000aa36a7000000000000000000000000a16dfb32eb140a6f3f2ac68f41dad8c7e83c4941000000000000000000000000000002580000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000001e54657374696e67206164646974696f6e616c206174746573746174696f6e0000000000000000000000000000000000000000000000000000000000000000000877757575757575740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000003a7b2274797065223a2275726c222c22737263223a2268747470733a2f2f7078686572652e636f6d2f656e2f70686f746f2f31333833373237227d00000000000000000000000000000000000000000000000000000000000000000000000000b27b2274797065223a22696d6167652f6a706567222c226e616d65223a22676f61745f62726f776e5f616e696d616c5f6e61747572655f62696c6c795f676f61745f6d616d6d616c5f63726561747572655f686f726e732d3635363235322d313533313531373336392e6a7067222c22737263223a226261666b72656964676d613237367a326d756178717a79797467676979647437627a617073736479786b7333376737736f37377372347977776775227d0000000000000000000000000000", |
| 87 | +// "time": 1737648084, |
| 88 | +// "refUID": "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 89 | +// "schema": "0x48e3e1be1e08084b408a7035ac889f2a840b440bbf10758d14fb722831a200c3", |
| 90 | +// "attester": "0xdf2C3dacE6F31e650FD03B8Ff72beE82Cb1C199A", |
| 91 | +// "recipient": "0x0000000000000000000000000000000000000000", |
| 92 | +// "revocable": false, |
| 93 | +// "expirationTime": 0, |
| 94 | +// "revocationTime": 0 |
| 95 | +// } |
| 96 | + |
| 97 | +// Example decoded attestation data |
| 98 | + |
| 99 | +// { |
| 100 | +// "title": "Testing additional attestation", |
| 101 | +// "sources": [ |
| 102 | +// "{\"type\":\"url\",\"src\":\"https://pxhere.com/en/photo/1383727\"}", |
| 103 | +// "{\"type\":\"image/jpeg\",\"name\":\"goat_brown_animal_nature_billy_goat_mammal_creature_horns-656252-1531517369.jpg\",\"src\":\"bafkreidgma276z2muaxqzyytggiydt7bzapssdyxks37g7so77sr4ywwgu\"}" |
| 104 | +// ], |
| 105 | +// "chain_id": 11155111, |
| 106 | +// "token_id": 2.0416942015256308e+41, |
| 107 | +// "description": "wuuuuuut", |
| 108 | +// "contract_address": "0xa16DFb32Eb140a6f3F2AC68f41dAd8c7e83C4941" |
| 109 | +// } |
| 110 | + |
| 111 | +export class AttestationValidator extends ZodSchemaValidator<AttestationData> { |
| 112 | + constructor() { |
| 113 | + super(AttestationSchema); |
| 114 | + } |
| 115 | +} |
0 commit comments