applesauce-accounts 0.0.0-next-20241218172722
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 +3 -0
- package/dist/account.d.ts +31 -0
- package/dist/account.js +77 -0
- package/dist/accounts/amber-clipboard-account.d.ts +10 -0
- package/dist/accounts/amber-clipboard-account.js +16 -0
- package/dist/accounts/extension-account.d.ts +7 -0
- package/dist/accounts/extension-account.js +13 -0
- package/dist/accounts/index.d.ts +6 -0
- package/dist/accounts/index.js +6 -0
- package/dist/accounts/nostr-connect-account.d.ts +11 -0
- package/dist/accounts/nostr-connect-account.js +36 -0
- package/dist/accounts/nsec-account.d.ts +13 -0
- package/dist/accounts/nsec-account.js +39 -0
- package/dist/accounts/password-account.d.ts +15 -0
- package/dist/accounts/password-account.js +36 -0
- package/dist/accounts/pubkey-account.d.ts +5 -0
- package/dist/accounts/pubkey-account.js +7 -0
- package/dist/accounts/readonly-account.d.ts +9 -0
- package/dist/accounts/readonly-account.js +18 -0
- package/dist/accounts/serial-port-account.d.ts +10 -0
- package/dist/accounts/serial-port-account.js +25 -0
- package/dist/accounts/simple-account.d.ts +14 -0
- package/dist/accounts/simple-account.js +24 -0
- package/dist/helpers/address-pointer.d.ts +16 -0
- package/dist/helpers/address-pointer.js +67 -0
- package/dist/helpers/array.d.ts +4 -0
- package/dist/helpers/array.js +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/loaders/index.d.ts +3 -0
- package/dist/loaders/index.js +3 -0
- package/dist/loaders/loader.d.ts +14 -0
- package/dist/loaders/loader.js +17 -0
- package/dist/loaders/replaceable-loader.d.ts +31 -0
- package/dist/loaders/replaceable-loader.js +163 -0
- package/dist/loaders/single-relay-replaceable-loader.d.ts +14 -0
- package/dist/loaders/single-relay-replaceable-loader.js +51 -0
- package/dist/manager.d.ts +2 -0
- package/dist/manager.js +2 -0
- package/dist/operators/address-pointers-request.d.ts +5 -0
- package/dist/operators/address-pointers-request.js +25 -0
- package/dist/operators/generator-sequence.d.ts +3 -0
- package/dist/operators/generator-sequence.js +45 -0
- package/dist/operators/index.d.ts +4 -0
- package/dist/operators/index.js +4 -0
- package/dist/operators/max-filters.d.ts +4 -0
- package/dist/operators/max-filters.js +8 -0
- package/dist/operators/relay-request.d.ts +4 -0
- package/dist/operators/relay-request.js +9 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/package.json +51 -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,31 @@
|
|
|
1
|
+
import { Nip07Interface } from "applesauce-signer";
|
|
2
|
+
import { EventTemplate } from "nostr-tools";
|
|
3
|
+
import { IAccount, SerializedAccount } from "./types.js";
|
|
4
|
+
export declare class SignerMismatchError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export declare class AccountLockedError extends Error {
|
|
7
|
+
}
|
|
8
|
+
export declare class BaseAccount<T extends string, S> implements IAccount<T, S> {
|
|
9
|
+
pubkey: string;
|
|
10
|
+
signer: Nip07Interface;
|
|
11
|
+
name?: string;
|
|
12
|
+
locked: boolean;
|
|
13
|
+
nip04?: {
|
|
14
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
15
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
16
|
+
} | undefined;
|
|
17
|
+
nip44?: {
|
|
18
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
19
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
20
|
+
} | undefined;
|
|
21
|
+
constructor(pubkey: string, signer: Nip07Interface);
|
|
22
|
+
unlock(): Promise<boolean>;
|
|
23
|
+
lock(): void;
|
|
24
|
+
toJSON(): SerializedAccount<T, S>;
|
|
25
|
+
/** throws if account is locked */
|
|
26
|
+
protected checkLocked(): void;
|
|
27
|
+
/** Gets the pubkey from the signer */
|
|
28
|
+
getPublicKey(): Promise<string>;
|
|
29
|
+
/** sign the event and make sure its signed with the correct pubkey */
|
|
30
|
+
signEvent(template: EventTemplate): Promise<import("nostr-tools").Event>;
|
|
31
|
+
}
|
package/dist/account.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// errors
|
|
2
|
+
export class SignerMismatchError extends Error {
|
|
3
|
+
}
|
|
4
|
+
export class AccountLockedError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export class BaseAccount {
|
|
7
|
+
pubkey;
|
|
8
|
+
signer;
|
|
9
|
+
name;
|
|
10
|
+
locked = true;
|
|
11
|
+
// encryption interfaces
|
|
12
|
+
nip04;
|
|
13
|
+
nip44;
|
|
14
|
+
constructor(pubkey, signer) {
|
|
15
|
+
this.pubkey = pubkey;
|
|
16
|
+
this.signer = signer;
|
|
17
|
+
// setup encryption interfaces to check if account is locked
|
|
18
|
+
if (this.signer.nip04) {
|
|
19
|
+
this.nip04 = {
|
|
20
|
+
encrypt: (pubkey, plaintext) => {
|
|
21
|
+
this.checkLocked();
|
|
22
|
+
return this.signer.nip04.encrypt(pubkey, plaintext);
|
|
23
|
+
},
|
|
24
|
+
decrypt: (pubkey, plaintext) => {
|
|
25
|
+
this.checkLocked();
|
|
26
|
+
return this.signer.nip04.decrypt(pubkey, plaintext);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (this.signer.nip44) {
|
|
31
|
+
this.nip44 = {
|
|
32
|
+
encrypt: (pubkey, plaintext) => {
|
|
33
|
+
this.checkLocked();
|
|
34
|
+
return this.signer.nip44.encrypt(pubkey, plaintext);
|
|
35
|
+
},
|
|
36
|
+
decrypt: (pubkey, plaintext) => {
|
|
37
|
+
this.checkLocked();
|
|
38
|
+
return this.signer.nip44.decrypt(pubkey, plaintext);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async unlock() {
|
|
44
|
+
this.locked = false;
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
lock() {
|
|
48
|
+
this.locked = true;
|
|
49
|
+
}
|
|
50
|
+
// This should be overwritten by a sub class
|
|
51
|
+
toJSON() {
|
|
52
|
+
throw new Error("Not implemented");
|
|
53
|
+
}
|
|
54
|
+
/** throws if account is locked */
|
|
55
|
+
checkLocked() {
|
|
56
|
+
if (this.locked)
|
|
57
|
+
throw new AccountLockedError("Account is locked");
|
|
58
|
+
}
|
|
59
|
+
/** Gets the pubkey from the signer */
|
|
60
|
+
async getPublicKey() {
|
|
61
|
+
this.checkLocked();
|
|
62
|
+
const signerKey = await this.signer.getPublicKey();
|
|
63
|
+
if (this.pubkey !== signerKey)
|
|
64
|
+
throw new Error("Account signer mismatch");
|
|
65
|
+
return this.pubkey;
|
|
66
|
+
}
|
|
67
|
+
/** sign the event and make sure its signed with the correct pubkey */
|
|
68
|
+
async signEvent(template) {
|
|
69
|
+
this.checkLocked();
|
|
70
|
+
if (!Reflect.has(template, "pubkey"))
|
|
71
|
+
Reflect.set(template, "pubkey", this.pubkey);
|
|
72
|
+
const signed = await this.signer.signEvent(template);
|
|
73
|
+
if (signed.pubkey !== this.pubkey)
|
|
74
|
+
throw new SignerMismatchError("Signer signed with wrong pubkey");
|
|
75
|
+
return signed;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AmberClipboardSigner } from "applesauce-signer/signers/amber-clipboard-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
import { IAccount, SerializedAccount } from "../types.js";
|
|
4
|
+
/** An account for the amber clipboard api */
|
|
5
|
+
export declare class AmberClipboardAccount extends BaseAccount<"amber-clipboard", void> {
|
|
6
|
+
signer: AmberClipboardSigner;
|
|
7
|
+
constructor(pubkey: string, signer: AmberClipboardSigner);
|
|
8
|
+
toJSON(): SerializedAccount<"amber-clipboard", void>;
|
|
9
|
+
static fromJSON(json: SerializedAccount<"amber-clipboard", void>): IAccount<"amber-clipboard", void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AmberClipboardSigner } from "applesauce-signer/signers/amber-clipboard-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
/** An account for the amber clipboard api */
|
|
4
|
+
export class AmberClipboardAccount extends BaseAccount {
|
|
5
|
+
signer;
|
|
6
|
+
constructor(pubkey, signer) {
|
|
7
|
+
super(pubkey, signer);
|
|
8
|
+
this.signer = signer;
|
|
9
|
+
}
|
|
10
|
+
toJSON() {
|
|
11
|
+
return { type: "amber-clipboard", pubkey: this.pubkey, name: this.name, signer: void 0 };
|
|
12
|
+
}
|
|
13
|
+
static fromJSON(json) {
|
|
14
|
+
return new AmberClipboardAccount(json.pubkey, new AmberClipboardSigner());
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseAccount } from "../account.js";
|
|
2
|
+
import { SerializedAccount } from "../types.js";
|
|
3
|
+
export default class ExtensionAccount extends BaseAccount<"extension", void> {
|
|
4
|
+
constructor(pubkey: string);
|
|
5
|
+
toJSON(): SerializedAccount<"extension", void>;
|
|
6
|
+
static fromJSON(json: SerializedAccount<"extension", void>): ExtensionAccount;
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExtensionSigner } from "applesauce-signer/signers/extension-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
export default class ExtensionAccount extends BaseAccount {
|
|
4
|
+
constructor(pubkey) {
|
|
5
|
+
super(pubkey, new ExtensionSigner());
|
|
6
|
+
}
|
|
7
|
+
toJSON() {
|
|
8
|
+
return { type: "extension", pubkey: this.pubkey, signer: undefined };
|
|
9
|
+
}
|
|
10
|
+
static fromJSON(json) {
|
|
11
|
+
return new ExtensionAccount(json.pubkey);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { NostrConnectSigner } from "applesauce-signer/signers";
|
|
2
|
+
import { Account } from "./account";
|
|
3
|
+
export default class NostrConnectAccount extends Account {
|
|
4
|
+
readonly type = "nostr-connect";
|
|
5
|
+
protected _signer: NostrConnectSigner;
|
|
6
|
+
get signer(): NostrConnectSigner;
|
|
7
|
+
set signer(value: NostrConnectSigner);
|
|
8
|
+
constructor(pubkey: string, signer?: NostrConnectSigner);
|
|
9
|
+
toJSON(): any;
|
|
10
|
+
fromJSON(data: any): this;
|
|
11
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { NostrConnectSigner, SimpleSigner } from "applesauce-signer/signers";
|
|
2
|
+
import { hexToBytes } from "@noble/hashes/utils";
|
|
3
|
+
import { DEFAULT_NOSTR_CONNECT_RELAYS } from "../../const";
|
|
4
|
+
import { Account } from "./account";
|
|
5
|
+
import relayPoolService from "../../services/relay-pool";
|
|
6
|
+
function createSigner(pubkey, relays, secretKey, provider) {
|
|
7
|
+
const signer = secretKey ? new SimpleSigner(hexToBytes(secretKey)) : undefined;
|
|
8
|
+
const client = new NostrConnectSigner({ pool: relayPoolService, pubkey, relays, signer, remote: provider });
|
|
9
|
+
return client;
|
|
10
|
+
}
|
|
11
|
+
export default class NostrConnectAccount extends Account {
|
|
12
|
+
type = "nostr-connect";
|
|
13
|
+
get signer() {
|
|
14
|
+
return this._signer;
|
|
15
|
+
}
|
|
16
|
+
set signer(value) {
|
|
17
|
+
this._signer = value;
|
|
18
|
+
}
|
|
19
|
+
constructor(pubkey, signer) {
|
|
20
|
+
super(pubkey);
|
|
21
|
+
this.signer = signer || createSigner(pubkey, DEFAULT_NOSTR_CONNECT_RELAYS);
|
|
22
|
+
}
|
|
23
|
+
toJSON() {
|
|
24
|
+
const json = this.signer.toJSON();
|
|
25
|
+
return {
|
|
26
|
+
...super.toJSON(),
|
|
27
|
+
signerRelays: this.signer.relays,
|
|
28
|
+
clientSecretKey: json.client,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
fromJSON(data) {
|
|
32
|
+
super.fromJSON(data);
|
|
33
|
+
this.signer = createSigner(data.pubkey, data.signerRelays, data.clientSecretKey);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
|
|
2
|
+
import { Account } from "./account";
|
|
3
|
+
export default class NsecAccount extends Account {
|
|
4
|
+
readonly type = "nsec";
|
|
5
|
+
protected _signer?: SimpleSigner | undefined;
|
|
6
|
+
get signer(): SimpleSigner | undefined;
|
|
7
|
+
set signer(value: SimpleSigner | undefined);
|
|
8
|
+
constructor(pubkey: string);
|
|
9
|
+
static newKey(): NsecAccount;
|
|
10
|
+
static fromKey(key: Uint8Array): NsecAccount;
|
|
11
|
+
toJSON(): any;
|
|
12
|
+
fromJSON(data: any): this;
|
|
13
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { generateSecretKey, getPublicKey, nip19 } from "nostr-tools";
|
|
2
|
+
import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
|
|
3
|
+
import { Account } from "./account";
|
|
4
|
+
export default class NsecAccount extends Account {
|
|
5
|
+
type = "nsec";
|
|
6
|
+
get signer() {
|
|
7
|
+
return this._signer;
|
|
8
|
+
}
|
|
9
|
+
set signer(value) {
|
|
10
|
+
this._signer = value;
|
|
11
|
+
}
|
|
12
|
+
constructor(pubkey) {
|
|
13
|
+
super(pubkey);
|
|
14
|
+
}
|
|
15
|
+
static newKey() {
|
|
16
|
+
const key = generateSecretKey();
|
|
17
|
+
const account = new NsecAccount(getPublicKey(key));
|
|
18
|
+
account.signer = new SimpleSigner(key);
|
|
19
|
+
return account;
|
|
20
|
+
}
|
|
21
|
+
static fromKey(key) {
|
|
22
|
+
const account = new NsecAccount(getPublicKey(key));
|
|
23
|
+
account.signer = new SimpleSigner(key);
|
|
24
|
+
return account;
|
|
25
|
+
}
|
|
26
|
+
toJSON() {
|
|
27
|
+
return {
|
|
28
|
+
...super.toJSON(),
|
|
29
|
+
nsec: this.signer && nip19.nsecEncode(this.signer.key),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
fromJSON(data) {
|
|
33
|
+
const parse = nip19.decode(data.nsec);
|
|
34
|
+
if (parse.type !== "nsec")
|
|
35
|
+
throw new Error("Unknown nsec type");
|
|
36
|
+
this.signer = new SimpleSigner(parse.data);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PasswordSigner } from "applesauce-signer/signers/password-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
import { SerializedAccount } from "../types.js";
|
|
4
|
+
type SignerData = {
|
|
5
|
+
ncryptsec: string;
|
|
6
|
+
};
|
|
7
|
+
export default class PasswordAccount extends BaseAccount<"ncryptsec", SignerData> {
|
|
8
|
+
signer: PasswordSigner;
|
|
9
|
+
constructor(pubkey: string, signer: PasswordSigner);
|
|
10
|
+
unlock(): Promise<boolean>;
|
|
11
|
+
toJSON(): SerializedAccount<"ncryptsec", SignerData>;
|
|
12
|
+
static fromJSON(json: SerializedAccount<"ncryptsec", SignerData>): PasswordAccount;
|
|
13
|
+
static fromNcryptsec(pubkey: string, ncryptsec: string): PasswordAccount;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PasswordSigner } from "applesauce-signer/signers/password-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
export default class PasswordAccount extends BaseAccount {
|
|
4
|
+
signer;
|
|
5
|
+
constructor(pubkey, signer) {
|
|
6
|
+
super(pubkey, signer);
|
|
7
|
+
this.signer = signer;
|
|
8
|
+
}
|
|
9
|
+
async unlock() {
|
|
10
|
+
try {
|
|
11
|
+
const password = prompt("Unlock password");
|
|
12
|
+
if (password === null)
|
|
13
|
+
return false;
|
|
14
|
+
await this.signer.unlock(password);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
toJSON() {
|
|
22
|
+
if (!this.signer.ncryptsec)
|
|
23
|
+
throw new Error("Cant save account without ncryptsec");
|
|
24
|
+
return { type: "ncryptsec", pubkey: this.pubkey, signer: { ncryptsec: this.signer.ncryptsec } };
|
|
25
|
+
}
|
|
26
|
+
static fromJSON(json) {
|
|
27
|
+
const signer = new PasswordSigner();
|
|
28
|
+
signer.ncryptsec = json.signer.ncryptsec;
|
|
29
|
+
return new PasswordAccount(json.pubkey, signer);
|
|
30
|
+
}
|
|
31
|
+
static fromNcryptsec(pubkey, ncryptsec) {
|
|
32
|
+
const signer = new PasswordSigner();
|
|
33
|
+
signer.ncryptsec = ncryptsec;
|
|
34
|
+
return new PasswordAccount(pubkey, signer);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ReadonlySigner } from "applesauce-signer/signers/readonly-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
import { SerializedAccount } from "../types.js";
|
|
4
|
+
/** An account that cannot sign or encrypt anything */
|
|
5
|
+
export default class ReadonlyAccount extends BaseAccount<"readonly", void> {
|
|
6
|
+
constructor(pubkey: string, signer?: ReadonlySigner);
|
|
7
|
+
toJSON(): SerializedAccount<"readonly", void>;
|
|
8
|
+
static fromJSON(json: SerializedAccount<"readonly", void>): ReadonlyAccount;
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ReadonlySigner } from "applesauce-signer/signers/readonly-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
/** An account that cannot sign or encrypt anything */
|
|
4
|
+
export default class ReadonlyAccount extends BaseAccount {
|
|
5
|
+
constructor(pubkey, signer) {
|
|
6
|
+
super(pubkey, signer || new ReadonlySigner(pubkey));
|
|
7
|
+
}
|
|
8
|
+
toJSON() {
|
|
9
|
+
return {
|
|
10
|
+
type: "readonly",
|
|
11
|
+
pubkey: this.pubkey,
|
|
12
|
+
signer: undefined,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
static fromJSON(json) {
|
|
16
|
+
return new ReadonlyAccount(json.pubkey);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SerialPortSigner } from "applesauce-signer/signers/serial-port-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
import { SerializedAccount } from "../types.js";
|
|
4
|
+
/** An account for SerialPortSigner */
|
|
5
|
+
export default class SerialPortAccount extends BaseAccount<"serial-port", void> {
|
|
6
|
+
constructor(pubkey: string, signer?: SerialPortSigner);
|
|
7
|
+
unlock(): Promise<boolean>;
|
|
8
|
+
toJSON(): SerializedAccount<"serial-port", void>;
|
|
9
|
+
static fromJSON(json: SerializedAccount<"serial-port", void>): SerialPortAccount;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SerialPortSigner } from "applesauce-signer/signers/serial-port-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
/** An account for SerialPortSigner */
|
|
4
|
+
export default class SerialPortAccount extends BaseAccount {
|
|
5
|
+
constructor(pubkey, signer) {
|
|
6
|
+
super(pubkey, signer || new SerialPortSigner());
|
|
7
|
+
}
|
|
8
|
+
async unlock() {
|
|
9
|
+
try {
|
|
10
|
+
const pubkey = await this.signer.getPublicKey();
|
|
11
|
+
if (pubkey !== this.pubkey)
|
|
12
|
+
throw new Error("Signer returned incorrect pubkey");
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
toJSON() {
|
|
20
|
+
return { type: "serial-port", pubkey: this.pubkey, signer: undefined };
|
|
21
|
+
}
|
|
22
|
+
static fromJSON(json) {
|
|
23
|
+
return new SerialPortAccount(json.pubkey);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
|
|
2
|
+
import { BaseAccount } from "../account.js";
|
|
3
|
+
import { SerializedAccount } from "../types.js";
|
|
4
|
+
type SignerData = {
|
|
5
|
+
key: string;
|
|
6
|
+
};
|
|
7
|
+
export default class SimpleAccount extends BaseAccount<"nsec", SignerData> {
|
|
8
|
+
signer: SimpleSigner;
|
|
9
|
+
constructor(pubkey: string, signer: SimpleSigner);
|
|
10
|
+
static fromKey(key: Uint8Array | string): SimpleAccount;
|
|
11
|
+
toJSON(): SerializedAccount<"nsec", SignerData>;
|
|
12
|
+
static fromJSON(json: SerializedAccount<"nsec", SignerData>): SimpleAccount;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getPublicKey } from "nostr-tools";
|
|
2
|
+
import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
|
|
3
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
4
|
+
import { BaseAccount } from "../account.js";
|
|
5
|
+
export default class SimpleAccount extends BaseAccount {
|
|
6
|
+
signer;
|
|
7
|
+
constructor(pubkey, signer) {
|
|
8
|
+
super(pubkey, signer);
|
|
9
|
+
this.signer = signer;
|
|
10
|
+
}
|
|
11
|
+
static fromKey(key) {
|
|
12
|
+
if (typeof key === "string")
|
|
13
|
+
key = hexToBytes(key);
|
|
14
|
+
const pubkey = getPublicKey(key);
|
|
15
|
+
return new SimpleAccount(pubkey, new SimpleSigner(key));
|
|
16
|
+
}
|
|
17
|
+
toJSON() {
|
|
18
|
+
return { type: "nsec", pubkey: this.pubkey, signer: { key: bytesToHex(this.signer.key) } };
|
|
19
|
+
}
|
|
20
|
+
static fromJSON(json) {
|
|
21
|
+
const key = hexToBytes(json.signer.key);
|
|
22
|
+
return new SimpleAccount(json.pubkey, new SimpleSigner(key));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AddressPointerWithoutD } from "applesauce-core/helpers";
|
|
2
|
+
import { Filter } from "nostr-tools";
|
|
3
|
+
/** Converts an array of address pointers to a filter */
|
|
4
|
+
export declare function createFilterFromAddressPointers(pointers: AddressPointerWithoutD[]): Filter;
|
|
5
|
+
/** Takes a set of address pointers, groups them, then returns filters for the groups */
|
|
6
|
+
export declare function createFiltersFromAddressPointers(pointers: AddressPointerWithoutD[]): Filter[];
|
|
7
|
+
/** Checks if a relay will understand an address pointer */
|
|
8
|
+
export declare function isLoadableAddressPointer<T extends AddressPointerWithoutD>(pointer: T): boolean;
|
|
9
|
+
/** Group an array of address pointers by kind */
|
|
10
|
+
export declare function groupAddressPointersByKind(pointers: AddressPointerWithoutD[]): Map<number, AddressPointerWithoutD[]>;
|
|
11
|
+
/** Group an array of address pointers by pubkey */
|
|
12
|
+
export declare function groupAddressPointersByPubkey(pointers: AddressPointerWithoutD[]): Map<string, AddressPointerWithoutD[]>;
|
|
13
|
+
/** Groups address pointers by kind or pubkey depending on which is most optimal */
|
|
14
|
+
export declare function groupAddressPointersByPubkeyOrKind(pointers: AddressPointerWithoutD[]): Map<number, AddressPointerWithoutD[]> | Map<string, AddressPointerWithoutD[]>;
|
|
15
|
+
export declare function getRelaysFromPointers(pointers: AddressPointerWithoutD[]): Set<string>;
|
|
16
|
+
export declare function getAddressPointerId<T extends AddressPointerWithoutD>(pointer: T): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { getReplaceableUID } from "applesauce-core/helpers";
|
|
2
|
+
import { isParameterizedReplaceableKind, isReplaceableKind } from "nostr-tools/kinds";
|
|
3
|
+
import { unique } from "./array.js";
|
|
4
|
+
/** Converts an array of address pointers to a filter */
|
|
5
|
+
export function createFilterFromAddressPointers(pointers) {
|
|
6
|
+
const filter = {};
|
|
7
|
+
filter.kinds = unique(pointers.map((p) => p.kind));
|
|
8
|
+
filter.authors = unique(pointers.map((p) => p.pubkey));
|
|
9
|
+
const identifiers = unique(pointers.map((p) => p.identifier).filter((d) => !!d));
|
|
10
|
+
if (identifiers.length > 0)
|
|
11
|
+
filter["#d"] = identifiers;
|
|
12
|
+
return filter;
|
|
13
|
+
}
|
|
14
|
+
/** Takes a set of address pointers, groups them, then returns filters for the groups */
|
|
15
|
+
export function createFiltersFromAddressPointers(pointers) {
|
|
16
|
+
const groups = groupAddressPointersByPubkeyOrKind(pointers);
|
|
17
|
+
return Array.from(groups.values()).map((pointers) => createFilterFromAddressPointers(pointers));
|
|
18
|
+
}
|
|
19
|
+
/** Checks if a relay will understand an address pointer */
|
|
20
|
+
export function isLoadableAddressPointer(pointer) {
|
|
21
|
+
if (isParameterizedReplaceableKind(pointer.kind))
|
|
22
|
+
return !!pointer.identifier;
|
|
23
|
+
else
|
|
24
|
+
return isReplaceableKind(pointer.kind);
|
|
25
|
+
}
|
|
26
|
+
/** Group an array of address pointers by kind */
|
|
27
|
+
export function groupAddressPointersByKind(pointers) {
|
|
28
|
+
const byKind = new Map();
|
|
29
|
+
for (const pointer of pointers) {
|
|
30
|
+
if (byKind.has(pointer.kind))
|
|
31
|
+
byKind.get(pointer.kind).push(pointer);
|
|
32
|
+
else
|
|
33
|
+
byKind.set(pointer.kind, [pointer]);
|
|
34
|
+
}
|
|
35
|
+
return byKind;
|
|
36
|
+
}
|
|
37
|
+
/** Group an array of address pointers by pubkey */
|
|
38
|
+
export function groupAddressPointersByPubkey(pointers) {
|
|
39
|
+
const byPubkey = new Map();
|
|
40
|
+
for (const pointer of pointers) {
|
|
41
|
+
if (byPubkey.has(pointer.pubkey))
|
|
42
|
+
byPubkey.get(pointer.pubkey).push(pointer);
|
|
43
|
+
else
|
|
44
|
+
byPubkey.set(pointer.pubkey, [pointer]);
|
|
45
|
+
}
|
|
46
|
+
return byPubkey;
|
|
47
|
+
}
|
|
48
|
+
/** Groups address pointers by kind or pubkey depending on which is most optimal */
|
|
49
|
+
export function groupAddressPointersByPubkeyOrKind(pointers) {
|
|
50
|
+
const kinds = new Set(pointers.map((p) => p.kind));
|
|
51
|
+
const pubkeys = new Set(pointers.map((p) => p.pubkey));
|
|
52
|
+
return pubkeys.size > kinds.size ? groupAddressPointersByKind(pointers) : groupAddressPointersByPubkey(pointers);
|
|
53
|
+
}
|
|
54
|
+
export function getRelaysFromPointers(pointers) {
|
|
55
|
+
const relays = new Set();
|
|
56
|
+
for (const pointer of pointers) {
|
|
57
|
+
if (!pointer.relays)
|
|
58
|
+
continue;
|
|
59
|
+
for (const relay of pointer.relays) {
|
|
60
|
+
relays.add(relay);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return relays;
|
|
64
|
+
}
|
|
65
|
+
export function getAddressPointerId(pointer) {
|
|
66
|
+
return getReplaceableUID(pointer.kind, pointer.pubkey, pointer.identifier);
|
|
67
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Does not preserve order */
|
|
2
|
+
export function unique(arr) {
|
|
3
|
+
return Array.from(new Set(arr));
|
|
4
|
+
}
|
|
5
|
+
/** split array into a set of arrays no larger than batchSize */
|
|
6
|
+
export function reduceToBatches(arr, batchSize) {
|
|
7
|
+
const batches = [];
|
|
8
|
+
for (const value of arr) {
|
|
9
|
+
if (batches.length === 0) {
|
|
10
|
+
batches.push([value]);
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const current = batches[batches.length - 1];
|
|
14
|
+
if (current.length <= batchSize) {
|
|
15
|
+
current.push(value);
|
|
16
|
+
}
|
|
17
|
+
else
|
|
18
|
+
batches.push([value]);
|
|
19
|
+
}
|
|
20
|
+
return batches;
|
|
21
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Observable, OperatorFunction, Subject, Subscribable } from "rxjs";
|
|
2
|
+
export interface ILoader<T, R> extends Subscribable<R> {
|
|
3
|
+
next: (value: T) => void;
|
|
4
|
+
pipe: Observable<R>["pipe"];
|
|
5
|
+
}
|
|
6
|
+
/** Base loader class */
|
|
7
|
+
export declare class Loader<T, R> implements ILoader<T, R> {
|
|
8
|
+
protected subject: Subject<T>;
|
|
9
|
+
protected observable: Observable<R>;
|
|
10
|
+
pipe: Observable<R>["pipe"];
|
|
11
|
+
subscribe: Observable<R>["subscribe"];
|
|
12
|
+
constructor(transform: OperatorFunction<T, R>);
|
|
13
|
+
next(value: T): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Subject } from "rxjs";
|
|
2
|
+
/** Base loader class */
|
|
3
|
+
export class Loader {
|
|
4
|
+
subject = new Subject();
|
|
5
|
+
observable;
|
|
6
|
+
pipe;
|
|
7
|
+
subscribe;
|
|
8
|
+
constructor(transform) {
|
|
9
|
+
this.observable = this.subject.pipe(transform);
|
|
10
|
+
// copy pipe function
|
|
11
|
+
this.pipe = this.observable.pipe.bind(this.observable);
|
|
12
|
+
this.subscribe = this.observable.subscribe.bind(this.observable);
|
|
13
|
+
}
|
|
14
|
+
next(value) {
|
|
15
|
+
this.subject.next(value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { EventPacket, RxNostr } from "rx-nostr";
|
|
3
|
+
import { Filter, NostrEvent } from "nostr-tools";
|
|
4
|
+
import { logger } from "applesauce-core";
|
|
5
|
+
import { Loader } from "./loader.js";
|
|
6
|
+
export type LoadableAddressPointer = {
|
|
7
|
+
kind: number;
|
|
8
|
+
pubkey: string;
|
|
9
|
+
/** Optional "d" tag for paramaritized replaceable */
|
|
10
|
+
identifier?: string;
|
|
11
|
+
/** Relays to load from */
|
|
12
|
+
relays?: string[];
|
|
13
|
+
/** Load this address pointer even if it has already been loaded */
|
|
14
|
+
force?: boolean;
|
|
15
|
+
};
|
|
16
|
+
export type CacheRequest = (filters: Filter[]) => Observable<NostrEvent>;
|
|
17
|
+
export type ReplaceableLoaderOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Time interval to buffer requests in ms
|
|
20
|
+
* @default 1000
|
|
21
|
+
*/
|
|
22
|
+
bufferTime?: number;
|
|
23
|
+
/** Request events from the cache first */
|
|
24
|
+
cacheRequest?: CacheRequest;
|
|
25
|
+
/** Fallback lookup relays to check when event cant be found */
|
|
26
|
+
lookupRelays?: string[];
|
|
27
|
+
};
|
|
28
|
+
export declare class ReplaceableLoader extends Loader<LoadableAddressPointer, EventPacket> {
|
|
29
|
+
log: typeof logger;
|
|
30
|
+
constructor(rxNostr: RxNostr, opts?: ReplaceableLoaderOptions);
|
|
31
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { share, tap, from, filter, bufferTime, map, mergeMap } from "rxjs";
|
|
2
|
+
import { getEventUID, getReplaceableUID, markFromCache } from "applesauce-core/helpers";
|
|
3
|
+
import { logger } from "applesauce-core";
|
|
4
|
+
import { nanoid } from "nanoid";
|
|
5
|
+
import { Loader } from "./loader.js";
|
|
6
|
+
import { generatorSequence } from "../operators/generator-sequence.js";
|
|
7
|
+
import { replaceableRequest } from "../operators/address-pointers-request.js";
|
|
8
|
+
import { createFiltersFromAddressPointers, getAddressPointerId, getRelaysFromPointers, isLoadableAddressPointer, } from "../helpers/address-pointer.js";
|
|
9
|
+
import { unique } from "../helpers/array.js";
|
|
10
|
+
/** deep clone a loadable pointer to ensure its safe to modify */
|
|
11
|
+
function cloneLoadablePointer(pointer) {
|
|
12
|
+
const clone = { ...pointer };
|
|
13
|
+
if (pointer.relays)
|
|
14
|
+
clone.relays = [...pointer.relays];
|
|
15
|
+
return clone;
|
|
16
|
+
}
|
|
17
|
+
/** A generator that tries to load the address pointers from the cache first, then tries the relays */
|
|
18
|
+
function* cacheFirstSequence(rxNostr, pointers, log, opts) {
|
|
19
|
+
const id = nanoid(8);
|
|
20
|
+
log = log.extend(id);
|
|
21
|
+
let remaining = Array.from(pointers);
|
|
22
|
+
const pointerRelays = Array.from(getRelaysFromPointers(pointers));
|
|
23
|
+
// handle previous step results and decide if to exit
|
|
24
|
+
const handleResults = (results) => {
|
|
25
|
+
if (results.length) {
|
|
26
|
+
const coordinates = new Set(results.map((p) => getEventUID(p.event)));
|
|
27
|
+
// if there where results, filter out any pointers that where found
|
|
28
|
+
remaining = remaining.filter((pointer) => {
|
|
29
|
+
const found = coordinates.has(getReplaceableUID(pointer.kind, pointer.pubkey, pointer.identifier));
|
|
30
|
+
if (found && pointer.force !== true)
|
|
31
|
+
return false;
|
|
32
|
+
else
|
|
33
|
+
return true;
|
|
34
|
+
});
|
|
35
|
+
if (remaining.length === 0)
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
40
|
+
// first attempt, load from cache relays
|
|
41
|
+
if (opts?.cacheRequest) {
|
|
42
|
+
log(`Checking cache`, remaining);
|
|
43
|
+
const results = yield from([remaining]).pipe(
|
|
44
|
+
// convert pointers to filters
|
|
45
|
+
map(createFiltersFromAddressPointers),
|
|
46
|
+
// make requests
|
|
47
|
+
mergeMap((filters) => opts.cacheRequest(filters)),
|
|
48
|
+
// mark the event as from the cache
|
|
49
|
+
tap((event) => markFromCache(event)),
|
|
50
|
+
// convert to event packets
|
|
51
|
+
map((e) => ({ event: e, from: "cache", subId: "cache", type: "EVENT" })));
|
|
52
|
+
if (handleResults(results))
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
// load from pointer relays and default relays
|
|
56
|
+
const defaultRelays = Object.entries(rxNostr.getDefaultRelays())
|
|
57
|
+
.filter(([_relay, config]) => config.read)
|
|
58
|
+
.map(([relay]) => relay);
|
|
59
|
+
const remoteRelays = [...pointerRelays, ...defaultRelays];
|
|
60
|
+
if (remoteRelays.length > 0) {
|
|
61
|
+
log(`Requesting`, remoteRelays, remaining);
|
|
62
|
+
const results = yield from([remaining]).pipe(replaceableRequest(rxNostr, id, remoteRelays));
|
|
63
|
+
if (handleResults(results))
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// finally load from lookup relays
|
|
67
|
+
if (opts?.lookupRelays) {
|
|
68
|
+
// make sure we aren't asking a relay twice
|
|
69
|
+
const relays = opts.lookupRelays.filter((r) => !pointerRelays.includes(r));
|
|
70
|
+
if (relays.length > 0) {
|
|
71
|
+
log(`Request from lookup`, relays, remaining);
|
|
72
|
+
const results = yield from([remaining]).pipe(replaceableRequest(rxNostr, id, relays));
|
|
73
|
+
if (handleResults(results))
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Batches address pointers and consolidates them */
|
|
79
|
+
function multiRelayBatcher(buffer) {
|
|
80
|
+
const requestedFrom = new Map();
|
|
81
|
+
const requestedDefault = new Set();
|
|
82
|
+
return (source) => source.pipe(
|
|
83
|
+
// buffer on time
|
|
84
|
+
bufferTime(buffer),
|
|
85
|
+
// ignore empty buffers
|
|
86
|
+
filter((buffer) => buffer.length > 0),
|
|
87
|
+
// consolidate buffered pointers
|
|
88
|
+
map((pointers) => {
|
|
89
|
+
const byId = new Map();
|
|
90
|
+
for (const pointer of pointers) {
|
|
91
|
+
const id = getAddressPointerId(pointer);
|
|
92
|
+
if (byId.has(id)) {
|
|
93
|
+
// duplicate, merge pointers
|
|
94
|
+
const current = byId.get(id);
|
|
95
|
+
// merge relays
|
|
96
|
+
if (pointer.relays) {
|
|
97
|
+
if (current.relays)
|
|
98
|
+
current.relays = unique([...current.relays, ...pointer.relays]);
|
|
99
|
+
else
|
|
100
|
+
current.relays = pointer.relays;
|
|
101
|
+
}
|
|
102
|
+
// merge force flag
|
|
103
|
+
if (pointer.force !== undefined) {
|
|
104
|
+
current.force = current.force || pointer.force;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
byId.set(id, cloneLoadablePointer(pointer));
|
|
109
|
+
}
|
|
110
|
+
// return consolidated pointers
|
|
111
|
+
return Array.from(byId.values());
|
|
112
|
+
}),
|
|
113
|
+
// ignore empty buffer
|
|
114
|
+
filter((buffer) => buffer.length > 0),
|
|
115
|
+
// ensure pointers are only requested from each relay once
|
|
116
|
+
map((pointers) => {
|
|
117
|
+
return pointers.filter((pointer) => {
|
|
118
|
+
const id = getAddressPointerId(pointer);
|
|
119
|
+
// skip if forced
|
|
120
|
+
if (pointer.force)
|
|
121
|
+
return true;
|
|
122
|
+
// skip if already requested from default relays
|
|
123
|
+
if (!pointer.relays) {
|
|
124
|
+
if (requestedDefault.has(id))
|
|
125
|
+
return false;
|
|
126
|
+
else {
|
|
127
|
+
requestedDefault.add(id);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
let set = requestedFrom.get(id);
|
|
132
|
+
if (!set) {
|
|
133
|
+
set = new Set();
|
|
134
|
+
requestedFrom.set(id, set);
|
|
135
|
+
}
|
|
136
|
+
// remove any relays that have already been used
|
|
137
|
+
pointer.relays = pointer.relays.filter((relay) => !set.has(relay));
|
|
138
|
+
// remember used relays
|
|
139
|
+
for (const relay of pointer.relays)
|
|
140
|
+
set.add(relay);
|
|
141
|
+
// if there are no new relays, ignore pointer
|
|
142
|
+
return pointer.relays.length > 0;
|
|
143
|
+
});
|
|
144
|
+
}),
|
|
145
|
+
// ignore empty buffer
|
|
146
|
+
filter((buffer) => buffer.length > 0));
|
|
147
|
+
}
|
|
148
|
+
export class ReplaceableLoader extends Loader {
|
|
149
|
+
log = logger.extend("ReplaceableLoader");
|
|
150
|
+
constructor(rxNostr, opts) {
|
|
151
|
+
super((source) => {
|
|
152
|
+
return source.pipe(
|
|
153
|
+
// filter out invalid pointers
|
|
154
|
+
filter(isLoadableAddressPointer),
|
|
155
|
+
// batch and filter
|
|
156
|
+
multiRelayBatcher(opts?.bufferTime ?? 1000),
|
|
157
|
+
// check cache, relays, lookup relays in that order
|
|
158
|
+
generatorSequence((pointers) => cacheFirstSequence(rxNostr, pointers, this.log, opts)),
|
|
159
|
+
// share the response with all subscribers
|
|
160
|
+
share());
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EventPacket, RxNostr } from "rx-nostr";
|
|
2
|
+
import { Loader } from "./loader.js";
|
|
3
|
+
import { LoadableAddressPointer } from "./replaceable-loader.js";
|
|
4
|
+
export type SingleRelayReplaceableOptions = Partial<{
|
|
5
|
+
/**
|
|
6
|
+
* Wait X ms before making a request
|
|
7
|
+
* @default 1000
|
|
8
|
+
*/
|
|
9
|
+
bufferTime: number;
|
|
10
|
+
}>;
|
|
11
|
+
/** A wrapper class around {@link singleRelayBatcher'}, {@link replaceableRequest}, and share */
|
|
12
|
+
export declare class SingleRelayReplaceableLoader extends Loader<LoadableAddressPointer, EventPacket> {
|
|
13
|
+
constructor(rxNostr: RxNostr, relay: string, opts?: SingleRelayReplaceableOptions);
|
|
14
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { bufferTime, filter, from, map, mergeMap, share } from "rxjs";
|
|
2
|
+
import { getReplaceableUID } from "applesauce-core/helpers";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { Loader } from "./loader.js";
|
|
5
|
+
import { isLoadableAddressPointer } from "../helpers/address-pointer.js";
|
|
6
|
+
import { replaceableRequest } from "../operators/address-pointers-request.js";
|
|
7
|
+
const defaultRelayReplaceableLoaderOptions = {
|
|
8
|
+
bufferTime: 1000,
|
|
9
|
+
};
|
|
10
|
+
/** Ensures an address pointer is only loaded once from a relay */
|
|
11
|
+
function singleRelayBatcher(opts) {
|
|
12
|
+
return (source) => {
|
|
13
|
+
const loaded = new Set();
|
|
14
|
+
const options = opts ? { ...defaultRelayReplaceableLoaderOptions, ...opts } : defaultRelayReplaceableLoaderOptions;
|
|
15
|
+
return source.pipe(
|
|
16
|
+
// buffer the requests, so we only send requests every second
|
|
17
|
+
bufferTime(options.bufferTime),
|
|
18
|
+
// ignore empty batches
|
|
19
|
+
filter((batch) => batch.length > 0),
|
|
20
|
+
// filter out duplicates
|
|
21
|
+
map((batch) => {
|
|
22
|
+
const filtered = [];
|
|
23
|
+
for (const pointer of batch) {
|
|
24
|
+
const uid = getReplaceableUID(pointer.kind, pointer.pubkey, pointer.identifier);
|
|
25
|
+
if (!loaded.has(uid)) {
|
|
26
|
+
filtered.push(pointer);
|
|
27
|
+
loaded.add(uid);
|
|
28
|
+
}
|
|
29
|
+
else if (pointer.force)
|
|
30
|
+
filtered.push(pointer);
|
|
31
|
+
}
|
|
32
|
+
return filtered;
|
|
33
|
+
}),
|
|
34
|
+
// ignore empty batches
|
|
35
|
+
filter((batch) => batch.length > 0));
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** A wrapper class around {@link singleRelayBatcher'}, {@link replaceableRequest}, and share */
|
|
39
|
+
export class SingleRelayReplaceableLoader extends Loader {
|
|
40
|
+
constructor(rxNostr, relay, opts) {
|
|
41
|
+
super((source) => source.pipe(
|
|
42
|
+
// ignore invalid address pointers
|
|
43
|
+
filter(isLoadableAddressPointer),
|
|
44
|
+
// batch and filter
|
|
45
|
+
singleRelayBatcher(opts),
|
|
46
|
+
// breakout the batches so they can complete
|
|
47
|
+
mergeMap((pointers) => from([pointers]).pipe(replaceableRequest(rxNostr, nanoid(8), [relay]))),
|
|
48
|
+
// share the response with all subscribers
|
|
49
|
+
share()));
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/manager.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { EventPacket, RxNostr } from "rx-nostr";
|
|
2
|
+
import { OperatorFunction } from "rxjs";
|
|
3
|
+
import { AddressPointerWithoutD } from "applesauce-core/helpers";
|
|
4
|
+
/** Makes a request to relays for every set of address pointers */
|
|
5
|
+
export declare function replaceableRequest<T extends AddressPointerWithoutD>(rxNostr: RxNostr, id: string, relays?: string[]): OperatorFunction<T[], EventPacket>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { map, tap } from "rxjs";
|
|
2
|
+
import { logger } from "applesauce-core";
|
|
3
|
+
import { relaysRequest } from "./relay-request.js";
|
|
4
|
+
import { createFiltersFromAddressPointers } from "../helpers/address-pointer.js";
|
|
5
|
+
/** Makes a request to relays for every set of address pointers */
|
|
6
|
+
export function replaceableRequest(rxNostr, id, relays) {
|
|
7
|
+
return (source) => {
|
|
8
|
+
const log = logger.extend(`replaceableRequest:${id}`);
|
|
9
|
+
return source.pipe(
|
|
10
|
+
// convert pointers to filters
|
|
11
|
+
map(createFiltersFromAddressPointers),
|
|
12
|
+
// make requests
|
|
13
|
+
tap((filters) => {
|
|
14
|
+
log(`Requesting`, relays, filters);
|
|
15
|
+
}),
|
|
16
|
+
// make requests
|
|
17
|
+
relaysRequest(rxNostr, id, relays),
|
|
18
|
+
// log when complete
|
|
19
|
+
tap({
|
|
20
|
+
complete: () => {
|
|
21
|
+
log(`Complete`);
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
/** Keeps retrying a value until the generator returns */
|
|
3
|
+
export function generatorSequence(createGenerator) {
|
|
4
|
+
return (source) => {
|
|
5
|
+
return new Observable((observer) => {
|
|
6
|
+
let complete = false;
|
|
7
|
+
const sub = source.subscribe({
|
|
8
|
+
next: (value) => {
|
|
9
|
+
const generator = createGenerator(value);
|
|
10
|
+
const next = (prevResults) => {
|
|
11
|
+
const result = generator.next(prevResults);
|
|
12
|
+
// generator complete, exit
|
|
13
|
+
if (result.done)
|
|
14
|
+
return;
|
|
15
|
+
const results = [];
|
|
16
|
+
result.value.subscribe({
|
|
17
|
+
next: (v) => {
|
|
18
|
+
// track results and pass along values
|
|
19
|
+
results.push(v);
|
|
20
|
+
observer.next(v);
|
|
21
|
+
},
|
|
22
|
+
error: (err) => {
|
|
23
|
+
observer.error(err);
|
|
24
|
+
},
|
|
25
|
+
complete: () => {
|
|
26
|
+
// if the upstream observable was complete. exit
|
|
27
|
+
if (complete)
|
|
28
|
+
return observer.complete();
|
|
29
|
+
// run next step
|
|
30
|
+
next(results);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
// start running steps
|
|
35
|
+
next();
|
|
36
|
+
},
|
|
37
|
+
complete: () => {
|
|
38
|
+
// source is complete
|
|
39
|
+
complete = true;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return () => sub.unsubscribe();
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { LazyFilter } from "rx-nostr";
|
|
2
|
+
import { OperatorFunction } from "rxjs";
|
|
3
|
+
/** Splits a batch of filters into multiple requests if necessary */
|
|
4
|
+
export declare function splitMaxFiltersPerRequest(maxFiltersPerRequest: number): OperatorFunction<LazyFilter[], LazyFilter[]>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { mergeMap } from "rxjs";
|
|
2
|
+
import { reduceToBatches } from "../helpers/array.js";
|
|
3
|
+
/** Splits a batch of filters into multiple requests if necessary */
|
|
4
|
+
export function splitMaxFiltersPerRequest(maxFiltersPerRequest) {
|
|
5
|
+
return (source) => source.pipe(
|
|
6
|
+
// batch the filters into sets no larger than maxFiltersPerRequest
|
|
7
|
+
mergeMap((filters) => reduceToBatches(filters, maxFiltersPerRequest)));
|
|
8
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { EventPacket, LazyFilter, RxNostr } from "rx-nostr";
|
|
2
|
+
import { OperatorFunction } from "rxjs";
|
|
3
|
+
/** Makes a request to relays for every set of filters */
|
|
4
|
+
export declare function relaysRequest(rxNostr: RxNostr, id: string, relays?: string[]): OperatorFunction<LazyFilter | LazyFilter[], EventPacket>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createRxOneshotReq } from "rx-nostr";
|
|
2
|
+
import { map, mergeAll } from "rxjs";
|
|
3
|
+
/** Makes a request to relays for every set of filters */
|
|
4
|
+
export function relaysRequest(rxNostr, id, relays) {
|
|
5
|
+
return (source) => source.pipe(map((filters) => {
|
|
6
|
+
const req = createRxOneshotReq({ filters, rxReqId: id });
|
|
7
|
+
return rxNostr.use(req, { on: { relays } });
|
|
8
|
+
}), mergeAll());
|
|
9
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Nip07Interface } from "applesauce-signer";
|
|
2
|
+
export type SerializedAccount<T extends string, S> = {
|
|
3
|
+
type: T;
|
|
4
|
+
name?: string;
|
|
5
|
+
pubkey: string;
|
|
6
|
+
signer: S;
|
|
7
|
+
};
|
|
8
|
+
export interface IAccount<T extends string, S> extends Nip07Interface {
|
|
9
|
+
name?: string;
|
|
10
|
+
pubkey: string;
|
|
11
|
+
locked: boolean;
|
|
12
|
+
unlock(): Promise<boolean>;
|
|
13
|
+
lock(): void;
|
|
14
|
+
toJSON(): SerializedAccount<T, S>;
|
|
15
|
+
}
|
|
16
|
+
export interface IAccountConstructor<T extends string, S> {
|
|
17
|
+
new (pubkey: string, signer: Nip07Interface): IAccount<T, S>;
|
|
18
|
+
fromJSON(json: SerializedAccount<T, S>): IAccount<T, S>;
|
|
19
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "applesauce-accounts",
|
|
3
|
+
"version": "0.0.0-next-20241218172722",
|
|
4
|
+
"description": "A simple nostr account management system",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"nostr"
|
|
10
|
+
],
|
|
11
|
+
"author": "hzrd149",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./accounts": {
|
|
22
|
+
"import": "./dist/accounts/index.js",
|
|
23
|
+
"types": "./dist/accounts/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./accounts/*": {
|
|
26
|
+
"import": "./dist/accounts/*.js",
|
|
27
|
+
"types": "./dist/accounts/*.d.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@noble/hashes": "^1.5.0",
|
|
32
|
+
"applesauce-signer": "0.0.0-next-20241218172722",
|
|
33
|
+
"nanoid": "^5.0.9",
|
|
34
|
+
"nostr-tools": "^2.10.3",
|
|
35
|
+
"rxjs": "^7.8.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"typescript": "^5.6.3",
|
|
39
|
+
"vitest": "^2.1.8"
|
|
40
|
+
},
|
|
41
|
+
"funding": {
|
|
42
|
+
"type": "lightning",
|
|
43
|
+
"url": "lightning:nostrudel@geyser.fund"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc",
|
|
47
|
+
"watch:build": "tsc --watch > /dev/null",
|
|
48
|
+
"test": "vitest run --passWithNoTests",
|
|
49
|
+
"watch:test": "vitest"
|
|
50
|
+
}
|
|
51
|
+
}
|