Skip to content

Commit

Permalink
feat: make peerId persistent, fix thrown errors (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko authored Sep 29, 2024
1 parent 31738ba commit eb13f3e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
42 changes: 23 additions & 19 deletions examples/dogfooding/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions examples/dogfooding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
"start": "webpack-dev-server"
},
"dependencies": {
"@libp2p/peer-id": "^4.1.2",
"@libp2p/crypto": "^4.0.1",
"@libp2p/peer-id-factory": "^4.2.4",
"@multiformats/multiaddr": "^12.3.1",
"@waku/sdk": "0.0.28",
"protobufjs": "^7.3.0"
"libp2p": "^1.8.3",
"protobufjs": "^7.3.0",
"uint8arrays": "^5.1.0"
},
"devDependencies": {
"@libp2p/interface": "^1.7.0",
Expand Down
30 changes: 21 additions & 9 deletions examples/dogfooding/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
LightNode,
utils,
} from "@waku/sdk";
import { createFromPrivKey } from "@libp2p/peer-id-factory";
import { unmarshalPrivateKey, generateKeyPairFromSeed } from "@libp2p/crypto/keys";
import { fromString } from "uint8arrays";

import { Type, Field } from "protobufjs";
import {
Expand All @@ -32,18 +35,31 @@ const messageSentEvent = new CustomEvent("messageSent");
const messageReceivedEvent = new CustomEvent("messageReceived");

const wakuNode = async (): Promise<LightNode> => {
let seed = localStorage.getItem("seed");

if (!seed) {
seed = (await sha256(generateRandomNumber())).slice(0, 32)
localStorage.setItem("seed", seed);
}

const privateKey = await generateKeyPairFromSeed("Ed25519", fromString(seed));

return await createLightNode({
networkConfig: {
contentTopics: [DEFAULT_CONTENT_TOPIC],
},
defaultBootstrap: true,
libp2p: {
peerId: await createFromPrivKey(await unmarshalPrivateKey(privateKey.bytes))
}
});
};

export async function app(telemetryClient: TelemetryClient) {
const node = await wakuNode();
(window as any).waku = node;

await node.start();

await waitForRemotePeer(node);

const peerId = node.libp2p.peerId.toString();
Expand Down Expand Up @@ -103,11 +119,8 @@ export async function app(telemetryClient: TelemetryClient) {
timestamp: new Date(),
});

console.log("===");
console.log("light push successes: ", result.successes.length);
console.log(result.successes);
console.log("light push failures: ", result.failures.length);
console.log(result.failures);
console.log("DEBUG: light push successes: ", result.successes.length, result.successes);
console.log("DEBUG: light push failures: ", result.failures.length, result.failures);

const successEvents = result
.successes
Expand Down Expand Up @@ -311,10 +324,9 @@ export async function app(telemetryClient: TelemetryClient) {

(async () => {
const telemetryClient = new TelemetryClient(TELEMETRY_URL, 5000);
const { node, startLightPushSequence, startFilterSubscription } = await app(
const { startLightPushSequence, startFilterSubscription } = await app(
telemetryClient
);
(window as any).waku = node;

const runningScreen = document.getElementById("runningScreen");
runningScreen.style.display = "block";
Expand Down Expand Up @@ -342,7 +354,7 @@ export async function app(telemetryClient: TelemetryClient) {

function startSequence() {
const numMessages = Math.floor(Math.random() * 16) + 5;
const messagePeriod = Math.floor(Math.random() * 2001) + 5000;
const messagePeriod = Math.floor(Math.random() * 2001) + 5_000;
startLightPushSequence(numMessages, messagePeriod);
}

Expand Down
3 changes: 1 addition & 2 deletions examples/dogfooding/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { peerIdFromString } from "@libp2p/peer-id";
import { Peer } from "@libp2p/interface";
import type { LightNode } from "@waku/sdk";

Expand All @@ -20,7 +19,7 @@ export const DEFAULT_EXTRA_DATA_STR = JSON.stringify(DEFAULT_EXTRA_DATA);

export const buildExtraData = async (node: LightNode, peerId: string): Promise<string> => {
let extraData = { ...DEFAULT_EXTRA_DATA };
const peer: Peer = await node.libp2p.peerStore.get(peerIdFromString(peerId));
const peer: Peer = (await node.libp2p.peerStore.all()).find(p => p.id.toString() === peerId);

if (!peer || peerId === node.libp2p.peerId.toString()) {
return JSON.stringify(extraData);
Expand Down

0 comments on commit eb13f3e

Please sign in to comment.