fix: when decrypting file, have better error messages #17
@ -359,39 +359,4 @@ export class NostrController extends EventEmitter {
|
|||||||
generateDelegatedKey = (): string => {
|
generateDelegatedKey = (): string => {
|
||||||
return NDKPrivateKeySigner.generate().privateKey!
|
return NDKPrivateKeySigner.generate().privateKey!
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates Nostr HTTP Auth token
|
|
||||||
* @param npub npub in hex format
|
|
||||||
* @param nostrTags tags to be included in the authevent (auth token)
|
|
||||||
*/
|
|
||||||
createNostrHttpAuthToken = async (
|
|
||||||
nostrTags: string[][] = []
|
|
||||||
): Promise<string> => {
|
|
||||||
const createdAt = Math.round(Date.now() / 1000)
|
|
||||||
|
|
||||||
const authEvent = new NDKEvent(undefined)
|
|
||||||
authEvent.kind = 27235
|
|
||||||
authEvent.tags = nostrTags
|
|
||||||
authEvent.content = `sigit-${createdAt}`
|
|
||||||
authEvent.created_at = createdAt
|
|
||||||
|
|
||||||
await this.signEvent(authEvent.rawEvent() as UnsignedEvent)
|
|
||||||
|
|
||||||
console.info('Signed auth event')
|
|
||||||
|
|
||||||
const base64Encoded = this.base64EncodeSignedEvent(authEvent.rawEvent())
|
|
||||||
|
|
||||||
return base64Encoded
|
|
||||||
}
|
|
||||||
|
|
||||||
base64EncodeSignedEvent = (event: NostrEvent) => {
|
|
||||||
try {
|
|
||||||
const authEventSerialized = JSON.stringify(event)
|
|
||||||
const token = btoa(authEventSerialized)
|
|
||||||
return token
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error('An error occurred in JSON.stringy of signedAuthEvent')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import styles from './style.module.scss'
|
|||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { useSearchParams } from 'react-router-dom'
|
import { useSearchParams } from 'react-router-dom'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
import { DecryptionError } from '../../types/errors/DecryptionError'
|
||||||
|
|
||||||
export const DecryptZip = () => {
|
export const DecryptZip = () => {
|
||||||
const [searchParams] = useSearchParams()
|
const [searchParams] = useSearchParams()
|
||||||
@ -60,17 +61,10 @@ export const DecryptZip = () => {
|
|||||||
const arrayBuffer = await decryptArrayBuffer(
|
const arrayBuffer = await decryptArrayBuffer(
|
||||||
encryptedArrayBuffer,
|
encryptedArrayBuffer,
|
||||||
encryptionKey
|
encryptionKey
|
||||||
).catch((err) => {
|
).catch((err: DecryptionError) => {
|
||||||
console.log('err in decryption:>> ', err)
|
console.log('err in decryption:>> ', err)
|
||||||
|
|
||||||
m marked this conversation as resolved
|
|||||||
if (err.message.toLowerCase().includes('expected')) {
|
toast.error(err.message)
|
||||||
m marked this conversation as resolved
Outdated
y
commented
The message should be The message should be `The decryption key length or format is invalid.`
|
|||||||
toast.error(`The Key seems to be invalid length or format`)
|
|
||||||
} else if (err.message.includes('The JWK "alg" member was inconsistent')) {
|
|
||||||
toast.error(`The Key seems to be invalid.`)
|
|
||||||
} else {
|
|
||||||
toast.error(err.message || 'An error occurred while decrypting file.')
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
return null
|
return null
|
||||||
m marked this conversation as resolved
Outdated
y
commented
The message should be The message should be `The decryption key is invalid.`
|
|||||||
})
|
})
|
||||||
|
20
src/types/errors/DecryptionError.ts
Normal file
20
src/types/errors/DecryptionError.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
export class DecryptionError extends Error {
|
||||||
|
public message: string = ''
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public inputError: any
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
if (inputError.message.toLowerCase().includes('expected')) {
|
||||||
|
this.message = `The decryption key length or format is invalid`
|
||||||
|
} else if (inputError.message.includes('The JWK "alg" member was inconsistent')) {
|
||||||
|
this.message = `The decryption key is invalid.`
|
||||||
|
} else {
|
||||||
|
this.message = inputError.message || 'An error occurred while decrypting file.'
|
||||||
|
}
|
||||||
|
|
||||||
|
this.name = 'DecryptionError'
|
||||||
|
Object.setPrototypeOf(this, DecryptionError.prototype)
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import {
|
|||||||
stringToHex,
|
stringToHex,
|
||||||
uint8ArrayToHexString
|
uint8ArrayToHexString
|
||||||
} from '.'
|
} from '.'
|
||||||
|
import { DecryptionError } from '../types/errors/DecryptionError'
|
||||||
|
|
||||||
const ENCRYPTION_ALGO_NAME = 'AES-GCM'
|
const ENCRYPTION_ALGO_NAME = 'AES-GCM'
|
||||||
|
|
||||||
@ -63,14 +64,18 @@ export const decryptArrayBuffer = async (
|
|||||||
encryptedData: ArrayBuffer,
|
encryptedData: ArrayBuffer,
|
||||||
key: string
|
key: string
|
||||||
) => {
|
) => {
|
||||||
const { cryptoKey, iv } = await importKey(key)
|
try {
|
||||||
|
const { cryptoKey, iv } = await importKey(key)
|
||||||
|
|
||||||
// Decrypt the data
|
// Decrypt the data
|
||||||
const decryptedData = await window.crypto.subtle.decrypt(
|
const decryptedData = await window.crypto.subtle.decrypt(
|
||||||
{ name: ENCRYPTION_ALGO_NAME, iv },
|
{ name: ENCRYPTION_ALGO_NAME, iv },
|
||||||
cryptoKey,
|
cryptoKey,
|
||||||
encryptedData
|
encryptedData
|
||||||
)
|
)
|
||||||
|
|
||||||
return decryptedData
|
return decryptedData
|
||||||
|
} catch (err) {
|
||||||
|
throw new DecryptionError(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
I would say that a more convenient and reliable approach to handle errors is to have dedicated errors of custom type