OverviewFilesCommitsIssuesDocsReleasesPeers

wirecore / examples / anproto-compat.js

124 lines · javascript ref:
/**
 * ANProto Compatibility Example
 *
 * Demonstrates how Wirecore messages are compatible
 * with original ANProto format and can be:
 * - Exported to Wiredove
 * - Verified by any ANProto implementation
 * - Sent via HTTP, email, USB, etc.
 */

import { Wirecore } from '../lib/core/wirecore.js'
import { ANProto } from '../lib/core/anproto.js'

async function main() {
  console.log('=== ANProto Compatibility Example ===\n')

  const fc = new Wirecore('./example-storage/compat')
  await fc.init()

  // 1. Create a post using Wirecore
  console.log('--- Creating post with Wirecore ---')
  const post = await fc.post({
    text: 'This message uses ANProto signatures!'
  })

  console.log('Posted message:')
  console.log(JSON.stringify(post, null, 2))

  // 2. Extract the ANProto signature
  console.log('\n--- ANProto Signature ---')
  console.log('Signature:', post.anproto)
  console.log('Length:', post.anproto.length, 'characters')

  // 3. Verify the signature using ANProto
  console.log('\n--- Verifying ANProto signature ---')
  const verified = ANProto.open(post.anproto)
  console.log('✓ Signature verified!')
  console.log('Timestamp:', new Date(verified.timestamp).toISOString())
  console.log('Hash:', verified.hash)
  console.log('Public key:', verified.publicKey)

  // 4. Show content hash verification
  console.log('\n--- Content Hash Verification ---')
  const contentHash = ANProto.hash(JSON.stringify(post.content))
  console.log('Computed hash:', contentHash)
  console.log('Stored hash:', post.hash)
  console.log('Match:', contentHash === post.hash ? '✓ YES' : '✗ NO')

  // 5. Export message for other systems
  console.log('\n--- Export for Wiredove/Other Systems ---')

  const exportedMessage = {
    // ANProto signature (portable!)
    anproto: post.anproto,

    // Content (YAML or JSON)
    content: post.content,

    // Metadata
    timestamp: post.timestamp,
    author: fc.getPublicKey(),

    // Can be sent via:
    // - HTTP: POST to any server
    // - WebRTC: Trystero rooms (Wiredove)
    // - Email: Base64 in email body
    // - USB: JSON file on flash drive
    // - Bluetooth: Send to nearby device
  }

  console.log('Exported message (portable):')
  console.log(JSON.stringify(exportedMessage, null, 2))

  // 6. Simulate importing from another system
  console.log('\n--- Import from External Source ---')

  // Pretend this came from Wiredove, email, etc.
  const externalMessage = {
    anproto: post.anproto,
    content: {
      type: 'post',
      text: 'This came from Wiredove via WebRTC!'
    }
  }

  // Verify it's authentic
  try {
    const externalVerified = ANProto.verifyMessage(externalMessage)
    console.log('✓ External message verified!')
    console.log('Author:', externalVerified.author)
    console.log('Text:', externalVerified.content.text)
  } catch (err) {
    console.log('✗ Verification failed:', err.message)
  }

  // 7. Show URL embed format (like Wiredove)
  console.log('\n--- URL Embed Format (Wiredove-style) ---')
  const urlSafeMessage = encodeURIComponent(post.anproto)
  const shareUrl = `fedicore://message/${urlSafeMessage}`
  console.log('Share URL:', shareUrl)
  console.log('\nAnyone with this URL can:')
  console.log('- Verify the signature')
  console.log('- See who posted it')
  console.log('- Import it to their feed')

  // 8. Cross-platform compatibility
  console.log('\n--- Cross-Platform Compatibility ---')
  console.log('This ANProto message works with:')
  console.log('✓ Wirecore (Hypercore)')
  console.log('✓ Wiredove (WebRTC)')
  console.log('✓ Go implementation (github.com/vic/goan)')
  console.log('✓ Rust implementation (github.com/vic/anproto-rs)')
  console.log('✓ Python implementation (github.com/macauleyjustin/ANproto-Python)')
  console.log('✓ Any system that understands ed25519 + base64')

  await fc.close()
  console.log('\n✓ Compatibility demo complete!')
}

main().catch(err => {
  console.error('Error:', err)
  process.exit(1)
})