applesauce-signers 2.0.0 → 3.1.0
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/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/nostr-connect.d.ts +92 -0
- package/dist/helpers/nostr-connect.js +93 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interop.d.ts +49 -0
- package/dist/interop.js +19 -0
- package/dist/signers/amber-clipboard-signer.d.ts +6 -6
- package/dist/signers/extension-signer.d.ts +9 -16
- package/dist/signers/extension-signer.js +9 -20
- package/dist/signers/index.d.ts +1 -0
- package/dist/signers/index.js +1 -0
- package/dist/signers/nostr-connect-provider.d.ts +121 -0
- package/dist/signers/nostr-connect-provider.js +406 -0
- package/dist/signers/nostr-connect-signer.d.ts +23 -101
- package/dist/signers/nostr-connect-signer.js +52 -81
- package/dist/signers/password-signer.d.ts +6 -6
- package/dist/signers/password-signer.js +8 -3
- package/dist/signers/readonly-signer.d.ts +12 -13
- package/dist/signers/readonly-signer.js +6 -9
- package/dist/signers/serial-port-signer.d.ts +4 -4
- package/dist/signers/simple-signer.d.ts +2 -1
- package/package.json +8 -4
- package/dist/nip-07.d.ts +0 -20
- package/dist/nip-07.js +0 -1
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export declare function isErrorResponse(response: any): response is NostrConnectErrorResponse;
|
|
2
|
+
export declare enum Permission {
|
|
3
|
+
GetPublicKey = "get_pubic_key",
|
|
4
|
+
SignEvent = "sign_event",
|
|
5
|
+
Nip04Encrypt = "nip04_encrypt",
|
|
6
|
+
Nip04Decrypt = "nip04_decrypt",
|
|
7
|
+
Nip44Encrypt = "nip44_encrypt",
|
|
8
|
+
Nip44Decrypt = "nip44_decrypt"
|
|
9
|
+
}
|
|
10
|
+
export declare enum NostrConnectMethod {
|
|
11
|
+
Connect = "connect",
|
|
12
|
+
CreateAccount = "create_account",
|
|
13
|
+
GetPublicKey = "get_public_key",
|
|
14
|
+
SignEvent = "sign_event",
|
|
15
|
+
Nip04Encrypt = "nip04_encrypt",
|
|
16
|
+
Nip04Decrypt = "nip04_decrypt",
|
|
17
|
+
Nip44Encrypt = "nip44_encrypt",
|
|
18
|
+
Nip44Decrypt = "nip44_decrypt"
|
|
19
|
+
}
|
|
20
|
+
export type ConnectRequestParams = {
|
|
21
|
+
[NostrConnectMethod.Connect]: [string] | [string, string] | [string, string, string];
|
|
22
|
+
[NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string] | [string, string, string, string];
|
|
23
|
+
[NostrConnectMethod.GetPublicKey]: [];
|
|
24
|
+
[NostrConnectMethod.SignEvent]: [string];
|
|
25
|
+
[NostrConnectMethod.Nip04Encrypt]: [string, string];
|
|
26
|
+
[NostrConnectMethod.Nip04Decrypt]: [string, string];
|
|
27
|
+
[NostrConnectMethod.Nip44Encrypt]: [string, string];
|
|
28
|
+
[NostrConnectMethod.Nip44Decrypt]: [string, string];
|
|
29
|
+
};
|
|
30
|
+
export type ConnectResponseResults = {
|
|
31
|
+
[NostrConnectMethod.Connect]: "ack" | string;
|
|
32
|
+
[NostrConnectMethod.CreateAccount]: string;
|
|
33
|
+
[NostrConnectMethod.GetPublicKey]: string;
|
|
34
|
+
[NostrConnectMethod.SignEvent]: string;
|
|
35
|
+
[NostrConnectMethod.Nip04Encrypt]: string;
|
|
36
|
+
[NostrConnectMethod.Nip04Decrypt]: string;
|
|
37
|
+
[NostrConnectMethod.Nip44Encrypt]: string;
|
|
38
|
+
[NostrConnectMethod.Nip44Decrypt]: string;
|
|
39
|
+
};
|
|
40
|
+
export type NostrConnectRequest<N extends NostrConnectMethod> = {
|
|
41
|
+
id: string;
|
|
42
|
+
method: N;
|
|
43
|
+
params: ConnectRequestParams[N];
|
|
44
|
+
};
|
|
45
|
+
export type NostrConnectResponse<N extends NostrConnectMethod> = {
|
|
46
|
+
id: string;
|
|
47
|
+
result: ConnectResponseResults[N];
|
|
48
|
+
error?: string;
|
|
49
|
+
};
|
|
50
|
+
export type NostrConnectErrorResponse = {
|
|
51
|
+
id: string;
|
|
52
|
+
result: string;
|
|
53
|
+
error: string;
|
|
54
|
+
};
|
|
55
|
+
/** A bunker:// URI */
|
|
56
|
+
export type BunkerURI = {
|
|
57
|
+
remote: string;
|
|
58
|
+
relays: string[];
|
|
59
|
+
secret?: string;
|
|
60
|
+
};
|
|
61
|
+
/** Parse a bunker:// URI */
|
|
62
|
+
export declare function parseBunkerURI(uri: string): BunkerURI;
|
|
63
|
+
/** Creates a bunker:// URI from a {@link BunkerURI} object */
|
|
64
|
+
export declare function createBunkerURI(data: BunkerURI): string;
|
|
65
|
+
/** App metadata for a nostrconnect:// URI */
|
|
66
|
+
export type NostrConnectAppMetadata = {
|
|
67
|
+
/** The name of the client */
|
|
68
|
+
name?: string;
|
|
69
|
+
/** An image for the client */
|
|
70
|
+
image?: string;
|
|
71
|
+
/** The url of the client */
|
|
72
|
+
url?: string | URL;
|
|
73
|
+
/** The permissions the client has */
|
|
74
|
+
permissions?: string[];
|
|
75
|
+
};
|
|
76
|
+
/** A nostrconnect:// URI */
|
|
77
|
+
export type NostrConnectURI = {
|
|
78
|
+
/** The pubkey of the client */
|
|
79
|
+
client: string;
|
|
80
|
+
/** The secret used by the signer to connect to the client */
|
|
81
|
+
secret: string;
|
|
82
|
+
/** The relays used to communicate with the remote signer */
|
|
83
|
+
relays: string[];
|
|
84
|
+
/** The metadata of the client */
|
|
85
|
+
metadata?: NostrConnectAppMetadata;
|
|
86
|
+
};
|
|
87
|
+
/** Parse a nostrconnect:// URI */
|
|
88
|
+
export declare function parseNostrConnectURI(uri: string): NostrConnectURI;
|
|
89
|
+
/** Create a nostrconnect:// URI from a {@link NostrConnectURI} object */
|
|
90
|
+
export declare function createNostrConnectURI(data: NostrConnectURI): string;
|
|
91
|
+
/** Build an array of signing permissions for event kinds */
|
|
92
|
+
export declare function buildSigningPermissions(kinds: number[]): string[];
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { setHiddenContentEncryptionMethod } from "applesauce-core/helpers";
|
|
2
|
+
import { isHexKey } from "applesauce-core/helpers/string";
|
|
3
|
+
import { kinds } from "nostr-tools";
|
|
4
|
+
// Set encryption types for nostr connect events
|
|
5
|
+
setHiddenContentEncryptionMethod(kinds.NostrConnect, "nip44");
|
|
6
|
+
export function isErrorResponse(response) {
|
|
7
|
+
return !!response.error;
|
|
8
|
+
}
|
|
9
|
+
export var Permission;
|
|
10
|
+
(function (Permission) {
|
|
11
|
+
Permission["GetPublicKey"] = "get_pubic_key";
|
|
12
|
+
Permission["SignEvent"] = "sign_event";
|
|
13
|
+
Permission["Nip04Encrypt"] = "nip04_encrypt";
|
|
14
|
+
Permission["Nip04Decrypt"] = "nip04_decrypt";
|
|
15
|
+
Permission["Nip44Encrypt"] = "nip44_encrypt";
|
|
16
|
+
Permission["Nip44Decrypt"] = "nip44_decrypt";
|
|
17
|
+
})(Permission || (Permission = {}));
|
|
18
|
+
export var NostrConnectMethod;
|
|
19
|
+
(function (NostrConnectMethod) {
|
|
20
|
+
NostrConnectMethod["Connect"] = "connect";
|
|
21
|
+
NostrConnectMethod["CreateAccount"] = "create_account";
|
|
22
|
+
NostrConnectMethod["GetPublicKey"] = "get_public_key";
|
|
23
|
+
NostrConnectMethod["SignEvent"] = "sign_event";
|
|
24
|
+
NostrConnectMethod["Nip04Encrypt"] = "nip04_encrypt";
|
|
25
|
+
NostrConnectMethod["Nip04Decrypt"] = "nip04_decrypt";
|
|
26
|
+
NostrConnectMethod["Nip44Encrypt"] = "nip44_encrypt";
|
|
27
|
+
NostrConnectMethod["Nip44Decrypt"] = "nip44_decrypt";
|
|
28
|
+
})(NostrConnectMethod || (NostrConnectMethod = {}));
|
|
29
|
+
/** Parse a bunker:// URI */
|
|
30
|
+
export function parseBunkerURI(uri) {
|
|
31
|
+
const url = new URL(uri);
|
|
32
|
+
// firefox puts pubkey part in host, chrome puts pubkey in pathname
|
|
33
|
+
const remote = url.host || url.pathname.replace("//", "");
|
|
34
|
+
if (!isHexKey(remote))
|
|
35
|
+
throw new Error("Invalid bunker URI: remote is not a valid hex key");
|
|
36
|
+
const relays = url.searchParams.getAll("relay");
|
|
37
|
+
if (relays.length === 0)
|
|
38
|
+
throw new Error("Invalid bunker URI: missing relays");
|
|
39
|
+
const secret = url.searchParams.get("secret") ?? undefined;
|
|
40
|
+
return { remote, relays, secret };
|
|
41
|
+
}
|
|
42
|
+
/** Creates a bunker:// URI from a {@link BunkerURI} object */
|
|
43
|
+
export function createBunkerURI(data) {
|
|
44
|
+
const url = new URL(`bunker://${data.remote}`);
|
|
45
|
+
data.relays.forEach((relay) => url.searchParams.append("relay", relay));
|
|
46
|
+
if (data.secret)
|
|
47
|
+
url.searchParams.set("secret", data.secret);
|
|
48
|
+
return url.toString();
|
|
49
|
+
}
|
|
50
|
+
/** Parse a nostrconnect:// URI */
|
|
51
|
+
export function parseNostrConnectURI(uri) {
|
|
52
|
+
const url = new URL(uri);
|
|
53
|
+
const client = url.host || url.pathname.replace("//", "");
|
|
54
|
+
if (!isHexKey(client))
|
|
55
|
+
throw new Error("Invalid nostrconnect URI: client is not a valid hex key");
|
|
56
|
+
const secret = url.searchParams.get("secret");
|
|
57
|
+
const relays = url.searchParams.getAll("relay");
|
|
58
|
+
if (!secret)
|
|
59
|
+
throw new Error("Invalid nostrconnect URI: missing secret");
|
|
60
|
+
if (relays.length === 0)
|
|
61
|
+
throw new Error("Invalid nostrconnect URI: missing relays");
|
|
62
|
+
const metadata = {
|
|
63
|
+
name: url.searchParams.get("name") ?? undefined,
|
|
64
|
+
image: url.searchParams.get("image") ?? undefined,
|
|
65
|
+
url: url.searchParams.get("url") ?? undefined,
|
|
66
|
+
permissions: url.searchParams.get("perms")?.split(",") ?? undefined,
|
|
67
|
+
};
|
|
68
|
+
/** Omit metadata if all values are undefined */
|
|
69
|
+
if (Object.values(metadata).every((v) => v === undefined))
|
|
70
|
+
return { client, secret, relays };
|
|
71
|
+
else
|
|
72
|
+
return { client, secret, relays, metadata };
|
|
73
|
+
}
|
|
74
|
+
/** Create a nostrconnect:// URI from a {@link NostrConnectURI} object */
|
|
75
|
+
export function createNostrConnectURI(data) {
|
|
76
|
+
const params = new URLSearchParams();
|
|
77
|
+
params.set("secret", data.secret);
|
|
78
|
+
if (data.metadata?.name)
|
|
79
|
+
params.set("name", data.metadata.name);
|
|
80
|
+
if (data.metadata?.url)
|
|
81
|
+
params.set("url", String(data.metadata.url));
|
|
82
|
+
if (data.metadata?.image)
|
|
83
|
+
params.set("image", data.metadata.image);
|
|
84
|
+
if (data.metadata?.permissions)
|
|
85
|
+
params.set("perms", data.metadata.permissions.join(","));
|
|
86
|
+
for (const relay of data.relays)
|
|
87
|
+
params.append("relay", relay);
|
|
88
|
+
return `nostrconnect://${data.client}?` + params.toString();
|
|
89
|
+
}
|
|
90
|
+
/** Build an array of signing permissions for event kinds */
|
|
91
|
+
export function buildSigningPermissions(kinds) {
|
|
92
|
+
return [Permission.GetPublicKey, ...kinds.map((k) => `${Permission.SignEvent}:${k}`)];
|
|
93
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { EventTemplate, Filter, NostrEvent } from "nostr-tools";
|
|
2
|
+
import { ObservableInput } from "rxjs";
|
|
3
|
+
export type ISigner = {
|
|
4
|
+
getPublicKey: () => Promise<string>;
|
|
5
|
+
signEvent: (template: EventTemplate) => Promise<NostrEvent>;
|
|
6
|
+
nip04?: {
|
|
7
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
8
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
9
|
+
};
|
|
10
|
+
nip44?: {
|
|
11
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
12
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
/** @deprecated Use ISigner instead */
|
|
16
|
+
export type Nip07Interface = ISigner;
|
|
17
|
+
/** A method used to subscribe to events on a set of relays */
|
|
18
|
+
export type NostrSubscriptionMethod = (relays: string[], filters: Filter[]) => ObservableInput<NostrEvent | string>;
|
|
19
|
+
/** A method used for publishing an event, can return a Promise that completes when published or an Observable that completes when published*/
|
|
20
|
+
export type NostrPublishMethod = (relays: string[], event: NostrEvent) => Promise<any> | ObservableInput<any>;
|
|
21
|
+
/** A simple pool type that combines the subscription and publish methods */
|
|
22
|
+
export type NostrPool = {
|
|
23
|
+
subscription: NostrSubscriptionMethod;
|
|
24
|
+
publish: NostrPublishMethod;
|
|
25
|
+
};
|
|
26
|
+
/** Options for setting the subscription and publish methods */
|
|
27
|
+
export type NostrConnectionMethodsOptions = {
|
|
28
|
+
/** An optional method for subscribing to relays */
|
|
29
|
+
subscriptionMethod?: NostrSubscriptionMethod;
|
|
30
|
+
/** An optional method for publishing events */
|
|
31
|
+
publishMethod?: NostrPublishMethod;
|
|
32
|
+
/** An optional pool for connection methods */
|
|
33
|
+
pool?: NostrPool;
|
|
34
|
+
};
|
|
35
|
+
/** A class that implements has global fallback methods for subscription and publish methods */
|
|
36
|
+
export interface NostrConnectionClassMethods {
|
|
37
|
+
new (...args: any[]): any;
|
|
38
|
+
/** A fallback method to use for subscriptionMethod if none is passed in when creating the client */
|
|
39
|
+
subscriptionMethod: NostrSubscriptionMethod | undefined;
|
|
40
|
+
/** A fallback method to use for publishMethod if none is passed in when creating the client */
|
|
41
|
+
publishMethod: NostrPublishMethod | undefined;
|
|
42
|
+
/** A fallback pool to use if none is pass in when creating the signer */
|
|
43
|
+
pool: NostrPool | undefined;
|
|
44
|
+
}
|
|
45
|
+
/** Get the subscription and publish methods for a NostrConnect class */
|
|
46
|
+
export declare function getConnectionMethods(options: NostrConnectionMethodsOptions, cls?: NostrConnectionClassMethods): {
|
|
47
|
+
subscriptionMethod: NostrSubscriptionMethod;
|
|
48
|
+
publishMethod: NostrPublishMethod;
|
|
49
|
+
};
|
package/dist/interop.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Get the subscription and publish methods for a NostrConnect class */
|
|
2
|
+
export function getConnectionMethods(options, cls) {
|
|
3
|
+
const subscriptionMethod = options.subscriptionMethod ||
|
|
4
|
+
options.pool?.subscription.bind(options.pool) ||
|
|
5
|
+
cls?.subscriptionMethod ||
|
|
6
|
+
cls?.pool?.subscription.bind(cls.pool);
|
|
7
|
+
if (!subscriptionMethod)
|
|
8
|
+
throw new Error("Missing subscriptionMethod, either pass a method or set subscriptionMethod globally on the class");
|
|
9
|
+
const publishMethod = options.publishMethod ||
|
|
10
|
+
options.pool?.publish.bind(options.pool) ||
|
|
11
|
+
cls?.publishMethod ||
|
|
12
|
+
cls?.pool?.publish.bind(cls.pool);
|
|
13
|
+
if (!publishMethod)
|
|
14
|
+
throw new Error("Missing publishMethod, either pass a method or set publishMethod globally on the class");
|
|
15
|
+
return {
|
|
16
|
+
subscriptionMethod,
|
|
17
|
+
publishMethod,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { EventTemplate, VerifiedEvent, verifyEvent } from "nostr-tools";
|
|
2
|
-
import {
|
|
2
|
+
import { ISigner } from "../interop.js";
|
|
3
3
|
/**
|
|
4
4
|
* A Signer for [amber](https://github.com/greenart7c3/Amber) clipboard API
|
|
5
5
|
* @see https://github.com/greenart7c3/Amber/blob/master/docs/web-apps.md
|
|
6
6
|
*/
|
|
7
|
-
export declare class AmberClipboardSigner implements
|
|
7
|
+
export declare class AmberClipboardSigner implements ISigner {
|
|
8
8
|
/** If the signer is supported on this platform */
|
|
9
9
|
static SUPPORTED: false | (() => Promise<string>);
|
|
10
10
|
private pendingRequest;
|
|
11
11
|
pubkey?: string;
|
|
12
12
|
verifyEvent: typeof verifyEvent;
|
|
13
13
|
nip04: {
|
|
14
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string
|
|
15
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string
|
|
14
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
15
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
16
16
|
};
|
|
17
17
|
nip44: {
|
|
18
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string
|
|
19
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string
|
|
18
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
19
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
20
20
|
};
|
|
21
21
|
constructor();
|
|
22
22
|
private onVisibilityChange;
|
|
@@ -1,26 +1,19 @@
|
|
|
1
|
-
import { EventTemplate } from "nostr-tools";
|
|
2
|
-
import {
|
|
1
|
+
import { EventTemplate, VerifiedEvent } from "nostr-tools";
|
|
2
|
+
import { ISigner } from "../interop.js";
|
|
3
3
|
/** AN error that is throw when the window.nostr extension is missing */
|
|
4
4
|
export declare class ExtensionMissingError extends Error {
|
|
5
5
|
}
|
|
6
6
|
/** A signer that is a proxy for window.nostr */
|
|
7
|
-
export declare class ExtensionSigner implements
|
|
7
|
+
export declare class ExtensionSigner implements ISigner {
|
|
8
8
|
get nip04(): {
|
|
9
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string
|
|
10
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string
|
|
9
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
10
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
11
11
|
} | undefined;
|
|
12
12
|
get nip44(): {
|
|
13
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string
|
|
14
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string
|
|
13
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
14
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
15
15
|
} | undefined;
|
|
16
16
|
protected pubkey: string | undefined;
|
|
17
|
-
getPublicKey():
|
|
18
|
-
|
|
19
|
-
read: boolean;
|
|
20
|
-
write: boolean;
|
|
21
|
-
}> | Promise<Record<string, {
|
|
22
|
-
read: boolean;
|
|
23
|
-
write: boolean;
|
|
24
|
-
}>>;
|
|
25
|
-
signEvent(template: EventTemplate): import("nostr-tools").Event | Promise<import("nostr-tools").Event>;
|
|
17
|
+
getPublicKey(): Promise<string>;
|
|
18
|
+
signEvent(template: EventTemplate): Promise<VerifiedEvent>;
|
|
26
19
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { verifyEvent } from "nostr-tools";
|
|
1
2
|
/** AN error that is throw when the window.nostr extension is missing */
|
|
2
3
|
export class ExtensionMissingError extends Error {
|
|
3
4
|
}
|
|
@@ -10,32 +11,20 @@ export class ExtensionSigner {
|
|
|
10
11
|
return window.nostr?.nip44;
|
|
11
12
|
}
|
|
12
13
|
pubkey = undefined;
|
|
13
|
-
getPublicKey() {
|
|
14
|
+
async getPublicKey() {
|
|
14
15
|
if (!window.nostr)
|
|
15
16
|
throw new ExtensionMissingError("Signer extension missing");
|
|
16
17
|
if (this.pubkey)
|
|
17
18
|
return this.pubkey;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return p.then((pubkey) => {
|
|
21
|
-
this.pubkey = pubkey;
|
|
22
|
-
return pubkey;
|
|
23
|
-
});
|
|
24
|
-
else {
|
|
25
|
-
this.pubkey = p;
|
|
26
|
-
return p;
|
|
27
|
-
}
|
|
19
|
+
this.pubkey = await window.nostr.getPublicKey();
|
|
20
|
+
return this.pubkey;
|
|
28
21
|
}
|
|
29
|
-
|
|
22
|
+
async signEvent(template) {
|
|
30
23
|
if (!window.nostr)
|
|
31
24
|
throw new ExtensionMissingError("Signer extension missing");
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
signEvent(template) {
|
|
37
|
-
if (!window.nostr)
|
|
38
|
-
throw new ExtensionMissingError("Signer extension missing");
|
|
39
|
-
return window.nostr.signEvent(template);
|
|
25
|
+
const event = await window.nostr.signEvent(template);
|
|
26
|
+
if (!verifyEvent(event))
|
|
27
|
+
throw new Error("Extension returned an invalid event");
|
|
28
|
+
return event;
|
|
40
29
|
}
|
|
41
30
|
}
|
package/dist/signers/index.d.ts
CHANGED
package/dist/signers/index.js
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
2
|
+
import { Subscription } from "rxjs";
|
|
3
|
+
import { ConnectRequestParams, ConnectResponseResults, NostrConnectMethod, NostrConnectResponse, NostrConnectURI } from "../helpers/nostr-connect.js";
|
|
4
|
+
import { ISigner, NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "../interop.js";
|
|
5
|
+
export interface ProviderAuthorization {
|
|
6
|
+
/** A method used to accept or reject `connect` requests */
|
|
7
|
+
onConnect?: (client: string, permissions: string[]) => boolean | Promise<boolean>;
|
|
8
|
+
/** A method used to accept or reject `sign_event` requests */
|
|
9
|
+
onSignEvent?: (draft: EventTemplate, client: string) => boolean | Promise<boolean>;
|
|
10
|
+
/** A method used to accept or reject `nip04_encrypt` requests */
|
|
11
|
+
onNip04Encrypt?: (pubkey: string, plaintext: string, client: string) => boolean | Promise<boolean>;
|
|
12
|
+
/** A method used to accept or reject `nip04_decrypt` requests */
|
|
13
|
+
onNip04Decrypt?: (pubkey: string, ciphertext: string, client: string) => boolean | Promise<boolean>;
|
|
14
|
+
/** A method used to accept or reject `nip44_encrypt` requests */
|
|
15
|
+
onNip44Encrypt?: (pubkey: string, plaintext: string, client: string) => boolean | Promise<boolean>;
|
|
16
|
+
/** A method used to accept or reject `nip44_decrypt` requests */
|
|
17
|
+
onNip44Decrypt?: (pubkey: string, ciphertext: string, client: string) => boolean | Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
export type NostrConnectProviderOptions = ProviderAuthorization & NostrConnectionMethodsOptions & {
|
|
20
|
+
/** The relays to communicate over */
|
|
21
|
+
relays: string[];
|
|
22
|
+
/** The signer to use for signing events and encryption */
|
|
23
|
+
upstream: ISigner;
|
|
24
|
+
/** Optional signer for provider identity */
|
|
25
|
+
signer?: ISigner;
|
|
26
|
+
/** A random secret used to authorize clients to connect */
|
|
27
|
+
secret?: string;
|
|
28
|
+
/** Callback for when a client connects (receives a `connect` request) */
|
|
29
|
+
onClientConnect?: (client: string) => any;
|
|
30
|
+
/** Callback for when a client disconnects (previously connected and the provider stops) */
|
|
31
|
+
onClientDisconnect?: (client: string) => void;
|
|
32
|
+
};
|
|
33
|
+
export declare class NostrConnectProvider implements ProviderAuthorization {
|
|
34
|
+
/** A fallback method to use for subscriptionMethod if none is passed in when creating the provider */
|
|
35
|
+
static subscriptionMethod: NostrSubscriptionMethod | undefined;
|
|
36
|
+
/** A fallback method to use for publishMethod if none is passed in when creating the provider */
|
|
37
|
+
static publishMethod: NostrPublishMethod | undefined;
|
|
38
|
+
/** A fallback pool to use if none is pass in when creating the provider */
|
|
39
|
+
static pool: NostrPool | undefined;
|
|
40
|
+
/** A method that is called when an event needs to be published */
|
|
41
|
+
protected publishMethod: NostrPublishMethod;
|
|
42
|
+
/** The active nostr subscription */
|
|
43
|
+
protected subscriptionMethod: NostrSubscriptionMethod;
|
|
44
|
+
/** Internal logger */
|
|
45
|
+
protected log: import("debug").Debugger;
|
|
46
|
+
/** A set of nostr requests that have been seen */
|
|
47
|
+
protected seen: Set<string>;
|
|
48
|
+
/** The main signer for the actual signing operations */
|
|
49
|
+
upstream: ISigner;
|
|
50
|
+
/** The identity signer (provider's identity) */
|
|
51
|
+
signer: ISigner;
|
|
52
|
+
/** Whether the provider is listening for events */
|
|
53
|
+
listening: boolean;
|
|
54
|
+
/** The connected client's public key */
|
|
55
|
+
client?: string;
|
|
56
|
+
/** The secret used to authorize clients to connect */
|
|
57
|
+
secret?: string;
|
|
58
|
+
/** Relays to communicate over */
|
|
59
|
+
readonly relays: string[];
|
|
60
|
+
/** Whether a client is connected (received a `connect` request) */
|
|
61
|
+
connected: boolean;
|
|
62
|
+
/** Callbacks */
|
|
63
|
+
onClientConnect?: (client: string) => any;
|
|
64
|
+
onClientDisconnect?: (client: string) => any;
|
|
65
|
+
/** A method used to accept or reject `connect` requests */
|
|
66
|
+
onConnect?: (client: string, permissions: string[]) => boolean | Promise<boolean>;
|
|
67
|
+
/** A method used to accept or reject `sign_event` requests */
|
|
68
|
+
onSignEvent?: (draft: EventTemplate, client: string) => boolean | Promise<boolean>;
|
|
69
|
+
/** A method used to accept or reject `nip04_encrypt` requests */
|
|
70
|
+
onNip04Encrypt?: (pubkey: string, plaintext: string, client: string) => boolean | Promise<boolean>;
|
|
71
|
+
/** A method used to accept or reject `nip04_decrypt` requests */
|
|
72
|
+
onNip04Decrypt?: (pubkey: string, ciphertext: string, client: string) => boolean | Promise<boolean>;
|
|
73
|
+
/** A method used to accept or reject `nip44_encrypt` requests */
|
|
74
|
+
onNip44Encrypt?: (pubkey: string, plaintext: string, client: string) => boolean | Promise<boolean>;
|
|
75
|
+
/** A method used to accept or reject `nip44_decrypt` requests */
|
|
76
|
+
onNip44Decrypt?: (pubkey: string, ciphertext: string, client: string) => boolean | Promise<boolean>;
|
|
77
|
+
constructor(options: NostrConnectProviderOptions);
|
|
78
|
+
/** The currently active REQ subscription */
|
|
79
|
+
protected req?: Subscription;
|
|
80
|
+
/** Updates the relay subscription to listen for request events */
|
|
81
|
+
protected updateSubscription(): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Start the provider
|
|
84
|
+
* @param request - An inital `connect` request to respond to or a {@link NostrConnectURI}
|
|
85
|
+
*/
|
|
86
|
+
start(request?: NostrEvent | NostrConnectURI | string): Promise<void>;
|
|
87
|
+
/** Stop the provider */
|
|
88
|
+
stop(): Promise<void>;
|
|
89
|
+
private waitingPromise;
|
|
90
|
+
/** Wait for a client to connect */
|
|
91
|
+
waitForClient(abort?: AbortSignal): Promise<string>;
|
|
92
|
+
/** Call this method with incoming events */
|
|
93
|
+
handleEvent(event: NostrEvent): Promise<void>;
|
|
94
|
+
/** Handle an initial NostrConnectURI */
|
|
95
|
+
handleNostrConnectURI(uri: NostrConnectURI | string): Promise<void>;
|
|
96
|
+
/** Handle connect request */
|
|
97
|
+
protected handleConnect(client: string, [target, secret, permissionsStr]: ConnectRequestParams[NostrConnectMethod.Connect]): Promise<ConnectResponseResults[NostrConnectMethod.Connect]>;
|
|
98
|
+
/** Handle sign event request */
|
|
99
|
+
protected handleSignEvent([eventJson]: ConnectRequestParams[NostrConnectMethod.SignEvent]): Promise<ConnectResponseResults[NostrConnectMethod.SignEvent]>;
|
|
100
|
+
/** Handle NIP-04 encryption */
|
|
101
|
+
protected handleNip04Encrypt([pubkey, plaintext,]: ConnectRequestParams[NostrConnectMethod.Nip04Encrypt]): Promise<ConnectResponseResults[NostrConnectMethod.Nip04Encrypt]>;
|
|
102
|
+
/** Handle NIP-04 decryption */
|
|
103
|
+
protected handleNip04Decrypt([pubkey, ciphertext,]: ConnectRequestParams[NostrConnectMethod.Nip04Decrypt]): Promise<ConnectResponseResults[NostrConnectMethod.Nip04Decrypt]>;
|
|
104
|
+
/** Handle NIP-44 encryption */
|
|
105
|
+
protected handleNip44Encrypt([pubkey, plaintext,]: ConnectRequestParams[NostrConnectMethod.Nip44Encrypt]): Promise<ConnectResponseResults[NostrConnectMethod.Nip44Encrypt]>;
|
|
106
|
+
/** Handle NIP-44 decryption */
|
|
107
|
+
protected handleNip44Decrypt([pubkey, ciphertext,]: ConnectRequestParams[NostrConnectMethod.Nip44Decrypt]): Promise<ConnectResponseResults[NostrConnectMethod.Nip44Decrypt]>;
|
|
108
|
+
/**
|
|
109
|
+
* Send a response to the client
|
|
110
|
+
* @param clientOrRequest - The client pubkey or request event
|
|
111
|
+
* @param requestId - The id of the request
|
|
112
|
+
* @param result - The result of the request
|
|
113
|
+
*/
|
|
114
|
+
protected sendResponse<T extends NostrConnectMethod>(clientOrRequest: NostrEvent | string, requestId: string, result: ConnectResponseResults[T]): Promise<void>;
|
|
115
|
+
/** Send an error response to the client */
|
|
116
|
+
protected sendErrorResponse(event: NostrEvent, requestId: string, error: string): Promise<void>;
|
|
117
|
+
/** Send an encrypted message to the client */
|
|
118
|
+
protected sendMessage(clientOrRequest: string | NostrEvent, message: NostrConnectResponse<any>): Promise<void>;
|
|
119
|
+
/** Get the connection string that clients can use to connect */
|
|
120
|
+
getBunkerURI(): Promise<string>;
|
|
121
|
+
}
|