test: replace nwaku ip with local ip

This commit is contained in:
fryorcraken.eth 2023-01-27 15:37:57 +11:00
parent c8fa8ea20e
commit afa7262604
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 26 additions and 12 deletions

View File

@ -99,6 +99,7 @@ export class Nwaku {
private pid?: number; private pid?: number;
private peerId?: PeerId; private peerId?: PeerId;
private multiaddrWithId?: Multiaddr; private multiaddrWithId?: Multiaddr;
private websocketPort?: number;
private readonly logPath: string; private readonly logPath: string;
private rpcPort?: number; private rpcPort?: number;
@ -171,6 +172,8 @@ export class Nwaku {
args args
); );
this.websocketPort = mergedArgs.websocketPort;
process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = ""; process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = "";
const argsArray = argsToArray(mergedArgs); const argsArray = argsToArray(mergedArgs);
@ -373,29 +376,40 @@ export class Nwaku {
} }
async getPeerId(): Promise<PeerId> { async getPeerId(): Promise<PeerId> {
return await this._getPeerId().then((res) => res.peerId); if (this.peerId) return this.peerId;
this.peerId = await this._getPeerId();
return this.peerId;
} }
async getMultiaddrWithId(): Promise<Multiaddr> { async getMultiaddrWithId(): Promise<Multiaddr> {
return await this._getPeerId().then((res) => res.multiaddrWithId); if (this.multiaddrWithId) return this.multiaddrWithId;
if (!this.websocketPort) {
return Promise.reject("No websocket port defined.");
} }
private async _getPeerId(): Promise<{ const peerId = await this.getPeerId();
peerId: PeerId;
multiaddrWithId: Multiaddr; this.multiaddrWithId = multiaddr(
}> { `/ip4/127.0.0.1/tcp/${this.websocketPort}/ws/p2p/${peerId.toString()}`
if (this.peerId && this.multiaddrWithId) { );
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId }; return this.multiaddrWithId;
}
private async _getPeerId(): Promise<PeerId> {
if (this.peerId) {
return this.peerId;
} }
const res = await this.info(); const res = await this.info();
this.multiaddrWithId = res.listenAddresses const multiaddrWithId = res.listenAddresses
.map((ma) => multiaddr(ma)) .map((ma) => multiaddr(ma))
.find((ma) => ma.protoNames().includes("ws")); .find((ma) => ma.protoNames().includes("ws"));
if (!this.multiaddrWithId) throw "Nwaku did not return a ws multiaddr"; if (!multiaddrWithId) throw "Nwaku did not return a ws multiaddr";
const peerIdStr = this.multiaddrWithId.getPeerId(); const peerIdStr = multiaddrWithId.getPeerId();
if (!peerIdStr) throw "Nwaku multiaddr does not contain peerId"; if (!peerIdStr) throw "Nwaku multiaddr does not contain peerId";
this.peerId = peerIdFromString(peerIdStr); this.peerId = peerIdFromString(peerIdStr);
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
return this.peerId;
} }
get rpcUrl(): string { get rpcUrl(): string {