Skip to content

Commit 3780d5e

Browse files
committed
Update wallet backup blueprint to use wallet event
1 parent 2f99e8a commit 3780d5e

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed

.changeset/nasty-mirrors-push.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"applesauce-core": minor
3+
---
4+
5+
Add `setEventContentEncryptionMethod` method

packages/core/src/helpers/hidden-content.ts

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export const EventContentEncryptionMethod: Record<number, "nip04" | "nip44"> = {
4747
[kinds.Interestsets]: "nip04",
4848
};
4949

50+
/** Sets the encryption method that is used for the contents of a specific event kind */
51+
export function setEventContentEncryptionMethod(kind: number, method: "nip04" | "nip44") {
52+
EventContentEncryptionMethod[kind] = method;
53+
}
54+
5055
/** Checks if an event can have hidden content */
5156
export function canHaveHiddenContent(kind: number): boolean {
5257
return EventContentEncryptionMethod[kind] !== undefined;

packages/wallet/src/actions/__tests__/wallet.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ActionHub } from "applesauce-actions";
55

66
import { FakeUser } from "../../__tests__/fake-user.js";
77
import { CreateWallet } from "../wallet.js";
8-
import { WALLET_BACKUP_KIND, WALLET_KIND } from "../../helpers/wallet.js";
8+
import { WALLET_BACKUP_KIND } from "../../helpers/wallet.js";
99
import { NostrEvent } from "nostr-tools";
1010
import { unlockHiddenTags } from "applesauce-core/helpers";
1111

packages/wallet/src/actions/wallet.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export function CreateWallet(mints: string[], privateKey = generateSecretKey()):
99
const existing = events.getReplaceable(WALLET_KIND, self);
1010
if (existing) throw new Error("Wallet already exists");
1111

12-
const backup = await factory.create(WalletBackupBlueprint, privateKey, mints);
13-
const wallet = await factory.create(WalletBlueprint, privateKey, mints);
12+
const wallet = await factory.sign(await factory.create(WalletBlueprint, privateKey, mints));
13+
const backup = await factory.sign(await factory.create(WalletBackupBlueprint, wallet));
1414

15-
await publish("Wallet backup", await factory.sign(backup));
16-
await publish("New wallet", await factory.sign(wallet));
15+
await publish("Wallet backup", backup);
16+
await publish("Create wallet", wallet);
1717
};
1818
}

packages/wallet/src/blueprints/wallet.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { modifyHiddenTags } from "applesauce-factory/operations/event";
33

44
import { WALLET_BACKUP_KIND, WALLET_KIND } from "../helpers/wallet.js";
55
import { setMintTags, setPrivateKeyTag } from "../operations/index.js";
6+
import { NostrEvent } from "nostr-tools";
7+
import { setWalletBackupContent } from "../operations/wallet.js";
68

79
/** A blueprint to create a new 17375 wallet */
810
export function WalletBlueprint(privateKey: Uint8Array, mints: string[]): EventBlueprint {
@@ -15,11 +17,6 @@ export function WalletBlueprint(privateKey: Uint8Array, mints: string[]): EventB
1517
}
1618

1719
/** A blueprint that creates a new 375 wallet backup event */
18-
export function WalletBackupBlueprint(privateKey: Uint8Array, mints: string[]): EventBlueprint {
19-
return (ctx) =>
20-
EventFactory.runProcess(
21-
{ kind: WALLET_BACKUP_KIND },
22-
ctx,
23-
modifyHiddenTags(setPrivateKeyTag(privateKey), setMintTags(mints)),
24-
);
20+
export function WalletBackupBlueprint(wallet: NostrEvent): EventBlueprint {
21+
return (ctx) => EventFactory.runProcess({ kind: WALLET_BACKUP_KIND }, ctx, setWalletBackupContent(wallet));
2522
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from "vitest";
2+
import { setWalletBackupContent } from "../wallet.js";
3+
import { FakeUser } from "../../__tests__/fake-user.js";
4+
import { EventFactory } from "applesauce-factory";
5+
import { WalletBlueprint } from "../../blueprints/wallet.js";
6+
import { generateSecretKey } from "nostr-tools";
7+
import { WALLET_BACKUP_KIND } from "../../helpers/wallet.js";
8+
import { unixNow } from "applesauce-core/helpers";
9+
10+
const user = new FakeUser();
11+
const factory = new EventFactory({ signer: user });
12+
13+
describe("setWalletBackupContent", () => {
14+
it("should throw if kind is not wallet kind", async () => {
15+
const note = user.note();
16+
17+
await expect(
18+
setWalletBackupContent(note)(
19+
{ kind: WALLET_BACKUP_KIND, tags: [], created_at: unixNow(), content: "" },
20+
factory.context,
21+
),
22+
).rejects.toThrow();
23+
});
24+
25+
it("should throw if pubkey does not match", async () => {
26+
const wallet = await factory.sign(await factory.create(WalletBlueprint, generateSecretKey(), []));
27+
const user2 = new FakeUser();
28+
29+
await expect(
30+
setWalletBackupContent(wallet)(
31+
{ kind: WALLET_BACKUP_KIND, tags: [], created_at: unixNow(), content: "" },
32+
{ signer: user2 },
33+
),
34+
).rejects.toThrow();
35+
});
36+
37+
it("should copy the content of the wallet event", async () => {
38+
const wallet = await factory.sign(await factory.create(WalletBlueprint, generateSecretKey(), []));
39+
40+
expect(
41+
await setWalletBackupContent(wallet)(
42+
{ kind: WALLET_BACKUP_KIND, tags: [], created_at: unixNow(), content: "" },
43+
factory.context,
44+
),
45+
).toEqual(expect.objectContaining({ content: wallet.content }));
46+
});
47+
});
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { EventOperation } from "applesauce-factory";
2+
import { NostrEvent } from "nostr-tools";
3+
import { WALLET_KIND } from "../helpers/wallet.js";
4+
5+
/** Sets the content of a kind 375 wallet backup event */
6+
export function setWalletBackupContent(wallet: NostrEvent): EventOperation {
7+
return async (draft, ctx) => {
8+
if (wallet.kind !== WALLET_KIND) throw new Error(`Cant create a wallet backup from kind ${wallet.kind}`);
9+
if (!wallet.content) throw new Error("Wallet missing content");
10+
11+
const pubkey = await ctx.signer?.getPublicKey();
12+
if (wallet.pubkey !== pubkey) throw new Error("Wallet pubkey dose not match signer pubkey");
13+
14+
return { ...draft, content: wallet.content };
15+
};
16+
}

0 commit comments

Comments
 (0)