Install `js-waku@^0.30.0`

This commit is contained in:
Felicio Mununga 2022-11-14 23:06:34 +01:00
parent 2edf74c27f
commit d68409eb6f
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
6 changed files with 147 additions and 4575 deletions

View File

@ -97,8 +97,6 @@ For an example, see [examples/with-vite/src/app.tsx](./examples/with-vite/src/ap
**Simply visit your Community's URL from the browser:**
For example, <https://status-devcon.vercel.app/>.
If the Community doesn't require a joining request, that would be it. So look around and get the feel for the space.
**Once ready to chat, create a throwaway profile:**

View File

@ -32,7 +32,7 @@
},
"dependencies": {
"ethereum-cryptography": "^1.0.3",
"js-waku": "^0.25.0",
"js-waku": "^0.30.0",
"long": "^5.2.0",
"protobufjs": "^6.11.3",
"protons-runtime": "^1.0.4"

View File

@ -1,4 +1,5 @@
import { PageDirection } from 'js-waku'
import { SymDecoder } from 'js-waku/lib/waku_message/version_1'
import { containsOnlyEmoji } from '../helpers/contains-only-emoji'
import {
@ -21,7 +22,6 @@ import type { Client } from './client'
import type { Community } from './community/community'
import type { Reactions } from './community/get-reactions'
import type { Member } from './member'
import type { WakuMessage } from 'js-waku'
export type ChatMessage = ChatMessageProto & {
messageId: string
@ -197,7 +197,14 @@ export class Chat {
endTime = new Date()
}
await this.client.waku.store.queryHistory([this.contentTopic], {
await this.client.waku.store.queryOrderedCallback(
[new SymDecoder(this.contentTopic, this.symmetricKey)],
wakuMessage => {
this.#fetchingMessages = true
this.client.handleWakuMessage(wakuMessage)
this.#fetchingMessages = false
},
{
timeFilter: {
startTime: startTime,
endTime: endTime,
@ -205,18 +212,8 @@ export class Chat {
pageSize: 50,
// most recent page first
pageDirection: PageDirection.BACKWARD,
decryptionKeys: [this.symmetricKey],
callback: (wakuMessages: WakuMessage[]) => {
let index = wakuMessages.length
this.#fetchingMessages = true
// most recent message first
while (--index >= 0) {
this.client.handleWakuMessage(wakuMessages[index])
}
this.#fetchingMessages = false
},
})
)
this.#previousFetchedStartTime = startTime

View File

@ -3,10 +3,11 @@
*/
import { hexToBytes } from 'ethereum-cryptography/utils'
import { Protocols, WakuMessage } from 'js-waku'
import { createWaku } from 'js-waku/lib/create_waku'
import { Protocols } from 'js-waku'
import { createFullNode } from 'js-waku/lib/create_waku'
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
import { SymEncoder } from 'js-waku/lib/waku_message/version_1'
import { peers } from '../consts/peers'
import { ApplicationMetadataMessage } from '../protos/application-metadata-message'
@ -17,7 +18,7 @@ import { handleWakuMessage } from './community/handle-waku-message'
import { LocalStorage } from './storage'
import type { Storage } from './storage'
import type { Waku } from 'js-waku'
import type { Message as WakuMessage, WakuFull } from 'js-waku/lib/interfaces'
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
@ -35,7 +36,7 @@ export interface ClientOptions {
}
class Client {
public waku: Waku
public waku: WakuFull
public readonly wakuMessages: Set<string>
/**
* Tracks open connections which had their streams silently destroyed
@ -60,7 +61,7 @@ class Client {
storage: Storage
constructor(
waku: Waku,
waku: WakuFull,
wakuDisconnectionTimer: ReturnType<typeof setInterval>,
options: ClientOptions
) {
@ -89,7 +90,7 @@ class Client {
// Waku
const { environment = 'production' } = options
const waku = await createWaku({
const waku = await createFullNode({
defaultBootstrap: false,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
@ -188,12 +189,14 @@ class Client {
payload,
})
const wakuMesage = await WakuMessage.fromBytes(message, contentTopic, {
sigPrivKey: hexToBytes(this.#account.privateKey),
await this.waku.relay.send(
new SymEncoder(
contentTopic,
symKey,
})
await this.waku.relay.send(wakuMesage)
hexToBytes(this.#account.privateKey)
),
{ payload: message }
)
}
public handleWakuMessage = (wakuMessage: WakuMessage): void => {

View File

@ -1,5 +1,5 @@
import { hexToBytes } from 'ethereum-cryptography/utils'
import { waku_message } from 'js-waku'
import { SymDecoder } from 'js-waku/lib/waku_message/version_1'
import { getDifferenceByKeys } from '../../helpers/get-difference-by-keys'
import { getObjectsDifference } from '../../helpers/get-objects-difference'
@ -31,6 +31,7 @@ export class Community {
public chats: Map<string, Chat>
#members: Map<string, Member>
#callbacks: Set<(description: CommunityDescription) => void>
#chatUnobserveFns: Map<string, () => void>
constructor(client: Client, publicKey: string) {
this.client = client
@ -42,20 +43,13 @@ export class Community {
this.chats = new Map()
this.#members = new Map()
this.#callbacks = new Set()
this.#chatUnobserveFns = new Map()
}
public async start() {
this.contentTopic = idToContentTopic(this.publicKey)
this.symmetricKey = await generateKeyFromPassword(this.publicKey)
// Waku
this.client.waku.store.addDecryptionKey(this.symmetricKey, {
contentTopics: [this.contentTopic],
})
this.client.waku.relay.addDecryptionKey(this.symmetricKey, {
contentTopics: [this.contentTopic],
})
// Community
const description = await this.fetch()
@ -89,28 +83,25 @@ export class Community {
public fetch = async () => {
// most recent page first
await this.client.waku.store.queryHistory([this.contentTopic], {
callback: wakuMessages => {
let index = wakuMessages.length
// most recent message first
while (--index >= 0) {
this.client.handleWakuMessage(wakuMessages[index])
await this.client.waku.store.queryOrderedCallback(
[new SymDecoder(this.contentTopic, this.symmetricKey)],
wakuMessage => {
this.client.handleWakuMessage(wakuMessage)
if (this.description) {
return true
}
}
},
})
)
return this.description
}
private observe = () => {
this.client.waku.relay.addObserver(this.client.handleWakuMessage, [
this.contentTopic,
])
this.client.waku.relay.addObserver(
new SymDecoder(this.contentTopic, this.symmetricKey),
this.client.handleWakuMessage
)
}
private observeChatMessages = async (
@ -125,25 +116,19 @@ export class Community {
MessageType.COMMUNITY_CHAT,
chatDescription
)
const contentTopic = chat.contentTopic
this.chats.set(chatUuid, chat)
this.client.waku.relay.addDecryptionKey(chat.symmetricKey, {
method: waku_message.DecryptionMethod.Symmetric,
contentTopics: [contentTopic],
})
const unobserveFn = this.client.waku.relay.addObserver(
new SymDecoder(chat.contentTopic, chat.symmetricKey),
this.client.handleWakuMessage
)
return contentTopic
this.#chatUnobserveFns.set(chat.contentTopic, unobserveFn)
}
)
const contentTopics = await Promise.all(chatPromises)
this.client.waku.relay.addObserver(
this.client.handleWakuMessage,
contentTopics
)
await Promise.all(chatPromises)
}
private unobserveChatMessages = (
@ -168,9 +153,8 @@ export class Community {
return
}
this.client.waku.relay.deleteObserver(
this.client.handleWakuMessage,
contentTopics
contentTopics.forEach(contentTopic =>
this.#chatUnobserveFns.get(contentTopic)?.()
)
}

4596
yarn.lock

File diff suppressed because it is too large Load Diff