applesauce-accounts
Advanced tools
Comparing version 0.0.0-next-20250211165042 to 0.0.0-next-20250211174631
@@ -12,2 +12,3 @@ import { SimpleSigner } from "applesauce-signers/signers/simple-signer"; | ||
static fromKey<Metadata extends unknown>(key: Uint8Array | string): SimpleAccount<Metadata>; | ||
static generateNew<Metadata extends unknown>(): SimpleAccount<Metadata>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { getPublicKey } from "nostr-tools"; | ||
import { generateSecretKey, getPublicKey } from "nostr-tools"; | ||
import { SimpleSigner } from "applesauce-signers/signers/simple-signer"; | ||
@@ -23,2 +23,6 @@ import { bytesToHex, hexToBytes } from "@noble/hashes/utils"; | ||
} | ||
static generateNew() { | ||
const key = generateSecretKey(); | ||
return SimpleAccount.fromKey(key); | ||
} | ||
} |
@@ -10,4 +10,7 @@ import { Nip07Interface } from "applesauce-signers"; | ||
get accounts(): IAccount<any, any, Metadata>[]; | ||
/** Proxy signer for currently active account */ | ||
signer: IAccount<any, any, Metadata>; | ||
/** Disable request queueing for any accounts added to this manager */ | ||
disableQueue?: boolean; | ||
constructor(); | ||
/** Add account type class */ | ||
@@ -14,0 +17,0 @@ registerType<S extends Nip07Interface>(accountType: IAccountConstructor<S, any, Metadata>): void; |
@@ -12,4 +12,20 @@ import { BehaviorSubject } from "rxjs"; | ||
} | ||
/** Proxy signer for currently active account */ | ||
signer; | ||
/** Disable request queueing for any accounts added to this manager */ | ||
disableQueue; | ||
constructor() { | ||
this.signer = new Proxy({}, { | ||
get: (_, p) => { | ||
if (!this.active) | ||
throw new Error("No active account"); | ||
return Reflect.get(this.active, p); | ||
}, | ||
has: (_, p) => { | ||
if (!this.active) | ||
throw new Error("No active account"); | ||
return Reflect.has(this.active, p); | ||
}, | ||
}); | ||
} | ||
// Account type CRUD | ||
@@ -16,0 +32,0 @@ /** Add account type class */ |
@@ -45,2 +45,15 @@ import { describe, it, expect, beforeEach } from "vitest"; | ||
}); | ||
describe("signer", () => { | ||
it("should proxy active account", async () => { | ||
const account = SimpleAccount.generateNew(); | ||
manager.addAccount(account); | ||
manager.setActive(account); | ||
expect(await manager.signer.getPublicKey()).toBe(getPublicKey(account.signer.key)); | ||
}); | ||
it("should throw if there is no active account", () => { | ||
expect(() => { | ||
manager.signer.getPublicKey(); | ||
}).toThrow("No active account"); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "applesauce-accounts", | ||
"version": "0.0.0-next-20250211165042", | ||
"version": "0.0.0-next-20250211174631", | ||
"description": "A simple nostr account management system", | ||
@@ -36,3 +36,3 @@ "type": "module", | ||
"@noble/hashes": "^1.7.1", | ||
"applesauce-signers": "0.0.0-next-20250211165042", | ||
"applesauce-signers": "0.0.0-next-20250211174631", | ||
"nanoid": "^5.0.9", | ||
@@ -39,0 +39,0 @@ "nostr-tools": "^2.10.4", |
39847
893
+ Addedapplesauce-core@0.0.0-next-20250211174631(transitive)
+ Addedapplesauce-signers@0.0.0-next-20250211174631(transitive)
- Removedapplesauce-core@0.0.0-next-20250211165042(transitive)
- Removedapplesauce-signers@0.0.0-next-20250211165042(transitive)