Skip to content

Commit

Permalink
update readme and add accurast impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
od41 committed Mar 11, 2024
1 parent 1c2d1f2 commit 13d773b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 12 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

> This project was bootstrpped with the ![](!inkathon template). [/setup.md](setup instructions)
> This project was bootstrpped with the [!inkathon template](/SETUP.md)

---
Expand All @@ -8,19 +8,18 @@

Whisper is an app where privacy focused individuals can have ephemeral conversations that disappears almost like a whisper, right after they leave.

[Live Demo](https://whisper-chat-git-main-od41.vercel.app/)
[Live Demo](https://blue-whisper.vercel.app/)



## How it works

1. Get some test tokens for aleph zero and accurast
2. Install your talisman wallet
3. Connect your wallet
4. Create a chatroom
5. Invite your friends
1. Get some test tokens for Aleph Zero or Accurast
2. Install a wallet([Talisman](https://talisman.xyz/) is a good option)
4. Create or join a chatroom
5. Invite your friends (if you created)
6. Talk and talk
7. Destroy chatroom and all messages
7. Delete chatroom and all messages

[![Whisper cover photo](https://images.unsplash.com/photo-1576769267415-9642010aa962?q=80&w=1886&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D)](https://youtube.com)

Expand Down
5 changes: 4 additions & 1 deletion frontend/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ NEXT_PUBLIC_DEFAULT_CHAIN=alephzero-testnet

## [Optional] Multiple supported chain identifers the frontend connects to
## IMPORTANT: It's mandatory to use double quotes in the array
# NEXT_PUBLIC_SUPPORTED_CHAINS=[ "development", "alephzero-testnet", "rococo", "shibuya" ]
# NEXT_PUBLIC_SUPPORTED_CHAINS=[ "development", "alephzero-testnet", "rococo", "shibuya" ]

## Accurast websocket url
# NEXT_PUBLIC_ACCU_DEV_WSS=wss://websocket-proxy.dev.gke.papers.tech/
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@727-ventures/typechain-types": "^1.1.2",
"@acurast/dapp": "^1.0.0",
"@azns/resolver-core": "^1.6.0",
"@azns/resolver-react": "^1.6.0",
"@hookform/resolvers": "^3.3.4",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const env = {
description: 'Lorem ipsum',
},
],
ACCU_DEV_WSS: process.env.NEXT_PUBLIC_ACCU_DEV_WSS!,
}
11 changes: 11 additions & 0 deletions frontend/src/context/app-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { createContext, useEffect, useState } from 'react'

import { ContractIds } from '@/deployments/deployments'
import { useAccurast } from '@/deployments/use-accurast'
import {
contractQuery,
decodeOutput,
Expand All @@ -23,6 +24,7 @@ type AppContextProps = {
inviteFriends: (participants: string[]) => Promise<void>
joinChatroom: (chatroomId: string) => Promise<void>
refreshMessages: () => Promise<void>
viaAccurast: () => Promise<void>
messages: MessageProps[]
chatroomId: string | undefined
isAppLoading: boolean
Expand All @@ -39,6 +41,7 @@ const defaultData: AppContextProps = {
inviteFriends: async (participants: string[]) => {},
joinChatroom: async (chatroomId: string) => {},
refreshMessages: async () => {},
viaAccurast: async () => {},
messages: [],
chatroomId: undefined,
isAppLoading: true,
Expand All @@ -61,6 +64,7 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
// web3 state
const { api, activeAccount, activeSigner } = useInkathon()
const { contract } = useRegisteredContract(ContractIds.Chatroom)
const { id, send } = useAccurast()

// set app as loaded
useEffect(() => {
Expand Down Expand Up @@ -260,6 +264,12 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
setIsMessagesLoading(false)
}

// accurast
async function viaAccurast() {
// TODO additional testing & implementation
const res = await send(activeAccount?.address, '')
}

return (
<AppContext.Provider
value={{
Expand All @@ -274,6 +284,7 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
inviteFriends,
joinChatroom,
sendMessage,
viaAccurast,
messages,
chatroomId,
}}
Expand Down
78 changes: 78 additions & 0 deletions frontend/src/deployments/use-accurast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useEffect, useMemo, useState } from 'react'

import { AcurastClient } from '@acurast/dapp'

import { env } from '@/config/environment'

const DEV_WSS = env.ACCU_DEV_WSS

export const useAccurast = () => {
const [acurastClient, setAcurastClient] = useState<any>(null)
const [id, setId] = useState<string>('')
const [keyPair, setKeyPair] = useState()
const [message, setMessage] = useState()

useEffect(() => {
const init = async () => {
const acurast = new AcurastClient(DEV_WSS)
setAcurastClient(acurast)

const keyPair = await crypto.subtle.generateKey(
{
name: 'ECDSA',
namedCurve: 'P-256',
},
true,
['sign'],
)

const [privateKeyRaw, publicKeyRaw] = await Promise.all([
// @ts-expect-error can't find the argument that matches call
crypto.subtle
.exportKey('jwk', keyPair.privateKey)
.then((jwk) => Buffer.from(jwk.d, 'base64')),
crypto.subtle
.exportKey('raw', keyPair.publicKey)
.then((arrayBuffer) => Buffer.from(arrayBuffer)),
])

const publicKeyCompressedSize = (publicKeyRaw.length - 1) / 2
const publicKeyCompressed = Buffer.concat([
new Uint8Array([publicKeyRaw[2 * publicKeyCompressedSize] % 2 ? 3 : 2]),
publicKeyRaw.subarray(1, publicKeyCompressedSize + 1),
])
const publicKeyHash = await crypto.subtle.digest('SHA-256', publicKeyCompressed)
setId(Buffer.from(publicKeyHash.slice(0, 16)).toString('hex'))
setKeyPair({
// @ts-expect-error allow
privateKey: privateKeyRaw.toString('hex'),
publicKey: publicKeyRaw.toString('hex'),
})

return () => {
acurast.close()
}
}

init()
}, [])

const send = (recipient: any, payload: any) => {
acurastClient.send(recipient, payload)
}

useEffect(() => {
if (acurastClient) {
acurastClient.onMessage((message: any) => {
setMessage({
// @ts-expect-error allow
sender: Buffer.from(message.sender).toString('hex'),
recipient: Buffer.from(message.recipient).toString('hex'),
payload: Buffer.from(message.payload).toString('hex'),
})
})
}
}, [acurastClient])

return { id, keyPair, message, send }
}
114 changes: 111 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 13d773b

Please sign in to comment.