Skip to content

Commit

Permalink
add applesauce-wallet package
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Mar 9, 2025
1 parent 91621b5 commit e176601
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-buses-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-core": minor
---

Update `unlockHiddenTags` to return tags array
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"author": "hzrd149",
"license": "MIT",
"files": [
"dist",
"applesauce"
"dist"
],
"exports": {
".": {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/helpers/__tests__/hidden-tags.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { describe, beforeEach, it, expect } from "vitest";
import { finalizeEvent, generateSecretKey, getPublicKey, kinds, nip04, NostrEvent } from "nostr-tools";

import { getHiddenTags, HiddenTagsSigner, unlockHiddenTags } from "../hidden-tags.js";
import { HiddenContentSigner } from "../hidden-content.js";
import { getHiddenTags, unlockHiddenTags } from "../hidden-tags.js";
import { unixNow } from "../time.js";

const key = generateSecretKey();
const pubkey = getPublicKey(key);
const signer: HiddenTagsSigner = {
const signer: HiddenContentSigner = {
nip04: {
encrypt: (pubkey: string, plaintext: string) => nip04.encrypt(key, pubkey, plaintext),
decrypt: (pubkey: string, ciphertext: string) => nip04.decrypt(key, pubkey, ciphertext),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/helpers/hidden-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function unlockHiddenTags<T extends { kind: number; pubkey: string;
event: T,
signer: HiddenContentSigner,
store?: EventStore,
): Promise<T> {
): Promise<string[][]> {
if (!canHaveHiddenTags(event.kind)) throw new Error("Event kind does not support hidden tags");
const plaintext = await unlockHiddenContent(event, signer);

Expand All @@ -62,7 +62,7 @@ export async function unlockHiddenTags<T extends { kind: number; pubkey: string;

if (store && isEvent(event)) store.update(event);

return event;
return tags;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/factory/src/operations/event/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export function modifyHiddenTags(...operations: TagOperation[]): EventOperation
if (hasHiddenTags(draft)) {
// draft is an existing event, attempt to unlock tags
const pubkey = await ctx.signer.getPublicKey();
const unlocked = await unlockHiddenTags({ ...draft, pubkey }, ctx.signer);
// try again
hidden = getHiddenTags(unlocked);
hidden = await unlockHiddenTags({ ...draft, pubkey }, ctx.signer);
} else {
// create a new array of hidden tags
hidden = [];
Expand Down
21 changes: 21 additions & 0 deletions packages/wallet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 hzrd149

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions packages/wallet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "applesauce-wallet",
"version": "0.11.0",
"description": "",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"watch:build": "tsc --watch > /dev/null",
"test": "vitest run --passWithNoTests",
"watch:test": "vitest"
},
"keywords": [
"nostr",
"applesauce"
],
"author": "hzrd149",
"license": "MIT",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./helpers": {
"import": "./dist/helpers/index.js",
"require": "./dist/helpers/index.js",
"types": "./dist/helpers/index.d.ts"
},
"./helpers/*": {
"import": "./dist/helpers/*.js",
"require": "./dist/helpers/*.js",
"types": "./dist/helpers/*.d.ts"
},
"./queries": {
"import": "./dist/queries/index.js",
"require": "./dist/queries/index.js",
"types": "./dist/queries/index.d.ts"
},
"./queries/*": {
"import": "./dist/queries/*.js",
"require": "./dist/queries/*.js",
"types": "./dist/queries/*.d.ts"
}
},
"dependencies": {
"applesauce-core": "^0.11.0",
"nostr-tools": "^2.10.4",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@hirez_io/observer-spy": "^2.2.0",
"@types/debug": "^4.1.12",
"typescript": "^5.7.3",
"vitest": "^3.0.5"
},
"funding": {
"type": "lightning",
"url": "lightning:nostrudel@geyser.fund"
}
}
1 change: 1 addition & 0 deletions packages/wallet/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./wallet.js";
44 changes: 44 additions & 0 deletions packages/wallet/src/helpers/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
getHiddenTags,
getOrComputeCachedValue,
HiddenContentSigner,
isHiddenTagsLocked,
unlockHiddenTags,
} from "applesauce-core/helpers";
import { NostrEvent } from "nostr-tools";

export const WALLET_KIND = 17375;

export const WalletPrivateKeySymbol = Symbol.for("wallet-private-key");
export const WalletMintsSymbol = Symbol.for("wallet-mints");

/** Returns if a wallet is locked */
export function isWalletLocked(wallet: NostrEvent): boolean {
return isHiddenTagsLocked(wallet);
}

/** Unlocks a wallet and returns the hidden tags */
export async function unlockWallet(wallet: NostrEvent, signer: HiddenContentSigner): Promise<string[][]> {
return await unlockHiddenTags(wallet, signer);
}

/** Returns the wallets mints */
export function getWalletMints(wallet: NostrEvent): string[] {
return getOrComputeCachedValue(wallet, WalletMintsSymbol, () => {
const tags = getHiddenTags(wallet);
if (!tags) throw new Error("Wallet is locked");
return tags.filter((t) => t[0] === "mint").map((t) => t[1]);
});
}

/** Returns the wallets private key as a string */
export function getWalletPrivateKey(wallet: NostrEvent): string {
return getOrComputeCachedValue(wallet, WalletPrivateKeySymbol, () => {
const tags = getHiddenTags(wallet);
if (!tags) throw new Error("Wallet is locked");

const key = tags.find((t) => t[0] === "privkey" && t[1])?.[1];
if (!key) throw new Error("Wallet missing private key");
return key;
});
}
2 changes: 2 additions & 0 deletions packages/wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as Queries from "./queries/index.js";
export * as Helpers from "./helpers/index.js";
1 change: 1 addition & 0 deletions packages/wallet/src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./wallet.js";
36 changes: 36 additions & 0 deletions packages/wallet/src/queries/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Query } from "applesauce-core";
import { filter, map, merge } from "rxjs";
import { getWalletMints, getWalletPrivateKey, isWalletLocked, WALLET_KIND } from "../helpers/wallet.js";

export type WalletInfo =
| { locked: true }
| {
locked: false;
privateKey: string;
mints: string[];
};

/** A query to get the state of a NIP-60 wallet */
export function WalletQuery(pubkey: string): Query<WalletInfo | undefined> {
return {
key: pubkey,
run: (events) =>
merge(
// get the latest replaceable event
events.replaceable(WALLET_KIND, pubkey),
// and listen for any updates to matching events
events.database.updated.pipe(filter((e) => e.kind === WALLET_KIND && e.pubkey === pubkey)),
).pipe(
map((wallet) => {
if (!wallet) return;

if (isWalletLocked(wallet)) return { locked: true };

const mints = getWalletMints(wallet);
const privateKey = getWalletPrivateKey(wallet);

return { locked: false, mints, privateKey };
}),
),
};
}
27 changes: 27 additions & 0 deletions packages/wallet/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2022", "DOM"],

"rootDir": "src",
"outDir": "dist",

"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,

"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"downlevelIteration": true,
"declaration": true,
"strict": true,

"pretty": true
},
"include": ["src/**/*"]
}
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e176601

Please sign in to comment.