Encrypted, ephemeral, private memos on atproto

fix(producer): cache pubkey uri with record value

graham.systems 8195c329 72e309f6

verified
Changed files
+24 -22
packages
producer
+16 -15
packages/producer/mod.ts
··· 4 4 ProducerOptions, 5 5 ProducerParams, 6 6 } from "./types.ts"; 7 - import { type Did, is } from "@atcute/lexicons"; 7 + import { type Did, parse, type ResourceUri } from "@atcute/lexicons"; 8 8 import type { Client, CredentialManager } from "@atcute/client"; 9 - import { AppCisternLexiconPubkey } from "@cistern/lexicon"; 9 + import { 10 + AppCisternLexiconPubkey, 11 + } from "@cistern/lexicon"; 10 12 11 13 import type {} from "@atcute/atproto"; 12 14 13 15 export async function createProducer( 14 - { publicKey, ...opts }: ProducerOptions, 16 + { publicKey: rkey, ...opts }: ProducerOptions, 15 17 ): Promise<Producer> { 16 18 const reqs = await produceRequirements(opts); 17 19 18 - let record: AppCisternLexiconPubkey.Main | undefined; 19 - if (typeof publicKey === "string") { 20 + let publicKey: LocalPublicKey | undefined; 21 + if (rkey) { 20 22 // Fetch record contents from PDS 21 23 const res = await reqs.rpc.get("com.atproto.repo.getRecord", { 22 24 params: { 23 25 repo: reqs.miniDoc.did, 24 - rkey: publicKey, 26 + rkey, 25 27 collection: "app.cistern.lexicon.pubkey", 26 28 }, 27 29 }); ··· 32 34 ); 33 35 } 34 36 35 - record = res.data.value as AppCisternLexiconPubkey.Main; 36 - } else if (is(AppCisternLexiconPubkey.mainSchema, publicKey)) { 37 - record = publicKey; 38 - } else if (publicKey) { 39 - throw new Error( 40 - "provided public key does not validate against lexicon schema", 41 - ); 37 + const record = parse(AppCisternLexiconPubkey.mainSchema, res.data.value); 38 + 39 + publicKey = { uri: res.data.uri, record }; 42 40 } 43 41 44 - return new Producer({ ...reqs, options: { ...opts, publicKey: record } }); 42 + return new Producer({ 43 + ...reqs, 44 + publicKey, 45 + }); 45 46 } 46 47 47 48 export class Producer { ··· 54 55 this.did = params.miniDoc.did; 55 56 this.rpc = params.rpc; 56 57 this.manager = params.manager; 57 - this.publicKey = params.options.publicKey; 58 + this.publicKey = params.publicKey; 58 59 } 59 60 60 61 /**
+8 -7
packages/producer/types.ts
··· 1 1 import type { BaseClientOptions, ClientRequirements } from "@cistern/shared"; 2 2 import type { AppCisternLexiconPubkey } from "@cistern/lexicon"; 3 - import type { RecordKey } from "@atcute/lexicons"; 4 - 5 - export type LocalPublicKey = AppCisternLexiconPubkey.Main; 3 + import type { RecordKey, ResourceUri } from "@atcute/lexicons"; 6 4 7 5 export interface ProducerOptions extends BaseClientOptions { 8 - publicKey?: RecordKey | LocalPublicKey; 6 + publicKey?: RecordKey; 9 7 } 10 8 11 9 export type ProducerParams = ClientRequirements<ProducerOptions> & { 12 - options: { 13 - publicKey?: AppCisternLexiconPubkey.Main; 14 - }; 10 + publicKey?: LocalPublicKey; 15 11 }; 12 + 13 + export interface LocalPublicKey { 14 + uri: ResourceUri; 15 + record: AppCisternLexiconPubkey.Main; 16 + }