mirror of
https://github.com/status-im/js-waku.git
synced 2025-02-22 09:58:10 +00:00
fix: Do not use names
import
This commit is contained in:
parent
b9aef19a47
commit
157e42e148
3
package-lock.json
generated
3
package-lock.json
generated
@ -34,8 +34,7 @@
|
|||||||
"multiformats": "^9.6.5",
|
"multiformats": "^9.6.5",
|
||||||
"protons-runtime": "^1.0.4",
|
"protons-runtime": "^1.0.4",
|
||||||
"uint8arrays": "^3.0.0",
|
"uint8arrays": "^3.0.0",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2"
|
||||||
"varint": "^6.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
||||||
|
@ -91,8 +91,7 @@
|
|||||||
"multiformats": "^9.6.5",
|
"multiformats": "^9.6.5",
|
||||||
"protons-runtime": "^1.0.4",
|
"protons-runtime": "^1.0.4",
|
||||||
"uint8arrays": "^3.0.0",
|
"uint8arrays": "^3.0.0",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2"
|
||||||
"varint": "^6.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
||||||
|
@ -4,12 +4,10 @@ import { Multiaddr } from "@multiformats/multiaddr";
|
|||||||
import {
|
import {
|
||||||
convertToBytes,
|
convertToBytes,
|
||||||
convertToString,
|
convertToString,
|
||||||
} from "@multiformats/multiaddr/src/convert";
|
} from "@multiformats/multiaddr/convert";
|
||||||
import { names } from "@multiformats/multiaddr/src/protocols-table";
|
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
import { fromString } from "uint8arrays/from-string";
|
import { fromString } from "uint8arrays/from-string";
|
||||||
import { toString } from "uint8arrays/to-string";
|
import { toString } from "uint8arrays/to-string";
|
||||||
import { encode as varintEncode } from "varint";
|
|
||||||
|
|
||||||
import { compressPublicKey, keccak256, verifySignature } from "../crypto";
|
import { compressPublicKey, keccak256, verifySignature } from "../crypto";
|
||||||
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
||||||
@ -22,6 +20,7 @@ import {
|
|||||||
IKeypair,
|
IKeypair,
|
||||||
KeypairType,
|
KeypairType,
|
||||||
} from "./keypair";
|
} from "./keypair";
|
||||||
|
import { mulitaddrFromFields } from "./multiaddr_from_fields";
|
||||||
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
|
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
|
||||||
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
|
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
|
||||||
import * as v4 from "./v4";
|
import * as v4 from "./v4";
|
||||||
@ -192,7 +191,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
get ip(): string | undefined {
|
get ip(): string | undefined {
|
||||||
const raw = this.get("ip");
|
const raw = this.get("ip");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return convertToString(names.ip4.code, raw) as string;
|
return convertToString("ip4", raw) as string;
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -200,7 +199,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
|
|
||||||
set ip(ip: string | undefined) {
|
set ip(ip: string | undefined) {
|
||||||
if (ip) {
|
if (ip) {
|
||||||
this.set("ip", convertToBytes(names.ip4.code, ip));
|
this.set("ip", convertToBytes("ip4", ip));
|
||||||
} else {
|
} else {
|
||||||
this.delete("ip");
|
this.delete("ip");
|
||||||
}
|
}
|
||||||
@ -209,7 +208,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
get tcp(): number | undefined {
|
get tcp(): number | undefined {
|
||||||
const raw = this.get("tcp");
|
const raw = this.get("tcp");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return Number(convertToString(names.tcp.code, raw));
|
return Number(convertToString("tcp", raw));
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -219,14 +218,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
if (port === undefined) {
|
if (port === undefined) {
|
||||||
this.delete("tcp");
|
this.delete("tcp");
|
||||||
} else {
|
} else {
|
||||||
this.set("tcp", convertToBytes(names.tcp.code, port.toString(10)));
|
this.set("tcp", convertToBytes("tcp", port.toString(10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get udp(): number | undefined {
|
get udp(): number | undefined {
|
||||||
const raw = this.get("udp");
|
const raw = this.get("udp");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return Number(convertToString(names.udp.code, raw));
|
return Number(convertToString("udp", raw));
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -236,14 +235,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
if (port === undefined) {
|
if (port === undefined) {
|
||||||
this.delete("udp");
|
this.delete("udp");
|
||||||
} else {
|
} else {
|
||||||
this.set("udp", convertToBytes(names.udp.code, port.toString(10)));
|
this.set("udp", convertToBytes("udp", port.toString(10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get ip6(): string | undefined {
|
get ip6(): string | undefined {
|
||||||
const raw = this.get("ip6");
|
const raw = this.get("ip6");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return convertToString(names.ip6.code, raw) as string;
|
return convertToString("ip6", raw) as string;
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -251,7 +250,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
|
|
||||||
set ip6(ip: string | undefined) {
|
set ip6(ip: string | undefined) {
|
||||||
if (ip) {
|
if (ip) {
|
||||||
this.set("ip6", convertToBytes(names.ip6.code, ip));
|
this.set("ip6", convertToBytes("ip6", ip));
|
||||||
} else {
|
} else {
|
||||||
this.delete("ip6");
|
this.delete("ip6");
|
||||||
}
|
}
|
||||||
@ -260,7 +259,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
get tcp6(): number | undefined {
|
get tcp6(): number | undefined {
|
||||||
const raw = this.get("tcp6");
|
const raw = this.get("tcp6");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return Number(convertToString(names.tcp.code, raw));
|
return Number(convertToString("tcp", raw));
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -270,14 +269,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
if (port === undefined) {
|
if (port === undefined) {
|
||||||
this.delete("tcp6");
|
this.delete("tcp6");
|
||||||
} else {
|
} else {
|
||||||
this.set("tcp6", convertToBytes(names.tcp.code, port.toString(10)));
|
this.set("tcp6", convertToBytes("tcp", port.toString(10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get udp6(): number | undefined {
|
get udp6(): number | undefined {
|
||||||
const raw = this.get("udp6");
|
const raw = this.get("udp6");
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return Number(convertToString(names.udp.code, raw));
|
return Number(convertToString("udp", raw));
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -287,7 +286,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
if (port === undefined) {
|
if (port === undefined) {
|
||||||
this.delete("udp6");
|
this.delete("udp6");
|
||||||
} else {
|
} else {
|
||||||
this.set("udp6", convertToBytes(names.udp.code, port.toString(10)));
|
this.set("udp6", convertToBytes("udp", port.toString(10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,29 +360,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
protoName = "tcp";
|
protoName = "tcp";
|
||||||
protoVal = isIpv6 ? this.get("tcp6") : this.get("tcp");
|
protoVal = isIpv6 ? this.get("tcp6") : this.get("tcp");
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!protoVal) {
|
if (!protoVal) {
|
||||||
return undefined;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create raw multiaddr buffer
|
return mulitaddrFromFields(isIpv6, protoName, ipVal, protoVal);
|
||||||
// multiaddr length is:
|
|
||||||
// 1 byte for the ip protocol (ip4 or ip6)
|
|
||||||
// N bytes for the ip address
|
|
||||||
// 1 or 2 bytes for the protocol as buffer (tcp or udp)
|
|
||||||
// 2 bytes for the port
|
|
||||||
const ipMa = names[isIpv6 ? "ip6" : "ip4"];
|
|
||||||
const ipByteLen = ipMa.size / 8;
|
|
||||||
const protoMa = names[protoName];
|
|
||||||
const protoBuf = varintEncode(protoMa.code);
|
|
||||||
const maBuf = new Uint8Array(3 + ipByteLen + protoBuf.length);
|
|
||||||
maBuf[0] = ipMa.code;
|
|
||||||
maBuf.set(ipVal, 1);
|
|
||||||
maBuf.set(protoBuf, 1 + ipByteLen);
|
|
||||||
maBuf.set(protoVal, 1 + ipByteLen + protoBuf.length);
|
|
||||||
|
|
||||||
return new Multiaddr(maBuf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setLocationMultiaddr(multiaddr: Multiaddr): void {
|
setLocationMultiaddr(multiaddr: Multiaddr): void {
|
||||||
|
15
src/lib/enr/multiaddr_from_fields.spec.ts
Normal file
15
src/lib/enr/multiaddr_from_fields.spec.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { convertToBytes } from "@multiformats/multiaddr/convert";
|
||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { mulitaddrFromFields } from "./multiaddr_from_fields";
|
||||||
|
|
||||||
|
describe("Multiaddr from fields", () => {
|
||||||
|
it("tcp ip4 address", function () {
|
||||||
|
const ipBytes = convertToBytes("ip4", "1.2.3.4");
|
||||||
|
const portBytes = convertToBytes("tcp", "3333");
|
||||||
|
|
||||||
|
const ma = mulitaddrFromFields(false, "tcp", ipBytes, portBytes);
|
||||||
|
|
||||||
|
expect(ma.toString()).to.eq("/ip4/1.2.3.4/tcp/3333");
|
||||||
|
});
|
||||||
|
});
|
17
src/lib/enr/multiaddr_from_fields.ts
Normal file
17
src/lib/enr/multiaddr_from_fields.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { Multiaddr } from "@multiformats/multiaddr";
|
||||||
|
|
||||||
|
export function mulitaddrFromFields(
|
||||||
|
isIpv6: boolean,
|
||||||
|
protocol: string,
|
||||||
|
ipBytes: Uint8Array,
|
||||||
|
portBytes: Uint8Array
|
||||||
|
): Multiaddr {
|
||||||
|
const familyStr = isIpv6 ? "ip6" : "ip4";
|
||||||
|
let ma = new Multiaddr("/" + familyStr);
|
||||||
|
|
||||||
|
ma = ma.encapsulate(new Multiaddr(ipBytes));
|
||||||
|
ma = ma.encapsulate(new Multiaddr("/" + protocol));
|
||||||
|
ma = ma.encapsulate(portBytes);
|
||||||
|
|
||||||
|
return ma;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user