fix(accounts): Do not attempt to decode ID key if password is not provided

This commit is contained in:
Shibo Lyu 2025-04-14 03:40:51 +08:00
parent 924427a810
commit 35315ca5f3
2 changed files with 7 additions and 6 deletions

View file

@ -4,7 +4,7 @@ import {
type BlahIdentityDescription, type BlahIdentityDescription,
type BlahProfile type BlahProfile
} from '@blah-im/core/identity'; } from '@blah-im/core/identity';
import { type IdentityDB, openIdentityDB } from './identityFileDB'; import { type IdentityDB, openIdentityDB } from './identityDB';
import { BlahKeyPair } from '@blah-im/core/crypto'; import { BlahKeyPair } from '@blah-im/core/crypto';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
@ -89,7 +89,8 @@ class AccountManager {
const accountCreds = await this.keyDB.fetchAccount(idKeyId); const accountCreds = await this.keyDB.fetchAccount(idKeyId);
const encodedIdKeyPair = accountCreds?.encodedIdKeyPair; const encodedIdKeyPair = accountCreds?.encodedIdKeyPair;
const idKeyPair = encodedIdKeyPair const idKeyPair =
encodedIdKeyPair && password
? await BlahKeyPair.fromEncoded(encodedIdKeyPair, password) ? await BlahKeyPair.fromEncoded(encodedIdKeyPair, password)
: undefined; : undefined;
const actKeyPair = accountCreds?.actKeyPair; const actKeyPair = accountCreds?.actKeyPair;
@ -97,7 +98,7 @@ class AccountManager {
return await BlahIdentity.fromIdentityDescription(identityFile, idKeyPair, actKeyPair); return await BlahIdentity.fromIdentityDescription(identityFile, idKeyPair, actKeyPair);
} }
async saveIdentityDescription(identity: BlahIdentity) { async saveIdentity(identity: BlahIdentity) {
if (!this.identityDB) throw new Error('Account manager not initialized'); if (!this.identityDB) throw new Error('Account manager not initialized');
const identityDesc = identity.generateIdentityDescription(); const identityDesc = identity.generateIdentityDescription();
@ -113,7 +114,7 @@ class AccountManager {
const identity = await BlahIdentity.create(idKeyPair, actKeyPair, profile); const identity = await BlahIdentity.create(idKeyPair, actKeyPair, profile);
const encodedIdKeyPair = await idKeyPair.encode(password); const encodedIdKeyPair = await idKeyPair.encode(password);
await this.keyDB.addAccount(idKeyPair.id, actKeyPair, encodedIdKeyPair); await this.keyDB.addAccount(idKeyPair.id, actKeyPair, encodedIdKeyPair);
await this.saveIdentityDescription(identity); await this.saveIdentity(identity);
return idKeyPair.id; return idKeyPair.id;
} }
} }