/**
* Post signing for hyper-social-web
*
* Signs outgoing posts with ANProto content-signing (Wiredove-compatible).
* Server-side verification of incoming signed posts.
*/
import { ANProto } from 'hyper-identity'
/**
* Sign a post's content with ANProto
*
* @param {string} text - Post text
* @param {{ publicKey: Buffer, secretKey: Buffer }} keypair - Server or user keypair
* @returns {{ anproto: string, hash: string }}
*/
export function signPost (text, keypair) {
const hash = ANProto.hash(text)
const anproto = ANProto.signContent(hash, keypair)
return { anproto, hash }
}
/**
* Verify a signed post
*
* @param {{ text: string, anproto: string }} post
* @returns {{ verified: boolean, author: string, timestamp: number } | { verified: false }}
*/
export function verifyPost (post) {
if (!post || !post.anproto) {
return { verified: false }
}
try {
const opened = ANProto.open(post.anproto)
const expectedHash = ANProto.hash(post.text)
if (opened.hash !== expectedHash) {
return { verified: false }
}
return {
verified: true,
author: opened.publicKey,
timestamp: opened.timestamp
}
} catch {
return { verified: false }
}
}