|
| 1 | +import { connectInteropTests } from '@libp2p/interop' |
| 2 | +import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop' |
| 3 | +import { createServer } from '@libp2p/daemon-server' |
| 4 | +import { createClient } from '@libp2p/daemon-client' |
| 5 | +import { createLibp2p, Libp2pOptions } from 'libp2p' |
| 6 | +import { TCP } from '@libp2p/tcp' |
| 7 | +import { Multiaddr } from '@multiformats/multiaddr' |
| 8 | +import { path as p2pd } from 'go-libp2p' |
| 9 | +import { execa } from 'execa' |
| 10 | +import pDefer from 'p-defer' |
| 11 | +import { logger } from '@libp2p/logger' |
| 12 | +import { Mplex } from '@libp2p/mplex' |
| 13 | +import fs from 'fs' |
| 14 | +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' |
| 15 | +import type { PeerId } from '@libp2p/interface-peer-id' |
| 16 | +import { peerIdFromKeys } from '@libp2p/peer-id' |
| 17 | +import { Noise } from '../src/index.js' |
| 18 | + |
| 19 | +async function createGoPeer (options: SpawnOptions): Promise<Daemon> { |
| 20 | + const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000 |
| 21 | + const apiAddr = new Multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`) |
| 22 | + |
| 23 | + const log = logger(`go-libp2p:${controlPort}`) |
| 24 | + |
| 25 | + const opts = [ |
| 26 | + `-listen=${apiAddr.toString()}`, |
| 27 | + '-hostAddrs=/ip4/0.0.0.0/tcp/0' |
| 28 | + ] |
| 29 | + |
| 30 | + if (options.noise === true) { |
| 31 | + opts.push('-noise=true') |
| 32 | + } |
| 33 | + |
| 34 | + if (options.key != null) { |
| 35 | + opts.push(`-id=${options.key}`) |
| 36 | + } |
| 37 | + |
| 38 | + const deferred = pDefer() |
| 39 | + const proc = execa(p2pd(), opts) |
| 40 | + |
| 41 | + proc.stdout?.on('data', (buf: Buffer) => { |
| 42 | + const str = buf.toString() |
| 43 | + log(str) |
| 44 | + |
| 45 | + // daemon has started |
| 46 | + if (str.includes('Control socket:')) { |
| 47 | + deferred.resolve() |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + proc.stderr?.on('data', (buf) => { |
| 52 | + log.error(buf.toString()) |
| 53 | + }) |
| 54 | + |
| 55 | + await deferred.promise |
| 56 | + |
| 57 | + return { |
| 58 | + client: createClient(apiAddr), |
| 59 | + stop: async () => { |
| 60 | + await proc.kill() |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async function createJsPeer (options: SpawnOptions): Promise<Daemon> { |
| 66 | + let peerId: PeerId | undefined |
| 67 | + |
| 68 | + if (options.key != null) { |
| 69 | + const keyFile = fs.readFileSync(options.key) |
| 70 | + const privateKey = await unmarshalPrivateKey(keyFile) |
| 71 | + peerId = await peerIdFromKeys(privateKey.public.bytes, privateKey.bytes) |
| 72 | + } |
| 73 | + |
| 74 | + const opts: Libp2pOptions = { |
| 75 | + peerId, |
| 76 | + addresses: { |
| 77 | + listen: ['/ip4/0.0.0.0/tcp/0'] |
| 78 | + }, |
| 79 | + transports: [new TCP()], |
| 80 | + streamMuxers: [new Mplex()], |
| 81 | + connectionEncryption: [new Noise()] |
| 82 | + } |
| 83 | + |
| 84 | + const node = await createLibp2p(opts) |
| 85 | + const server = await createServer(new Multiaddr('/ip4/0.0.0.0/tcp/0'), node as any) |
| 86 | + await server.start() |
| 87 | + |
| 88 | + return { |
| 89 | + client: createClient(server.getMultiaddr()), |
| 90 | + stop: async () => { |
| 91 | + await server.stop() |
| 92 | + await node.stop() |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +async function main (): Promise<void> { |
| 98 | + const factory: DaemonFactory = { |
| 99 | + async spawn (options: SpawnOptions) { |
| 100 | + if (options.type === 'go') { |
| 101 | + return await createGoPeer(options) |
| 102 | + } |
| 103 | + |
| 104 | + return await createJsPeer(options) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + await connectInteropTests(factory) |
| 109 | +} |
| 110 | + |
| 111 | +main().catch(err => { |
| 112 | + console.error(err) // eslint-disable-line no-console |
| 113 | + process.exit(1) |
| 114 | +}) |
0 commit comments