chore(relay-js): bump js-waku to 0.29.0

This commit is contained in:
fryorcraken.eth 2022-09-20 14:34:06 +10:00
parent 8b020a32e1
commit 9b3093bee4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 15 additions and 29 deletions

View File

@ -26,15 +26,10 @@
* Recommended payload is protobuf. Using simple utf-8 string for demo purposes only. * Recommended payload is protobuf. Using simple utf-8 string for demo purposes only.
*/ */
import { import {utils} from 'https://unpkg.com/js-waku@0.29.0/bundle/index.js';
WakuMessage import {createPrivacyNode} from 'https://unpkg.com/js-waku@0.29.0/bundle/lib/create_waku.js'
} from 'https://unpkg.com/js-waku@0.28.1/bundle/index.js'; import {waitForRemotePeer} from 'https://unpkg.com/js-waku@0.29.0/bundle/lib/wait_for_remote_peer.js'
import { import {DecoderV0, EncoderV0} from "https://unpkg.com/js-waku@0.29.0/bundle/lib/waku_message/version_0.js";
createPrivacyNode
} from 'https://unpkg.com/js-waku@0.28.1/bundle/lib/create_waku.js'
import {
waitForRemotePeer
} from 'https://unpkg.com/js-waku@0.28.1/bundle/lib/wait_for_remote_peer.js'
const statusDiv = document.getElementById('status'); const statusDiv = document.getElementById('status');
const messagesDiv = document.getElementById('messages'); const messagesDiv = document.getElementById('messages');
@ -52,6 +47,11 @@
// for simplicity's sake. // for simplicity's sake.
const contentTopic = '/js-waku-examples/1/chat/utf8'; const contentTopic = '/js-waku-examples/1/chat/utf8';
// Prepare encoder and decoder, `V0` for clear text messages.
const encoder = new EncoderV0(contentTopic);
const decoder = new DecoderV0(contentTopic);
try { try {
statusDiv.innerHTML = '<p>Starting</p>'; statusDiv.innerHTML = '<p>Starting</p>';
@ -59,20 +59,20 @@
// `default: true` bootstraps by connecting to pre-defined/hardcoded Waku nodes. // `default: true` bootstraps by connecting to pre-defined/hardcoded Waku nodes.
// We are currently working on migrating this method to DNS Discovery. // We are currently working on migrating this method to DNS Discovery.
// //
// https://js-waku.wakuconnect.dev/classes/waku.Waku.html#create // https://js.waku.org/functions/lib_create_waku.createPrivacyNode.html
const waku = await createPrivacyNode({defaultBootstrap: true}); const waku = await createPrivacyNode({defaultBootstrap: true});
await waku.start(); await waku.start();
// Had a hook to process all incoming messages on a specified content topic. // Had a hook to process all incoming messages on a specified content topic.
// //
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#addObserver // https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#addObserver
waku.relay.addObserver((wakuMessage) => { waku.relay.addObserver(decoder, (message) => {
// Checks there is a payload on the message. // Checks there is a payload on the message.
// Waku Message is encoded in protobuf, in proto v3 fields are always optional. // Waku Message is encoded in protobuf, in proto v3 fields are always optional.
// //
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payload // https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payload
if (!wakuMessage.payload) if (!message.payload)
return; return;
// Helper method to decode the payload to utf-8. A production dApp should // Helper method to decode the payload to utf-8. A production dApp should
@ -80,7 +80,7 @@
// structure of their choice. // structure of their choice.
// //
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payloadAsUtf8 // https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payloadAsUtf8
const text = wakuMessage.payloadAsUtf8; const text = utils.bytesToUtf8(message.payload);
messagesDiv.innerHTML = `<p>${text}</p><br />` + messagesDiv.innerHTML; messagesDiv.innerHTML = `<p>${text}</p><br />` + messagesDiv.innerHTML;
}, [contentTopic]); }, [contentTopic]);
@ -96,22 +96,8 @@
// function that sends the text input over Waku Relay, the gossipsub // function that sends the text input over Waku Relay, the gossipsub
// protocol. // protocol.
sendButton.onclick = async () => { sendButton.onclick = async () => {
const text = textInput.value; const payload = utils.utf8ToBytes(textInput.value)
await waku.relay.send(encoder, {payload});
// Helper functions are available to create a Waku Message.
// These functions also provide native symmetric, asymmetric encryption,
// signing and signature verification. Check the `Options` object for details:
// https://js-waku.wakuconnect.dev/interfaces/waku_message.Options.html
//
// `WakuMessage.fromBytes` should be preferred for a production dApp to
// serialize a data structure.
//
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#fromUtf8String
const wakuMessage = await WakuMessage.fromUtf8String(text, contentTopic);
// Once the message is constructed, send it over Waku Relay.
//
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#send
await waku.relay.send(wakuMessage);
console.log('Message sent!'); console.log('Message sent!');
// Reset the text input. // Reset the text input.