fix: use dedicated key from nip78 in auth event for uploading files.zip

This commit is contained in:
SwiftHawk 2024-07-09 00:01:03 +05:00
parent 6b135ac54d
commit 8eaf9cb61c
2 changed files with 13 additions and 7 deletions

View File

@ -410,7 +410,7 @@ export const CreatePage = () => {
type: 'application/sigit' type: 'application/sigit'
}) })
const fileUrl = await uploadToFileStorage(file, nostrController) const fileUrl = await uploadToFileStorage(file)
.then((url) => { .then((url) => {
toast.success('files.zip uploaded to file storage') toast.success('files.zip uploaded to file storage')
return url return url

View File

@ -2,6 +2,7 @@ import axios from 'axios'
import { import {
Event, Event,
EventTemplate, EventTemplate,
finalizeEvent,
generateSecretKey, generateSecretKey,
getPublicKey, getPublicKey,
nip04, nip04,
@ -14,6 +15,7 @@ import store from '../store/store'
import { CreateSignatureEventContent, Meta } from '../types' import { CreateSignatureEventContent, Meta } from '../types'
import { hexToNpub, now } from './nostr' import { hexToNpub, now } from './nostr'
import { parseJson } from './string' import { parseJson } from './string'
import { hexToBytes } from '@noble/hashes/utils'
/** /**
* Uploads a file to a file storage service. * Uploads a file to a file storage service.
@ -21,10 +23,7 @@ import { parseJson } from './string'
* @param nostrController The NostrController instance for handling authentication. * @param nostrController The NostrController instance for handling authentication.
* @returns The URL of the uploaded file. * @returns The URL of the uploaded file.
*/ */
export const uploadToFileStorage = async ( export const uploadToFileStorage = async (file: File) => {
file: File,
nostrController: NostrController
) => {
// Define event metadata for authorization // Define event metadata for authorization
const event: EventTemplate = { const event: EventTemplate = {
kind: 24242, kind: 24242,
@ -38,8 +37,15 @@ export const uploadToFileStorage = async (
] ]
} }
// Sign the authorization event using the NostrController const key = store.getState().userAppData?.keyPair?.private
const authEvent = await nostrController.signEvent(event) if (!key) {
throw new Error(
'Key to interact with blossom server is not defined in user app data'
)
}
// Sign the authorization event using the dedicated key stored in user app data
const authEvent = finalizeEvent(event, hexToBytes(key))
// URL of the file storage service // URL of the file storage service
const FILE_STORAGE_URL = 'https://blossom.sigit.io' // REFACTOR: should be an env const FILE_STORAGE_URL = 'https://blossom.sigit.io' // REFACTOR: should be an env