(urlOrToken: string)
| 21 | const MAX_SHARE_BYTES = 2 * 1024 * 1024; |
| 22 | |
| 23 | export 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."); |
no test coverage detected