private-wrap : Safety checks and return null
This commit is contained in:
+10
-3
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user