UNPKG

@waku/enr

Version:
157 lines 5.01 kB
import { convertToBytes, convertToString } from "@multiformats/multiaddr/convert"; import { decodeRelayShard } from "@waku/utils"; import { bytesToUtf8 } from "@waku/utils/bytes"; import { ERR_INVALID_ID } from "./constants.js"; import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js"; import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js"; export class RawEnr extends Map { seq; signature; constructor(kvs = {}, seq = BigInt(1), signature) { super(Object.entries(kvs)); this.seq = seq; this.signature = signature; } set(k, v) { this.signature = undefined; this.seq++; return super.set(k, v); } get id() { const id = this.get("id"); if (!id) throw new Error("id not found."); return bytesToUtf8(id); } get publicKey() { switch (this.id) { case "v4": return this.get("secp256k1"); default: throw new Error(ERR_INVALID_ID); } } get rs() { const rs = this.get("rs"); if (!rs) return undefined; return decodeRelayShard(rs); } get rsv() { const rsv = this.get("rsv"); if (!rsv) return undefined; return decodeRelayShard(rsv); } get ip() { return getStringValue(this, "ip", "ip4"); } set ip(ip) { setStringValue(this, "ip", "ip4", ip); } get tcp() { return getNumberAsStringValue(this, "tcp", "tcp"); } set tcp(port) { setNumberAsStringValue(this, "tcp", "tcp", port); } get udp() { return getNumberAsStringValue(this, "udp", "udp"); } set udp(port) { setNumberAsStringValue(this, "udp", "udp", port); } get ip6() { return getStringValue(this, "ip6", "ip6"); } set ip6(ip) { setStringValue(this, "ip6", "ip6", ip); } get tcp6() { return getNumberAsStringValue(this, "tcp6", "tcp"); } set tcp6(port) { setNumberAsStringValue(this, "tcp6", "tcp", port); } get udp6() { return getNumberAsStringValue(this, "udp6", "udp"); } set udp6(port) { setNumberAsStringValue(this, "udp6", "udp", port); } /** * Get the `multiaddrs` field from ENR. * * This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys. * These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host * address (e.g. `dns4`, `dnsaddr`, etc).. * * If the peer information only contains information that can be represented with the ENR pre-defined keys * (ip, tcp, etc) then the usage of { @link ENR.getLocationMultiaddr } should be preferred. * * The multiaddresses stored in this field are expected to be location multiaddresses, ie, peer id less. */ get multiaddrs() { const raw = this.get("multiaddrs"); if (raw) return decodeMultiaddrs(raw); return; } /** * Set the `multiaddrs` field on the ENR. * * This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys. * These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host * address (e.g. `dns4`, `dnsaddr`, etc).. * * If the peer information only contains information that can be represented with the ENR pre-defined keys * (ip, tcp, etc) then the usage of { @link ENR.setLocationMultiaddr } should be preferred. * The multiaddresses stored in this field must be location multiaddresses, * ie, without a peer id. */ set multiaddrs(multiaddrs) { deleteUndefined(this, "multiaddrs", multiaddrs, encodeMultiaddrs); } /** * Get the `waku2` field from ENR. */ get waku2() { const raw = this.get("waku2"); if (raw) return decodeWaku2(raw[0]); return; } /** * Set the `waku2` field on the ENR. */ set waku2(waku2) { deleteUndefined(this, "waku2", waku2, (w) => new Uint8Array([encodeWaku2(w)])); } } function getStringValue(map, key, proto) { const raw = map.get(key); if (!raw) return; return convertToString(proto, raw); } function getNumberAsStringValue(map, key, proto) { const raw = map.get(key); if (!raw) return; return Number(convertToString(proto, raw)); } function setStringValue(map, key, proto, value) { deleteUndefined(map, key, value, convertToBytes.bind({}, proto)); } function setNumberAsStringValue(map, key, proto, value) { setStringValue(map, key, proto, value?.toString(10)); } function deleteUndefined(map, key, value, transform) { if (value !== undefined) { map.set(key, transform(value)); } else { map.delete(key); } } //# sourceMappingURL=raw_enr.js.map