forked from ChainSafe/js-libp2p-noise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterop.ts
More file actions
116 lines (96 loc) · 3.16 KB
/
interop.ts
File metadata and controls
116 lines (96 loc) · 3.16 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { connectInteropTests } from '@libp2p/interop'
import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop'
import { createServer } from '@libp2p/daemon-server'
import { createClient } from '@libp2p/daemon-client'
import { createLibp2p, Libp2pOptions } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { path as p2pd } from 'go-libp2p'
import { execa } from 'execa'
import pDefer from 'p-defer'
import { logger } from '@libp2p/logger'
import { Mplex } from '@libp2p/mplex'
import fs from 'fs'
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
import type { PeerId } from '@libp2p/interface-peer-id'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { Noise } from '../src/noise.js'
async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000
const apiAddr = multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`)
const log = logger(`go-libp2p:${controlPort}`)
const opts = [
`-listen=${apiAddr.toString()}`,
'-hostAddrs=/ip4/0.0.0.0/tcp/0'
]
if (options.noise === true) {
opts.push('-noise=true')
}
if (options.key != null) {
opts.push(`-id=${options.key}`)
}
const deferred = pDefer()
const proc = execa(p2pd(), opts)
proc.stdout?.on('data', (buf: Buffer) => {
const str = buf.toString()
log(str)
// daemon has started
if (str.includes('Control socket:')) {
deferred.resolve()
}
})
proc.stderr?.on('data', (buf) => {
log.error(buf.toString())
})
await deferred.promise
return {
client: createClient(apiAddr),
stop: async () => {
await proc.kill()
}
}
}
async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
let peerId: PeerId | undefined
if (options.key != null) {
const keyFile = fs.readFileSync(options.key)
const privateKey = await unmarshalPrivateKey(keyFile)
peerId = await peerIdFromKeys(privateKey.public.bytes, privateKey.bytes)
}
const opts: Libp2pOptions = {
peerId,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [new TCP()],
// @ts-expect-error libp2p options is still referencing the old stream muxer interface https://github.com/libp2p/js-libp2p/pull/1402
streamMuxers: [new Mplex()],
// @ts-expect-error libp2p options is still referencing the old connection encrypter interface https://github.com/libp2p/js-libp2p/pull/1402
connectionEncryption: [new Noise()]
}
const node = await createLibp2p(opts)
const server = await createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node as any)
await server.start()
return {
client: createClient(server.getMultiaddr()),
stop: async () => {
await server.stop()
await node.stop()
}
}
}
async function main (): Promise<void> {
const factory: DaemonFactory = {
async spawn (options: SpawnOptions) {
if (options.type === 'go') {
return await createGoPeer(options)
}
return await createJsPeer(options)
}
}
await connectInteropTests(factory)
}
main().catch(err => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})