feat: use nip04 for encryption and decryption of userData to store on blossom server #162

Merged
enes merged 4 commits from nip44 into staging 2024-08-22 13:52:20 +00:00
4 changed files with 32 additions and 18 deletions

View File

@ -18,7 +18,7 @@ jobs:
node-version: 18 node-version: 18
- name: Audit - name: Audit
run: npm audit run: npm audit --omit=dev
- name: Install Dependencies - name: Install Dependencies
run: npm ci run: npm ci

View File

@ -19,7 +19,7 @@ jobs:
node-version: 18 node-version: 18
- name: Audit - name: Audit
run: npm audit run: npm audit --omit=dev
- name: Install Dependencies - name: Install Dependencies
run: npm ci run: npm ci

View File

@ -11,6 +11,7 @@ import {
getEventHash, getEventHash,
getPublicKey, getPublicKey,
kinds, kinds,
nip04,
nip19, nip19,
nip44, nip44,
verifyEvent verifyEvent
@ -30,10 +31,26 @@ import { AuthState, Keys } from '../store/auth/types'
import { RelaysState } from '../store/relays/types' import { RelaysState } from '../store/relays/types'
import store from '../store/store' import store from '../store/store'
import { Meta, SignedEvent, UserAppData } from '../types' import { Meta, SignedEvent, UserAppData } from '../types'
import { getHash } from './hash' import { getDefaultRelayMap } from './relays'
import { parseJson, removeLeadingSlash } from './string' import { parseJson, removeLeadingSlash } from './string'
import { timeout } from './utils' import { timeout } from './utils'
import { getDefaultRelayMap } from './relays' import { getHash } from './hash'
/**
* Generates a `d` tag for userAppData
*/
const getDTagForUserAppData = async (): Promise<string | null> => {
const isLoggedIn = store.getState().auth?.loggedIn
const pubkey = store.getState().auth?.usersPubkey
if (!isLoggedIn || !pubkey) {
throw new Error(
'For generating d tag user must be logged in and a valid pubkey should exists in app Store'
)
}
return getHash(`938_${pubkey}`)
}
/** /**
* @param hexKey hex private or public key * @param hexKey hex private or public key
@ -377,13 +394,13 @@ export const getUsersAppData = async (): Promise<UserAppData | null> => {
} }
// Generate an identifier for the user's nip78 // Generate an identifier for the user's nip78
const hash = await getHash('938' + usersPubkey) const dTag = await getDTagForUserAppData()
if (!hash) return null if (!dTag) return null
// Define a filter for fetching events // Define a filter for fetching events
const filter: Filter = { const filter: Filter = {
kinds: [kinds.Application], kinds: [kinds.Application],
'#d': [hash] '#d': [dTag]
} }
const encryptedContent = await relayController const encryptedContent = await relayController
@ -578,14 +595,14 @@ export const updateUsersAppData = async (meta: Meta) => {
if (!encryptedContent) return null if (!encryptedContent) return null
// generate the identifier for user's appData event // generate the identifier for user's appData event
const hash = await getHash('938' + usersPubkey) const dTag = await getDTagForUserAppData()
if (!hash) return null if (!dTag) return null
const updatedEvent: UnsignedEvent = { const updatedEvent: UnsignedEvent = {
kind: kinds.Application, kind: kinds.Application,
pubkey: usersPubkey!, pubkey: usersPubkey!,
created_at: unixNow(), created_at: unixNow(),
tags: [['d', hash]], tags: [['d', dTag]],
content: encryptedContent content: encryptedContent
} }
@ -693,9 +710,10 @@ const uploadUserAppDataToBlossom = async (
// Convert the private key from hex to bytes // Convert the private key from hex to bytes
const secretKey = hexToBytes(privateKey) const secretKey = hexToBytes(privateKey)
// Encrypt the JSON string using the secret key // Encrypt the JSON string using the secret key
const encrypted = nip44.v2.encrypt( const encrypted = await nip04.encrypt(
stringified, secretKey,
nip44ConversationKey(secretKey, getPublicKey(secretKey)) getPublicKey(secretKey),
stringified
) )
// Create a blob from the encrypted data // Create a blob from the encrypted data
@ -788,10 +806,7 @@ const getUserAppDataFromBlossom = async (url: string, privateKey: string) => {
const pubkey = getPublicKey(secret) const pubkey = getPublicKey(secret)
// Decrypt the encrypted data using the secret and public key // Decrypt the encrypted data using the secret and public key
const decrypted = nip44.v2.decrypt( const decrypted = await nip04.decrypt(secret, pubkey, encrypted)
encrypted,
nip44ConversationKey(secret, pubkey)
)
// Parse the decrypted JSON content // Parse the decrypted JSON content
const parsedContent = await parseJson<{ const parsedContent = await parseJson<{

View File

@ -30,7 +30,6 @@ const findRelayListAndUpdateCache = async (
authors: [hexKey] authors: [hexKey]
} }
console.count('findRelayListAndUpdateCache')
const event = await relayController.fetchEvent(eventFilter, lookUpRelays) const event = await relayController.fetchEvent(eventFilter, lookUpRelays)
if (event) { if (event) {
await localCache.addUserRelayListMetadata(event) await localCache.addUserRelayListMetadata(event)