-
Notifications
You must be signed in to change notification settings - Fork 31
/
interop.ts
112 lines (92 loc) · 2.75 KB
/
interop.ts
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
import fs from 'fs'
import { yamux } from '@chainsafe/libp2p-yamux'
import { privateKeyFromProtobuf } from '@libp2p/crypto/keys'
import { createClient } from '@libp2p/daemon-client'
import { createServer } from '@libp2p/daemon-server'
import { connectInteropTests } from '@libp2p/interop'
import { logger } from '@libp2p/logger'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { execa } from 'execa'
import { path as p2pd } from 'go-libp2p'
import { createLibp2p, type Libp2pOptions } from 'libp2p'
import pDefer from 'p-defer'
import { noise } from '../src/index.js'
import type { PrivateKey } from '@libp2p/interface'
import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop'
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.encryption === 'noise') {
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 () => {
proc.kill()
}
}
}
async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
let privateKey: PrivateKey | undefined
if (options.key != null) {
const keyFile = fs.readFileSync(options.key)
privateKey = privateKeyFromProtobuf(keyFile)
}
const opts: Libp2pOptions = {
privateKey,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [tcp()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
}
const node = await createLibp2p(opts)
const server = 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 createGoPeer(options)
}
return createJsPeer(options)
}
}
connectInteropTests(factory)
}
main().catch(err => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})