@nostrify/nostrify 0.46.9 → 0.46.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +17 -122
- package/CHANGELOG.md +8 -0
- package/dist/BunkerURI.js +48 -0
- package/dist/BunkerURI.ts +58 -0
- package/dist/NBrowserSigner.js +82 -0
- package/dist/NBrowserSigner.ts +100 -0
- package/dist/NCache.js +39 -0
- package/dist/NCache.ts +73 -0
- package/dist/NConnectSigner.js +132 -0
- package/dist/NConnectSigner.ts +188 -0
- package/dist/NIP05.d.ts +1 -1
- package/dist/NIP05.js +35 -0
- package/dist/NIP05.ts +51 -0
- package/dist/NIP50.js +21 -0
- package/dist/NIP50.ts +24 -0
- package/dist/NIP98.js +74 -0
- package/dist/NIP98.ts +97 -0
- package/dist/NKinds.js +25 -0
- package/dist/NKinds.ts +26 -0
- package/dist/NPool.js +154 -0
- package/dist/NPool.ts +243 -0
- package/dist/NRelay1.js +318 -0
- package/dist/NRelay1.ts +447 -0
- package/dist/NSchema.d.ts +2 -2
- package/dist/NSchema.d.ts.map +1 -1
- package/dist/NSchema.js +216 -0
- package/dist/NSchema.ts +291 -0
- package/dist/NSecSigner.js +38 -0
- package/dist/NSecSigner.ts +62 -0
- package/dist/NSet.js +143 -0
- package/dist/NSet.ts +210 -0
- package/dist/RelayError.js +20 -0
- package/dist/RelayError.ts +22 -0
- package/dist/ln/LNURL.js +101 -0
- package/dist/ln/LNURL.ts +146 -0
- package/dist/ln/mod.js +4 -0
- package/dist/ln/mod.ts +4 -0
- package/dist/ln/types/LNURLCallback.js +0 -0
- package/dist/ln/types/LNURLCallback.ts +7 -0
- package/dist/ln/types/LNURLDetails.js +0 -0
- package/dist/ln/types/LNURLDetails.ts +19 -0
- package/dist/mod.js +30 -0
- package/dist/mod.ts +16 -0
- package/dist/test/ErrorRelay.js +23 -0
- package/dist/test/ErrorRelay.ts +52 -0
- package/dist/test/MockRelay.js +61 -0
- package/dist/test/MockRelay.ts +92 -0
- package/dist/test/TestRelayServer.js +149 -0
- package/dist/test/TestRelayServer.ts +185 -0
- package/dist/test/mod.js +24 -0
- package/dist/test/mod.ts +28 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/uploaders/BlossomUploader.js +70 -0
- package/dist/uploaders/BlossomUploader.ts +98 -0
- package/dist/uploaders/NostrBuildUploader.js +65 -0
- package/dist/uploaders/NostrBuildUploader.ts +89 -0
- package/dist/uploaders/mod.js +6 -0
- package/dist/uploaders/mod.ts +2 -0
- package/dist/utils/CircularSet.js +29 -0
- package/dist/utils/CircularSet.ts +36 -0
- package/dist/utils/Machina.js +38 -0
- package/dist/utils/Machina.ts +66 -0
- package/dist/utils/N64.js +17 -0
- package/dist/utils/N64.ts +23 -0
- package/dist/utils/mod.js +6 -0
- package/dist/utils/mod.ts +2 -0
- package/package.json +2 -2
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,124 +1,19 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @nostrify/nostrify@0.46.
|
|
4
|
-
> tsc -p tsconfig.json && node ../../esbuild.config.js --package ./
|
|
5
|
-
|
|
6
|
-
[
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
|
|
14
|
-
[
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
[7m [0m [91m ~~~~~~[0m
|
|
21
|
-
|
|
22
|
-
[96mNSchema.ts[0m:[93m108[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"EVENT">, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrClientEVENT, ZodTypeDef, NostrClientEVENT>'.
|
|
23
|
-
Types of property '_type' are incompatible.
|
|
24
|
-
Type '["EVENT", NostrEvent, ...unknown[]]' is not assignable to type 'NostrClientEVENT'.
|
|
25
|
-
Target allows only 2 element(s) but source may have more.
|
|
26
|
-
|
|
27
|
-
[7m108[0m return z.tuple([z.literal('EVENT'), NSchema.event()]);
|
|
28
|
-
[7m [0m [91m ~~~~~~[0m
|
|
29
|
-
|
|
30
|
-
[96mNSchema.ts[0m:[93m123[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"CLOSE">, ZodString], null>' is not assignable to type 'ZodType<NostrClientCLOSE, ZodTypeDef, NostrClientCLOSE>'.
|
|
31
|
-
Types of property '_type' are incompatible.
|
|
32
|
-
Type '["CLOSE", string, ...unknown[]]' is not assignable to type 'NostrClientCLOSE'.
|
|
33
|
-
Target allows only 2 element(s) but source may have more.
|
|
34
|
-
|
|
35
|
-
[7m123[0m return z.tuple([z.literal('CLOSE'), z.string()]);
|
|
36
|
-
[7m [0m [91m ~~~~~~[0m
|
|
37
|
-
|
|
38
|
-
[96mNSchema.ts[0m:[93m128[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"AUTH">, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrClientAUTH, ZodTypeDef, NostrClientAUTH>'.
|
|
39
|
-
Types of property '_type' are incompatible.
|
|
40
|
-
Type '["AUTH", NostrEvent, ...unknown[]]' is not assignable to type 'NostrClientAUTH'.
|
|
41
|
-
Target allows only 2 element(s) but source may have more.
|
|
42
|
-
|
|
43
|
-
[7m128[0m return z.tuple([z.literal('AUTH'), NSchema.event()]);
|
|
44
|
-
[7m [0m [91m ~~~~~~[0m
|
|
45
|
-
|
|
46
|
-
[96mNSchema.ts[0m:[93m144[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"EVENT">, ZodString, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrRelayEVENT, ZodTypeDef, NostrRelayEVENT>'.
|
|
47
|
-
Types of property '_type' are incompatible.
|
|
48
|
-
Type '["EVENT", string, NostrEvent, ...unknown[]]' is not assignable to type 'NostrRelayEVENT'.
|
|
49
|
-
Target allows only 3 element(s) but source may have more.
|
|
50
|
-
|
|
51
|
-
[7m144[0m return z.tuple([z.literal('EVENT'), z.string(), NSchema.event()]);
|
|
52
|
-
[7m [0m [91m ~~~~~~[0m
|
|
53
|
-
|
|
54
|
-
[96mNSchema.ts[0m:[93m149[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"OK">, ZodString, ZodBoolean, ZodString], null>' is not assignable to type 'ZodType<NostrRelayOK, ZodTypeDef, NostrRelayOK>'.
|
|
55
|
-
Types of property '_type' are incompatible.
|
|
56
|
-
Type '["OK", string, boolean, string, ...unknown[]]' is not assignable to type 'NostrRelayOK'.
|
|
57
|
-
Target allows only 4 element(s) but source may have more.
|
|
58
|
-
|
|
59
|
-
[7m149[0m return z.tuple([z.literal('OK'), NSchema.id(), z.boolean(), z.string()]);
|
|
60
|
-
[7m [0m [91m ~~~~~~[0m
|
|
61
|
-
|
|
62
|
-
[96mNSchema.ts[0m:[93m154[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"EOSE">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayEOSE, ZodTypeDef, NostrRelayEOSE>'.
|
|
63
|
-
Types of property '_type' are incompatible.
|
|
64
|
-
Type '["EOSE", string, ...unknown[]]' is not assignable to type 'NostrRelayEOSE'.
|
|
65
|
-
Target allows only 2 element(s) but source may have more.
|
|
66
|
-
|
|
67
|
-
[7m154[0m return z.tuple([z.literal('EOSE'), z.string()]);
|
|
68
|
-
[7m [0m [91m ~~~~~~[0m
|
|
69
|
-
|
|
70
|
-
[96mNSchema.ts[0m:[93m159[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"NOTICE">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayNOTICE, ZodTypeDef, NostrRelayNOTICE>'.
|
|
71
|
-
Types of property '_type' are incompatible.
|
|
72
|
-
Type '["NOTICE", string, ...unknown[]]' is not assignable to type 'NostrRelayNOTICE'.
|
|
73
|
-
Target allows only 2 element(s) but source may have more.
|
|
74
|
-
|
|
75
|
-
[7m159[0m return z.tuple([z.literal('NOTICE'), z.string()]);
|
|
76
|
-
[7m [0m [91m ~~~~~~[0m
|
|
77
|
-
|
|
78
|
-
[96mNSchema.ts[0m:[93m164[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"CLOSED">, ZodString, ZodString], null>' is not assignable to type 'ZodType<NostrRelayCLOSED, ZodTypeDef, NostrRelayCLOSED>'.
|
|
79
|
-
Types of property '_type' are incompatible.
|
|
80
|
-
Type '["CLOSED", string, string, ...unknown[]]' is not assignable to type 'NostrRelayCLOSED'.
|
|
81
|
-
Target allows only 3 element(s) but source may have more.
|
|
82
|
-
|
|
83
|
-
[7m164[0m return z.tuple([z.literal('CLOSED'), z.string(), z.string()]);
|
|
84
|
-
[7m [0m [91m ~~~~~~[0m
|
|
85
|
-
|
|
86
|
-
[96mNSchema.ts[0m:[93m169[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"AUTH">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayAUTH, ZodTypeDef, NostrRelayAUTH>'.
|
|
87
|
-
Types of property '_type' are incompatible.
|
|
88
|
-
Type '["AUTH", string, ...unknown[]]' is not assignable to type 'NostrRelayAUTH'.
|
|
89
|
-
Target allows only 2 element(s) but source may have more.
|
|
90
|
-
|
|
91
|
-
[7m169[0m return z.tuple([z.literal('AUTH'), z.string()]);
|
|
92
|
-
[7m [0m [91m ~~~~~~[0m
|
|
93
|
-
|
|
94
|
-
[96mNSchema.ts[0m:[93m174[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodTuple<[ZodLiteral<"COUNT">, ZodString, ZodObject<{ count: ZodNumber; approximate: ZodOptional<ZodBoolean>; }, "strip", ZodTypeAny, { ...; }, { ...; }>], null>' is not assignable to type 'ZodType<NostrRelayCOUNT, ZodTypeDef, NostrRelayCOUNT>'.
|
|
95
|
-
Types of property '_type' are incompatible.
|
|
96
|
-
Type '["COUNT", string, { count?: number; approximate?: boolean; }, ...unknown[]]' is not assignable to type 'NostrRelayCOUNT'.
|
|
97
|
-
Target allows only 3 element(s) but source may have more.
|
|
98
|
-
|
|
99
|
-
[7m174[0m return z.tuple([
|
|
100
|
-
[7m [0m [91m ~~~~~~[0m
|
|
101
|
-
|
|
102
|
-
[96mNSchema.ts[0m:[93m215[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodObject<{ id: ZodString; method: ZodString; params: ZodArray<ZodString, "many">; }, "strip", ZodTypeAny, { id?: string; params?: string[]; method?: string; }, { ...; }>' is not assignable to type 'ZodType<NostrConnectRequest, ZodTypeDef, NostrConnectRequest>'.
|
|
103
|
-
Types of property '_type' are incompatible.
|
|
104
|
-
Type '{ id?: string; params?: string[]; method?: string; }' is not assignable to type 'NostrConnectRequest'.
|
|
105
|
-
Property 'id' is optional in type '{ id?: string; params?: string[]; method?: string; }' but required in type 'NostrConnectRequest'.
|
|
106
|
-
|
|
107
|
-
[7m215[0m return z.object({
|
|
108
|
-
[7m [0m [91m ~~~~~~[0m
|
|
109
|
-
|
|
110
|
-
[96mNSchema.ts[0m:[93m224[0m:[93m5[0m - [91merror[0m[90m TS2322: [0mType 'ZodObject<{ id: ZodString; result: ZodString; error: ZodOptional<ZodString>; }, "strip", ZodTypeAny, { id?: string; result?: string; error?: string; }, { ...; }>' is not assignable to type 'ZodType<NostrConnectResponse, ZodTypeDef, NostrConnectResponse>'.
|
|
111
|
-
Types of property '_type' are incompatible.
|
|
112
|
-
Type '{ id?: string; result?: string; error?: string; }' is not assignable to type 'NostrConnectResponse'.
|
|
113
|
-
Property 'id' is optional in type '{ id?: string; result?: string; error?: string; }' but required in type 'NostrConnectResponse'.
|
|
114
|
-
|
|
115
|
-
[7m224[0m return z.object({
|
|
116
|
-
[7m [0m [91m ~~~~~~[0m
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
Found 14 errors in 2 files.
|
|
120
|
-
|
|
121
|
-
Errors Files
|
|
122
|
-
1 NIP05.ts[90m:18[0m
|
|
123
|
-
13 NSchema.ts[90m:45[0m
|
|
124
|
-
[41m[30m ELIFECYCLE [39m[49m [31mCommand failed with exit code 2.[39m
|
|
3
|
+
> @nostrify/nostrify@0.46.9 build /home/sid/repos/nostrify/packages/nostrify
|
|
4
|
+
> npx tsc -p tsconfig.json && node ../../esbuild.config.js --package ./
|
|
5
|
+
|
|
6
|
+
[1mnpm[22m [33mwarn[39m [94mUnknown env config "verify-deps-before-run". This will stop working in the next major version of npm.[39m
|
|
7
|
+
[1mnpm[22m [33mwarn[39m [94mUnknown env config "_jsr-registry". This will stop working in the next major version of npm.[39m
|
|
8
|
+
[1G[0K[1G[0K⠙[1G[0KBuilding with esbuild...
|
|
9
|
+
|
|
10
|
+
[37mdist/[0m[1mNRelay1.js[0m [36m8.9kb[0m
|
|
11
|
+
[37mdist/[0m[1mNSchema.js[0m [36m5.8kb[0m
|
|
12
|
+
[37mdist/[0m[1mNPool.js[0m [36m5.0kb[0m
|
|
13
|
+
[37mdist/test/[0m[1mTestRelayServer.js[0m [36m4.4kb[0m
|
|
14
|
+
[37mdist/[0m[1mNSet.js[0m [36m4.1kb[0m
|
|
15
|
+
[37m...and 25 more output files...[0m
|
|
16
|
+
|
|
17
|
+
⚡ [32mDone in 24ms[0m
|
|
18
|
+
Copying source files...
|
|
19
|
+
Done!
|
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class BunkerURI {
|
|
2
|
+
/** Remote signer pubkey. */
|
|
3
|
+
pubkey;
|
|
4
|
+
/** Relay URLs on which the client is listening for responses from the remote-signer. */
|
|
5
|
+
relays;
|
|
6
|
+
/** A short random string that the remote-signer should return as the `result` field of its response. */
|
|
7
|
+
secret;
|
|
8
|
+
constructor(uri) {
|
|
9
|
+
const url = new URL(uri);
|
|
10
|
+
const params = new URLSearchParams(url.search);
|
|
11
|
+
const pubkey = url.hostname || url.pathname.slice(2);
|
|
12
|
+
const relays = params.getAll("relay");
|
|
13
|
+
const secret = params.get("secret") ?? void 0;
|
|
14
|
+
if (!pubkey) {
|
|
15
|
+
throw new Error("Invalid bunker URI");
|
|
16
|
+
}
|
|
17
|
+
this.pubkey = pubkey;
|
|
18
|
+
this.relays = relays;
|
|
19
|
+
this.secret = secret;
|
|
20
|
+
}
|
|
21
|
+
/** Convert into a `bunker://` URI string. */
|
|
22
|
+
get href() {
|
|
23
|
+
return this.toString();
|
|
24
|
+
}
|
|
25
|
+
/** Convert into a `bunker://` URI string. */
|
|
26
|
+
toString() {
|
|
27
|
+
return BunkerURI.toString(this);
|
|
28
|
+
}
|
|
29
|
+
/** Convert a bunker data object into a `BunkerURI` instance. */
|
|
30
|
+
static fromJSON(data) {
|
|
31
|
+
const uri = BunkerURI.toString(data);
|
|
32
|
+
return new BunkerURI(uri);
|
|
33
|
+
}
|
|
34
|
+
/** Convert a bunker data object into a bunker URI string. */
|
|
35
|
+
static toString(data) {
|
|
36
|
+
const search = new URLSearchParams();
|
|
37
|
+
for (const relay of data.relays) {
|
|
38
|
+
search.append("relay", relay);
|
|
39
|
+
}
|
|
40
|
+
if (data.secret) {
|
|
41
|
+
search.set("secret", data.secret);
|
|
42
|
+
}
|
|
43
|
+
return `bunker://${data.pubkey}?${search.toString()}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
BunkerURI
|
|
48
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** Construct a [NIP-46](https://github.com/nostr-protocol/nips/blob/master/46.md) bunker URI. */
|
|
2
|
+
export class BunkerURI {
|
|
3
|
+
/** Remote signer pubkey. */
|
|
4
|
+
pubkey: string;
|
|
5
|
+
/** Relay URLs on which the client is listening for responses from the remote-signer. */
|
|
6
|
+
relays: string[];
|
|
7
|
+
/** A short random string that the remote-signer should return as the `result` field of its response. */
|
|
8
|
+
secret?: string;
|
|
9
|
+
|
|
10
|
+
constructor(uri: string) {
|
|
11
|
+
const url = new URL(uri);
|
|
12
|
+
const params = new URLSearchParams(url.search);
|
|
13
|
+
|
|
14
|
+
// https://github.com/denoland/deno/issues/26440
|
|
15
|
+
const pubkey = url.hostname || url.pathname.slice(2);
|
|
16
|
+
const relays = params.getAll('relay');
|
|
17
|
+
const secret = params.get('secret') ?? undefined;
|
|
18
|
+
|
|
19
|
+
if (!pubkey) {
|
|
20
|
+
throw new Error('Invalid bunker URI');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.pubkey = pubkey;
|
|
24
|
+
this.relays = relays;
|
|
25
|
+
this.secret = secret;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Convert into a `bunker://` URI string. */
|
|
29
|
+
get href(): string {
|
|
30
|
+
return this.toString();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Convert into a `bunker://` URI string. */
|
|
34
|
+
toString(): string {
|
|
35
|
+
return BunkerURI.toString(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Convert a bunker data object into a `BunkerURI` instance. */
|
|
39
|
+
static fromJSON(data: { pubkey: string; relays: string[]; secret?: string }): BunkerURI {
|
|
40
|
+
const uri = BunkerURI.toString(data);
|
|
41
|
+
return new BunkerURI(uri);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Convert a bunker data object into a bunker URI string. */
|
|
45
|
+
private static toString(data: { pubkey: string; relays: string[]; secret?: string }): string {
|
|
46
|
+
const search = new URLSearchParams();
|
|
47
|
+
|
|
48
|
+
for (const relay of data.relays) {
|
|
49
|
+
search.append('relay', relay);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (data.secret) {
|
|
53
|
+
search.set('secret', data.secret);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return `bunker://${data.pubkey}?${search.toString()}`;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
class NBrowserSigner {
|
|
2
|
+
get nostr() {
|
|
3
|
+
const nostr = globalThis.nostr;
|
|
4
|
+
if (!nostr) {
|
|
5
|
+
throw new Error("Browser extension not available");
|
|
6
|
+
}
|
|
7
|
+
return nostr;
|
|
8
|
+
}
|
|
9
|
+
async getPublicKey() {
|
|
10
|
+
const pubkey = await this.nostr.getPublicKey();
|
|
11
|
+
if (typeof pubkey !== "string") {
|
|
12
|
+
throw new Error(`Nostr public key retrieval failed: expected string, got ${JSON.stringify(pubkey)}`);
|
|
13
|
+
}
|
|
14
|
+
return pubkey;
|
|
15
|
+
}
|
|
16
|
+
async signEvent(event) {
|
|
17
|
+
const signed = await this.nostr.signEvent(event);
|
|
18
|
+
if (typeof signed !== "object" || !signed.id || !signed.pubkey || !signed.sig) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
`Nostr event signing failed: expected object with id, pubkey, and sig, got ${JSON.stringify(signed)}`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return signed;
|
|
24
|
+
}
|
|
25
|
+
async getRelays() {
|
|
26
|
+
if (!this.nostr.getRelays) {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
const relays = await this.nostr.getRelays();
|
|
30
|
+
if (typeof relays !== "object" || relays === null) {
|
|
31
|
+
throw new Error(`Nostr getRelays failed: expected object, got ${JSON.stringify(relays)}`);
|
|
32
|
+
}
|
|
33
|
+
return this.nostr.getRelays();
|
|
34
|
+
}
|
|
35
|
+
get nip04() {
|
|
36
|
+
const nostr = this.nostr;
|
|
37
|
+
if (!nostr.nip04) {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
encrypt: async (pubkey, plaintext) => {
|
|
42
|
+
const encrypted = await nostr.nip04.encrypt(pubkey, plaintext);
|
|
43
|
+
if (typeof encrypted !== "string") {
|
|
44
|
+
throw new Error(`NIP-04 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
|
|
45
|
+
}
|
|
46
|
+
return encrypted;
|
|
47
|
+
},
|
|
48
|
+
decrypt: async (pubkey, ciphertext) => {
|
|
49
|
+
const decrypted = await nostr.nip04.decrypt(pubkey, ciphertext);
|
|
50
|
+
if (typeof decrypted !== "string") {
|
|
51
|
+
throw new Error(`NIP-04 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
|
|
52
|
+
}
|
|
53
|
+
return decrypted;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get nip44() {
|
|
58
|
+
const nostr = this.nostr;
|
|
59
|
+
if (!nostr.nip44) {
|
|
60
|
+
return void 0;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
encrypt: async (pubkey, plaintext) => {
|
|
64
|
+
const encrypted = await nostr.nip44.encrypt(pubkey, plaintext);
|
|
65
|
+
if (typeof encrypted !== "string") {
|
|
66
|
+
throw new Error(`NIP-44 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
|
|
67
|
+
}
|
|
68
|
+
return encrypted;
|
|
69
|
+
},
|
|
70
|
+
decrypt: async (pubkey, ciphertext) => {
|
|
71
|
+
const decrypted = await nostr.nip44.decrypt(pubkey, ciphertext);
|
|
72
|
+
if (typeof decrypted !== "string") {
|
|
73
|
+
throw new Error(`NIP-44 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
|
|
74
|
+
}
|
|
75
|
+
return decrypted;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
NBrowserSigner
|
|
82
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { NostrEvent, NostrSigner } from '@nostrify/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NIP-07-compatible signer that proxies to browser extension, normalizing behavior across different implementations.
|
|
5
|
+
*
|
|
6
|
+
* This signer delegates all operations to the browser's `window.nostr` object,
|
|
7
|
+
* which is typically provided by browser extensions like Alby, nos2x, etc.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const signer = new NBrowserSigner();
|
|
13
|
+
* const pubkey = await signer.getPublicKey();
|
|
14
|
+
* const event = await signer.signEvent({ kind: 1, content: 'Hello, world!', tags: [], created_at: 0 });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class NBrowserSigner implements NostrSigner {
|
|
18
|
+
private get nostr(): NostrSigner {
|
|
19
|
+
const nostr = (globalThis as { nostr?: NostrSigner }).nostr;
|
|
20
|
+
if (!nostr) {
|
|
21
|
+
throw new Error('Browser extension not available');
|
|
22
|
+
}
|
|
23
|
+
return nostr;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async getPublicKey(): Promise<string> {
|
|
27
|
+
const pubkey = await this.nostr.getPublicKey();
|
|
28
|
+
if (typeof pubkey !== 'string') {
|
|
29
|
+
throw new Error(`Nostr public key retrieval failed: expected string, got ${JSON.stringify(pubkey)}`);
|
|
30
|
+
}
|
|
31
|
+
return pubkey;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async signEvent(event: Omit<NostrEvent, 'id' | 'pubkey' | 'sig'>): Promise<NostrEvent> {
|
|
35
|
+
const signed = await this.nostr.signEvent(event);
|
|
36
|
+
if (typeof signed !== 'object' || !signed.id || !signed.pubkey || !signed.sig) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Nostr event signing failed: expected object with id, pubkey, and sig, got ${JSON.stringify(signed)}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return signed;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async getRelays(): Promise<Record<string, { read: boolean; write: boolean }>> {
|
|
45
|
+
if (!this.nostr.getRelays) {
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
const relays = await this.nostr.getRelays();
|
|
49
|
+
if (typeof relays !== 'object' || relays === null) {
|
|
50
|
+
throw new Error(`Nostr getRelays failed: expected object, got ${JSON.stringify(relays)}`);
|
|
51
|
+
}
|
|
52
|
+
return this.nostr.getRelays();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get nip04(): NostrSigner['nip04'] {
|
|
56
|
+
const nostr = this.nostr;
|
|
57
|
+
if (!nostr.nip04) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
encrypt: async (pubkey: string, plaintext: string): Promise<string> => {
|
|
62
|
+
const encrypted = await nostr.nip04!.encrypt(pubkey, plaintext);
|
|
63
|
+
if (typeof encrypted !== 'string') {
|
|
64
|
+
throw new Error(`NIP-04 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
|
|
65
|
+
}
|
|
66
|
+
return encrypted;
|
|
67
|
+
},
|
|
68
|
+
decrypt: async (pubkey: string, ciphertext: string): Promise<string> => {
|
|
69
|
+
const decrypted = await nostr.nip04!.decrypt(pubkey, ciphertext);
|
|
70
|
+
if (typeof decrypted !== 'string') {
|
|
71
|
+
throw new Error(`NIP-04 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
|
|
72
|
+
}
|
|
73
|
+
return decrypted;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get nip44(): NostrSigner['nip44'] {
|
|
79
|
+
const nostr = this.nostr;
|
|
80
|
+
if (!nostr.nip44) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
encrypt: async (pubkey: string, plaintext: string): Promise<string> => {
|
|
85
|
+
const encrypted = await nostr.nip44!.encrypt(pubkey, plaintext);
|
|
86
|
+
if (typeof encrypted !== 'string') {
|
|
87
|
+
throw new Error(`NIP-44 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
|
|
88
|
+
}
|
|
89
|
+
return encrypted;
|
|
90
|
+
},
|
|
91
|
+
decrypt: async (pubkey: string, ciphertext: string): Promise<string> => {
|
|
92
|
+
const decrypted = await nostr.nip44!.decrypt(pubkey, ciphertext);
|
|
93
|
+
if (typeof decrypted !== 'string') {
|
|
94
|
+
throw new Error(`NIP-44 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
|
|
95
|
+
}
|
|
96
|
+
return decrypted;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
package/dist/NCache.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LRUCache } from "lru-cache";
|
|
2
|
+
import { matchFilters } from "nostr-tools";
|
|
3
|
+
import { NSet } from "./NSet.js";
|
|
4
|
+
class NCache extends NSet {
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(new LRUCache(...args));
|
|
7
|
+
}
|
|
8
|
+
async event(event) {
|
|
9
|
+
this.add(event);
|
|
10
|
+
}
|
|
11
|
+
async query(filters) {
|
|
12
|
+
const events = [];
|
|
13
|
+
for (const event of this) {
|
|
14
|
+
if (matchFilters(filters, event)) {
|
|
15
|
+
this.cache.get(event.id);
|
|
16
|
+
events.push(event);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return events;
|
|
20
|
+
}
|
|
21
|
+
async remove(filters) {
|
|
22
|
+
for (const event of this) {
|
|
23
|
+
if (matchFilters(filters, event)) {
|
|
24
|
+
this.delete(event);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async count(filters) {
|
|
29
|
+
const events = await this.query(filters);
|
|
30
|
+
return {
|
|
31
|
+
count: events.length,
|
|
32
|
+
approximate: false
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
[Symbol.toStringTag] = "NCache";
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
NCache
|
|
39
|
+
};
|
package/dist/NCache.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// deno-lint-ignore-file require-await
|
|
2
|
+
|
|
3
|
+
import type { NostrEvent, NostrFilter, NostrRelayCOUNT, NStore } from '@nostrify/types';
|
|
4
|
+
import { LRUCache } from 'lru-cache';
|
|
5
|
+
import { matchFilters } from 'nostr-tools';
|
|
6
|
+
|
|
7
|
+
import { NSet } from './NSet.ts';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Nostr LRU cache based on [`npm:lru-cache`](https://www.npmjs.com/package/lru-cache).
|
|
11
|
+
* It implements both `NStore` and `NSet` interfaces.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // Accepts the options of `npm:lru-cache`:
|
|
15
|
+
* const cache = new NCache({ max: 1000 });
|
|
16
|
+
*
|
|
17
|
+
* // Events can be added like a regular `Set`:
|
|
18
|
+
* cache.add(event1);
|
|
19
|
+
* cache.add(event2);
|
|
20
|
+
*
|
|
21
|
+
* // Can be queried like `NStore`:
|
|
22
|
+
* const events = await cache.query([{ kinds: [1] }]);
|
|
23
|
+
*
|
|
24
|
+
* // Can be iterated like `NSet`:
|
|
25
|
+
* for (const event of cache) {
|
|
26
|
+
* console.log(event);
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
class NCache extends NSet implements NStore {
|
|
31
|
+
constructor(
|
|
32
|
+
...args: ConstructorParameters<typeof LRUCache<string, NostrEvent>>
|
|
33
|
+
) {
|
|
34
|
+
super(new LRUCache<string, NostrEvent>(...args) as Map<string, NostrEvent>);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async event(event: NostrEvent): Promise<void> {
|
|
38
|
+
this.add(event);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async query(filters: NostrFilter[]): Promise<NostrEvent[]> {
|
|
42
|
+
const events: NostrEvent[] = [];
|
|
43
|
+
|
|
44
|
+
for (const event of this) {
|
|
45
|
+
if (matchFilters(filters, event)) {
|
|
46
|
+
this.cache.get(event.id);
|
|
47
|
+
events.push(event);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return events;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async remove(filters: NostrFilter[]): Promise<void> {
|
|
55
|
+
for (const event of this) {
|
|
56
|
+
if (matchFilters(filters, event)) {
|
|
57
|
+
this.delete(event);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async count(filters: NostrFilter[]): Promise<NostrRelayCOUNT[2]> {
|
|
63
|
+
const events = await this.query(filters);
|
|
64
|
+
return {
|
|
65
|
+
count: events.length,
|
|
66
|
+
approximate: false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override [Symbol.toStringTag] = 'NCache';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { NCache };
|