export type AIInferenceResult = { aiGenerated: boolean; aiProbability: number; humanProbability: number; }; export const C2PASourceTypeScreenCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/screenCapture"; export const C2PASourceTypeDigitalCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture"; export const C2PASourceTypeComputationalCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/computationalCapture"; export const C2PASourceTypeCompositeCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/compositeCapture"; export const C2PASourceTypeTrainedAlgorithmicMedia = "http://cv.iptc.org/newscodes/digitalsourcetype/trainedAlgorithmicMedia"; export const C2PASourceTypeCompositeWithTrainedAlgorithmicMedia = "http://cv.iptc.org/newscodes/digitalsourcetype/compositeWithTrainedAlgorithmicMedia"; export type C2PAActionsAssertion = { actions: { action: string; softwareAgent?: string; digitalSourceType?: string; }[]; }; export type C2PAAssertion = { label: string; data: C2PAActionsAssertion | undefined; }; export type C2PAManifest = { assertions: C2PAAssertion[]; signature_info: { time: string; }; }; export type C2PAValidationResults = { activeManifest?: { failure: any[]; success: any[]; informational: any[]; }; }; export type C2PAManifestInfo = { active_manifest: string; manifests: { [key: string]: C2PAManifest }; validation_results?: C2PAValidationResults; }; export type C2PAData = { manifest_info: C2PAManifestInfo; }; export type Proof = { data?: any; name?: string; json?: string; integrity?: { pgp?: any; c2pa?: C2PAData; exif?: { [key: string]: string | Object }; opentimestamps?: any }; ai?: { inferenceResult?: AIInferenceResult }; }; export type ProofHintFlagsGenerator = "unknown" | "camera" | "screenshot" | "ai"; export type ProofHintFlagSource = "c2pa" | "exif" | "metadata"; //export type ProofHintFlagsEditor = "unknown" | "manual" | "ai"; export type ProofHintFlagsEdit = { editor: string; date?: Date; } export type ProofHintFlags = { device?: string; creationDate?: Date; creationDateSource?: ProofHintFlagSource; generator?: ProofHintFlagsGenerator; generatorSource?: ProofHintFlagSource; edits?: ProofHintFlagsEdit[]; containsC2PA?: boolean; containsEXIF?: boolean; }; type FlagMatchRule = { field: string; match: string[]; description: string; }; type FlagMatchRulePathSegment = { object: any; path: string; } type FlagMatchRuleValue = { path: FlagMatchRulePathSegment[]; value: string | object; }; type FlagMatchInfo = { field: string; value: string; re: string; }; const ruleScreenshotC2PA = (): FlagMatchRule[] => { return [ { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeScreenCapture], description: "Screen capture", }, { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions.v2]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeScreenCapture], description: "Screen capture", }, ]; }; const ruleScreenshotMeta = (): FlagMatchRule[] => { return [ { field: "name", match: ["screenshot"], description: "Screen capture", }, ]; }; const ruleCamera = (): FlagMatchRule[] => { return [ { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeDigitalCapture, C2PASourceTypeComputationalCapture, C2PASourceTypeCompositeCapture], description: "Captured by camera", }, { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions.v2]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeDigitalCapture, C2PASourceTypeComputationalCapture, C2PASourceTypeCompositeCapture], description: "Captured by camera", }, ]; }; const ruleAiGenerated = (): FlagMatchRule[] => { return [ { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeTrainedAlgorithmicMedia, C2PASourceTypeCompositeWithTrainedAlgorithmicMedia], description: "Generated by AI", }, { field: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions.v2]/data/actions[action=c2pa.created]/digitalSourceType", match: [C2PASourceTypeTrainedAlgorithmicMedia, C2PASourceTypeCompositeWithTrainedAlgorithmicMedia], description: "Generated by AI", }, ]; }; const ruleAiMeta = (): FlagMatchRule[] => { const knownAIServices = [ "ChatGPT", "OpenAI-API", "Adobe Firefly", "RunwayML", "Runway AI", "Google AI", "Stable Diffusion", ]; return [ { field: "name", match: ["^DALL_E_", "^Gen-3"], description: "File name" }, { field: "integrity/c2pa/manifest_info/manifests[]/claim_generator", match: knownAIServices, description: "C2PA claim generator", }, { field: "iptc/Credit", match: knownAIServices, description: "IPTC Credit" }, { field: "iptc/Provider", match: knownAIServices, description: "IPTC Provider" }, { field: "iptc/ImageSupplier[]", match: knownAIServices, description: "IPTC ImageSupplier" }, { field: "iptc/ImageCreator[]", match: knownAIServices, description: "IPTC ImageCreator" }, ]; }; const matchFlag = (rules: FlagMatchRule[], path: FlagMatchRulePathSegment[]) => { let result = false; let resultInfo: FlagMatchInfo[] = []; for (let rule of rules) { try { const re: RegExp[] = (!Array.isArray(rule.match) ? [rule.match] : rule.match).map((m) => new RegExp(m, "gi")); const values = extractFlagValues(rule.field, path); values.forEach((v) => { re.forEach((r) => { if (typeof v.value === "string" && r.test(v.value)) { result = true; resultInfo.push({ field: v.path.map((v) => v.path).join("/"), value: v.value, re: r.source }); } }); }); } catch (e) { console.error("Invalid RE", e); } } return { result: result, matches: resultInfo }; }; const extractFlagValues = (flagPath: string, path: FlagMatchRulePathSegment[]): FlagMatchRuleValue[] => { const getValues = ( keys: string[], path: FlagMatchRulePathSegment[] ): FlagMatchRuleValue[] | undefined => { if (keys.length == 0 || path.length == 0) return undefined; const o = path[0].object; let key = keys[0]; const lastBracket = key.lastIndexOf("["); if (key === "..") { return getValues(keys.slice(1), path.slice(1)); } if (key.endsWith("]") && lastBracket > 0) { const optionalConstraint = key.substring(lastBracket + 1, key.length - 1); key = key.substring(0, lastBracket); if (o[key] != undefined) { let opart: any[] = o[key]; if (!Array.isArray(opart)) { opart = Object.values(opart) ?? []; } // Any constraints controlling what array object(s) to consider? if (optionalConstraint) { if (optionalConstraint.startsWith("!!")) { // Ignore this path in the tree if ANY of the values contain the constraint match. const [prop, val] = optionalConstraint.substring(2).split("="); if (opart.some((item) => item[prop] === val)) { return undefined; } } else if (optionalConstraint.startsWith("!")) { const [prop, val] = optionalConstraint.substring(1).split("="); opart = opart.filter((item) => item[prop] !== val); } else { const [prop, val] = optionalConstraint.split("="); opart = opart.filter((item) => item[prop] === val); } } if (keys.length == 1) { return opart.map((s, i) => { return { value: s, path: [{ object: o, path: key + "[" + i + "]"}, ... path] }; }); } else { return opart.reduce((res: FlagMatchRuleValue[] | undefined, oin: any, i: number) => { let matches = getValues(keys.slice(1), [ { object: oin, path: key + "[" + i + "]"}, ... path]); if (matches) { const r2 = res || []; r2.push(...matches); return r2; } return res; }, undefined); } } else { return undefined; } } else { if (o[key] != undefined) { if (keys.length == 1) { return [{ value: o[key], path: [{object: o[key], path: key}, ...path] }]; } else { return getValues(keys.slice(1), [{object: o[key], path: key}, ...path]); } } else { return undefined; } } }; let result: FlagMatchRuleValue[] = []; try { let keys = flagPath.split("/"); result = getValues(keys, path) ?? []; } catch (e) { console.error("Invalid RE", e); } return result; }; const getFirstWithData = (flagPaths: string[], path: FlagMatchRulePathSegment[]): string | undefined => { for (let idx = 0; idx < flagPaths.length; idx++) { const result = extractFlagValues(flagPaths[idx], path); if (result.length > 0) { return result[0].value as string; } } return undefined; }; const getFirstWithDataAsDate = (flagPaths: string[], path: FlagMatchRulePathSegment[]): Date | undefined => { const val = getFirstWithData(flagPaths, path); if (val) { try { const date = new Date(Date.parse(val)); return date; } catch (error) {} } return undefined; }; const softwareAgentFromAction = (action: FlagMatchRuleValue): string | undefined => { const agent = getFirstWithData([ "softwareAgent", "../../../claim_generator" ], action.path); return agent; }; export const extractProofHintFlags = (proof?: Proof): ProofHintFlags | undefined => { if (!proof) return undefined; let edits: ProofHintFlagsEdit[] | undefined = undefined; let valid = false; try { let results = proof.integrity?.c2pa?.manifest_info.validation_results?.activeManifest; if (results) { valid = results.failure.length == 0 && results.success.length > 0; } const rootMatchPath = [{object: proof, path: ""}]; let source: string | undefined = undefined; let dateCreated: Date | undefined = undefined; let dateCreatedSource: ProofHintFlagSource | undefined = undefined; let creationAction = extractFlagValues( "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions]/data/actions[action=c2pa.created]", rootMatchPath ); if (creationAction.length == 0) { creationAction = extractFlagValues( "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions.v2]/data/actions[action=c2pa.created]", rootMatchPath ); } if (creationAction.length > 0) { source = softwareAgentFromAction(creationAction[0]); dateCreated = getFirstWithDataAsDate(["when", "../../metadata/dateTime", "../../../signature_info/time"], creationAction[0].path); dateCreatedSource = "c2pa"; } console.log("DATE CREATED", dateCreated); let generator: ProofHintFlagsGenerator = matchFlag(ruleAiGenerated(), rootMatchPath).result ? "ai" : matchFlag(ruleScreenshotC2PA(), rootMatchPath).result ? "screenshot" : matchFlag(ruleCamera(), rootMatchPath).result ? "camera" : "unknown"; let generatorSource: ProofHintFlagSource | undefined = undefined; if (generator !== "unknown" && valid) { generatorSource = "c2pa"; } else { if (matchFlag(ruleScreenshotMeta(), rootMatchPath).result) { generator = "screenshot"; generatorSource = "metadata"; } else if (matchFlag(ruleAiMeta(), rootMatchPath).result) { generator = "ai"; generatorSource = "metadata"; } } console.error("PROOF", proof); const c2paEdits = extractFlagValues( "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions]/data/actions[!!action=c2pa.created]", rootMatchPath ); if (c2paEdits.length > 0) { edits = c2paEdits.map((edit) => { return { editor: softwareAgentFromAction(edit) ?? "", date: getFirstWithDataAsDate(["when", "../../metadata/dateTime", "../../../signature_info/time"], edit.path) } }); } // Do we have any data? Else, return "undefined", we don't just want to send an object with all defaults. if (source === undefined && dateCreated === undefined && generator === "unknown" && (!edits || edits.length == 0)) { return undefined; } const flags: ProofHintFlags = { device: source, creationDate: dateCreated, creationDateSource: dateCreatedSource, generator: generator, generatorSource: generatorSource, edits: edits, containsC2PA: proof.integrity?.c2pa !== undefined, containsEXIF: proof.integrity?.exif !== undefined }; return flags; } catch (error) {} return undefined; };