/**
* Basic Wirecore Example
*
* Demonstrates:
* - Creating a Wirecore instance
* - Posting messages
* - Following users
* - Reading timeline
*/
import { Wirecore } from '../lib/core/wirecore.js'
async function main() {
console.log('=== Wirecore Basic Example ===\n')
// Initialize two users (Alice and Bob)
const alice = new Wirecore('./example-storage/alice')
const bob = new Wirecore('./example-storage/bob')
console.log('Initializing Alice and Bob...')
await alice.init()
await bob.init()
console.log('\nAlice public key:', alice.getPublicKey())
console.log('Bob public key:', bob.getPublicKey())
// Alice posts some messages
console.log('\n--- Alice posts messages ---')
await alice.post({ text: 'Hello decentralized world!' })
await alice.post({ text: 'This is my second post using Wirecore' })
await alice.post({ text: 'P2P social networking is awesome!' })
// Bob posts some messages
console.log('\n--- Bob posts messages ---')
await bob.post({ text: 'Hi everyone! Bob here.' })
await bob.post({ text: 'Excited to try Wirecore!' })
// Alice follows Bob
console.log('\n--- Alice follows Bob ---')
await alice.follow(bob.getPublicKey(), 'Bob')
// Bob follows Alice
console.log('\n--- Bob follows Alice ---')
await bob.follow(alice.getPublicKey(), 'Alice')
// Wait for replication (give swarm time to sync)
console.log('\nWaiting for feeds to replicate...')
await new Promise(resolve => setTimeout(resolve, 3000))
// View Alice's timeline (should include Bob's posts)
console.log('\n--- Alice\'s Timeline ---')
const aliceTimeline = await alice.getTimeline({ limit: 10 })
for (const post of aliceTimeline) {
const authorName = post.author === alice.getPublicKey() ? 'Alice' : 'Bob'
console.log(`${authorName}: ${post.content.text}`)
}
// View Bob's timeline (should include Alice's posts)
console.log('\n--- Bob\'s Timeline ---')
const bobTimeline = await bob.getTimeline({ limit: 10 })
for (const post of bobTimeline) {
const authorName = post.author === bob.getPublicKey() ? 'Bob' : 'Alice'
console.log(`${authorName}: ${post.content.text}`)
}
// Alice likes Bob's first post
console.log('\n--- Alice likes Bob\'s post ---')
const bobFirstPost = bobTimeline.find(p => p.author === bob.getPublicKey())
if (bobFirstPost) {
await alice.like(bob.getPublicKey(), bobFirstPost.sequence)
console.log('Alice liked Bob\'s post!')
}
// Bob boosts Alice's post
console.log('\n--- Bob boosts Alice\'s post ---')
const aliceFirstPost = aliceTimeline.find(p => p.author === alice.getPublicKey())
if (aliceFirstPost) {
await bob.boost(alice.getPublicKey(), aliceFirstPost.sequence)
console.log('Bob boosted Alice\'s post!')
}
// View profiles
console.log('\n--- Profiles ---')
const aliceProfile = await alice.getProfile(alice.getPublicKey())
console.log('Alice:', aliceProfile)
const bobProfile = await bob.getProfile(bob.getPublicKey())
console.log('Bob:', bobProfile)
// Cleanup
console.log('\nClosing connections...')
await alice.close()
await bob.close()
console.log('\n✓ Example complete!')
}
main().catch(err => {
console.error('Error:', err)
process.exit(1)
})