applesauce-signers 4.0.0 → 4.2.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/README.md +2 -2
- package/dist/signers/extension-signer.js +5 -1
- package/dist/signers/index.d.ts +1 -0
- package/dist/signers/index.js +1 -0
- package/dist/signers/nostr-connect-provider.js +2 -2
- package/dist/signers/nostr-connect-signer.d.ts +5 -5
- package/dist/signers/nostr-connect-signer.js +7 -4
- package/dist/signers/private-key-signer.d.ts +19 -0
- package/dist/signers/private-key-signer.js +27 -0
- package/dist/signers/simple-signer.d.ts +4 -19
- package/dist/signers/simple-signer.js +4 -27
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -37,12 +37,12 @@ A basic signer that holds the secret key in memory with NIP-04 and NIP-44 encryp
|
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
39
|
// Create new signer with random key
|
|
40
|
-
const signer = new
|
|
40
|
+
const signer = new PrivateKeySigner();
|
|
41
41
|
|
|
42
42
|
// Or import existing key
|
|
43
43
|
const key = new Uint8Array(32);
|
|
44
44
|
window.crypto.getRandomValues(key);
|
|
45
|
-
const signer = new
|
|
45
|
+
const signer = new PrivateKeySigner(key);
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
### Nostr Connect Signer (NIP-46)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { verifyEvent } from "nostr-tools";
|
|
2
|
+
import { isHexKey } from "applesauce-core/helpers";
|
|
2
3
|
/** AN error that is throw when the window.nostr extension is missing */
|
|
3
4
|
export class ExtensionMissingError extends Error {
|
|
4
5
|
}
|
|
@@ -16,7 +17,10 @@ export class ExtensionSigner {
|
|
|
16
17
|
throw new ExtensionMissingError("Signer extension missing");
|
|
17
18
|
if (this.pubkey)
|
|
18
19
|
return this.pubkey;
|
|
19
|
-
|
|
20
|
+
const key = await window.nostr.getPublicKey();
|
|
21
|
+
if (!isHexKey(key))
|
|
22
|
+
throw new Error("Extension returned an invalid public key");
|
|
23
|
+
this.pubkey = key;
|
|
20
24
|
return this.pubkey;
|
|
21
25
|
}
|
|
22
26
|
async signEvent(template) {
|
package/dist/signers/index.d.ts
CHANGED
package/dist/signers/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { filter, from, repeat, retry } from "rxjs";
|
|
|
7
7
|
import { isNIP04 } from "../helpers/encryption.js";
|
|
8
8
|
import { createBunkerURI, NostrConnectMethod, parseNostrConnectURI, } from "../helpers/nostr-connect.js";
|
|
9
9
|
import { getConnectionMethods, } from "../interop.js";
|
|
10
|
-
import {
|
|
10
|
+
import { PrivateKeySigner } from "./private-key-signer.js";
|
|
11
11
|
export class NostrConnectProvider {
|
|
12
12
|
/** A fallback method to use for subscriptionMethod if none is passed in when creating the provider */
|
|
13
13
|
static subscriptionMethod = undefined;
|
|
@@ -55,7 +55,7 @@ export class NostrConnectProvider {
|
|
|
55
55
|
constructor(options) {
|
|
56
56
|
this.relays = options.relays;
|
|
57
57
|
this.upstream = options.upstream;
|
|
58
|
-
this.signer = options.signer ?? new
|
|
58
|
+
this.signer = options.signer ?? new PrivateKeySigner();
|
|
59
59
|
this.secret = options.secret;
|
|
60
60
|
// Get the subscription and publish methods
|
|
61
61
|
const { subscriptionMethod, publishMethod } = getConnectionMethods(options, NostrConnectProvider);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Deferred } from "applesauce-core/promise";
|
|
2
|
-
import { ISigner, NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod,
|
|
2
|
+
import { ISigner, NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod, PrivateKeySigner } from "applesauce-signers";
|
|
3
3
|
import { EventTemplate, NostrEvent, verifyEvent } from "nostr-tools";
|
|
4
4
|
import { Subscription } from "rxjs";
|
|
5
5
|
import { BunkerURI, NostrConnectAppMetadata } from "../helpers/nostr-connect.js";
|
|
6
6
|
export type NostrConnectSignerOptions = NostrConnectionMethodsOptions & {
|
|
7
7
|
/** The relays to communicate over */
|
|
8
8
|
relays: string[];
|
|
9
|
-
/** A {@link
|
|
10
|
-
signer?:
|
|
9
|
+
/** A {@link PrivateKeySigner} for this client */
|
|
10
|
+
signer?: PrivateKeySigner;
|
|
11
11
|
/** pubkey of the remote signer application */
|
|
12
12
|
remote?: string;
|
|
13
13
|
/** Users pubkey */
|
|
@@ -30,7 +30,7 @@ export declare class NostrConnectSigner implements ISigner {
|
|
|
30
30
|
protected subscriptionMethod: NostrSubscriptionMethod;
|
|
31
31
|
protected log: import("debug").Debugger;
|
|
32
32
|
/** The local client signer */
|
|
33
|
-
signer:
|
|
33
|
+
signer: PrivateKeySigner;
|
|
34
34
|
/** Whether the signer is listening for events */
|
|
35
35
|
listening: boolean;
|
|
36
36
|
/** Whether the signer is connected to the remote signer */
|
|
@@ -97,6 +97,6 @@ export declare class NostrConnectSigner implements ISigner {
|
|
|
97
97
|
/** Create a {@link NostrConnectSigner} from a bunker:// URI */
|
|
98
98
|
static fromBunkerURI(uri: string, options?: Omit<NostrConnectSignerOptions, "relays"> & {
|
|
99
99
|
permissions?: string[];
|
|
100
|
-
signer?:
|
|
100
|
+
signer?: PrivateKeySigner;
|
|
101
101
|
}): Promise<NostrConnectSigner>;
|
|
102
102
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { logger } from "applesauce-core";
|
|
2
|
-
import { getHiddenContent, unixNow } from "applesauce-core/helpers";
|
|
2
|
+
import { getHiddenContent, isHexKey, unixNow } from "applesauce-core/helpers";
|
|
3
3
|
import { createDefer } from "applesauce-core/promise";
|
|
4
|
-
import {
|
|
4
|
+
import { PrivateKeySigner, getConnectionMethods, } from "applesauce-signers";
|
|
5
5
|
import { nanoid } from "nanoid";
|
|
6
6
|
import { getPublicKey, kinds, verifyEvent } from "nostr-tools";
|
|
7
7
|
import { filter, from, repeat, retry } from "rxjs";
|
|
@@ -58,7 +58,7 @@ export class NostrConnectSigner {
|
|
|
58
58
|
if (options.onAuth)
|
|
59
59
|
this.onAuth = options.onAuth;
|
|
60
60
|
// Get or create the local signer
|
|
61
|
-
this.signer = options?.signer || new
|
|
61
|
+
this.signer = options?.signer || new PrivateKeySigner();
|
|
62
62
|
this.nip04 = {
|
|
63
63
|
encrypt: this.nip04Encrypt.bind(this),
|
|
64
64
|
decrypt: this.nip04Decrypt.bind(this),
|
|
@@ -265,7 +265,10 @@ export class NostrConnectSigner {
|
|
|
265
265
|
if (this.pubkey)
|
|
266
266
|
return this.pubkey;
|
|
267
267
|
await this.requireConnection();
|
|
268
|
-
|
|
268
|
+
const key = await this.makeRequest(NostrConnectMethod.GetPublicKey, []);
|
|
269
|
+
if (!isHexKey(key))
|
|
270
|
+
throw new Error("Remote signer returned an invalid public key");
|
|
271
|
+
return key;
|
|
269
272
|
}
|
|
270
273
|
/** Request to sign an event */
|
|
271
274
|
async signEvent(template) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EventTemplate } from "nostr-tools";
|
|
2
|
+
import { ISigner } from "../interop.js";
|
|
3
|
+
/** A Simple signer that holds the private key in memory */
|
|
4
|
+
export declare class PrivateKeySigner implements ISigner {
|
|
5
|
+
key: Uint8Array;
|
|
6
|
+
constructor(key?: Uint8Array);
|
|
7
|
+
getPublicKey(): Promise<string>;
|
|
8
|
+
signEvent(event: EventTemplate): Promise<import("nostr-tools").VerifiedEvent>;
|
|
9
|
+
nip04: {
|
|
10
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
11
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
12
|
+
};
|
|
13
|
+
nip44: {
|
|
14
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
15
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
16
|
+
};
|
|
17
|
+
/** Creates a PrivateKeySigner from a hex private key or NIP-19 nsec */
|
|
18
|
+
static fromKey(privateKey: Uint8Array | string): PrivateKeySigner;
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { normalizeToSecretKey } from "applesauce-core/helpers";
|
|
2
|
+
import { finalizeEvent, generateSecretKey, getPublicKey, nip04, nip44 } from "nostr-tools";
|
|
3
|
+
/** A Simple signer that holds the private key in memory */
|
|
4
|
+
export class PrivateKeySigner {
|
|
5
|
+
key;
|
|
6
|
+
constructor(key) {
|
|
7
|
+
this.key = key || generateSecretKey();
|
|
8
|
+
}
|
|
9
|
+
async getPublicKey() {
|
|
10
|
+
return getPublicKey(this.key);
|
|
11
|
+
}
|
|
12
|
+
async signEvent(event) {
|
|
13
|
+
return finalizeEvent(event, this.key);
|
|
14
|
+
}
|
|
15
|
+
nip04 = {
|
|
16
|
+
encrypt: async (pubkey, plaintext) => nip04.encrypt(this.key, pubkey, plaintext),
|
|
17
|
+
decrypt: async (pubkey, ciphertext) => nip04.decrypt(this.key, pubkey, ciphertext),
|
|
18
|
+
};
|
|
19
|
+
nip44 = {
|
|
20
|
+
encrypt: async (pubkey, plaintext) => nip44.v2.encrypt(plaintext, nip44.v2.utils.getConversationKey(this.key, pubkey)),
|
|
21
|
+
decrypt: async (pubkey, ciphertext) => nip44.v2.decrypt(ciphertext, nip44.v2.utils.getConversationKey(this.key, pubkey)),
|
|
22
|
+
};
|
|
23
|
+
/** Creates a PrivateKeySigner from a hex private key or NIP-19 nsec */
|
|
24
|
+
static fromKey(privateKey) {
|
|
25
|
+
return new PrivateKeySigner(normalizeToSecretKey(privateKey));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
key: Uint8Array;
|
|
6
|
-
constructor(key?: Uint8Array);
|
|
7
|
-
getPublicKey(): Promise<string>;
|
|
8
|
-
signEvent(event: EventTemplate): Promise<import("nostr-tools").VerifiedEvent>;
|
|
9
|
-
nip04: {
|
|
10
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
11
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
12
|
-
};
|
|
13
|
-
nip44: {
|
|
14
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string>;
|
|
15
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
|
|
16
|
-
};
|
|
17
|
-
/** Creates a SimpleSigner from a hex private key or NIP-19 nsec */
|
|
18
|
-
static fromKey(privateKey: Uint8Array | string): SimpleSigner;
|
|
19
|
-
}
|
|
1
|
+
import { PrivateKeySigner } from "./private-key-signer.js";
|
|
2
|
+
/** @deprecated Use PrivateKeySigner instead */
|
|
3
|
+
declare const SimpleSigner: typeof PrivateKeySigner;
|
|
4
|
+
export { SimpleSigner };
|
|
@@ -1,27 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
key;
|
|
6
|
-
constructor(key) {
|
|
7
|
-
this.key = key || generateSecretKey();
|
|
8
|
-
}
|
|
9
|
-
async getPublicKey() {
|
|
10
|
-
return getPublicKey(this.key);
|
|
11
|
-
}
|
|
12
|
-
async signEvent(event) {
|
|
13
|
-
return finalizeEvent(event, this.key);
|
|
14
|
-
}
|
|
15
|
-
nip04 = {
|
|
16
|
-
encrypt: async (pubkey, plaintext) => nip04.encrypt(this.key, pubkey, plaintext),
|
|
17
|
-
decrypt: async (pubkey, ciphertext) => nip04.decrypt(this.key, pubkey, ciphertext),
|
|
18
|
-
};
|
|
19
|
-
nip44 = {
|
|
20
|
-
encrypt: async (pubkey, plaintext) => nip44.v2.encrypt(plaintext, nip44.v2.utils.getConversationKey(this.key, pubkey)),
|
|
21
|
-
decrypt: async (pubkey, ciphertext) => nip44.v2.decrypt(ciphertext, nip44.v2.utils.getConversationKey(this.key, pubkey)),
|
|
22
|
-
};
|
|
23
|
-
/** Creates a SimpleSigner from a hex private key or NIP-19 nsec */
|
|
24
|
-
static fromKey(privateKey) {
|
|
25
|
-
return new SimpleSigner(normalizeToSecretKey(privateKey));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
import { PrivateKeySigner } from "./private-key-signer.js";
|
|
2
|
+
/** @deprecated Use PrivateKeySigner instead */
|
|
3
|
+
const SimpleSigner = PrivateKeySigner;
|
|
4
|
+
export { SimpleSigner };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-signers",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Signer classes for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@noble/hashes": "^1.7.1",
|
|
48
48
|
"@noble/secp256k1": "^1.7.1",
|
|
49
49
|
"@scure/base": "^1.2.4",
|
|
50
|
-
"applesauce-core": "^4.
|
|
50
|
+
"applesauce-core": "^4.2.0",
|
|
51
51
|
"debug": "^4.4.0",
|
|
52
52
|
"nanoid": "^5.0.9",
|
|
53
53
|
"nostr-tools": "~2.17",
|