applesauce-signers 0.0.0-next-20250128152442
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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/helpers/encryption.d.ts +5 -0
- package/dist/helpers/encryption.js +10 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +2 -0
- package/dist/nip-07.d.ts +20 -0
- package/dist/nip-07.js +1 -0
- package/dist/signers/amber-clipboard-signer.d.ts +43 -0
- package/dist/signers/amber-clipboard-signer.js +145 -0
- package/dist/signers/extension-signer.d.ts +25 -0
- package/dist/signers/extension-signer.js +29 -0
- package/dist/signers/index.d.ts +7 -0
- package/dist/signers/index.js +7 -0
- package/dist/signers/nostr-connect-signer.d.ts +163 -0
- package/dist/signers/nostr-connect-signer.js +327 -0
- package/dist/signers/nostr-connect-signer.test.d.ts +1 -0
- package/dist/signers/nostr-connect-signer.test.js +25 -0
- package/dist/signers/password-signer.d.ts +29 -0
- package/dist/signers/password-signer.js +85 -0
- package/dist/signers/readonly-signer.d.ts +22 -0
- package/dist/signers/readonly-signer.js +38 -0
- package/dist/signers/serial-port-signer.d.ts +56 -0
- package/dist/signers/serial-port-signer.js +229 -0
- package/dist/signers/simple-signer.d.ts +16 -0
- package/dist/signers/simple-signer.js +22 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 hzrd149
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is encrypted with NIP-04 or NIP-44
|
|
3
|
+
* @see https://github.com/nostr-protocol/nips/pull/1248#issuecomment-2437731316
|
|
4
|
+
*/
|
|
5
|
+
export function isNIP04(ciphertext) {
|
|
6
|
+
const l = ciphertext.length;
|
|
7
|
+
if (l < 28)
|
|
8
|
+
return false;
|
|
9
|
+
return (ciphertext[l - 28] == "?" && ciphertext[l - 27] == "i" && ciphertext[l - 26] == "v" && ciphertext[l - 25] == "=");
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./encryption.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./encryption.js";
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/logger.d.ts
ADDED
package/dist/logger.js
ADDED
package/dist/nip-07.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
2
|
+
export type Nip07Interface = {
|
|
3
|
+
getPublicKey: () => Promise<string> | string;
|
|
4
|
+
signEvent: (template: EventTemplate) => Promise<NostrEvent> | NostrEvent;
|
|
5
|
+
getRelays?: () => Record<string, {
|
|
6
|
+
read: boolean;
|
|
7
|
+
write: boolean;
|
|
8
|
+
}> | Promise<Record<string, {
|
|
9
|
+
read: boolean;
|
|
10
|
+
write: boolean;
|
|
11
|
+
}>>;
|
|
12
|
+
nip04?: {
|
|
13
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
14
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
15
|
+
};
|
|
16
|
+
nip44?: {
|
|
17
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
18
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
19
|
+
};
|
|
20
|
+
};
|
package/dist/nip-07.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { EventTemplate, VerifiedEvent, verifyEvent } from "nostr-tools";
|
|
2
|
+
import { Nip07Interface } from "../nip-07.js";
|
|
3
|
+
/**
|
|
4
|
+
* A Signer for [amber](https://github.com/greenart7c3/Amber) clipboard API
|
|
5
|
+
* @see https://github.com/greenart7c3/Amber/blob/master/docs/web-apps.md
|
|
6
|
+
*/
|
|
7
|
+
export declare class AmberClipboardSigner implements Nip07Interface {
|
|
8
|
+
/** If the signer is supported on this platform */
|
|
9
|
+
static SUPPORTED: false | (() => Promise<string>);
|
|
10
|
+
private pendingRequest;
|
|
11
|
+
pubkey?: string;
|
|
12
|
+
verifyEvent: typeof verifyEvent;
|
|
13
|
+
nip04: {
|
|
14
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
15
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
16
|
+
};
|
|
17
|
+
nip44: {
|
|
18
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
19
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
20
|
+
};
|
|
21
|
+
constructor();
|
|
22
|
+
private onVisibilityChange;
|
|
23
|
+
private intentRequest;
|
|
24
|
+
/** Reject the currently pending request */
|
|
25
|
+
rejectPending(): void;
|
|
26
|
+
/** Removes any event listeners created */
|
|
27
|
+
destroy(): void;
|
|
28
|
+
private checkSupport;
|
|
29
|
+
getPublicKey(): Promise<string>;
|
|
30
|
+
signEvent(draft: EventTemplate & {
|
|
31
|
+
pubkey?: string;
|
|
32
|
+
}): Promise<VerifiedEvent>;
|
|
33
|
+
nip04Encrypt(pubkey: string, plaintext: string): Promise<string>;
|
|
34
|
+
nip04Decrypt(pubkey: string, data: string): Promise<string>;
|
|
35
|
+
nip44Encrypt(pubkey: string, plaintext: string): Promise<string>;
|
|
36
|
+
nip44Decrypt(pubkey: string, data: string): Promise<string>;
|
|
37
|
+
static createGetPublicKeyIntent(): string;
|
|
38
|
+
static createSignEventIntent(draft: EventTemplate): string;
|
|
39
|
+
static createNip04EncryptIntent(pubkey: string, plainText: string): string;
|
|
40
|
+
static createNip04DecryptIntent(pubkey: string, ciphertext: string): string;
|
|
41
|
+
static createNip44EncryptIntent(pubkey: string, plainText: string): string;
|
|
42
|
+
static createNip44DecryptIntent(pubkey: string, ciphertext: string): string;
|
|
43
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { getEventHash, nip19, verifyEvent } from "nostr-tools";
|
|
2
|
+
import { getPubkeyFromDecodeResult, isHexKey, isHex } from "applesauce-core/helpers";
|
|
3
|
+
import { createDefer } from "applesauce-core/promise";
|
|
4
|
+
/**
|
|
5
|
+
* A Signer for [amber](https://github.com/greenart7c3/Amber) clipboard API
|
|
6
|
+
* @see https://github.com/greenart7c3/Amber/blob/master/docs/web-apps.md
|
|
7
|
+
*/
|
|
8
|
+
export class AmberClipboardSigner {
|
|
9
|
+
/** If the signer is supported on this platform */
|
|
10
|
+
static SUPPORTED = `navigator` in globalThis &&
|
|
11
|
+
navigator.userAgent.includes("Android") &&
|
|
12
|
+
navigator.clipboard &&
|
|
13
|
+
navigator.clipboard.readText;
|
|
14
|
+
pendingRequest = null;
|
|
15
|
+
pubkey;
|
|
16
|
+
verifyEvent = verifyEvent;
|
|
17
|
+
nip04;
|
|
18
|
+
nip44;
|
|
19
|
+
constructor() {
|
|
20
|
+
document.addEventListener("visibilitychange", this.onVisibilityChange);
|
|
21
|
+
this.nip04 = {
|
|
22
|
+
encrypt: this.nip04Encrypt.bind(this),
|
|
23
|
+
decrypt: this.nip04Decrypt.bind(this),
|
|
24
|
+
};
|
|
25
|
+
this.nip44 = {
|
|
26
|
+
encrypt: this.nip44Encrypt.bind(this),
|
|
27
|
+
decrypt: this.nip44Decrypt.bind(this),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
onVisibilityChange = () => {
|
|
31
|
+
if (document.visibilityState === "visible") {
|
|
32
|
+
if (!this.pendingRequest || !navigator.clipboard)
|
|
33
|
+
return;
|
|
34
|
+
// read the result from the clipboard
|
|
35
|
+
setTimeout(() => {
|
|
36
|
+
navigator.clipboard
|
|
37
|
+
.readText()
|
|
38
|
+
.then((result) => this.pendingRequest?.resolve(result))
|
|
39
|
+
.catch((e) => this.pendingRequest?.reject(e));
|
|
40
|
+
}, 200);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
async intentRequest(intent) {
|
|
44
|
+
this.rejectPending();
|
|
45
|
+
const request = createDefer();
|
|
46
|
+
window.open(intent, "_blank");
|
|
47
|
+
// NOTE: wait 500ms before setting the pending request since the visibilitychange event fires as soon as window.open is called
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
this.pendingRequest = request;
|
|
50
|
+
}, 500);
|
|
51
|
+
const result = await request;
|
|
52
|
+
if (result.length === 0)
|
|
53
|
+
throw new Error("Empty clipboard");
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
/** Reject the currently pending request */
|
|
57
|
+
rejectPending() {
|
|
58
|
+
if (this.pendingRequest) {
|
|
59
|
+
this.pendingRequest.reject("Canceled");
|
|
60
|
+
this.pendingRequest = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Removes any event listeners created */
|
|
64
|
+
destroy() {
|
|
65
|
+
document.removeEventListener("visibilitychange", this.onVisibilityChange);
|
|
66
|
+
}
|
|
67
|
+
checkSupport() {
|
|
68
|
+
if (!AmberClipboardSigner.SUPPORTED)
|
|
69
|
+
throw new Error("Cant use Amber on non-Android device");
|
|
70
|
+
}
|
|
71
|
+
async getPublicKey() {
|
|
72
|
+
this.checkSupport();
|
|
73
|
+
if (this.pubkey)
|
|
74
|
+
return this.pubkey;
|
|
75
|
+
const result = await this.intentRequest(AmberClipboardSigner.createGetPublicKeyIntent());
|
|
76
|
+
if (isHexKey(result)) {
|
|
77
|
+
this.pubkey = result;
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
else if (result.startsWith("npub") || result.startsWith("nprofile")) {
|
|
81
|
+
const decode = nip19.decode(result);
|
|
82
|
+
const pubkey = getPubkeyFromDecodeResult(decode);
|
|
83
|
+
if (!pubkey)
|
|
84
|
+
throw new Error("Expected npub from clipboard");
|
|
85
|
+
this.pubkey = pubkey;
|
|
86
|
+
return pubkey;
|
|
87
|
+
}
|
|
88
|
+
throw new Error("Expected clipboard to have pubkey");
|
|
89
|
+
}
|
|
90
|
+
async signEvent(draft) {
|
|
91
|
+
this.checkSupport();
|
|
92
|
+
const pubkey = draft.pubkey || this.pubkey;
|
|
93
|
+
if (!pubkey)
|
|
94
|
+
throw new Error("Unknown signer pubkey");
|
|
95
|
+
const draftWithId = { ...draft, id: getEventHash({ ...draft, pubkey }) };
|
|
96
|
+
const sig = await this.intentRequest(AmberClipboardSigner.createSignEventIntent(draftWithId));
|
|
97
|
+
if (!isHex(sig))
|
|
98
|
+
throw new Error("Expected hex signature");
|
|
99
|
+
const event = { ...draftWithId, sig, pubkey };
|
|
100
|
+
if (!this.verifyEvent(event))
|
|
101
|
+
throw new Error("Invalid signature");
|
|
102
|
+
return event;
|
|
103
|
+
}
|
|
104
|
+
// NIP-04
|
|
105
|
+
async nip04Encrypt(pubkey, plaintext) {
|
|
106
|
+
this.checkSupport();
|
|
107
|
+
const data = await this.intentRequest(AmberClipboardSigner.createNip04EncryptIntent(pubkey, plaintext));
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
async nip04Decrypt(pubkey, data) {
|
|
111
|
+
this.checkSupport();
|
|
112
|
+
const plaintext = await this.intentRequest(AmberClipboardSigner.createNip04DecryptIntent(pubkey, data));
|
|
113
|
+
return plaintext;
|
|
114
|
+
}
|
|
115
|
+
// NIP-44
|
|
116
|
+
async nip44Encrypt(pubkey, plaintext) {
|
|
117
|
+
this.checkSupport();
|
|
118
|
+
const data = await this.intentRequest(AmberClipboardSigner.createNip44EncryptIntent(pubkey, plaintext));
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
async nip44Decrypt(pubkey, data) {
|
|
122
|
+
this.checkSupport();
|
|
123
|
+
const plaintext = await this.intentRequest(AmberClipboardSigner.createNip44DecryptIntent(pubkey, data));
|
|
124
|
+
return plaintext;
|
|
125
|
+
}
|
|
126
|
+
// static methods
|
|
127
|
+
static createGetPublicKeyIntent() {
|
|
128
|
+
return `intent:#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=get_public_key;end`;
|
|
129
|
+
}
|
|
130
|
+
static createSignEventIntent(draft) {
|
|
131
|
+
return `intent:${encodeURIComponent(JSON.stringify(draft))}#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=sign_event;end`;
|
|
132
|
+
}
|
|
133
|
+
static createNip04EncryptIntent(pubkey, plainText) {
|
|
134
|
+
return `intent:${encodeURIComponent(plainText)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_encrypt;end`;
|
|
135
|
+
}
|
|
136
|
+
static createNip04DecryptIntent(pubkey, ciphertext) {
|
|
137
|
+
return `intent:${encodeURIComponent(ciphertext)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_decrypt;end`;
|
|
138
|
+
}
|
|
139
|
+
static createNip44EncryptIntent(pubkey, plainText) {
|
|
140
|
+
return `intent:${encodeURIComponent(plainText)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip44_encrypt;end`;
|
|
141
|
+
}
|
|
142
|
+
static createNip44DecryptIntent(pubkey, ciphertext) {
|
|
143
|
+
return `intent:${encodeURIComponent(ciphertext)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip44_decrypt;end`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { EventTemplate } from "nostr-tools";
|
|
2
|
+
import { Nip07Interface } from "../nip-07.js";
|
|
3
|
+
/** AN error that is throw when the window.nostr extension is missing */
|
|
4
|
+
export declare class ExtensionMissingError extends Error {
|
|
5
|
+
}
|
|
6
|
+
/** A signer that is a proxy for window.nostr */
|
|
7
|
+
export declare class ExtensionSigner implements Nip07Interface {
|
|
8
|
+
get nip04(): {
|
|
9
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
10
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
11
|
+
} | undefined;
|
|
12
|
+
get nip44(): {
|
|
13
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
14
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
15
|
+
} | undefined;
|
|
16
|
+
getPublicKey(): string | Promise<string>;
|
|
17
|
+
getRelays(): Record<string, {
|
|
18
|
+
read: boolean;
|
|
19
|
+
write: boolean;
|
|
20
|
+
}> | Promise<Record<string, {
|
|
21
|
+
read: boolean;
|
|
22
|
+
write: boolean;
|
|
23
|
+
}>>;
|
|
24
|
+
signEvent(template: EventTemplate): import("nostr-tools").Event | Promise<import("nostr-tools").Event>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** AN error that is throw when the window.nostr extension is missing */
|
|
2
|
+
export class ExtensionMissingError extends Error {
|
|
3
|
+
}
|
|
4
|
+
/** A signer that is a proxy for window.nostr */
|
|
5
|
+
export class ExtensionSigner {
|
|
6
|
+
get nip04() {
|
|
7
|
+
return window.nostr?.nip04;
|
|
8
|
+
}
|
|
9
|
+
get nip44() {
|
|
10
|
+
return window.nostr?.nip44;
|
|
11
|
+
}
|
|
12
|
+
getPublicKey() {
|
|
13
|
+
if (!window.nostr)
|
|
14
|
+
throw new ExtensionMissingError("Signer extension missing");
|
|
15
|
+
return window.nostr.getPublicKey();
|
|
16
|
+
}
|
|
17
|
+
getRelays() {
|
|
18
|
+
if (!window.nostr)
|
|
19
|
+
throw new ExtensionMissingError("Signer extension missing");
|
|
20
|
+
if (!window.nostr.getRelays)
|
|
21
|
+
return {};
|
|
22
|
+
return window.nostr.getRelays();
|
|
23
|
+
}
|
|
24
|
+
signEvent(template) {
|
|
25
|
+
if (!window.nostr)
|
|
26
|
+
throw new ExtensionMissingError("Signer extension missing");
|
|
27
|
+
return window.nostr.signEvent(template);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./amber-clipboard-signer.js";
|
|
2
|
+
export * from "./extension-signer.js";
|
|
3
|
+
export * from "./nostr-connect-signer.js";
|
|
4
|
+
export * from "./password-signer.js";
|
|
5
|
+
export * from "./readonly-signer.js";
|
|
6
|
+
export * from "./serial-port-signer.js";
|
|
7
|
+
export * from "./simple-signer.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./amber-clipboard-signer.js";
|
|
2
|
+
export * from "./extension-signer.js";
|
|
3
|
+
export * from "./nostr-connect-signer.js";
|
|
4
|
+
export * from "./password-signer.js";
|
|
5
|
+
export * from "./readonly-signer.js";
|
|
6
|
+
export * from "./serial-port-signer.js";
|
|
7
|
+
export * from "./simple-signer.js";
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { EventTemplate, Filter, NostrEvent, verifyEvent } from "nostr-tools";
|
|
2
|
+
import { Nip07Interface, SimpleSigner } from "applesauce-signers";
|
|
3
|
+
import { Deferred } from "applesauce-core/promise";
|
|
4
|
+
export declare function isErrorResponse(response: any): response is NostrConnectErrorResponse;
|
|
5
|
+
export declare enum Permission {
|
|
6
|
+
GetPublicKey = "get_pubic_key",
|
|
7
|
+
SignEvent = "sign_event",
|
|
8
|
+
Nip04Encrypt = "nip04_encrypt",
|
|
9
|
+
Nip04Decrypt = "nip04_decrypt",
|
|
10
|
+
Nip44Encrypt = "nip44_encrypt",
|
|
11
|
+
Nip44Decrypt = "nip44_decrypt"
|
|
12
|
+
}
|
|
13
|
+
export declare enum NostrConnectMethod {
|
|
14
|
+
Connect = "connect",
|
|
15
|
+
CreateAccount = "create_account",
|
|
16
|
+
GetPublicKey = "get_public_key",
|
|
17
|
+
SignEvent = "sign_event",
|
|
18
|
+
Nip04Encrypt = "nip04_encrypt",
|
|
19
|
+
Nip04Decrypt = "nip04_decrypt",
|
|
20
|
+
Nip44Encrypt = "nip44_encrypt",
|
|
21
|
+
Nip44Decrypt = "nip44_decrypt"
|
|
22
|
+
}
|
|
23
|
+
type RequestParams = {
|
|
24
|
+
[NostrConnectMethod.Connect]: [string] | [string, string] | [string, string, string];
|
|
25
|
+
[NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string] | [string, string, string, string];
|
|
26
|
+
[NostrConnectMethod.GetPublicKey]: [];
|
|
27
|
+
[NostrConnectMethod.SignEvent]: [string];
|
|
28
|
+
[NostrConnectMethod.Nip04Encrypt]: [string, string];
|
|
29
|
+
[NostrConnectMethod.Nip04Decrypt]: [string, string];
|
|
30
|
+
[NostrConnectMethod.Nip44Encrypt]: [string, string];
|
|
31
|
+
[NostrConnectMethod.Nip44Decrypt]: [string, string];
|
|
32
|
+
};
|
|
33
|
+
type ResponseResults = {
|
|
34
|
+
[NostrConnectMethod.Connect]: "ack";
|
|
35
|
+
[NostrConnectMethod.CreateAccount]: string;
|
|
36
|
+
[NostrConnectMethod.GetPublicKey]: string;
|
|
37
|
+
[NostrConnectMethod.SignEvent]: string;
|
|
38
|
+
[NostrConnectMethod.Nip04Encrypt]: string;
|
|
39
|
+
[NostrConnectMethod.Nip04Decrypt]: string;
|
|
40
|
+
[NostrConnectMethod.Nip44Encrypt]: string;
|
|
41
|
+
[NostrConnectMethod.Nip44Decrypt]: string;
|
|
42
|
+
};
|
|
43
|
+
export type NostrConnectRequest<N extends NostrConnectMethod> = {
|
|
44
|
+
id: string;
|
|
45
|
+
method: N;
|
|
46
|
+
params: RequestParams[N];
|
|
47
|
+
};
|
|
48
|
+
export type NostrConnectResponse<N extends NostrConnectMethod> = {
|
|
49
|
+
id: string;
|
|
50
|
+
result: ResponseResults[N];
|
|
51
|
+
error?: string;
|
|
52
|
+
};
|
|
53
|
+
export type NostrConnectErrorResponse = {
|
|
54
|
+
id: string;
|
|
55
|
+
result: string;
|
|
56
|
+
error: string;
|
|
57
|
+
};
|
|
58
|
+
export type NostrConnectSignerOptions = {
|
|
59
|
+
/** The relays to communicate over */
|
|
60
|
+
relays: string[];
|
|
61
|
+
/** A {@link SimpleSigner} for this client */
|
|
62
|
+
signer?: SimpleSigner;
|
|
63
|
+
/** pubkey of the remote signer application */
|
|
64
|
+
remote?: string;
|
|
65
|
+
/** Users pubkey */
|
|
66
|
+
pubkey?: string;
|
|
67
|
+
};
|
|
68
|
+
export type NostrConnectConnectionMethods = {
|
|
69
|
+
/** A method that is called when the subscription needs to be updated */
|
|
70
|
+
onSubOpen: (filters: Filter[], relays: string[], onEvent: (event: NostrEvent) => void) => Promise<void>;
|
|
71
|
+
/** A method called when the subscription should be closed */
|
|
72
|
+
onSubClose: () => Promise<void>;
|
|
73
|
+
/** A method that is called when an event needs to be published */
|
|
74
|
+
onPublishEvent: (event: NostrEvent, relays: string[]) => Promise<void>;
|
|
75
|
+
/** A method for handling "auth" requests */
|
|
76
|
+
onAuth?: (url: string) => Promise<void>;
|
|
77
|
+
};
|
|
78
|
+
export type NostrConnectAppMetadata = {
|
|
79
|
+
name?: string;
|
|
80
|
+
image?: string;
|
|
81
|
+
url?: string | URL;
|
|
82
|
+
permissions?: string[];
|
|
83
|
+
};
|
|
84
|
+
export declare class NostrConnectSigner implements Nip07Interface {
|
|
85
|
+
/** A method that is called when the subscription needs to be updated */
|
|
86
|
+
onSubOpen?: (filters: Filter[], relays: string[], onEvent: (event: NostrEvent) => void) => Promise<void>;
|
|
87
|
+
/** A method called when the subscription should be closed */
|
|
88
|
+
onSubClose?: () => Promise<void>;
|
|
89
|
+
/** A method that is called when an event needs to be published */
|
|
90
|
+
onPublishEvent?: (event: NostrEvent, relays: string[]) => Promise<void>;
|
|
91
|
+
protected log: import("debug").Debugger;
|
|
92
|
+
/** The local client signer */
|
|
93
|
+
signer: SimpleSigner;
|
|
94
|
+
protected subscriptionOpen: boolean;
|
|
95
|
+
/** Whether the signer is connected to the remote signer */
|
|
96
|
+
isConnected: boolean;
|
|
97
|
+
/** The users pubkey */
|
|
98
|
+
protected pubkey?: string;
|
|
99
|
+
/** Relays to communicate over */
|
|
100
|
+
relays: string[];
|
|
101
|
+
/** The remote signer pubkey */
|
|
102
|
+
remote?: string;
|
|
103
|
+
/** Client pubkey */
|
|
104
|
+
get clientPubkey(): string;
|
|
105
|
+
onAuth: (url: string) => Promise<void>;
|
|
106
|
+
verifyEvent: typeof verifyEvent;
|
|
107
|
+
/** A secret used when initiating a connection from the client side */
|
|
108
|
+
protected clientSecret: string;
|
|
109
|
+
nip04?: {
|
|
110
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
111
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
112
|
+
} | undefined;
|
|
113
|
+
nip44?: {
|
|
114
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
115
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
116
|
+
} | undefined;
|
|
117
|
+
constructor(opts: NostrConnectConnectionMethods & NostrConnectSignerOptions);
|
|
118
|
+
/** Open the connection */
|
|
119
|
+
open(): Promise<void>;
|
|
120
|
+
/** Close the connection */
|
|
121
|
+
close(): Promise<void>;
|
|
122
|
+
protected requests: Map<string, Deferred<any>>;
|
|
123
|
+
protected auths: Set<string>;
|
|
124
|
+
/** Call this method with incoming events */
|
|
125
|
+
handleEvent(event: NostrEvent): Promise<void>;
|
|
126
|
+
protected createRequestEvent(content: string, target?: string | undefined, kind?: number): Promise<import("nostr-tools").VerifiedEvent>;
|
|
127
|
+
private makeRequest;
|
|
128
|
+
/** Connect to remote signer */
|
|
129
|
+
connect(secret?: string | undefined, permissions?: string[]): Promise<"ack">;
|
|
130
|
+
private waitingPromise;
|
|
131
|
+
/** Wait for a remote signer to connect */
|
|
132
|
+
waitForSigner(): Promise<void>;
|
|
133
|
+
/** Request to create an account on the remote signer */
|
|
134
|
+
createAccount(username: string, domain: string, email?: string, permissions?: string[]): Promise<string>;
|
|
135
|
+
/** Ensure the signer is connected to the remote signer */
|
|
136
|
+
requireConnection(): Promise<void>;
|
|
137
|
+
/** Get the users pubkey */
|
|
138
|
+
getPublicKey(): Promise<string>;
|
|
139
|
+
/** Request to sign an event */
|
|
140
|
+
signEvent(template: EventTemplate & {
|
|
141
|
+
pubkey?: string;
|
|
142
|
+
}): Promise<import("nostr-tools").VerifiedEvent>;
|
|
143
|
+
nip04Encrypt(pubkey: string, plaintext: string): Promise<string>;
|
|
144
|
+
nip04Decrypt(pubkey: string, ciphertext: string): Promise<string>;
|
|
145
|
+
nip44Encrypt(pubkey: string, plaintext: string): Promise<string>;
|
|
146
|
+
nip44Decrypt(pubkey: string, ciphertext: string): Promise<string>;
|
|
147
|
+
/** Returns the nostrconnect:// URI for this signer */
|
|
148
|
+
getNostrConnectURI(metadata?: NostrConnectAppMetadata): string;
|
|
149
|
+
/** Parses a bunker:// URI */
|
|
150
|
+
static parseBunkerURI(uri: string): {
|
|
151
|
+
remote: string;
|
|
152
|
+
relays: string[];
|
|
153
|
+
secret?: string;
|
|
154
|
+
};
|
|
155
|
+
/** Builds an array of signing permissions for event kinds */
|
|
156
|
+
static buildSigningPermissions(kinds: number[]): string[];
|
|
157
|
+
/** Create a {@link NostrConnectSigner} from a bunker:// URI */
|
|
158
|
+
static fromBunkerURI(uri: string, options: NostrConnectConnectionMethods & {
|
|
159
|
+
permissions?: string[];
|
|
160
|
+
signer?: SimpleSigner;
|
|
161
|
+
}): Promise<NostrConnectSigner>;
|
|
162
|
+
}
|
|
163
|
+
export {};
|