import { type Cfc, computed, NAME, pattern, UI, Writable } from "commonfabric"; type AuthorshipIntegrity = { readonly kind: "authored-by"; readonly subject: Author; }; type AuthorClaim = { readonly id: Author; readonly name: string; readonly avatar: string; }; type AuthoredMessage = { readonly id: string; readonly channel: string; readonly sender: AuthorClaim; readonly body: string; }; type AuthoredMessageWithIntegrity< IntegrityAuthor extends string, Sender extends string, > = Cfc< AuthoredMessage, { integrity: readonly [AuthorshipIntegrity] } >; export type AuthorshipChatOutput = { [NAME]: string; [UI]: unknown; verifiedAuthor: string; forgedClaim: string; unsignedState: string; }; export default pattern(() => { const verifiedMessage: Writable< AuthoredMessageWithIntegrity<"alice", "alice"> > = new Writable>( { id: "msg-verified", channel: "Project chat", sender: { id: "alice", name: "Alice Nguyen", avatar: "https://images.unsplash.com/photo-1494790108377-be9c29b29330?w=96&h=96&fit=crop", }, body: "I reviewed the launch copy and signed off on the customer-facing wording.", } as AuthoredMessageWithIntegrity<"alice", "alice">, ); const forgedMessage: Writable< AuthoredMessageWithIntegrity<"alice", "bob"> > = new Writable>( { id: "msg-forged", channel: "Project chat", sender: { id: "bob", name: "Bob Patel", avatar: "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?w=96&h=96&fit=crop", }, body: "I definitely authored Alice's approval, and the UI should not certify this claim.", } as AuthoredMessageWithIntegrity<"alice", "bob">, ); const unsignedMessage = new Writable>( { id: "msg-unsigned", channel: "Imported ticket thread", sender: { id: "casey", name: "Casey Morgan", avatar: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=96&h=96&fit=crop", }, body: "This imported comment has no persisted authorship integrity yet, so it stays uncertified.", }, ); const verifiedAuthor = computed(() => verifiedMessage.key("sender").key("name").get() ); const verifiedRequiredTextIntegrity = computed(() => ({ kind: "authored-by", subject: verifiedMessage.key("sender").key("id").get(), } satisfies AuthorshipIntegrity)); const forgedClaim = computed(() => forgedMessage.key("sender").key("name").get() ); const forgedRequiredTextIntegrity = computed(() => ({ kind: "authored-by", subject: forgedMessage.key("sender").key("id").get(), } satisfies AuthorshipIntegrity)); const unsignedState = computed(() => unsignedMessage.key("sender").key("name").get() ); const unsignedRequiredTextIntegrity = computed(() => ({ kind: "authored-by", subject: unsignedMessage.key("sender").key("id").get(), } satisfies AuthorshipIntegrity)); return { [NAME]: "CFC authorship chat demo", [UI]: ( Verified authorship for user content Each block is rendered by untrusted pattern code, but the cf-cfc-authorship component verifies the bound content cell against its persisted CFC integrity label before rendering a trusted avatar state. Matching content and author claim The rendered block claims Alice, and the whole message object carries authored-by Alice integrity. {verifiedMessage.key("channel")}
{verifiedMessage.key("sender").key("name")}

{verifiedMessage.key("body")}

Forged author claim The rendered block claims Bob, but the message object only carries authored-by Alice integrity. {forgedMessage.key("channel")}
{forgedMessage.key("sender").key("name")}

{forgedMessage.key("body")}

Unsigned imported content The content has no authorship integrity label, so the trusted avatar is withheld. {unsignedMessage.key("channel")}
{unsignedMessage.key("sender").key("name")}

{unsignedMessage.key("body")}

), verifiedAuthor, forgedClaim, unsignedState, }; });