Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encrypted chat #488

Merged
merged 11 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"@bonosoft/sveltekit-qrcode": "^0.0.3",
"@fontsource/source-sans-pro": "^5.0.4",
"@multiformats/multiaddr": "^12.1.3",
"@noble/ciphers": "^0.4.0",
"@noble/hashes": "^1.3.2",
"@noble/secp256k1": "^2.0.0",
"@sveltejs/adapter-auto": "^2.1.0",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.21.0",
Expand All @@ -33,7 +36,9 @@
"@waku-objects/luminance": "^2.0.1",
"@waku-objects/sandbox-example": "^0.4.0",
"@waku/interfaces": "^0.0.19",
"@waku/message-encryption": "^0.0.22",
"@waku/sdk": "^0.0.20",
"@waku/utils": "^0.0.12",
"copy-to-clipboard": "^3.3.3",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
46 changes: 45 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 0 additions & 188 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import {
getTransactionResponse,
sendTransaction,
} from '$lib/adapters/transaction'
import { connectWaku, sendMessage } from '$lib/adapters/waku/waku'
import { makeWakustore } from '$lib/adapters/waku/wakustore'
import type { StorageChat, StorageProfile } from '$lib/adapters/waku/types'
import type { Message } from '$lib/stores/chat'
import { PageDirection } from '@waku/interfaces'

async function main() {
const command = process.argv[2]
Expand All @@ -23,7 +18,6 @@ async function main() {
fund,
balance,
txinfo,
waku,
}

const fn = commands[command]
Expand Down Expand Up @@ -72,186 +66,4 @@ 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<string, (...args: string[]) => Promise<void>> = {
profile,
'set-profile': setProfile,
'group-chats': groupChats,
'set-group-chat': setGroupChat,
chats,
chat,
'chat-messages': chatMessages,
'private-message': privateMessage,
objects,
send,
invite,
}

const fn = commands[command]
if (!fn) {
throw `unknown command: ${command}\nUsage: cli waku ${Object.keys(commands).join('|')}`
}

await fn(...restArgs)
}

async function profile(address: string) {
if (!address) {
throw `usage: profile <address>`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

const profile = await ws.getDoc<StorageProfile>('profile', address)
console.log({ profile })
}

async function setProfile(address: string, name: string, avatar?: string) {
if (!address || !name) {
throw `usage: set-profile <address> <name> [avatar]`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

await ws.setDoc<StorageProfile>('profile', address, { name, avatar })
}

async function groupChats(address: string) {
if (!address) {
throw `usage: group-chats <address>`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

const result = await ws.getDoc('group-chats', address)
console.log({ 'group-chats': result })
}

async function setGroupChat(address: string, name: string, avatar: string, ...users: string[]) {
if (!address || !name) {
throw `usage: set-group-chat <address> <name> <avatar> [...users]`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

await ws.setDoc<StorageChat>('group-chats', address, {
name,
avatar,
users,
})
}

async function chats(address: string) {
if (!address) {
throw `usage: chats <address>`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

const chats = await ws.getDoc('chats', address)
console.log({ chats })
}

async function chat(address: string, chatId: string) {
if (!address || !chatId) {
throw `usage: chat <address> [chat-id]`
}
const waku = await connectWaku()
const ws = makeWakustore(waku)

const chats = (await ws.getDoc('chats', address)) as [id: string, chat: unknown][]
const chat = chats.find((c) => c[0] === chatId)
if (chat) {
console.log({ chat: chat[1] })
} else {
console.log(`unknown/invalid chatId: ${chatId}`)
}
}

async function chatMessages(address: string, chatId: string) {
if (!address || !chatId) {
throw `usage: chat-messages <address> [chat-id]`
}
const waku = await connectWaku()
const ws = makeWakustore(waku)

const chats = (await ws.getDoc('chats', address)) as [id: string, chat: unknown][]
const chat = chats.find((c) => c[0] === chatId)
if (chat) {
const messages = (chat[1] as { messages: Message[] })?.messages
for (const message of messages) {
console.log(message)
}
} else {
console.log(`unknown/invalid chatId: ${chatId}`)
}
}

async function privateMessage(address: string) {
if (!address) {
throw `usage: private-message <address>`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)
const subscription = await ws.onSnapshot<Message>(
ws.collectionQuery('private-message', address, {
pageDirection: PageDirection.BACKWARD,
pageSize: 10,
}),
console.log,
)
subscription()
}

async function objects(address: string) {
if (!address) {
throw `usage: objects <address>`
}

const waku = await connectWaku()
const ws = makeWakustore(waku)

const objects = await ws.getDoc('objects', address)
console.log({ objects })
}

async function send(from: string, to: string, message: string) {
if (!from || !to || !message) {
throw `usage: send <from> <to> <message>`
}

const waku = await connectWaku()

await sendMessage(waku, to, {
type: 'user',
fromAddress: from,
text: message,
timestamp: Date.now(),
})
}

async function invite(from: string, to: string, chatId: string) {
if (!from || !to || !chatId) {
throw `usage: invite <from> <to> chat-id>`
}

const waku = await connectWaku()

await sendMessage(waku, to, {
type: 'invite',
fromAddress: from,
chatId,
timestamp: Date.now(),
})
}

main().catch(console.error)
11 changes: 8 additions & 3 deletions src/lib/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import type { JSONSerializable } from '$lib/objects'
export interface Adapter {
onLogIn: (wallet: BaseWallet) => Promise<void>
onLogOut: () => void
saveUserProfile(address: string, name?: string, avatar?: string): Promise<void>
saveUserProfile(
wallet: BaseWallet,
address: string,
name?: string,
avatar?: string,
): Promise<void>
getUserProfile(address: string): Promise<User | undefined>

startChat(address: string, peerAddress: string): Promise<string>
startChat(wallet: BaseWallet, peerAddress: string): Promise<string>
startGroupChat(
wallet: BaseWallet,
chatId: string,
Expand All @@ -30,7 +35,7 @@ export interface Adapter {
instanceId: string,
data: JSONSerializable,
): Promise<void>
sendInvite(wallet: BaseWallet, chatId: string, users: string[]): Promise<void>
sendGroupChatInvite(wallet: BaseWallet, chatId: string, users: string[]): Promise<void>

updateStore(
address: string,
Expand Down
2 changes: 0 additions & 2 deletions src/lib/adapters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export function getFromLocalStorage<T extends JSONdecoded>(
throw new Error(`Error getting from local storage: invalid data. ${parseData.error.issues}`)
}

console.log('Retrieved', key, parseData.data)

return parseData.data
}

Expand Down
Loading