-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer_1.js
68 lines (53 loc) · 1.95 KB
/
peer_1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { CoreIdExtension } from '../mapeo-core-next/sync/CoreIdExtension.js'
import { CoreIdCache } from '../mapeo-core-next/sync/CoreIdCache.js'
import { Sqlite } from '../mapeo-core-next/lib/sqlite.js'
import Corestore from 'corestore'
import Hyperswarm from 'hyperswarm'
import {randomBytes} from 'node:crypto'
const store = new Corestore('./store_1')
const sqlite = new Sqlite()
const coreIdCache = new CoreIdCache(sqlite)
// create master core to register extension in
const masterCoreKey = Buffer.alloc(32).fill("example-master-core");
const masterCore = store.get({key:masterCoreKey})
await masterCore.ready()
// create three core examples and two random identities
const authCoreExample = store.get({name:'myAuthStore'})
const authCoreExample2 = store.get({name:'myAuthStore2'})
const authCoreExample3 = store.get({name:'myAuthStore3'})
const identityId = randomBytes(32).toString('hex')
const identityId2 = randomBytes(32).toString('hex')
// wait for the cores to be ready
await authCoreExample.ready()
await authCoreExample2.ready()
await authCoreExample3.ready()
// we are shareing to cores as part of one identity
coreIdCache.put({
namespace:'auth',
coreId:authCoreExample.key.toString('hex'),
identityId: identityId
})
coreIdCache.put({
namespace:'auth',
coreId:authCoreExample2.key.toString('hex'),
identityId: identityId
})
// and another core as part of other identity
coreIdCache.put({
namespace:'auth',
coreId:authCoreExample3.key.toString('hex'),
identityId: identityId2
})
// instance extension to sync auth
const coreIdExtension = new CoreIdExtension(masterCore, coreIdCache)
// we pass an empty fn since we know that peer_2 won't share anything
coreIdExtension.share('auth', () => {})
// SWARM
const topic = Buffer.alloc(32).fill('szgy')
const swarm = new Hyperswarm()
swarm.on('connection' , (conn,info) => {
console.log('replicating data')
store.replicate(conn)
})
const discovery = swarm.join(topic)
await discovery.flushed()