(
conversationId: string,
options: ShareCommandOptions = {}
)
| 36 | } |
| 37 | |
| 38 | export async function shareCommand( |
| 39 | conversationId: string, |
| 40 | options: ShareCommandOptions = {} |
| 41 | ): Promise<void> { |
| 42 | const selected = await resolveConversation(conversationId, options.client); |
| 43 | |
| 44 | const passphrase = keychainLoad(); |
| 45 | if (!passphrase) { |
| 46 | throw new Error( |
| 47 | "No passphrase found in system keychain. Run `sharedcontext init` again to store it." |
| 48 | ); |
| 49 | } |
| 50 | if (!isIdentityAvailable()) { |
| 51 | throw new Error("No local identity found. Run `sharedcontext init` first."); |
| 52 | } |
| 53 | |
| 54 | const { encryptionKey, identityKey, walletAddress } = resolveIdentity(passphrase); |
| 55 | const shareKey = new Uint8Array(randomBytes(32)); |
| 56 | const shareId = uuidv4(); |
| 57 | |
| 58 | const payload: ConversationSharePayload = { |
| 59 | v: 1, |
| 60 | createdAt: new Date().toISOString(), |
| 61 | conversation: selected, |
| 62 | }; |
| 63 | const serialized = new TextEncoder().encode(JSON.stringify(payload)); |
| 64 | const encrypted = encrypt(serialized, shareKey); |
| 65 | |
| 66 | const signature = signShard(encrypted, identityKey); |
| 67 | const tags: Tag[] = [ |
| 68 | { name: "App-Name", value: "sharedcontext" }, |
| 69 | { name: "Type", value: "conversation-share" }, |
| 70 | { name: "Share-Id", value: shareId }, |
| 71 | { name: "Wallet", value: walletAddress }, |
| 72 | { name: "Timestamp", value: String(Math.floor(Date.now() / 1000)) }, |
| 73 | { name: "Signature", value: signature }, |
| 74 | { name: "Content-Type", value: "application/octet-stream" }, |
| 75 | ]; |
| 76 | |
| 77 | const backend = new TurboBackend({ |
| 78 | privateKeyHex: Buffer.from(identityKey).toString("hex"), |
| 79 | testnet: process.env.SHAREDCONTEXT_TESTNET === "true", |
| 80 | }); |
| 81 | const result = await backend.upload(encrypted, tags); |
| 82 | |
| 83 | const token = encodeShareToken({ |
| 84 | v: 1, |
| 85 | sid: shareId, |
| 86 | k: Buffer.from(shareKey).toString("base64url"), |
| 87 | t: result.txId, |
| 88 | }); |
| 89 | const url = `${SHARE_URL_PREFIX}${token}`; |
| 90 | |
| 91 | console.log("Conversation shared."); |
| 92 | console.log(`Link: ${url}`); |
| 93 | console.log("\nImport on another device:"); |
| 94 | console.log(`sharedcontext sync ${url}`); |
| 95 |
no test coverage detected