@welshman/signer 0.0.1
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 +3 -0
- package/build/src/index.d.ts +5 -0
- package/build/src/index.mjs +6 -0
- package/build/src/index.mjs.map +1 -0
- package/build/src/signers/connect.d.ts +47 -0
- package/build/src/signers/connect.mjs +152 -0
- package/build/src/signers/connect.mjs.map +1 -0
- package/build/src/signers/extension.d.ts +22 -0
- package/build/src/signers/extension.mjs +48 -0
- package/build/src/signers/extension.mjs.map +1 -0
- package/build/src/signers/secret.d.ts +26 -0
- package/build/src/signers/secret.mjs +19 -0
- package/build/src/signers/secret.mjs.map +1 -0
- package/build/src/util.d.ts +52 -0
- package/build/src/util.mjs +29 -0
- package/build/src/util.mjs.map +1 -0
- package/build/src/wrapper.d.ts +21 -0
- package/build/src/wrapper.mjs +56 -0
- package/build/src/wrapper.mjs.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Emitter } from "@welshman/lib";
|
|
2
|
+
import { EventTemplate } from "@welshman/util";
|
|
3
|
+
import { Subscription } from "@welshman/net";
|
|
4
|
+
import { ISigner } from '../util';
|
|
5
|
+
export type NostrConnectHandler = {
|
|
6
|
+
pubkey: string;
|
|
7
|
+
relays: string[];
|
|
8
|
+
domain?: string;
|
|
9
|
+
};
|
|
10
|
+
export type ConnectResponse = {
|
|
11
|
+
id: string;
|
|
12
|
+
error?: string;
|
|
13
|
+
result?: string;
|
|
14
|
+
};
|
|
15
|
+
export declare class NostrConnectBroker extends Emitter {
|
|
16
|
+
#private;
|
|
17
|
+
readonly pubkey: string;
|
|
18
|
+
readonly secret: string;
|
|
19
|
+
readonly handler: NostrConnectHandler;
|
|
20
|
+
static get(pubkey: string, secret: string, handler: NostrConnectHandler): NostrConnectBroker;
|
|
21
|
+
constructor(pubkey: string, secret: string, handler: NostrConnectHandler);
|
|
22
|
+
subscribe(): Subscription;
|
|
23
|
+
request(method: string, params: string[], admin?: boolean): Promise<string>;
|
|
24
|
+
createAccount(username: string): Promise<string>;
|
|
25
|
+
connect(token?: string): Promise<boolean>;
|
|
26
|
+
signEvent(event: EventTemplate): Promise<any>;
|
|
27
|
+
nip04Encrypt(pk: string, message: string): Promise<string>;
|
|
28
|
+
nip04Decrypt(pk: string, message: string): Promise<string>;
|
|
29
|
+
nip44Encrypt(pk: string, message: string): Promise<string>;
|
|
30
|
+
nip44Decrypt(pk: string, message: string): Promise<string>;
|
|
31
|
+
teardown(): void;
|
|
32
|
+
}
|
|
33
|
+
export declare class ConnectSigner implements ISigner {
|
|
34
|
+
private broker;
|
|
35
|
+
constructor(broker: NostrConnectBroker);
|
|
36
|
+
isEnabled: () => boolean;
|
|
37
|
+
getPubkey: () => Promise<string>;
|
|
38
|
+
sign: (event: EventTemplate) => Promise<any>;
|
|
39
|
+
nip04: {
|
|
40
|
+
encrypt: (pk: string, message: string) => Promise<string>;
|
|
41
|
+
decrypt: (pk: string, message: string) => Promise<string>;
|
|
42
|
+
};
|
|
43
|
+
nip44: {
|
|
44
|
+
encrypt: (pk: string, message: string) => Promise<string>;
|
|
45
|
+
decrypt: (pk: string, message: string) => Promise<string>;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _NostrConnectBroker_sub, _NostrConnectBroker_signer, _NostrConnectBroker_ready, _NostrConnectBroker_closed, _NostrConnectBroker_connectResult;
|
|
13
|
+
import { nip04, finalizeEvent, getPublicKey } from "nostr-tools";
|
|
14
|
+
import { hexToBytes } from '@noble/hashes/utils';
|
|
15
|
+
import { Emitter, tryCatch, randomId, sleep, equals, now } from "@welshman/lib";
|
|
16
|
+
import { createEvent, NOSTR_CONNECT } from "@welshman/util";
|
|
17
|
+
import { subscribe, publish } from "@welshman/net";
|
|
18
|
+
import { decrypt } from "../util.mjs";
|
|
19
|
+
import { SecretSigner } from "./secret.mjs";
|
|
20
|
+
let singleton;
|
|
21
|
+
// FIXME set the full list of requested perms
|
|
22
|
+
const Perms = "nip04_encrypt,nip04_decrypt,sign_event:0,sign_event:1,sign_event:4,sign_event:6,sign_event:7";
|
|
23
|
+
export class NostrConnectBroker extends Emitter {
|
|
24
|
+
static get(pubkey, secret, handler) {
|
|
25
|
+
if ((singleton === null || singleton === void 0 ? void 0 : singleton.pubkey) !== pubkey ||
|
|
26
|
+
(singleton === null || singleton === void 0 ? void 0 : singleton.secret) !== secret ||
|
|
27
|
+
!equals(singleton === null || singleton === void 0 ? void 0 : singleton.handler, handler)) {
|
|
28
|
+
singleton === null || singleton === void 0 ? void 0 : singleton.teardown();
|
|
29
|
+
singleton = new NostrConnectBroker(pubkey, secret, handler);
|
|
30
|
+
}
|
|
31
|
+
return singleton;
|
|
32
|
+
}
|
|
33
|
+
constructor(pubkey, secret, handler) {
|
|
34
|
+
super();
|
|
35
|
+
this.pubkey = pubkey;
|
|
36
|
+
this.secret = secret;
|
|
37
|
+
this.handler = handler;
|
|
38
|
+
_NostrConnectBroker_sub.set(this, void 0);
|
|
39
|
+
_NostrConnectBroker_signer.set(this, void 0);
|
|
40
|
+
_NostrConnectBroker_ready.set(this, sleep(500));
|
|
41
|
+
_NostrConnectBroker_closed.set(this, false);
|
|
42
|
+
_NostrConnectBroker_connectResult.set(this, void 0);
|
|
43
|
+
__classPrivateFieldSet(this, _NostrConnectBroker_signer, new SecretSigner(secret), "f");
|
|
44
|
+
__classPrivateFieldSet(this, _NostrConnectBroker_sub, this.subscribe(), "f");
|
|
45
|
+
}
|
|
46
|
+
subscribe() {
|
|
47
|
+
const sub = subscribe({
|
|
48
|
+
relays: this.handler.relays,
|
|
49
|
+
filters: [
|
|
50
|
+
{
|
|
51
|
+
since: now() - 30,
|
|
52
|
+
kinds: [NOSTR_CONNECT],
|
|
53
|
+
"#p": [getPublicKey(hexToBytes(this.secret))],
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
sub.emitter.on("event", async (url, e) => {
|
|
58
|
+
const json = await decrypt(__classPrivateFieldGet(this, _NostrConnectBroker_signer, "f"), e.pubkey, e.content);
|
|
59
|
+
const res = await tryCatch(() => JSON.parse(json));
|
|
60
|
+
if (!res.id) {
|
|
61
|
+
console.error(`Invalid nostr-connect response: ${json}`);
|
|
62
|
+
}
|
|
63
|
+
if (res.result === "auth_url") {
|
|
64
|
+
this.emit(`auth-${res.id}`, res);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this.emit(`res-${res.id}`, res);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
sub.emitter.on("complete", () => {
|
|
71
|
+
if (!__classPrivateFieldGet(this, _NostrConnectBroker_closed, "f")) {
|
|
72
|
+
__classPrivateFieldSet(this, _NostrConnectBroker_sub, this.subscribe(), "f");
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return sub;
|
|
76
|
+
}
|
|
77
|
+
async request(method, params, admin = false) {
|
|
78
|
+
// nsecbunker has a race condition
|
|
79
|
+
await __classPrivateFieldGet(this, _NostrConnectBroker_ready, "f");
|
|
80
|
+
const id = randomId();
|
|
81
|
+
const pubkey = admin ? this.handler.pubkey : this.pubkey;
|
|
82
|
+
const payload = JSON.stringify({ id, method, params });
|
|
83
|
+
const content = await nip04.encrypt(this.secret, pubkey, payload);
|
|
84
|
+
const template = createEvent(NOSTR_CONNECT, { content, tags: [["p", pubkey]] });
|
|
85
|
+
const event = finalizeEvent(template, this.secret);
|
|
86
|
+
publish({ event, relays: this.handler.relays });
|
|
87
|
+
this.once(`auth-${id}`, res => {
|
|
88
|
+
window.open(res.result, "Coracle", "width=600,height=800,popup=yes");
|
|
89
|
+
});
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
this.once(`res-${id}`, ({ result, error }) => {
|
|
92
|
+
if (error) {
|
|
93
|
+
reject(error);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
resolve(result);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
createAccount(username) {
|
|
102
|
+
if (!this.handler.domain) {
|
|
103
|
+
throw new Error("Unable to create an account without a handler domain");
|
|
104
|
+
}
|
|
105
|
+
return this.request("create_account", [username, this.handler.domain, "", Perms], true);
|
|
106
|
+
}
|
|
107
|
+
async connect(token = "") {
|
|
108
|
+
if (!__classPrivateFieldGet(this, _NostrConnectBroker_connectResult, "f")) {
|
|
109
|
+
const params = [this.pubkey, token, Perms];
|
|
110
|
+
__classPrivateFieldSet(this, _NostrConnectBroker_connectResult, await this.request("connect", params), "f");
|
|
111
|
+
}
|
|
112
|
+
return __classPrivateFieldGet(this, _NostrConnectBroker_connectResult, "f") === "ack";
|
|
113
|
+
}
|
|
114
|
+
async signEvent(event) {
|
|
115
|
+
return JSON.parse(await this.request("sign_event", [JSON.stringify(event)]));
|
|
116
|
+
}
|
|
117
|
+
nip04Encrypt(pk, message) {
|
|
118
|
+
return this.request("nip04_encrypt", [pk, message]);
|
|
119
|
+
}
|
|
120
|
+
nip04Decrypt(pk, message) {
|
|
121
|
+
return this.request("nip04_decrypt", [pk, message]);
|
|
122
|
+
}
|
|
123
|
+
nip44Encrypt(pk, message) {
|
|
124
|
+
return this.request("nip44_encrypt", [pk, message]);
|
|
125
|
+
}
|
|
126
|
+
nip44Decrypt(pk, message) {
|
|
127
|
+
return this.request("nip44_decrypt", [pk, message]);
|
|
128
|
+
}
|
|
129
|
+
teardown() {
|
|
130
|
+
var _a;
|
|
131
|
+
__classPrivateFieldSet(this, _NostrConnectBroker_closed, true, "f");
|
|
132
|
+
(_a = __classPrivateFieldGet(this, _NostrConnectBroker_sub, "f")) === null || _a === void 0 ? void 0 : _a.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
_NostrConnectBroker_sub = new WeakMap(), _NostrConnectBroker_signer = new WeakMap(), _NostrConnectBroker_ready = new WeakMap(), _NostrConnectBroker_closed = new WeakMap(), _NostrConnectBroker_connectResult = new WeakMap();
|
|
136
|
+
export class ConnectSigner {
|
|
137
|
+
constructor(broker) {
|
|
138
|
+
this.broker = broker;
|
|
139
|
+
this.isEnabled = () => true;
|
|
140
|
+
this.getPubkey = async () => this.broker.pubkey;
|
|
141
|
+
this.sign = (event) => this.broker.signEvent(event);
|
|
142
|
+
this.nip04 = {
|
|
143
|
+
encrypt: this.broker.nip04Encrypt,
|
|
144
|
+
decrypt: this.broker.nip04Decrypt,
|
|
145
|
+
};
|
|
146
|
+
this.nip44 = {
|
|
147
|
+
encrypt: this.broker.nip44Encrypt,
|
|
148
|
+
decrypt: this.broker.nip44Decrypt,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=connect.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.mj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1,22 @@
|
|
|
1
|
+
import { EventTemplate } from '@welshman/util';
|
|
2
|
+
import { Sign, ISigner, EncryptionImplementation } from '../util';
|
|
3
|
+
export type Extension = {
|
|
4
|
+
sign: Sign;
|
|
5
|
+
nip04: EncryptionImplementation;
|
|
6
|
+
nip44: EncryptionImplementation;
|
|
7
|
+
getPublicKey: () => string | undefined;
|
|
8
|
+
};
|
|
9
|
+
export declare class ExtensionSigner implements ISigner {
|
|
10
|
+
#private;
|
|
11
|
+
isEnabled: () => boolean;
|
|
12
|
+
getPubkey: () => Promise<string>;
|
|
13
|
+
sign: (event: EventTemplate) => Promise<import("@welshman/util").SignedEvent>;
|
|
14
|
+
nip04: {
|
|
15
|
+
encrypt: (pubkey: string, message: string) => Promise<string>;
|
|
16
|
+
decrypt: (pubkey: string, message: string) => Promise<string>;
|
|
17
|
+
};
|
|
18
|
+
nip44: {
|
|
19
|
+
encrypt: (pubkey: string, message: string) => Promise<string>;
|
|
20
|
+
decrypt: (pubkey: string, message: string) => Promise<string>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _ExtensionSigner_lock, _ExtensionSigner_ext, _ExtensionSigner_then;
|
|
13
|
+
import { hash, own } from "../util.mjs";
|
|
14
|
+
export class ExtensionSigner {
|
|
15
|
+
constructor() {
|
|
16
|
+
_ExtensionSigner_lock.set(this, Promise.resolve());
|
|
17
|
+
_ExtensionSigner_ext.set(this, () => window.nostr);
|
|
18
|
+
_ExtensionSigner_then.set(this, async (f) => {
|
|
19
|
+
const promise = __classPrivateFieldGet(this, _ExtensionSigner_lock, "f").then(() => {
|
|
20
|
+
const ext = __classPrivateFieldGet(this, _ExtensionSigner_ext, "f").call(this);
|
|
21
|
+
if (!ext)
|
|
22
|
+
throw new Error("Extension is not enabled");
|
|
23
|
+
return f(ext);
|
|
24
|
+
});
|
|
25
|
+
// Recover from errors
|
|
26
|
+
__classPrivateFieldSet(this, _ExtensionSigner_lock, promise.then(() => undefined, () => undefined), "f");
|
|
27
|
+
return promise;
|
|
28
|
+
});
|
|
29
|
+
this.isEnabled = () => Boolean(__classPrivateFieldGet(this, _ExtensionSigner_ext, "f").call(this));
|
|
30
|
+
this.getPubkey = () => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => {
|
|
31
|
+
const pubkey = ext.getPublicKey();
|
|
32
|
+
if (!pubkey)
|
|
33
|
+
throw new Error("Failed to retrieve pubkey");
|
|
34
|
+
return pubkey;
|
|
35
|
+
});
|
|
36
|
+
this.sign = (event) => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => ext.sign(hash(own(ext.getPublicKey(), event))));
|
|
37
|
+
this.nip04 = {
|
|
38
|
+
encrypt: (pubkey, message) => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => ext.nip04.encrypt(pubkey, message)),
|
|
39
|
+
decrypt: (pubkey, message) => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => ext.nip04.decrypt(pubkey, message)),
|
|
40
|
+
};
|
|
41
|
+
this.nip44 = {
|
|
42
|
+
encrypt: (pubkey, message) => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => ext.nip44.encrypt(pubkey, message)),
|
|
43
|
+
decrypt: (pubkey, message) => __classPrivateFieldGet(this, _ExtensionSigner_then, "f").call(this, ext => ext.nip44.decrypt(pubkey, message)),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
_ExtensionSigner_lock = new WeakMap(), _ExtensionSigner_ext = new WeakMap(), _ExtensionSigner_then = new WeakMap();
|
|
48
|
+
//# sourceMappingURL=extension.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.mjs","sourceRoot":"","sources":["../../../src/signers/extension.ts"],"names":[],"mappings":";;;;;;;;;;;;OACO,EAAC,IAAI,EAAE,GAAG,EAA0C;AAS3D,MAAM,OAAO,eAAe;IAA5B;QACE,gCAAQ,OAAO,CAAC,OAAO,EAAE,EAAA;QAEzB,+BAAO,GAAG,EAAE,CAAE,MAA8B,CAAC,KAAK,EAAA;QAElD,gCAAQ,KAAK,EAAK,CAAqC,EAAE,EAAE;YACzD,MAAM,OAAO,GAAG,uBAAA,IAAI,6BAAM,CAAC,IAAI,CAAC,GAAG,EAAE;gBACnC,MAAM,GAAG,GAAG,uBAAA,IAAI,4BAAK,MAAT,IAAI,CAAO,CAAA;gBAEvB,IAAI,CAAC,GAAG;oBAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;gBAErD,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;YACf,CAAC,CAAC,CAAA;YAEF,sBAAsB;YACtB,uBAAA,IAAI,yBAAS,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,MAAA,CAAA;YAE3D,OAAO,OAAO,CAAA;QAChB,CAAC,EAAA;QAED,cAAS,GAAG,G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,OAAe,EAAE,EAAE,CAC3C,uBAAA,IAAI,6BAAM,MAAV,IAAI,EAAO,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;SACxD,CAAA;IACH,CAAC;CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EventTemplate } from '@welshman/util';
|
|
2
|
+
import { ISigner } from "../util";
|
|
3
|
+
export declare class SecretSigner implements ISigner {
|
|
4
|
+
private secret;
|
|
5
|
+
private pubkey;
|
|
6
|
+
constructor(secret: string);
|
|
7
|
+
isEnabled: () => boolean;
|
|
8
|
+
getPubkey: () => Promise<string>;
|
|
9
|
+
sign: (event: EventTemplate) => Promise<{
|
|
10
|
+
sig: string;
|
|
11
|
+
kind: number;
|
|
12
|
+
tags: string[][];
|
|
13
|
+
content: string;
|
|
14
|
+
created_at: number;
|
|
15
|
+
pubkey: string;
|
|
16
|
+
id: string;
|
|
17
|
+
}>;
|
|
18
|
+
nip04: {
|
|
19
|
+
encrypt: (pubkey: string, message: string) => Promise<string>;
|
|
20
|
+
decrypt: (pubkey: string, message: string) => Promise<string>;
|
|
21
|
+
};
|
|
22
|
+
nip44: {
|
|
23
|
+
encrypt: (pubkey: string, message: string) => Promise<string>;
|
|
24
|
+
decrypt: (pubkey: string, message: string) => Promise<string>;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { nip04, nip44, own, hash, sign, getPubkey } from "../util.mjs";
|
|
2
|
+
export class SecretSigner {
|
|
3
|
+
constructor(secret) {
|
|
4
|
+
this.secret = secret;
|
|
5
|
+
this.isEnabled = () => true;
|
|
6
|
+
this.getPubkey = async () => this.pubkey;
|
|
7
|
+
this.sign = async (event) => sign(hash(own(this.pubkey, event)), this.secret);
|
|
8
|
+
this.nip04 = {
|
|
9
|
+
encrypt: async (pubkey, message) => nip04.encrypt(pubkey, this.secret, message),
|
|
10
|
+
decrypt: async (pubkey, message) => nip04.decrypt(pubkey, this.secret, message),
|
|
11
|
+
};
|
|
12
|
+
this.nip44 = {
|
|
13
|
+
encrypt: async (pubkey, message) => nip44.encrypt(pubkey, this.secret, message),
|
|
14
|
+
decrypt: async (pubkey, message) => nip44.decrypt(pubkey, this.secret, message),
|
|
15
|
+
};
|
|
16
|
+
this.pubkey = getPubkey(this.secret);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=secret.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secret.mjs","sourceRoot":"","sources":["../../../src/signers/secret.ts"],"names":[],"mappings":"OACO,EAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAU;AAE1D,MAAM,OAAO,YAAY;IAGvB,YAAoB,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;QAIlC,cAAS,GAAG,GAAG,EAAE,CAAC,IAAI,CAAA;QAEtB,cAAS,GAAG,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,MAAM,CAAA;QAEnC,SAAI,GAAG,KAAK,EAAE,KAAoB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QAEvF,UAAK,GAAG;YACN,OAAO,EAAE,KAAK,EAAE,MAAc,EAAE,OAAe,EAAE,EAAE,CACjD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;YAC7C,OAAO,EAAE,KAAK,EAAE,MAAc,EAAE,OAAe,EAAE,EAAE,CACjD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;SAC9C,CAAA;QAED,UAAK,GAAG;YACN,OAAO,EAAE,KAAK,EAAE,MAAc,EAAE,OAAe,EAAE,EAAE,CACjD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;YAC7C,OAAO,EAAE,KAAK,EAAE,MAAc,EAAE,OAAe,EAAE,EAAE,CACjD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;SAC9C,CAAA;QArBC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACtC,CAAC;CAqBF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { SignedEvent, HashedEvent, EventTemplate, OwnedEvent } from '@welshman/util';
|
|
2
|
+
export declare const getSecret: () => string;
|
|
3
|
+
export declare const getPubkey: (secret: string) => string;
|
|
4
|
+
export declare const getHash: (event: OwnedEvent) => string;
|
|
5
|
+
export declare const getSig: (event: HashedEvent, secret: string) => string;
|
|
6
|
+
export declare const own: (pubkey: string, event: EventTemplate) => {
|
|
7
|
+
pubkey: string;
|
|
8
|
+
kind: number;
|
|
9
|
+
tags: string[][];
|
|
10
|
+
content: string;
|
|
11
|
+
created_at: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const hash: (event: OwnedEvent) => HashedEvent;
|
|
14
|
+
export declare const sign: (event: HashedEvent, secret: string) => {
|
|
15
|
+
sig: string;
|
|
16
|
+
kind: number;
|
|
17
|
+
tags: string[][];
|
|
18
|
+
content: string;
|
|
19
|
+
created_at: number;
|
|
20
|
+
pubkey: string;
|
|
21
|
+
id: string;
|
|
22
|
+
};
|
|
23
|
+
export declare const nip04: {
|
|
24
|
+
detect: (m: string) => boolean;
|
|
25
|
+
encrypt: (pubkey: string, secret: string, m: string) => Promise<string>;
|
|
26
|
+
decrypt: (pubkey: string, secret: string, m: string) => Promise<string>;
|
|
27
|
+
};
|
|
28
|
+
export declare const nip44: {
|
|
29
|
+
getSharedSecret: {
|
|
30
|
+
(...args: string[]): Uint8Array;
|
|
31
|
+
cache: import("@welshman/lib").LRUCache<string, Uint8Array>;
|
|
32
|
+
getKey: (args: string[]) => string;
|
|
33
|
+
getValue: (args: string[]) => Uint8Array;
|
|
34
|
+
};
|
|
35
|
+
encrypt: (pubkey: string, secret: string, m: string) => string;
|
|
36
|
+
decrypt: (pubkey: string, secret: string, m: string) => string;
|
|
37
|
+
};
|
|
38
|
+
export type Sign = (event: EventTemplate) => Promise<SignedEvent>;
|
|
39
|
+
export type Encrypt = (pubkey: string, message: string) => Promise<string>;
|
|
40
|
+
export type Decrypt = (pubkey: string, message: string) => Promise<string>;
|
|
41
|
+
export type EncryptionImplementation = {
|
|
42
|
+
encrypt: Encrypt;
|
|
43
|
+
decrypt: Decrypt;
|
|
44
|
+
};
|
|
45
|
+
export interface ISigner {
|
|
46
|
+
isEnabled: () => boolean;
|
|
47
|
+
getPubkey: () => Promise<string>;
|
|
48
|
+
sign: Sign;
|
|
49
|
+
nip04: EncryptionImplementation;
|
|
50
|
+
nip44: EncryptionImplementation;
|
|
51
|
+
}
|
|
52
|
+
export declare const decrypt: (signer: ISigner, pubkey: string, message: string) => Promise<string>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { schnorr } from '@noble/curves/secp256k1';
|
|
2
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
3
|
+
import { nip04 as nt04, nip44 as nt44, generateSecretKey, getPublicKey, getEventHash } from "nostr-tools";
|
|
4
|
+
import { cached } from '@welshman/lib';
|
|
5
|
+
export const getSecret = () => bytesToHex(generateSecretKey());
|
|
6
|
+
export const getPubkey = (secret) => getPublicKey(hexToBytes(secret));
|
|
7
|
+
export const getHash = (event) => getEventHash(event);
|
|
8
|
+
export const getSig = (event, secret) => bytesToHex(schnorr.sign(event.id, secret));
|
|
9
|
+
export const own = (pubkey, event) => ({ ...event, pubkey });
|
|
10
|
+
export const hash = (event) => ({ ...event, id: getHash(event) });
|
|
11
|
+
export const sign = (event, secret) => ({ ...event, sig: getSig(event, secret) });
|
|
12
|
+
export const nip04 = {
|
|
13
|
+
detect: (m) => m.includes("?iv="),
|
|
14
|
+
encrypt: (pubkey, secret, m) => nt04.encrypt(secret, pubkey, m),
|
|
15
|
+
decrypt: (pubkey, secret, m) => nt04.decrypt(secret, pubkey, m),
|
|
16
|
+
};
|
|
17
|
+
export const nip44 = {
|
|
18
|
+
getSharedSecret: cached({
|
|
19
|
+
maxSize: 10000,
|
|
20
|
+
getKey: ([secret, pubkey]) => [secret, pubkey].join(":"),
|
|
21
|
+
getValue: ([secret, pubkey]) => nt44.v2.utils.getConversationKey(hexToBytes(secret), pubkey),
|
|
22
|
+
}),
|
|
23
|
+
encrypt: (pubkey, secret, m) => nt44.v2.encrypt(m, nip44.getSharedSecret(secret, pubkey)),
|
|
24
|
+
decrypt: (pubkey, secret, m) => nt44.v2.decrypt(m, nip44.getSharedSecret(secret, pubkey)),
|
|
25
|
+
};
|
|
26
|
+
export const decrypt = async (signer, pubkey, message) => nip04.detect(message)
|
|
27
|
+
? signer.nip04.decrypt(pubkey, message)
|
|
28
|
+
: signer.nip44.decrypt(pubkey, message);
|
|
29
|
+
//# sourceMappingURL=util.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.mjs","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"OAAO,EAAC,OAAO,EAAC,MAAM,yBAAyB;OACxC,EAAC,UAAU,EAAE,UAAU,EAAC,MAAM,qBAAqB;OACnD,EAAC,KAAK,IAAI,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAC,MAAM,aAAa;OAChG,EAAC,MAAM,EAAC,MAAM,eAAe;AAGpC,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAC,CAAA;AAE9D,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC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AE,KAAK;QACd,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;QACxD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,CAAW,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;KACvG,CAAC;IACF,OAAO,EAAE,CAAC,MAAc,EAAE,MAAc,EAAE,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjH,OAAO,EAAE,CAAC,MAAc,EAAE,MAAc,EAAE,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClH,CAAA;AAqBD,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAAE,MAAe,EAAE,MAAc,EAAE,OAAe,EAAE,EAAE,CAChF,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC;IACnB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC;IACvC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { UnwrappedEvent, SignedEvent, HashedEvent, EventTemplate } from '@welshman/util';
|
|
2
|
+
import { ISigner } from './util';
|
|
3
|
+
export type WrapperParams = {
|
|
4
|
+
author?: string;
|
|
5
|
+
wrap?: {
|
|
6
|
+
author: string;
|
|
7
|
+
recipient: string;
|
|
8
|
+
tags?: string[][];
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare class Wrapper {
|
|
12
|
+
readonly userSigner: ISigner;
|
|
13
|
+
readonly wrapSigner: ISigner;
|
|
14
|
+
seen: Map<string, UnwrappedEvent | Error>;
|
|
15
|
+
constructor(userSigner: ISigner, wrapSigner: ISigner);
|
|
16
|
+
now: (drift?: number) => number;
|
|
17
|
+
getSeal: (pk: string, rumor: HashedEvent) => Promise<SignedEvent>;
|
|
18
|
+
getWrap: (pk: string, seal: SignedEvent) => Promise<SignedEvent>;
|
|
19
|
+
wrap: (pk: string, template: EventTemplate) => Promise<SignedEvent>;
|
|
20
|
+
unwrap: (wrap: SignedEvent) => Promise<any>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { WRAP, SEAL } from '@welshman/util';
|
|
2
|
+
import { own, hash, decrypt } from "./util.mjs";
|
|
3
|
+
export class Wrapper {
|
|
4
|
+
constructor(userSigner, wrapSigner) {
|
|
5
|
+
this.userSigner = userSigner;
|
|
6
|
+
this.wrapSigner = wrapSigner;
|
|
7
|
+
this.seen = new Map();
|
|
8
|
+
this.now = (drift = 0) => Math.round(Date.now() / 1000 - Math.random() * Math.pow(10, drift));
|
|
9
|
+
this.getSeal = async (pk, rumor) => this.userSigner.sign(hash({
|
|
10
|
+
kind: SEAL,
|
|
11
|
+
pubkey: await this.userSigner.getPubkey(),
|
|
12
|
+
content: await this.userSigner.nip44.encrypt(pk, JSON.stringify(rumor)),
|
|
13
|
+
created_at: this.now(5),
|
|
14
|
+
tags: [],
|
|
15
|
+
}));
|
|
16
|
+
this.getWrap = async (pk, seal) => this.wrapSigner.sign(hash({
|
|
17
|
+
kind: WRAP,
|
|
18
|
+
pubkey: await this.wrapSigner.getPubkey(),
|
|
19
|
+
content: await this.wrapSigner.nip44.encrypt(pk, JSON.stringify(seal)),
|
|
20
|
+
created_at: this.now(5),
|
|
21
|
+
tags: [["p", pk]],
|
|
22
|
+
}));
|
|
23
|
+
this.wrap = async (pk, template) => {
|
|
24
|
+
const pubkey = await this.userSigner.getPubkey();
|
|
25
|
+
const rumor = hash(own(pubkey, template));
|
|
26
|
+
const seal = await this.getSeal(pk, rumor);
|
|
27
|
+
const wrap = await this.getWrap(pk, seal);
|
|
28
|
+
return wrap;
|
|
29
|
+
};
|
|
30
|
+
this.unwrap = async (wrap) => {
|
|
31
|
+
// Avoid decrypting the same event multiple times
|
|
32
|
+
if (this.seen.has(wrap.id)) {
|
|
33
|
+
const rumorOrError = this.seen.get(wrap.id);
|
|
34
|
+
if (rumorOrError instanceof Error) {
|
|
35
|
+
throw rumorOrError;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return rumorOrError;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const seal = JSON.parse(await decrypt(this.wrapSigner, wrap.pubkey, wrap.content));
|
|
43
|
+
const rumor = JSON.parse(await decrypt(this.wrapSigner, seal.pubkey, seal.content));
|
|
44
|
+
if (seal.pubkey !== rumor.pubkey)
|
|
45
|
+
throw new Error("Seal pubkey does not match rumor pubkey");
|
|
46
|
+
this.seen.set(wrap.id, rumor);
|
|
47
|
+
return rumor;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
this.seen.set(wrap.id, error);
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=wrapper.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapper.mjs","sourceRoot":"","sources":["../../src/wrapper.ts"],"names":[],"mappings":"OAAO,EAA0D,IAAI,EAAE,IAAI,EAAC,MAAM,gBAAgB;OAC3F,EAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAU;AAapC,MAAM,OAAO,OAAO;IAGlB,YAAqB,UAAmB,EAAW,UAAmB;QAAjD,eAAU,GAAV,UAAU,CAAS;QAAW,eAAU,GAAV,UAAU,CAA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A;aACb;YAAC,OAAO,KAAK,EAAE;gBACd,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,KAAc,CAAC,CAAA;gBAEtC,MAAM,KAAK,CAAA;aACZ;QACH,CAAC,CAAA;IA1DwE,CAAC;CA2D3E"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@welshman/signer",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "hodlbod",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "A nostr signer implemenation supporting several login methods.",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"build"
|
|
13
|
+
],
|
|
14
|
+
"types": "./build/src/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./build/src/index.d.ts",
|
|
18
|
+
"import": "./build/src/index.mjs",
|
|
19
|
+
"require": "./build/src/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"pub": "npm run lint && npm run build && npm publish",
|
|
24
|
+
"build": "gts clean && tsc-multi",
|
|
25
|
+
"lint": "gts lint",
|
|
26
|
+
"fix": "gts fix"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"gts": "^5.0.1",
|
|
30
|
+
"tsc-multi": "^1.1.0",
|
|
31
|
+
"typescript": "~5.1.6"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@welshman/lib": "^0.0.13",
|
|
35
|
+
"@welshman/net": "^0.0.17",
|
|
36
|
+
"@welshman/util": "^0.0.24"
|
|
37
|
+
}
|
|
38
|
+
}
|