Install `js-waku@^0.30.0`
This commit is contained in:
parent
2edf74c27f
commit
d68409eb6f
|
@ -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:**
|
**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.
|
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:**
|
**Once ready to chat, create a throwaway profile:**
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ethereum-cryptography": "^1.0.3",
|
"ethereum-cryptography": "^1.0.3",
|
||||||
"js-waku": "^0.25.0",
|
"js-waku": "^0.30.0",
|
||||||
"long": "^5.2.0",
|
"long": "^5.2.0",
|
||||||
"protobufjs": "^6.11.3",
|
"protobufjs": "^6.11.3",
|
||||||
"protons-runtime": "^1.0.4"
|
"protons-runtime": "^1.0.4"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { PageDirection } from 'js-waku'
|
import { PageDirection } from 'js-waku'
|
||||||
|
import { SymDecoder } from 'js-waku/lib/waku_message/version_1'
|
||||||
|
|
||||||
import { containsOnlyEmoji } from '../helpers/contains-only-emoji'
|
import { containsOnlyEmoji } from '../helpers/contains-only-emoji'
|
||||||
import {
|
import {
|
||||||
|
@ -21,7 +22,6 @@ import type { Client } from './client'
|
||||||
import type { Community } from './community/community'
|
import type { Community } from './community/community'
|
||||||
import type { Reactions } from './community/get-reactions'
|
import type { Reactions } from './community/get-reactions'
|
||||||
import type { Member } from './member'
|
import type { Member } from './member'
|
||||||
import type { WakuMessage } from 'js-waku'
|
|
||||||
|
|
||||||
export type ChatMessage = ChatMessageProto & {
|
export type ChatMessage = ChatMessageProto & {
|
||||||
messageId: string
|
messageId: string
|
||||||
|
@ -197,7 +197,14 @@ export class Chat {
|
||||||
endTime = new Date()
|
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: {
|
timeFilter: {
|
||||||
startTime: startTime,
|
startTime: startTime,
|
||||||
endTime: endTime,
|
endTime: endTime,
|
||||||
|
@ -205,18 +212,8 @@ export class Chat {
|
||||||
pageSize: 50,
|
pageSize: 50,
|
||||||
// most recent page first
|
// most recent page first
|
||||||
pageDirection: PageDirection.BACKWARD,
|
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
|
this.#previousFetchedStartTime = startTime
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { hexToBytes } from 'ethereum-cryptography/utils'
|
import { hexToBytes } from 'ethereum-cryptography/utils'
|
||||||
import { Protocols, WakuMessage } from 'js-waku'
|
import { Protocols } from 'js-waku'
|
||||||
import { createWaku } from 'js-waku/lib/create_waku'
|
import { createFullNode } from 'js-waku/lib/create_waku'
|
||||||
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
||||||
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
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 { peers } from '../consts/peers'
|
||||||
import { ApplicationMetadataMessage } from '../protos/application-metadata-message'
|
import { ApplicationMetadataMessage } from '../protos/application-metadata-message'
|
||||||
|
@ -17,7 +18,7 @@ import { handleWakuMessage } from './community/handle-waku-message'
|
||||||
import { LocalStorage } from './storage'
|
import { LocalStorage } from './storage'
|
||||||
|
|
||||||
import type { Storage } 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'
|
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ export interface ClientOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public waku: Waku
|
public waku: WakuFull
|
||||||
public readonly wakuMessages: Set<string>
|
public readonly wakuMessages: Set<string>
|
||||||
/**
|
/**
|
||||||
* Tracks open connections which had their streams silently destroyed
|
* Tracks open connections which had their streams silently destroyed
|
||||||
|
@ -60,7 +61,7 @@ class Client {
|
||||||
storage: Storage
|
storage: Storage
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
waku: Waku,
|
waku: WakuFull,
|
||||||
wakuDisconnectionTimer: ReturnType<typeof setInterval>,
|
wakuDisconnectionTimer: ReturnType<typeof setInterval>,
|
||||||
options: ClientOptions
|
options: ClientOptions
|
||||||
) {
|
) {
|
||||||
|
@ -89,7 +90,7 @@ class Client {
|
||||||
// Waku
|
// Waku
|
||||||
const { environment = 'production' } = options
|
const { environment = 'production' } = options
|
||||||
|
|
||||||
const waku = await createWaku({
|
const waku = await createFullNode({
|
||||||
defaultBootstrap: false,
|
defaultBootstrap: false,
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -188,12 +189,14 @@ class Client {
|
||||||
payload,
|
payload,
|
||||||
})
|
})
|
||||||
|
|
||||||
const wakuMesage = await WakuMessage.fromBytes(message, contentTopic, {
|
await this.waku.relay.send(
|
||||||
sigPrivKey: hexToBytes(this.#account.privateKey),
|
new SymEncoder(
|
||||||
|
contentTopic,
|
||||||
symKey,
|
symKey,
|
||||||
})
|
hexToBytes(this.#account.privateKey)
|
||||||
|
),
|
||||||
await this.waku.relay.send(wakuMesage)
|
{ payload: message }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleWakuMessage = (wakuMessage: WakuMessage): void => {
|
public handleWakuMessage = (wakuMessage: WakuMessage): void => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { hexToBytes } from 'ethereum-cryptography/utils'
|
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 { getDifferenceByKeys } from '../../helpers/get-difference-by-keys'
|
||||||
import { getObjectsDifference } from '../../helpers/get-objects-difference'
|
import { getObjectsDifference } from '../../helpers/get-objects-difference'
|
||||||
|
@ -31,6 +31,7 @@ export class Community {
|
||||||
public chats: Map<string, Chat>
|
public chats: Map<string, Chat>
|
||||||
#members: Map<string, Member>
|
#members: Map<string, Member>
|
||||||
#callbacks: Set<(description: CommunityDescription) => void>
|
#callbacks: Set<(description: CommunityDescription) => void>
|
||||||
|
#chatUnobserveFns: Map<string, () => void>
|
||||||
|
|
||||||
constructor(client: Client, publicKey: string) {
|
constructor(client: Client, publicKey: string) {
|
||||||
this.client = client
|
this.client = client
|
||||||
|
@ -42,20 +43,13 @@ export class Community {
|
||||||
this.chats = new Map()
|
this.chats = new Map()
|
||||||
this.#members = new Map()
|
this.#members = new Map()
|
||||||
this.#callbacks = new Set()
|
this.#callbacks = new Set()
|
||||||
|
this.#chatUnobserveFns = new Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start() {
|
public async start() {
|
||||||
this.contentTopic = idToContentTopic(this.publicKey)
|
this.contentTopic = idToContentTopic(this.publicKey)
|
||||||
this.symmetricKey = await generateKeyFromPassword(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
|
// Community
|
||||||
const description = await this.fetch()
|
const description = await this.fetch()
|
||||||
|
|
||||||
|
@ -89,28 +83,25 @@ export class Community {
|
||||||
|
|
||||||
public fetch = async () => {
|
public fetch = async () => {
|
||||||
// most recent page first
|
// most recent page first
|
||||||
await this.client.waku.store.queryHistory([this.contentTopic], {
|
await this.client.waku.store.queryOrderedCallback(
|
||||||
callback: wakuMessages => {
|
[new SymDecoder(this.contentTopic, this.symmetricKey)],
|
||||||
let index = wakuMessages.length
|
wakuMessage => {
|
||||||
|
this.client.handleWakuMessage(wakuMessage)
|
||||||
// most recent message first
|
|
||||||
while (--index >= 0) {
|
|
||||||
this.client.handleWakuMessage(wakuMessages[index])
|
|
||||||
|
|
||||||
if (this.description) {
|
if (this.description) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
)
|
||||||
})
|
|
||||||
|
|
||||||
return this.description
|
return this.description
|
||||||
}
|
}
|
||||||
|
|
||||||
private observe = () => {
|
private observe = () => {
|
||||||
this.client.waku.relay.addObserver(this.client.handleWakuMessage, [
|
this.client.waku.relay.addObserver(
|
||||||
this.contentTopic,
|
new SymDecoder(this.contentTopic, this.symmetricKey),
|
||||||
])
|
this.client.handleWakuMessage
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private observeChatMessages = async (
|
private observeChatMessages = async (
|
||||||
|
@ -125,25 +116,19 @@ export class Community {
|
||||||
MessageType.COMMUNITY_CHAT,
|
MessageType.COMMUNITY_CHAT,
|
||||||
chatDescription
|
chatDescription
|
||||||
)
|
)
|
||||||
const contentTopic = chat.contentTopic
|
|
||||||
|
|
||||||
this.chats.set(chatUuid, chat)
|
this.chats.set(chatUuid, chat)
|
||||||
|
|
||||||
this.client.waku.relay.addDecryptionKey(chat.symmetricKey, {
|
const unobserveFn = this.client.waku.relay.addObserver(
|
||||||
method: waku_message.DecryptionMethod.Symmetric,
|
new SymDecoder(chat.contentTopic, chat.symmetricKey),
|
||||||
contentTopics: [contentTopic],
|
this.client.handleWakuMessage
|
||||||
})
|
)
|
||||||
|
|
||||||
return contentTopic
|
this.#chatUnobserveFns.set(chat.contentTopic, unobserveFn)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const contentTopics = await Promise.all(chatPromises)
|
await Promise.all(chatPromises)
|
||||||
|
|
||||||
this.client.waku.relay.addObserver(
|
|
||||||
this.client.handleWakuMessage,
|
|
||||||
contentTopics
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private unobserveChatMessages = (
|
private unobserveChatMessages = (
|
||||||
|
@ -168,9 +153,8 @@ export class Community {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.waku.relay.deleteObserver(
|
contentTopics.forEach(contentTopic =>
|
||||||
this.client.handleWakuMessage,
|
this.#chatUnobserveFns.get(contentTopic)?.()
|
||||||
contentTopics
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue