MCPcopy Create free account
hub / github.com/Eversmile12/sharedcontext / syncCommand

Function syncCommand

src/cli/sync.ts:23–112  ·  view source on GitHub ↗
(urlOrToken: string)

Source from the content-addressed store, hash-verified

21const MAX_SHARE_BYTES = 2 * 1024 * 1024;
22
23export async function syncCommand(urlOrToken: string): Promise<void> {
24 const dbPath = ensureInitialized();
25
26 const token = extractToken(urlOrToken);
27 const decoded = decodeShareToken(token);
28 let txId = decoded.txId;
29 let encrypted: Uint8Array | null = null;
30 let shareWallet = "";
31 let shareSignature = "";
32 let resolvedShareId = "";
33
34 if (txId) {
35 try {
36 const txMeta = await queryTransactionTagsById(txId);
37 if (!txMeta) {
38 throw new Error(`No transaction metadata found for tx id: ${txId}`);
39 }
40 const type = txMeta.tags.get("Type") ?? "";
41 const tagShareId = txMeta.tags.get("Share-Id") ?? "";
42 const wallet = txMeta.tags.get("Wallet") ?? "";
43 const signature = txMeta.tags.get("Signature")?.trim() ?? "";
44 if (type !== "conversation-share") {
45 throw new Error("Transaction is not a conversation share.");
46 }
47 if (tagShareId !== decoded.shareId) {
48 throw new Error("Share token does not match transaction share id.");
49 }
50 if (!wallet || !signature) {
51 throw new Error("Share transaction is missing signer metadata.");
52 }
53 encrypted = await downloadShard(txId, MAX_SHARE_BYTES);
54 shareWallet = wallet;
55 shareSignature = signature;
56 resolvedShareId = tagShareId;
57 } catch {
58 // Fall back to Share-Id lookup for eventual consistency or stale tx references.
59 encrypted = null;
60 }
61 }
62
63 if (!encrypted) {
64 const shareInfo = await queryConversationShare(decoded.shareId);
65 if (!shareInfo) {
66 throw new Error(`No share found for id: ${decoded.shareId}`);
67 }
68 if (!shareInfo.wallet || !shareInfo.signature) {
69 throw new Error("Share transaction is missing signer metadata.");
70 }
71 txId = shareInfo.txId;
72 resolvedShareId = shareInfo.shareId;
73 shareWallet = shareInfo.wallet;
74 shareSignature = shareInfo.signature;
75 encrypted = await downloadShard(txId, MAX_SHARE_BYTES);
76 }
77
78 const valid = verifySignature(encrypted, shareSignature, shareWallet);
79 if (!valid) {
80 throw new Error("Share signature verification failed.");

Callers 1

index.tsFile · 0.85

Calls 12

ensureInitializedFunction · 0.85
extractTokenFunction · 0.85
decodeShareTokenFunction · 0.85
queryTransactionTagsByIdFunction · 0.85
downloadShardFunction · 0.85
queryConversationShareFunction · 0.85
verifySignatureFunction · 0.85
decryptFunction · 0.85
validatePayloadFunction · 0.85
openDatabaseFunction · 0.85

Tested by

no test coverage detected