2022-12-13 12:31:27 +11:00
|
|
|
import type { Waku2 } from "@waku/interfaces";
|
2022-05-04 20:07:21 +10:00
|
|
|
|
|
|
|
export function encodeWaku2(protocols: Waku2): number {
|
|
|
|
let byte = 0;
|
|
|
|
|
|
|
|
if (protocols.lightPush) byte += 1;
|
|
|
|
byte = byte << 1;
|
|
|
|
if (protocols.filter) byte += 1;
|
|
|
|
byte = byte << 1;
|
|
|
|
if (protocols.store) byte += 1;
|
|
|
|
byte = byte << 1;
|
|
|
|
if (protocols.relay) byte += 1;
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decodeWaku2(byte: number): Waku2 {
|
|
|
|
const waku2 = {
|
|
|
|
relay: false,
|
|
|
|
store: false,
|
|
|
|
filter: false,
|
2023-08-16 20:18:13 +05:30
|
|
|
lightPush: false
|
2022-05-04 20:07:21 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
if (byte % 2) waku2.relay = true;
|
|
|
|
byte = byte >> 1;
|
|
|
|
if (byte % 2) waku2.store = true;
|
|
|
|
byte = byte >> 1;
|
|
|
|
if (byte % 2) waku2.filter = true;
|
|
|
|
byte = byte >> 1;
|
|
|
|
if (byte % 2) waku2.lightPush = true;
|
|
|
|
|
|
|
|
return waku2;
|
|
|
|
}
|