Skip to content

Commit

Permalink
feat: deviceId
Browse files Browse the repository at this point in the history
  • Loading branch information
agazso committed Oct 25, 2023
1 parent 453bcba commit 08528d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/adapters/waku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,13 @@ async function setDoc<T>(ws: Wakustore, contentTopic: ContentTopic, id: string,
export default class WakuAdapter implements Adapter {
private safeWaku = new SafeWaku()
private subscriptions: Array<() => void> = []
private deviceId: string | undefined

async onLogIn(wallet: BaseWallet): Promise<void> {
const address = wallet.address

this.deviceId = get(walletStore).deviceId

const ws = await this.makeWakustore()
const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet)

Expand Down Expand Up @@ -432,6 +435,7 @@ export default class WakuAdapter implements Adapter {
timestamp: Date.now(),
text,
fromAddress,
deviceId: this.deviceId,
}

const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet)
Expand All @@ -452,6 +456,7 @@ export default class WakuAdapter implements Adapter {
type: 'data',
timestamp: Date.now(),
fromAddress,
deviceId: this.deviceId,
objectId,
instanceId,
data,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export interface UserMessage {
type: 'user'
timestamp: number
fromAddress: string
deviceId?: string
text: string
}

export interface DataMessage<T extends JSONSerializable = JSONSerializable> {
type: 'data'
timestamp: number
fromAddress: string
deviceId?: string
objectId: string
instanceId: string
data: T
Expand All @@ -22,6 +24,7 @@ export interface InviteMessage {
type: 'invite'
timestamp: number
fromAddress: string
deviceId?: string
chatId: string
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { Mnemonic12Schema, type Mnemonic12 } from '$lib/utils/schemas'
import { getFromLocalStorage, removeFromLocalStorage, saveToLocalStorage } from '../adapters/utils'
import { writable, get } from 'svelte/store'
import type { Writable } from 'svelte/store'
import { genRandomHex } from '$lib/utils'
import { z } from 'zod'

interface WalletState {
wallet?: HDNodeWallet
deviceId?: string
loading: boolean
error?: Error
}
Expand Down Expand Up @@ -45,6 +48,14 @@ function createWalletStore(): WalletStore {
store.set({ loading: false })
}

const deviceId = getFromLocalStorage<string>('deviceId', z.string())
if (deviceId) {
store.update((state) => ({
...state,
deviceId,
}))
}

return {
...store,
restoreWallet,
Expand All @@ -69,6 +80,9 @@ function createWalletStore(): WalletStore {
store.set({ wallet, loading: false })

storeInLocalstorage(wallet)

const deviceId = genRandomHex(32)
saveToLocalStorage('deviceId', deviceId)
},
}
}
Expand Down

0 comments on commit 08528d1

Please sign in to comment.