chore: extract and simplify `getLocationMultiaddr`

Using switch/case when appropriate.
This commit is contained in:
fryorcraken.eth 2023-03-03 15:20:32 +11:00
parent af5c5733a5
commit 84f114bfa4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 50 additions and 43 deletions

View File

@ -17,7 +17,7 @@ import debug from "debug";
import { ERR_INVALID_ID } from "./constants.js"; import { ERR_INVALID_ID } from "./constants.js";
import { keccak256, verifySignature } from "./crypto.js"; import { keccak256, verifySignature } from "./crypto.js";
import { multiaddrFromFields } from "./multiaddr_from_fields.js"; import { locationMultiaddrFromEnrFields } from "./get_multiaddr.js";
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js"; import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
import { createPeerIdFromPublicKey } from "./peer_id.js"; import { createPeerIdFromPublicKey } from "./peer_id.js";
import * as v4 from "./v4.js"; import * as v4 from "./v4.js";
@ -231,49 +231,9 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
} }
} }
getLocationMultiaddr( getLocationMultiaddr: (
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6" protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
): Multiaddr | undefined { ) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
if (protocol === "udp") {
return (
this.getLocationMultiaddr("udp4") || this.getLocationMultiaddr("udp6")
);
}
if (protocol === "tcp") {
return (
this.getLocationMultiaddr("tcp4") || this.getLocationMultiaddr("tcp6")
);
}
const isIpv6 = protocol.endsWith("6");
const ipVal = this.get(isIpv6 ? "ip6" : "ip");
if (!ipVal) {
return;
}
const isUdp = protocol.startsWith("udp");
const isTcp = protocol.startsWith("tcp");
let protoName, protoVal;
if (isUdp) {
protoName = "udp";
protoVal = isIpv6 ? this.get("udp6") : this.get("udp");
} else if (isTcp) {
protoName = "tcp";
protoVal = isIpv6 ? this.get("tcp6") : this.get("tcp");
} else {
return;
}
if (!protoVal) {
return;
}
return multiaddrFromFields(
isIpv6 ? "ip6" : "ip4",
protoName,
ipVal,
protoVal
);
}
setLocationMultiaddr(multiaddr: Multiaddr): void { setLocationMultiaddr(multiaddr: Multiaddr): void {
const protoNames = multiaddr.protoNames(); const protoNames = multiaddr.protoNames();

View File

@ -0,0 +1,47 @@
import { Multiaddr } from "@multiformats/multiaddr";
import type { IEnr } from "@waku/interfaces";
import { multiaddrFromFields } from "./multiaddr_from_fields.js";
export function locationMultiaddrFromEnrFields(
enr: IEnr,
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
): Multiaddr | undefined {
switch (protocol) {
case "udp":
return (
locationMultiaddrFromEnrFields(enr, "udp4") ||
locationMultiaddrFromEnrFields(enr, "udp6")
);
case "tcp":
return (
locationMultiaddrFromEnrFields(enr, "tcp4") ||
locationMultiaddrFromEnrFields(enr, "tcp6")
);
}
const isIpv6 = protocol.endsWith("6");
const ipVal = enr.get(isIpv6 ? "ip6" : "ip");
if (!ipVal) return;
const protoName = protocol.slice(0, 3);
let protoVal;
switch (protoName) {
case "udp":
protoVal = isIpv6 ? enr.get("udp6") : enr.get("udp");
break;
case "tcp":
protoVal = isIpv6 ? enr.get("tcp6") : enr.get("tcp");
break;
default:
return;
}
if (!protoVal) return;
return multiaddrFromFields(
isIpv6 ? "ip6" : "ip4",
protoName,
ipVal,
protoVal
);
}