Skip to content

Commit f92f10c

Browse files
committed
add secret key and pubkey NIP-19 helpers
1 parent c861184 commit f92f10c

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

.changeset/tame-pandas-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"applesauce-core": minor
3+
---
4+
5+
Add `normalizeToPubkey` and `normalizeToSecretKey` NIP-19 helpers

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
}
6969
},
7070
"dependencies": {
71+
"@noble/hashes": "^1.7.1",
7172
"@scure/base": "^1.2.4",
7273
"debug": "^4.4.0",
7374
"fast-deep-equal": "^3.1.3",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, expect, it } from "vitest";
2+
import { bytesToHex } from "@noble/hashes/utils";
3+
import { normalizeToPubkey, normalizeToSecretKey } from "../nip-19.js";
4+
5+
describe("normalizeToPubkey", () => {
6+
it("should get pubkey from npub", () => {
7+
expect(normalizeToPubkey("npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr")).toEqual(
8+
"266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5",
9+
);
10+
});
11+
12+
it("should get pubkey from nprofile", () => {
13+
expect(
14+
normalizeToPubkey(
15+
"nprofile1qyw8wumn8ghj7umpw3jkcmrfw3jju6r6wfjrzdpe9e3k7mf0qyf8wumn8ghj7mn0wd68yat99e3k7mf0qqszv6q4uryjzr06xfxxew34wwc5hmjfmfpqn229d72gfegsdn2q3fg5g7lja",
16+
),
17+
).toEqual("266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5");
18+
});
19+
20+
it("should return hex pubkey", () => {
21+
expect(normalizeToPubkey("266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5")).toEqual(
22+
"266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5",
23+
);
24+
});
25+
26+
it("should throw on invalid hex pubkey", () => {
27+
expect(() => {
28+
normalizeToPubkey("5028372");
29+
}).toThrow();
30+
});
31+
32+
it("should throw on invalid string", () => {
33+
expect(() => {
34+
normalizeToPubkey("testing");
35+
}).toThrow();
36+
});
37+
});
38+
39+
describe("normalizeToSecretKey", () => {
40+
it("should get secret key from nsec", () => {
41+
expect(bytesToHex(normalizeToSecretKey("nsec1xe7znq745x5n68566l32ru72aajz3pk2cys9lnf3tuexvkw0dldsj8v2lm"))).toEqual(
42+
"367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb",
43+
);
44+
});
45+
46+
it("should get secret key from raw hex", () => {
47+
expect(
48+
bytesToHex(normalizeToSecretKey("367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb")),
49+
).toEqual("367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb");
50+
});
51+
52+
it("should throw on invalid hex key", () => {
53+
expect(() => {
54+
normalizeToSecretKey("209573290");
55+
}).toThrow();
56+
});
57+
58+
it("should throw on npub", () => {
59+
expect(() => {
60+
normalizeToSecretKey("npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr");
61+
}).toThrow();
62+
});
63+
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const HiddenTagsSymbol = Symbol.for("hidden-tags");
2121
/** Various event kinds that can have encrypted tags in their content and which encryption method they use */
2222
export const EventEncryptionMethod: Record<number, "nip04" | "nip44"> = {
2323
// NIP-60 wallet
24-
37375: "nip44",
24+
17375: "nip44",
2525

2626
// NIP-51 lists
2727
[kinds.BookmarkList]: "nip04",

packages/core/src/helpers/nip-19.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { nip19 } from "nostr-tools";
2+
import { hexToBytes } from "@noble/hashes/utils";
3+
4+
import { isHexKey } from "./string.js";
5+
import { getPubkeyFromDecodeResult } from "./pointers.js";
6+
7+
/** Gets the hex pubkey from any nip-19 encoded string */
8+
export function normalizeToPubkey(str: string): string {
9+
if (isHexKey(str)) return str;
10+
else {
11+
const decode = nip19.decode(str);
12+
const pubkey = getPubkeyFromDecodeResult(decode);
13+
if (!pubkey) throw new Error(`Cant find pubkey in ${decode.type}`);
14+
return pubkey;
15+
}
16+
}
17+
18+
/** Converts hex to nsec strings into Uint8 secret keys */
19+
export function normalizeToSecretKey(str: string): Uint8Array {
20+
if (isHexKey(str)) return hexToBytes(str);
21+
else {
22+
const decode = nip19.decode(str);
23+
if (decode.type !== "nsec") throw new Error(`Cant get secret key from ${decode.type}`);
24+
return decode.data;
25+
}
26+
}

pnpm-lock.yaml

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)