Install `js-waku@^0.30.0`
This commit is contained in:
parent
478553959b
commit
fc0e2305e8
|
@ -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:**
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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,26 +197,23 @@ export class Chat {
|
|||
endTime = new Date()
|
||||
}
|
||||
|
||||
await this.client.waku.store.queryHistory([this.contentTopic], {
|
||||
timeFilter: {
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
},
|
||||
pageSize: 50,
|
||||
// most recent page first
|
||||
pageDirection: PageDirection.BACKWARD,
|
||||
decryptionKeys: [this.symmetricKey],
|
||||
callback: (wakuMessages: WakuMessage[]) => {
|
||||
let index = wakuMessages.length
|
||||
|
||||
await this.client.waku.store.queryOrderedCallback(
|
||||
[new SymDecoder(this.contentTopic, this.symmetricKey)],
|
||||
wakuMessage => {
|
||||
this.#fetchingMessages = true
|
||||
// most recent message first
|
||||
while (--index >= 0) {
|
||||
this.client.handleWakuMessage(wakuMessages[index])
|
||||
}
|
||||
this.client.handleWakuMessage(wakuMessage)
|
||||
this.#fetchingMessages = false
|
||||
},
|
||||
})
|
||||
{
|
||||
timeFilter: {
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
},
|
||||
pageSize: 50,
|
||||
// most recent page first
|
||||
pageDirection: PageDirection.BACKWARD,
|
||||
}
|
||||
)
|
||||
|
||||
this.#previousFetchedStartTime = startTime
|
||||
|
||||
|
|
|
@ -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),
|
||||
symKey,
|
||||
})
|
||||
|
||||
await this.waku.relay.send(wakuMesage)
|
||||
await this.waku.relay.send(
|
||||
new SymEncoder(
|
||||
contentTopic,
|
||||
symKey,
|
||||
hexToBytes(this.#account.privateKey)
|
||||
),
|
||||
{ payload: message }
|
||||
)
|
||||
}
|
||||
|
||||
public handleWakuMessage = (wakuMessage: WakuMessage): void => {
|
||||
|
|
|
@ -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
|
||||
await this.client.waku.store.queryOrderedCallback(
|
||||
[new SymDecoder(this.contentTopic, this.symmetricKey)],
|
||||
wakuMessage => {
|
||||
this.client.handleWakuMessage(wakuMessage)
|
||||
|
||||
// most recent message first
|
||||
while (--index >= 0) {
|
||||
this.client.handleWakuMessage(wakuMessages[index])
|
||||
|
||||
if (this.description) {
|
||||
return true
|
||||
}
|
||||
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)?.()
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue