fix: use js-waku's and libp2p's new API

This commit is contained in:
fryorcraken.eth 2022-09-01 14:12:25 +10:00
parent 11659ada59
commit 5c1063528e
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 58 additions and 216 deletions

View File

@ -5,10 +5,8 @@
"homepage": "/examples/web-chat",
"dependencies": {
"@livechat/ui-kit": "^0.5.0-20",
"@multiformats/multiaddr": "^10.4.0",
"js-waku": "0.24.0-71ca4c9",
"libp2p-interfaces": "^4.0.6",
"multiaddr": "^10.0.1",
"peer-id": "^0.16.0",
"protobufjs": "^7.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",

View File

@ -2,6 +2,7 @@ lockfileVersion: 5.4
specifiers:
'@livechat/ui-kit': ^0.5.0-20
'@multiformats/multiaddr': ^10.4.0
'@types/jest': ^27.5.0
'@types/node': ^17.0.32
'@types/react': ^17.0.39
@ -9,10 +10,7 @@ specifiers:
cspell: ^6.0.0
gh-pages: ^4.0.0
js-waku: 0.24.0-71ca4c9
libp2p-interfaces: ^4.0.6
multiaddr: ^10.0.1
npm-run-all: ^4.1.5
peer-id: ^0.16.0
prettier: ^2.6.2
protobufjs: ^7.0.0
react: ^17.0.2
@ -24,10 +22,8 @@ specifiers:
dependencies:
'@livechat/ui-kit': 0.5.0-20_sk3eihvpffgp52mstba5zhq3vu
'@multiformats/multiaddr': 10.4.0
js-waku: 0.24.0-71ca4c9_x2utdhayajzrh747hktprshhby
libp2p-interfaces: 4.0.6
multiaddr: 10.0.1
peer-id: 0.16.0
protobufjs: 7.0.0
react: 17.0.2
react-dom: 17.0.2_react@17.0.2
@ -4209,12 +4205,6 @@ packages:
resolution: {integrity: sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA==}
dev: true
/abortable-iterator/3.0.2:
resolution: {integrity: sha512-qVP8HFfTpUQI2F+f1tpTriKDIZ4XrmwCrBCrQeRKO7DKWF3kgoT6NXiNDv2krrGcHxPwmI63eGQiec81sEaWIw==}
dependencies:
get-iterator: 1.0.2
dev: false
/abortable-iterator/4.0.2:
resolution: {integrity: sha512-SJGELER5yXr9v3kiL6mT5RZ1qlyJ9hV4nm34+vfsdIM1lp3zENQvpsqKgykpFLgRMUn3lzlizLTpiOASW05/+g==}
dependencies:
@ -4736,10 +4726,6 @@ packages:
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
dev: true
/base64-js/1.5.1:
resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
dev: false
/batch/0.6.1:
resolution: {integrity: sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==}
dev: true
@ -4763,14 +4749,6 @@ packages:
engines: {node: '>=8'}
dev: true
/bl/5.0.0:
resolution: {integrity: sha512-8vxFNZ0pflFfi0WXA3WQXlj6CaMEwsmh63I1CNp0q+wWv8sD0ARx1KovSQd0l2GkwrMIOyedq0EF1FxI+RCZLQ==}
dependencies:
buffer: 6.0.3
inherits: 2.0.4
readable-stream: 3.6.0
dev: false
/bluebird/3.7.2:
resolution: {integrity: sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==}
dev: true
@ -4852,13 +4830,6 @@ packages:
/buffer-from/1.1.2:
resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==}
/buffer/6.0.3:
resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==}
dependencies:
base64-js: 1.5.1
ieee754: 1.2.1
dev: false
/builtin-modules/3.3.0:
resolution: {integrity: sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==}
engines: {node: '>=6'}
@ -5118,10 +5089,6 @@ packages:
resolution: {integrity: sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==}
dev: true
/class-is/1.1.0:
resolution: {integrity: sha512-rhjH9AG1fvabIDoGRVH587413LPjTZgmDF9fOFCbFJQV4yuocX1mHxxvXI4g3cGwbVY9wAYIoKlg1N79frJKQw==}
dev: false
/clean-css/5.3.1:
resolution: {integrity: sha512-lCr8OHhiWCTw4v8POJovCoh4T7I9U11yVsPjMWWnnMmp9ZowCxyad1Pathle/9HjaDp+fdQKjO9fQydE6RHTZg==}
engines: {node: '>= 10.0'}
@ -5930,17 +5897,6 @@ packages:
resolution: {integrity: sha512-z+paD6YUQsk+AbGCEM4PrOXSss5gd66QfcVBFTKR/HpFL9jCqikS94HYwKww6fQyO7IxrIIyUu+g0Ka9tUS2Cg==}
dev: true
/dns-over-http-resolver/1.2.3:
resolution: {integrity: sha512-miDiVSI6KSNbi4SVifzO/reD8rMnxgrlnkrlkugOLQpWQTe2qMdHsZp5DmfKjxNE+/T3VAAYLQUZMv9SMr6+AA==}
dependencies:
debug: 4.3.4
native-fetch: 3.0.0
receptacle: 1.3.2
transitivePeerDependencies:
- node-fetch
- supports-color
dev: false
/dns-over-http-resolver/2.1.0:
resolution: {integrity: sha512-eb8RGy6k54JdD7Rjw8g65y1MyA4z3m3IIYh7uazkgZuKIdFn8gYt8dydMm3op+2UshDdk9EexrXcDluKNY/CDg==}
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
@ -7031,10 +6987,6 @@ packages:
has-symbols: 1.0.3
dev: true
/get-iterator/1.0.2:
resolution: {integrity: sha512-v+dm9bNVfOYsY1OrhaCrmyOcYoSeVvbt+hHZ0Au+T+p1y+0Uyj9aMaGIeUTT6xdpRbWzDeYKvfOslPhggQMcsg==}
dev: false
/get-iterator/2.0.0:
resolution: {integrity: sha512-BDJawD5PU2gZv6Vlp8O28H4GnZcsr3h9gZUvnAP5xXP3WOy/QAoOsyMepSkw21jur+4t5Vppde72ChjhTIzxzg==}
dev: false
@ -7456,10 +7408,6 @@ packages:
harmony-reflect: 1.6.2
dev: true
/ieee754/1.2.1:
resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
dev: false
/ignore/5.2.0:
resolution: {integrity: sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==}
engines: {node: '>= 4'}
@ -7503,6 +7451,7 @@ packages:
/inherits/2.0.4:
resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==}
dev: true
/ini/1.3.8:
resolution: {integrity: sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==}
@ -7775,14 +7724,6 @@ packages:
/isexe/2.0.0:
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
/iso-random-stream/2.0.2:
resolution: {integrity: sha512-yJvs+Nnelic1L2vH2JzWvvPQFA4r7kSTnpST/+LkAQjSz0hos2oqLD+qIVi9Qk38Hoe7mNDt3j0S27R58MVjLQ==}
engines: {node: '>=10'}
dependencies:
events: 3.3.0
readable-stream: 3.6.0
dev: false
/iso-url/1.2.1:
resolution: {integrity: sha512-9JPDgCN4B7QPkLtYAAOrEuAWvP9rWvR5offAr0/SeF046wIkglqH3VXgYYP6NcsKslH80UIVgmPqNe3j7tG2ng==}
engines: {node: '>=12'}
@ -7872,14 +7813,6 @@ packages:
uint8arraylist: 2.3.2
dev: false
/it-length-prefixed/5.0.3:
resolution: {integrity: sha512-b+jDHLcnOnPDQN79ronmzF5jeBjdJsy0ce2O6i6X4J5tnaO8Fd146ZA/tMbzaLlKnTpXa0eKtofpYhumXGENeg==}
dependencies:
bl: 5.0.0
buffer: 6.0.3
varint: 6.0.0
dev: false
/it-length-prefixed/7.0.1:
resolution: {integrity: sha512-UozKoT0zZPUa0LO9OSq5KaLKPn83U7Vsy/BNAN0TUXfTI/pKrOz6RuyTSOok6NDad12FZsShBGnl9DKlfDT95g==}
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
@ -7929,10 +7862,6 @@ packages:
uint8arraylist: 2.3.2
dev: false
/it-pipe/1.1.0:
resolution: {integrity: sha512-lF0/3qTVeth13TOnHVs0BTFaziwQF7m5Gg+E6JV0BXcLKutC92YjSi7bASgkPOXaLEb+YvNZrPorGMBIJvZfxg==}
dev: false
/it-pipe/2.0.4:
resolution: {integrity: sha512-lK0BV0egwfc64DFJva+0Jh1z8UxwmYBpAHDwq21s0OenRCaEDIntx/iOyWH/jg5efBU6Xa8igzmOqm2CPPNDgg==}
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
@ -8779,41 +8708,6 @@ packages:
type-check: 0.4.0
dev: true
/libp2p-crypto/0.21.2:
resolution: {integrity: sha512-EXFrhSpiHtJ+/L8xXDvQNK5VjUMG51u878jzZcaT5XhuN/zFg6PWJFnl/qB2Y2j7eMWnvCRP7Kp+ua2H36cG4g==}
engines: {node: '>=12.0.0'}
dependencies:
'@noble/ed25519': 1.7.0
'@noble/secp256k1': 1.6.3
err-code: 3.0.1
iso-random-stream: 2.0.2
multiformats: 9.7.1
node-forge: 1.3.1
protobufjs: 6.11.3
uint8arrays: 3.1.0
dev: false
/libp2p-interfaces/4.0.6:
resolution: {integrity: sha512-3KjzNEIWhi+VoOamLvgKKUE/xqwxSw/JYqsBnfMhAWVRvRtosROtVT03wci2XbuuowCYw+/hEX1xKJIR1w5n0A==}
dependencies:
abortable-iterator: 3.0.2
debug: 4.3.4
err-code: 3.0.1
it-length-prefixed: 5.0.3
it-pipe: 1.1.0
it-pushable: 1.4.2
libp2p-crypto: 0.21.2
multiaddr: 10.0.1
multiformats: 9.7.1
p-queue: 6.6.2
peer-id: 0.16.0
protobufjs: 6.11.3
uint8arrays: 3.1.0
transitivePeerDependencies:
- node-fetch
- supports-color
dev: false
/libp2p/0.38.0-0e7096d:
resolution: {integrity: sha512-OrdnREspiwmgFdd2WagZcwZsRnks/EwWOTOql2Kg5cmpdbrGP/SEElbC/kUiYIQVVj8PjA4HQhxyD0q8wFGm9w==}
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
@ -9181,20 +9075,6 @@ packages:
/ms/2.1.3:
resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}
/multiaddr/10.0.1:
resolution: {integrity: sha512-G5upNcGzEGuTHkzxezPrrD6CaIHR9uo+7MwqhNVcXTs33IInon4y7nMiGxl2CY5hG7chvYQUQhz5V52/Qe3cbg==}
dependencies:
dns-over-http-resolver: 1.2.3
err-code: 3.0.1
is-ip: 3.1.0
multiformats: 9.7.1
uint8arrays: 3.1.0
varint: 6.0.0
transitivePeerDependencies:
- node-fetch
- supports-color
dev: false
/multicast-dns/7.2.5:
resolution: {integrity: sha512-2eznPJP8z2BFLX50tf0LuODrpINqP1RVIm/CObbTcBRITQgmC/TjcREF1NeTBzIcR5XO/ukWo+YHOjBbFwIupg==}
hasBin: true
@ -9223,12 +9103,6 @@ packages:
hasBin: true
dev: false
/native-fetch/3.0.0:
resolution: {integrity: sha512-G3Z7vx0IFb/FQ4JxvtqGABsOTIqRWvgQz6e+erkB+JJD6LrszQtMozEHI4EkmgZQvnGHrpLVzUWk7t4sJCIkVw==}
peerDependencies:
node-fetch: '*'
dev: false
/native-fetch/4.0.2:
resolution: {integrity: sha512-4QcVlKFtv2EYVS5MBgsGX5+NWKtbDbIECdUXDBGDMAZXq3Jkv9zf+y8iS7Ub8fEdga3GpYeazp9gauNqXHJOCg==}
peerDependencies:
@ -9519,11 +9393,6 @@ packages:
p-defer: 3.0.0
dev: false
/p-finally/1.0.0:
resolution: {integrity: sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow==}
engines: {node: '>=4'}
dev: false
/p-limit/1.3.0:
resolution: {integrity: sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==}
engines: {node: '>=4'}
@ -9580,14 +9449,6 @@ packages:
p-limit: 3.1.0
dev: true
/p-queue/6.6.2:
resolution: {integrity: sha512-RwFpb72c/BhQLEXIZ5K2e+AhgNVmIejGlTgiB9MzZ0e93GRvqZ7uSi0dvRF7/XIXDeNkra2fNHBxTyPDGySpjQ==}
engines: {node: '>=8'}
dependencies:
eventemitter3: 4.0.7
p-timeout: 3.2.0
dev: false
/p-queue/7.3.0:
resolution: {integrity: sha512-5fP+yVQ0qp0rEfZoDTlP2c3RYBgxvRsw30qO+VtPPc95lyvSG+x6USSh1TuLB4n96IO6I8/oXQGsTgtna4q2nQ==}
engines: {node: '>=12'}
@ -9625,13 +9486,6 @@ packages:
p-reflect: 3.1.0
dev: false
/p-timeout/3.2.0:
resolution: {integrity: sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==}
engines: {node: '>=8'}
dependencies:
p-finally: 1.0.0
dev: false
/p-timeout/5.1.0:
resolution: {integrity: sha512-auFDyzzzGZZZdHz3BtET9VEz0SE/uMEAx7uWfGPucfzEwwe/xH0iVeZibQmANYE/hp9T2+UUZT5m+BKyrDp3Ew==}
engines: {node: '>=12'}
@ -9747,17 +9601,6 @@ packages:
resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==}
engines: {node: '>=8'}
/peer-id/0.16.0:
resolution: {integrity: sha512-EmL7FurFUduU9m1PS9cfJ5TAuCvxKQ7DKpfx3Yj6IKWyBRtosriFuOag/l3ni/dtPgPLwiA4R9IvpL7hsDLJuQ==}
engines: {node: '>=15.0.0'}
dependencies:
class-is: 1.1.0
libp2p-crypto: 0.21.2
multiformats: 9.7.1
protobufjs: 6.11.3
uint8arrays: 3.1.0
dev: false
/performance-now/2.1.0:
resolution: {integrity: sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==}
dev: true
@ -11091,6 +10934,7 @@ packages:
inherits: 2.0.4
string_decoder: 1.3.0
util-deprecate: 1.0.2
dev: true
/readdirp/3.6.0:
resolution: {integrity: sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==}
@ -11338,6 +11182,7 @@ packages:
/safe-buffer/5.2.1:
resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==}
dev: true
/safer-buffer/2.1.2:
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
@ -11814,6 +11659,7 @@ packages:
resolution: {integrity: sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==}
dependencies:
safe-buffer: 5.2.1
dev: true
/stringify-object/3.3.0:
resolution: {integrity: sha512-rHqiFh1elqCQ9WPLIC8I0Q/g/wj5J1eMkyoiD6eoQApWHP0FtlK7rqnhmabL5VUY9JQCcqwwvlOaSuutekgyrw==}
@ -12571,6 +12417,7 @@ packages:
/util-deprecate/1.0.2:
resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}
dev: true
/util.promisify/1.0.1:
resolution: {integrity: sha512-g9JpC/3He3bm38zsLupWryXHoEcS22YHthuPQSJdMy6KNrzIRzWqcsHzD/WUnqe45whVou4VIsPew37DoXWNrA==}

View File

@ -1,11 +1,14 @@
import { useEffect, useReducer, useState } from "react";
import "./App.css";
import {
discovery,
getPredefinedBootstrapNodes,
PageDirection,
Protocols,
Waku,
WakuFilter,
WakuLightPush,
WakuMessage,
WakuRelay,
WakuStore,
} from "js-waku";
import handleCommand from "./command";
import Room from "./Room";
@ -13,6 +16,13 @@ import { WakuContext } from "./WakuContext";
import { ThemeProvider } from "@livechat/ui-kit";
import { generate } from "server-name-generator";
import { Message } from "./Message";
import {
Fleet,
getPredefinedBootstrapNodes,
} from "js-waku/lib/predefined_bootstrap_nodes";
import { waitForRemotePeer } from "js-waku/lib/wait_for_remote_peer";
import { PeerDiscoveryStaticPeers } from "js-waku/lib/peer_discovery_static_list";
import { defaultLibp2p } from "js-waku/lib/create_waku";
const themes = {
AuthorName: {
@ -130,7 +140,7 @@ export default function App() {
if (historicalMessagesRetrieved) return;
const retrieveMessages = async () => {
await waku.waitForRemotePeer();
await waitForRemotePeer(waku, [Protocols.Relay, Protocols.Store]);
console.log(`Retrieving archived messages`);
try {
@ -175,19 +185,22 @@ export default function App() {
async function initWaku(setter: (waku: Waku) => void) {
try {
const waku = await Waku.create({
libp2p: {
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
},
bootstrap: {
peers: getPredefinedBootstrapNodes(selectFleetEnv()),
},
const wakuRelay = new WakuRelay({ emitSelf: true });
const libp2p = await defaultLibp2p(wakuRelay, {
peerDiscovery: [
new PeerDiscoveryStaticPeers(
getPredefinedBootstrapNodes(selectFleetEnv())
),
],
});
const wakuStore = new WakuStore(libp2p);
// TODO: Remove these two declarations once there are optional in js-waku
const wakuLightPush = new WakuLightPush(libp2p);
const wakuFilter = new WakuFilter(libp2p);
const waku = new Waku({}, libp2p, wakuStore, wakuLightPush, wakuFilter);
await waku.start();
setter(waku);
} catch (e) {
@ -198,9 +211,9 @@ async function initWaku(setter: (waku: Waku) => void) {
function selectFleetEnv() {
// Works with react-scripts
if (process?.env?.NODE_ENV === "development") {
return discovery.predefined.Fleet.Test;
return Fleet.Test;
} else {
return discovery.predefined.Fleet.Prod;
return Fleet.Prod;
}
}

View File

@ -41,7 +41,8 @@ export default function MessageInput(props: Props) {
// Enable the button if there are relay peers available or the user is sending a command
const activeButton =
(waku && waku.relay.getPeers().size !== 0) || inputText.startsWith("/");
(waku && waku.relay.getMeshPeers().length !== 0) ||
inputText.startsWith("/");
return (
<TextComposer

View File

@ -24,8 +24,8 @@ export default function Room(props: Props) {
if (!waku) return;
// Update relay peer count on heartbeat
waku.relay.on("gossipsub:heartbeat", () => {
setRelayPeers(waku.relay.getPeers().size);
waku.relay.addEventListener("gossipsub:heartbeat", () => {
setRelayPeers(waku.relay.getMeshPeers().length);
});
}, [waku]);
@ -33,13 +33,9 @@ export default function Room(props: Props) {
if (!waku) return;
// Update store peer when new peer connected & identified
waku.libp2p.peerStore.on("change:protocols", async () => {
let counter = 0;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _peer of waku.store.peers) {
counter++;
}
setStorePeers(counter);
waku.libp2p.peerStore.addEventListener("change:protocols", async () => {
const peers = await waku.store.peers();
setStorePeers(peers.length);
});
}, [waku]);

View File

@ -1,5 +1,4 @@
import { multiaddr } from "multiaddr";
import PeerId from "peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import { Waku } from "js-waku";
function help(): string[] {
@ -26,7 +25,7 @@ function info(waku: Waku | undefined): string[] {
if (!waku) {
return ["Waku node is starting"];
}
return [`PeerId: ${waku.libp2p.peerId.toB58String()}`];
return [`PeerId: ${waku.libp2p.peerId.toString()}`];
}
function connect(peer: string | undefined, waku: Waku | undefined): string[] {
@ -42,9 +41,7 @@ function connect(peer: string | undefined, waku: Waku | undefined): string[] {
if (!peerId) {
return ["Peer Id needed to dial"];
}
waku.addPeerToAddressBook(PeerId.createFromB58String(peerId), [
peerMultiaddr,
]);
waku.addPeerToAddressBook(peerId, [peerMultiaddr]);
return [
`${peerId}: ${peerMultiaddr.toString()} added to address book, autodial in progress`,
];
@ -58,13 +55,10 @@ async function peers(waku: Waku | undefined): Promise<string[]> {
return ["Waku node is starting"];
}
let response: string[] = [];
const peers = [];
const peers = await waku.libp2p.peerStore.all();
for await (const peer of waku.libp2p.peerStore.getPeers()) {
peers.push(peer);
}
Array.from(peers).forEach((peer) => {
response.push(peer.id.toB58String() + ":");
response.push(peer.id.toString() + ":");
let addresses = " addresses: [";
peer.addresses.forEach(({ multiaddr }) => {
addresses += " " + multiaddr.toString() + ",";
@ -88,21 +82,14 @@ function connections(waku: Waku | undefined): string[] {
return ["Waku node is starting"];
}
let response: string[] = [];
waku.libp2p.connections.forEach(
(
connections: import("libp2p-interfaces/src/connection/connection")[],
peerId
) => {
response.push(peerId + ":");
let strConnections = " connections: [";
connections.forEach((connection) => {
strConnections += JSON.stringify(connection.stat);
strConnections += "; " + JSON.stringify(connection.streams);
});
strConnections += "]";
response.push(strConnections);
}
);
let strConnections = " connections: \n";
waku.libp2p.connectionManager.getConnections().forEach((connection) => {
strConnections += connection.remotePeer.toString() + ", ";
strConnections += JSON.stringify(connection.stat);
strConnections += "; " + JSON.stringify(connection.streams);
strConnections += "\n";
});
response.push(strConnections);
if (response.length === 0) {
response.push("Not connected to any peer.");
}