private-wrap : Safety checks and return null

This commit is contained in:
2024-09-04 21:17:38 +02:00
parent 5d3d23ca4a
commit 628c52728d
+10 -3
View File
@@ -39,12 +39,19 @@ export class PrivateWrap {
const box = this.box.toString()
return `${pubkey}.${box}`
}
public static async fromString(data: string): Promise<PrivateWrap> {
public static async fromString(data: string): Promise<PrivateWrap | null> {
log.trace('fromString')
const parts = data.split('.', 2)
const pubkey_str = b642a(parts[0]).expect('Failed to decode pubkey')
const pubkey = await crypto.subtle.importKey('spki', pubkey_str, algorithm, true, [])
if (parts.length !== 2) return null
const pubkey_str = b642a(parts[0])
if (pubkey_str.is_err()) return null
const pubkey = await crypto.subtle.importKey('spki', pubkey_str.unwrap(), algorithm, true, [])
const box = SecretWrap.fromString(parts[1])
if (box === null) return null
return new PrivateWrap(box, pubkey)
}
}