mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-11 16:07:15 +02:00
feat: register ReadOnlyAccount with AccountManager
Register the ReadOnlyAccount type with the global AccountManager instance to enable read-only account creation and management. Changes: - Fixed ReadOnlyAccount constructor to match IAccountConstructor signature - Constructor now takes (pubkey, signer) instead of (pubkey, metadata) - Added createWithMetadata() helper for factory methods - Updated all factory methods to use createWithMetadata() - Registered ReadOnlyAccount type in accounts service All tests pass (48 tests), lint clean, build successful.
This commit is contained in:
@@ -61,10 +61,8 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
> {
|
||||
static readonly type = "readonly";
|
||||
|
||||
constructor(pubkey: string, metadata: ReadOnlyMetadata) {
|
||||
const signer = new ReadOnlySigner(pubkey);
|
||||
constructor(pubkey: string, signer: ReadOnlySigner) {
|
||||
super(pubkey, signer);
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
toJSON(): SerializedAccount<void, ReadOnlyMetadata> {
|
||||
@@ -76,10 +74,24 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
static fromJSON(
|
||||
data: SerializedAccount<void, ReadOnlyMetadata>,
|
||||
): ReadOnlyAccount {
|
||||
const account = new ReadOnlyAccount(data.pubkey, data.metadata!);
|
||||
const signer = new ReadOnlySigner(data.pubkey);
|
||||
const account = new ReadOnlyAccount(data.pubkey, signer);
|
||||
return BaseAccount.loadCommonFields(account, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to create account with metadata
|
||||
*/
|
||||
private static createWithMetadata(
|
||||
pubkey: string,
|
||||
metadata: ReadOnlyMetadata,
|
||||
): ReadOnlyAccount {
|
||||
const signer = new ReadOnlySigner(pubkey);
|
||||
const account = new ReadOnlyAccount(pubkey, signer);
|
||||
account.metadata = metadata;
|
||||
return account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create account from npub (NIP-19 encoded public key)
|
||||
*/
|
||||
@@ -89,7 +101,7 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
if (decoded.type !== "npub") {
|
||||
throw new Error("Invalid npub: expected npub format");
|
||||
}
|
||||
return new ReadOnlyAccount(decoded.data, {
|
||||
return ReadOnlyAccount.createWithMetadata(decoded.data, {
|
||||
source: "npub",
|
||||
originalInput: npub,
|
||||
});
|
||||
@@ -108,7 +120,7 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
if (!pubkey) {
|
||||
throw new Error(`Failed to resolve NIP-05 identifier: ${nip05}`);
|
||||
}
|
||||
return new ReadOnlyAccount(pubkey, {
|
||||
return ReadOnlyAccount.createWithMetadata(pubkey, {
|
||||
source: "nip05",
|
||||
originalInput: nip05,
|
||||
nip05,
|
||||
@@ -124,7 +136,7 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
if (decoded.type !== "nprofile") {
|
||||
throw new Error("Invalid nprofile: expected nprofile format");
|
||||
}
|
||||
return new ReadOnlyAccount(decoded.data.pubkey, {
|
||||
return ReadOnlyAccount.createWithMetadata(decoded.data.pubkey, {
|
||||
source: "nprofile",
|
||||
originalInput: nprofile,
|
||||
relays: decoded.data.relays,
|
||||
@@ -146,7 +158,7 @@ export class ReadOnlyAccount extends BaseAccount<
|
||||
"Invalid hex pubkey: expected 64 character hexadecimal string",
|
||||
);
|
||||
}
|
||||
return new ReadOnlyAccount(hex.toLowerCase(), {
|
||||
return ReadOnlyAccount.createWithMetadata(hex.toLowerCase(), {
|
||||
source: "hex",
|
||||
originalInput: hex,
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { AccountManager } from "applesauce-accounts";
|
||||
import { registerCommonAccountTypes } from "applesauce-accounts/accounts";
|
||||
import { ReadOnlyAccount } from "@/lib/account-types";
|
||||
|
||||
const ACCOUNTS = "nostr-accounts";
|
||||
const ACTIVE_ACCOUNT = "active-account";
|
||||
@@ -13,8 +14,13 @@ function safeParse(s: string) {
|
||||
}
|
||||
|
||||
const accountManager = new AccountManager();
|
||||
|
||||
// Register common account types (ExtensionAccount, etc.)
|
||||
registerCommonAccountTypes(accountManager);
|
||||
|
||||
// Register custom account types
|
||||
accountManager.registerType(ReadOnlyAccount);
|
||||
|
||||
// load all accounts
|
||||
if (localStorage.getItem(ACCOUNTS)) {
|
||||
const accounts = localStorage.getItem(ACCOUNTS);
|
||||
|
||||
Reference in New Issue
Block a user