mirror of
https://github.com/status-im/js-waku.git
synced 2025-02-24 19:08:12 +00:00
* chore: update noise * update: package.lock * update: @chainsafe/libp2p-gossipsub * rm unwanted libp2p interface deps & bump up libp2p * refactor code for new deps * update: new package.lock * setup prettier, refactor eslint and rm trailing commas * update package.lock * fix build * import type for interface * fix imports for merge * update typedoc exports * add: CustomEvent import * use new libp2p interface * add aegir as dev dep for tests
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { multiaddr } from "@multiformats/multiaddr";
|
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
|
|
import { MULTIADDR_LENGTH_SIZE } from "./constants.js";
|
|
|
|
export function decodeMultiaddrs(bytes: Uint8Array): Multiaddr[] {
|
|
const multiaddrs = [];
|
|
|
|
let index = 0;
|
|
|
|
while (index < bytes.length) {
|
|
const sizeDataView = new DataView(
|
|
bytes.buffer,
|
|
index,
|
|
MULTIADDR_LENGTH_SIZE
|
|
);
|
|
const size = sizeDataView.getUint16(0);
|
|
index += MULTIADDR_LENGTH_SIZE;
|
|
|
|
const multiaddrBytes = bytes.slice(index, index + size);
|
|
index += size;
|
|
|
|
multiaddrs.push(multiaddr(multiaddrBytes));
|
|
}
|
|
return multiaddrs;
|
|
}
|
|
|
|
export function encodeMultiaddrs(multiaddrs: Multiaddr[]): Uint8Array {
|
|
const totalLength = multiaddrs.reduce(
|
|
(acc, ma) => acc + MULTIADDR_LENGTH_SIZE + ma.bytes.length,
|
|
0
|
|
);
|
|
const bytes = new Uint8Array(totalLength);
|
|
const dataView = new DataView(bytes.buffer);
|
|
|
|
let index = 0;
|
|
multiaddrs.forEach((multiaddr) => {
|
|
if (multiaddr.getPeerId())
|
|
throw new Error("`multiaddr` field MUST not contain peer id");
|
|
|
|
// Prepend the size of the next entry
|
|
dataView.setUint16(index, multiaddr.bytes.length);
|
|
index += MULTIADDR_LENGTH_SIZE;
|
|
|
|
bytes.set(multiaddr.bytes, index);
|
|
index += multiaddr.bytes.length;
|
|
});
|
|
|
|
return bytes;
|
|
}
|