From f5c93bc484906377b7f0a043e0225fe0e380789f Mon Sep 17 00:00:00 2001 From: Attila Gazso <230163+agazso@users.noreply.github.com> Date: Tue, 5 Dec 2023 18:34:43 +0100 Subject: [PATCH] feat: cli waku list command --- src/cli/cli.ts | 76 +++++++++++++++++++++++++++++++++ src/lib/adapters/transaction.ts | 4 +- src/lib/adapters/waku/waku.ts | 4 +- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/cli/cli.ts b/src/cli/cli.ts index af960a56..3f098711 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -9,6 +9,12 @@ import { getTransactionResponse, sendTransaction, } from '$lib/adapters/transaction' +import { SafeWaku } from '$lib/adapters/waku/safe-waku' +import { hexToBytes } from '@waku/utils/bytes' +import { createSymmetricDecoder } from '$lib/adapters/waku/codec' +import { readStore, type QueryResult, decodeMessagePayload } from '$lib/adapters/waku/waku' +import type { DecodedMessage } from '@waku/message-encryption' +import type { ChatMessage } from '$lib/stores/chat' async function main() { const command = process.argv[2] @@ -18,6 +24,7 @@ async function main() { fund, balance, txinfo, + waku, } const fn = commands[command] @@ -66,4 +73,73 @@ async function txinfo(hash: string) { console.log({ tx, receipt }) } +async function waku(...args: string[]) { + const command = args[0] + const restArgs = args.slice(1) + + const commands: Record Promise> = { + list, + } + + const fn = commands[command] + if (!fn) { + throw `unknown command: ${command}\nUsage: cli waku ${Object.keys(commands).join('|')}` + } + + await fn(...restArgs) +} + +async function list(chatId: string) { + if (!chatId) { + throw `usage: list ` + } + + const safeWaku = new SafeWaku() + const waku = await safeWaku.connect() + const encryptionKey = hexToBytes(chatId) + const decoder = createSymmetricDecoder({ + contentTopic: 'private-message', + symKey: encryptionKey, + }) + + const chatMessages: ChatMessage[] = [] + + const result = await readStore(waku, decoder) + const messages = await getQueryResults(result) + + for (const message of messages) { + const value = decodeDoc(message) as ChatMessage + chatMessages.push(value) + } + + console.log( + chatMessages.length, + chatMessages.map((message) => + message.type === 'babble' + ? `${message.id}: ${message.timestamp} ${message.text} ${message.parentId ?? ''}` + : '', + ), + ) +} + +async function getQueryResults(results: QueryResult): Promise { + const decodedMessages: DecodedMessage[] = [] + for await (const messagePromises of results) { + for (const messagePromise of messagePromises) { + const message = await messagePromise + if (message) { + decodedMessages.push(message) + } + } + } + return decodedMessages +} + +function decodeDoc(message: DecodedMessage): T { + const decodedPayload = decodeMessagePayload(message) + const typedPayload = JSON.parse(decodedPayload) as T + + return typedPayload +} + main().catch(console.error) diff --git a/src/lib/adapters/transaction.ts b/src/lib/adapters/transaction.ts index 41ddc38c..93acaac0 100644 --- a/src/lib/adapters/transaction.ts +++ b/src/lib/adapters/transaction.ts @@ -9,7 +9,9 @@ import { Contract, } from 'ethers' import abi from '$lib/abis/erc20.json' -import { PUBLIC_BLOCKCHAIN } from '$env/static/public' +// import { PUBLIC_BLOCKCHAIN } from '$env/static/public' + +const PUBLIC_BLOCKCHAIN = 'gnosis' interface BlockchainExplorer { name: string diff --git a/src/lib/adapters/waku/waku.ts b/src/lib/adapters/waku/waku.ts index c3f9b4d9..8dc36cc7 100644 --- a/src/lib/adapters/waku/waku.ts +++ b/src/lib/adapters/waku/waku.ts @@ -9,10 +9,12 @@ import { type IEncoder, type IDecoder, } from '@waku/interfaces' -import { PUBLIC_WAKU } from '$env/static/public' +// import { PUBLIC_WAKU } from '$env/static/public' import type { DecodedMessage } from '@waku/message-encryption' import { hash } from './crypto' +const PUBLIC_WAKU = 'production' + function getPeers(): string[] { switch (PUBLIC_WAKU) { // Local version in docker