mirror of https://github.com/waku-org/js-waku.git
fix: Multiaddr from fields function
This commit is contained in:
parent
ab9919ba6d
commit
6d5c2acb99
|
@ -347,7 +347,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||
const isIpv6 = protocol.endsWith("6");
|
||||
const ipVal = this.get(isIpv6 ? "ip6" : "ip");
|
||||
if (!ipVal) {
|
||||
return undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
const isUdp = protocol.startsWith("udp");
|
||||
|
@ -367,7 +367,12 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||
return;
|
||||
}
|
||||
|
||||
return mulitaddrFromFields(isIpv6, protoName, ipVal, protoVal);
|
||||
return mulitaddrFromFields(
|
||||
isIpv6 ? "ip6" : "ip4",
|
||||
protoName,
|
||||
ipVal,
|
||||
protoVal
|
||||
);
|
||||
}
|
||||
|
||||
setLocationMultiaddr(multiaddr: Multiaddr): void {
|
||||
|
|
|
@ -8,8 +8,17 @@ describe("Multiaddr from fields", () => {
|
|||
const ipBytes = convertToBytes("ip4", "1.2.3.4");
|
||||
const portBytes = convertToBytes("tcp", "3333");
|
||||
|
||||
const ma = mulitaddrFromFields(false, "tcp", ipBytes, portBytes);
|
||||
const ma = mulitaddrFromFields("ip4", "tcp", ipBytes, portBytes);
|
||||
|
||||
expect(ma.toString()).to.eq("/ip4/1.2.3.4/tcp/3333");
|
||||
});
|
||||
|
||||
it("udp ip6 address", function () {
|
||||
const ipBytes = convertToBytes("ip6", "2345:425:2ca1::5673:23b5");
|
||||
const portBytes = convertToBytes("udp", "1111");
|
||||
|
||||
const ma = mulitaddrFromFields("ip6", "udp", ipBytes, portBytes);
|
||||
|
||||
expect(ma.toString()).to.eq("/ip6/2345:425:2ca1::5673:23b5/udp/1111");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
import { Multiaddr } from "@multiformats/multiaddr";
|
||||
import { convertToString } from "@multiformats/multiaddr/convert";
|
||||
|
||||
export function mulitaddrFromFields(
|
||||
isIpv6: boolean,
|
||||
ipFamily: string,
|
||||
protocol: string,
|
||||
ipBytes: Uint8Array,
|
||||
portBytes: Uint8Array
|
||||
protocolBytes: Uint8Array
|
||||
): Multiaddr {
|
||||
const familyStr = isIpv6 ? "ip6" : "ip4";
|
||||
let ma = new Multiaddr("/" + familyStr);
|
||||
let ma = new Multiaddr(
|
||||
"/" + ipFamily + "/" + convertToString(ipFamily, ipBytes)
|
||||
);
|
||||
|
||||
ma = ma.encapsulate(new Multiaddr(ipBytes));
|
||||
ma = ma.encapsulate(new Multiaddr("/" + protocol));
|
||||
ma = ma.encapsulate(portBytes);
|
||||
ma = ma.encapsulate(
|
||||
new Multiaddr(
|
||||
"/" + protocol + "/" + convertToString(protocol, protocolBytes)
|
||||
)
|
||||
);
|
||||
|
||||
return ma;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue