This commit is contained in:
Kieran 2023-07-22 19:37:46 +01:00
parent 74a3cd7754
commit b1f93c9fd8
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
65 changed files with 1115 additions and 1029 deletions

View File

@ -29,13 +29,13 @@ jobs:
- name: Rust cache
uses: swatinem/rust-cache@v2
with:
workspaces: './src-tauri -> target'
workspaces: "./src-tauri -> target"
- name: Sync node version and setup cache
uses: actions/setup-node@v3
with:
node-version: '16'
cache: 'yarn'
node-version: "16"
cache: "yarn"
- name: Install frontend dependencies
run: yarn install
- name: Build the app
@ -44,7 +44,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tagName: ${{ github.ref_name }}
releaseName: 'Snort v__VERSION__'
releaseBody: 'See the assets to download and install this version.'
releaseName: "Snort v__VERSION__"
releaseBody: "See the assets to download and install this version."
releaseDraft: true
prerelease: false

View File

@ -63,6 +63,7 @@ $ yarn build
Translations are managed on [Crowdin](https://crowdin.com/project/snort)
To extract translations run:
```bash
yarn workspace @snort/app intl-extract
yarn workspace @snort/app intl-compile

View File

@ -1,7 +1,6 @@
interface Env {
}
interface Env {}
export const onRequest: PagesFunction<Env> = async (context) => {
export const onRequest: PagesFunction<Env> = async context => {
const id = context.params.id as string;
const next = await context.next();
@ -11,16 +10,16 @@ export const onRequest: PagesFunction<Env> = async (context) => {
body: await next.arrayBuffer(),
headers: {
"user-agent": "Snort-Functions/1.0 (https://snort.social)",
"content-type": "text/plain"
}
"content-type": "text/plain",
},
});
if (rsp.ok) {
const body = await rsp.text();
if (body.length > 0) {
return new Response(body, {
headers: {
"content-type": "text/html"
}
"content-type": "text/html",
},
});
}
}
@ -28,4 +27,4 @@ export const onRequest: PagesFunction<Env> = async (context) => {
// ignore
}
return next;
}
};

View File

@ -1,7 +1,6 @@
interface Env {
}
interface Env {}
export const onRequest: PagesFunction<Env> = async (context) => {
export const onRequest: PagesFunction<Env> = async context => {
const id = context.params.id as string;
const next = await context.next();
@ -11,16 +10,16 @@ export const onRequest: PagesFunction<Env> = async (context) => {
body: await next.arrayBuffer(),
headers: {
"user-agent": "Snort-Functions/1.0 (https://snort.social)",
"content-type": "text/plain"
}
"content-type": "text/plain",
},
});
if (rsp.ok) {
const body = await rsp.text();
if (body.length > 0) {
return new Response(body, {
headers: {
"content-type": "text/html"
}
"content-type": "text/html",
},
});
}
}
@ -28,4 +27,4 @@ export const onRequest: PagesFunction<Env> = async (context) => {
// ignore
}
return next;
}
};

View File

@ -11,5 +11,10 @@
"devDependencies": {
"@tauri-apps/cli": "^1.2.3",
"@cloudflare/workers-types": "^4.20230307.0"
},
"prettier": {
"printWidth": 120,
"bracketSameLine": true,
"arrowParens": "avoid"
}
}

View File

@ -1,5 +0,0 @@
{
"printWidth": 120,
"bracketSameLine": true,
"arrowParens": "avoid"
}

View File

@ -1,7 +1,7 @@
import { NostrError } from "../common"
import { RawEvent, parseEvent } from "../event"
import { Conn } from "./conn"
import * as utils from "@noble/curves/abstract/utils";
import * as utils from "@noble/curves/abstract/utils"
import { EventEmitter } from "./emitter"
import { fetchRelayInfo, ReadyState, Relay } from "./relay"
import { Filters } from "../filters"
@ -128,7 +128,8 @@ export class Nostr extends EventEmitter {
if (conn.relay.readyState !== ReadyState.CONNECTING) {
this.#error(
new NostrError(
`bug: expected connection to ${relayUrl.toString()} to have readyState CONNECTING, got ${conn.relay.readyState
`bug: expected connection to ${relayUrl.toString()} to have readyState CONNECTING, got ${
conn.relay.readyState
}`
)
)

View File

@ -1,6 +1,6 @@
import * as secp from "@noble/curves/secp256k1"
import * as utils from "@noble/curves/abstract/utils";
import {sha256 as sha} from "@noble/hashes/sha256";
import * as utils from "@noble/curves/abstract/utils"
import { sha256 as sha } from "@noble/hashes/sha256"
import base64 from "base64-js"
import { bech32 } from "bech32"
@ -92,11 +92,7 @@ export function schnorrSign(data: Hex, priv: PrivateKey): Hex {
/**
* Verify that the elliptic curve signature is correct.
*/
export function schnorrVerify(
sig: Hex,
data: Hex,
key: PublicKey
): boolean {
export function schnorrVerify(sig: Hex, data: Hex, key: PublicKey): boolean {
return secp.schnorr.verify(sig.toString(), data.toString(), key.toString())
}

View File

@ -159,14 +159,14 @@ export async function signEvent<T extends RawEvent>(
* Parse an event from its raw format.
*/
export function parseEvent(event: RawEvent): Event {
if (event.id !== (serializeEventId(event))) {
if (event.id !== serializeEventId(event)) {
throw new NostrError(
`invalid id ${event.id} for event ${JSON.stringify(
event
)}, expected ${serializeEventId(event)}`
)
}
if (!(schnorrVerify(event.sig, event.id, event.pubkey))) {
if (!schnorrVerify(event.sig, event.id, event.pubkey)) {
throw new NostrError(`invalid signature for event ${JSON.stringify(event)}`)
}
@ -221,9 +221,7 @@ export function parseEvent(event: RawEvent): Event {
}
}
function serializeEventId(
event: UnsignedWithPubkey<RawEvent>
): EventId {
function serializeEventId(event: UnsignedWithPubkey<RawEvent>): EventId {
const serialized = JSON.stringify([
0,
event.pubkey,

View File

@ -1,6 +1,6 @@
const fs = require("fs")
const isProduction = process.env.NODE_ENV == "production";
const isProduction = process.env.NODE_ENV == "production"
const entry = {
lib: "./src/index.ts",

View File

@ -1,8 +1,6 @@
/**
* Regex to match email address
*/
export const EmailRegex =
// eslint-disable-next-line no-useless-escape
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

View File

@ -1,4 +1,3 @@
declare module "light-bolt11-decoder" {
export function decode(pr?: string): ParsedInvoice;

View File

@ -38,7 +38,7 @@ export abstract class FeedCache<TCached> {
}
async preload() {
const keys = await this.table?.toCollection().primaryKeys() ?? [];
const keys = (await this.table?.toCollection().primaryKeys()) ?? [];
this.onTable = new Set<string>(keys.map(a => a as string));
}

View File

@ -1,4 +1,3 @@
import { bytesToHex } from "@noble/hashes/utils";
import { decode as invoiceDecode } from "light-bolt11-decoder";
@ -34,8 +33,16 @@ export function decodeInvoice(pr: string): InvoiceDetails | undefined {
expire: timestamp && expire ? timestamp + expire : undefined,
timestamp: timestamp,
description: descriptionSection as string | undefined,
descriptionHash: descriptionHashSection ? (typeof descriptionHashSection === "string" ? descriptionHashSection as string : bytesToHex(descriptionHashSection as Uint8Array)) : undefined,
paymentHash: paymentHashSection ? (typeof paymentHashSection === "string" ? paymentHashSection as string : bytesToHex(paymentHashSection as Uint8Array)) : undefined,
descriptionHash: descriptionHashSection
? typeof descriptionHashSection === "string"
? (descriptionHashSection as string)
: bytesToHex(descriptionHashSection as Uint8Array)
: undefined,
paymentHash: paymentHashSection
? typeof paymentHashSection === "string"
? (paymentHashSection as string)
: bytesToHex(paymentHashSection as Uint8Array)
: undefined,
expired: false,
};
if (ret.expire) {

View File

@ -205,26 +205,26 @@ export class LNURL {
}
export interface LNURLService {
tag: string
nostrPubkey?: string
minSendable?: number
maxSendable?: number
metadata: string
callback: string
commentAllowed?: number
tag: string;
nostrPubkey?: string;
minSendable?: number;
maxSendable?: number;
metadata: string;
callback: string;
commentAllowed?: number;
}
export interface LNURLStatus {
status: "SUCCESS" | "ERROR"
reason?: string
status: "SUCCESS" | "ERROR";
reason?: string;
}
export interface LNURLInvoice extends LNURLStatus {
pr?: string
successAction?: LNURLSuccessAction
pr?: string;
successAction?: LNURLSuccessAction;
}
export interface LNURLSuccessAction {
description?: string
url?: string
description?: string;
url?: string;
}

View File

@ -71,7 +71,10 @@ export function countMembers(a: any) {
return ret;
}
export function equalProp(a: string | number | Array<string | number> | undefined, b: string | number | Array<string | number> | undefined) {
export function equalProp(
a: string | number | Array<string | number> | undefined,
b: string | number | Array<string | number> | undefined
) {
if ((a !== undefined && b === undefined) || (a === undefined && b !== undefined)) {
return false;
}
@ -130,7 +133,7 @@ export function appendDedupe<T>(a?: Array<T>, b?: Array<T>) {
export const sha256 = (str: string | Uint8Array): string => {
return utils.bytesToHex(sha2(str));
}
};
export function getPublicKey(privKey: string) {
return utils.bytesToHex(secp.schnorr.getPublicKey(privKey));

View File

@ -3,39 +3,34 @@
React hooks for @snort/system
Sample:
```js
import { useMemo } from "react"
import { useMemo } from "react";
import { useRequestBuilder, useUserProfile } from "@snort/system-react";
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system"
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system";
// singleton nostr system class
const System = new NostrSystem({});
// some bootstrap relays
[
"wss://relay.snort.social",
"wss://nos.lol"
].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
export function Note({ ev }: { ev: TaggedRawEvent }) {
// get profile from cache or request a profile from relays
const profile = useUserProfile(System, ev.pubkey);
return <div>
return (
<div>
Post by: {profile.name ?? profile.display_name}
<p>
{ev.content}
</p>
<p>{ev.content}</p>
</div>
);
}
export function UserPosts(props: { pubkey: string }) {
const sub = useMemo(() => {
const rb = new RequestBuilder("get-posts");
rb.withFilter()
.authors([props.pubkey])
.kinds([1])
.limit(10);
rb.withFilter().authors([props.pubkey]).kinds([1]).limit(10);
return rb;
}, [props.pubkey]);
@ -43,14 +38,14 @@ export function UserPosts(props: { pubkey: string }) {
const data = useRequestBuilder < FlatNoteStore > (System, FlatNoteStore, sub);
return (
<>
{data.data.map(a => <Note ev={a} />)}
{data.data.map(a => (
<Note ev={a} />
))}
</>
)
);
}
export function MyApp() {
return (
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
)
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
}
```

View File

@ -1,34 +1,28 @@
import { useMemo } from "react"
import { useMemo } from "react";
import { useRequestBuilder, useUserProfile } from "../src";
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system"
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system";
const System = new NostrSystem({});
// some bootstrap relays
[
"wss://relay.snort.social",
"wss://nos.lol"
].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
export function Note({ ev }: { ev: TaggedRawEvent }) {
const profile = useUserProfile(System, ev.pubkey);
return <div>
return (
<div>
Post by: {profile.name ?? profile.display_name}
<p>
{ev.content}
</p>
<p>{ev.content}</p>
</div>
);
}
export function UserPosts(props: { pubkey: string }) {
const sub = useMemo(() => {
const rb = new RequestBuilder("get-posts");
rb.withFilter()
.authors([props.pubkey])
.kinds([1])
.limit(10);
rb.withFilter().authors([props.pubkey]).kinds([1]).limit(10);
return rb;
}, [props.pubkey]);
@ -36,13 +30,13 @@ export function UserPosts(props: { pubkey: string }) {
const data = useRequestBuilder<FlatNoteStore>(System, FlatNoteStore, sub);
return (
<>
{data.data.map(a => <Note ev={a} />)}
{data.data.map(a => (
<Note ev={a} />
))}
</>
)
);
}
export function MyApp() {
return (
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
)
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
}

View File

@ -16,7 +16,7 @@ export function useUserProfile(system: NostrSystem, pubKey?: HexKey): MetadataCa
if (pubKey) {
system.ProfileLoader.UntrackMetadata(pubKey);
}
}
};
},
() => system.ProfileLoader.Cache.getFromCache(pubKey)
);

View File

@ -3,6 +3,7 @@
A collection of caching and querying techniquies used by https://snort.social to serve all content from the nostr protocol.
Simple example:
```js
import {
NostrSystem,

View File

@ -1,4 +1,4 @@
import { NostrSystem, EventPublisher, UserRelaysCache, RequestBuilder, FlatNoteStore, StoreSnapshot } from "../src"
import { NostrSystem, EventPublisher, UserRelaysCache, RequestBuilder, FlatNoteStore, StoreSnapshot } from "../src";
// Provided in-memory / indexedDb cache for relays
// You can also implement your own with "RelayCache" interface
@ -10,12 +10,12 @@ const AuthHandler = async (challenge: string, relay: string) => {
if (pub) {
return await pub.nip42Auth(challenge, relay);
}
}
};
// Singleton instance to store all connections and access query fetching system
const System = new NostrSystem({
relayCache: RelaysCache,
authHandler: AuthHandler // can be left undefined if you dont care about NIP-42 Auth
authHandler: AuthHandler, // can be left undefined if you dont care about NIP-42 Auth
});
(async () => {
@ -34,7 +34,7 @@ const System = new NostrSystem({
// basic usage using "onEvent", fired for every event added to the store
q.onEvent = (sub, e) => {
console.debug(sub, e);
}
};
// Hookable type using change notification, limited to every 500ms
const release = q.feed.hook(() => {
@ -43,7 +43,7 @@ const System = new NostrSystem({
const state = q.feed.snapshot as StoreSnapshot<ReturnType<FlatNoteStore["getSnapshotData"]>>;
// do something with snapshot of store
console.log(`We have ${state.data.length} events now!`)
console.log(`We have ${state.data.length} events now!`);
});
// release the hook when its not needed anymore

View File

@ -9,7 +9,7 @@ const STORES = {
users: "++pubkey, name, display_name, picture, nip05, npub",
relayMetrics: "++addr",
userRelays: "++pubkey",
events: "++id, pubkey, created_at"
events: "++id, pubkey, created_at",
};
export class SnortSystemDb extends Dexie {

View File

@ -421,7 +421,12 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
const lastActivity = unixNowMs() - this.#activity;
if (lastActivity > 30_000 && !this.IsClosed) {
if (this.ActiveRequests.size > 0) {
this.#log("%s Inactive connection has %d active requests! %O", this.Address, this.ActiveRequests.size, this.ActiveRequests);
this.#log(
"%s Inactive connection has %d active requests! %O",
this.Address,
this.ActiveRequests.size,
this.ActiveRequests
);
} else {
this.Close();
}

View File

@ -9,7 +9,6 @@ export const DefaultConnectTimeout = 2000;
// eslint-disable-next-line no-useless-escape
export const HashtagRegex = /(#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+)/g;
/**
* How long profile cache should be considered valid for
*/

View File

@ -6,17 +6,17 @@ import { EventKind, HexKey, NostrEvent } from ".";
import { Nip4WebCryptoEncryptor } from "./impl/nip4";
export interface Tag {
key: string
value?: string
relay?: string
marker?: string // NIP-10
key: string;
value?: string;
relay?: string;
marker?: string; // NIP-10
}
export interface Thread {
root?: Tag
replyTo?: Tag
mentions: Array<Tag>
pubKeys: Array<HexKey>
root?: Tag;
replyTo?: Tag;
mentions: Array<Tag>;
pubKeys: Array<HexKey>;
}
export abstract class EventExt {
@ -41,7 +41,7 @@ export abstract class EventExt {
const sig = secp.schnorr.sign(e.id, key);
e.sig = utils.bytesToHex(sig);
if (!(secp.schnorr.verify(e.sig, e.id, e.pubkey))) {
if (!secp.schnorr.verify(e.sig, e.id, e.pubkey)) {
throw new Error("Signing failed");
}
}
@ -84,12 +84,12 @@ export abstract class EventExt {
static parseTag(tag: Array<string>) {
if (tag.length < 1) {
throw new Error("Invalid tag, must have more than 2 items")
throw new Error("Invalid tag, must have more than 2 items");
}
const ret = {
key: tag[0],
value: tag[1]
value: tag[1],
} as Tag;
switch (ret.key) {
case "e": {

View File

@ -95,7 +95,7 @@ export class EventPublisher {
* Create an EventPublisher for a private key
*/
static privateKey(privateKey: string) {
const signer = new PrivateKeySigner(privateKey)
const signer = new PrivateKeySigner(privateKey);
return new EventPublisher(signer, signer.getPubKey());
}

View File

@ -1,6 +1,7 @@
import { ReqFilter, UsersRelays } from ".";
import { unwrap } from "@snort/shared";
import { dedupe, unwrap } from "@snort/shared";
import debug from "debug";
import { FlatReqFilter } from "request-expander";
const PickNRelays = 2;
@ -9,6 +10,11 @@ export interface RelayTaggedFilter {
filter: ReqFilter;
}
export interface RelayTaggedFlatFilters {
relay: string;
filters: Array<FlatReqFilter>;
}
export interface RelayTaggedFilters {
relay: string;
filters: Array<ReqFilter>;
@ -43,11 +49,10 @@ export function splitAllByWriteRelays(cache: RelayCache, filters: Array<ReqFilte
/**
* Split filters by authors
* @param filter
* @returns
*/
export function splitByWriteRelays(cache: RelayCache, filter: ReqFilter): Array<RelayTaggedFilter> {
if ((filter.authors?.length ?? 0) === 0) {
const authors = filter.authors;
if ((authors?.length ?? 0) === 0) {
return [
{
relay: "",
@ -56,10 +61,13 @@ export function splitByWriteRelays(cache: RelayCache, filter: ReqFilter): Array<
];
}
const allRelays = unwrap(filter.authors).map(a => {
const allRelays = unwrap(authors).map(a => {
return {
key: a,
relays: cache.getFromCache(a)?.relays?.filter(a => a.settings.write).sort(() => Math.random() < 0.5 ? 1 : -1),
relays: cache
.getFromCache(a)
?.relays?.filter(a => a.settings.write)
.sort(() => (Math.random() < 0.5 ? 1 : -1)),
};
});
@ -83,7 +91,7 @@ export function splitByWriteRelays(cache: RelayCache, filter: ReqFilter): Array<
// <key, relay[]> - pick n top relays
// <relay, key[]> - map keys per relay (for subscription filter)
const userPickedRelays = unwrap(filter.authors).map(k => {
const userPickedRelays = unwrap(authors).map(k => {
// pick top 3 relays for this key
const relaysForKey = topRelays
.filter(([, v]) => v.has(k))
@ -116,3 +124,98 @@ export function splitByWriteRelays(cache: RelayCache, filter: ReqFilter): Array<
debug("GOSSIP")("Picked %o", picked);
return picked;
}
/**
* Split filters by author
*/
export function splitFlatByWriteRelays(cache: RelayCache, input: Array<FlatReqFilter>): Array<RelayTaggedFlatFilters> {
const authors = input.filter(a => a.authors).map(a => unwrap(a.authors));
if (authors.length === 0) {
return [
{
relay: "",
filters: input,
},
];
}
const topRelays = pickTopRelays(cache, authors, PickNRelays);
const pickedRelays = dedupe(topRelays.flatMap(a => a.relays));
const picked = pickedRelays.map(a => {
const keysOnPickedRelay = new Set(userPickedRelays.filter(b => b.relaysForKey.includes(a)).map(b => b.k));
return {
relay: a,
filter: {
...filter,
authors: [...keysOnPickedRelay],
},
} as RelayTaggedFilter;
});
if (missing.length > 0) {
picked.push({
relay: "",
filter: {
...filter,
authors: missing.map(a => a.key),
},
});
}
debug("GOSSIP")("Picked %o", picked);
return picked;
}
/**
* Pick most popular relays for each authors
*/
function pickTopRelays(cache: RelayCache, authors: Array<string>, n: number) {
// map of pubkey -> [write relays]
const allRelays = authors.map(a => {
return {
key: a,
relays: cache
.getFromCache(a)
?.relays?.filter(a => a.settings.write)
.sort(() => (Math.random() < 0.5 ? 1 : -1)),
};
});
const missing = allRelays.filter(a => a.relays === undefined || a.relays.length === 0);
const hasRelays = allRelays.filter(a => a.relays !== undefined && a.relays.length > 0);
// map of relay -> [pubkeys]
const relayUserMap = hasRelays.reduce((acc, v) => {
for (const r of unwrap(v.relays)) {
if (!acc.has(r.url)) {
acc.set(r.url, new Set([v.key]));
} else {
unwrap(acc.get(r.url)).add(v.key);
}
}
return acc;
}, new Map<string, Set<string>>());
// selection algo will just pick relays with the most users
const topRelays = [...relayUserMap.entries()].sort(([, v], [, v1]) => v1.size - v.size);
// <relay, key[]> - count keys per relay
// <key, relay[]> - pick n top relays
// <relay, key[]> - map keys per relay (for subscription filter)
return hasRelays
.map(k => {
// pick top N relays for this key
const relaysForKey = topRelays
.filter(([, v]) => v.has(k.key))
.slice(0, n)
.map(([k]) => k);
return { key: k.key, relays: relaysForKey };
})
.concat(
missing.map(a => {
return {
key: a.key,
relays: [],
};
})
);
}

View File

@ -1,40 +1,39 @@
import { MessageEncryptor } from "index";
import { base64 } from "@scure/base";
import { randomBytes } from '@noble/hashes/utils'
import { streamXOR as xchacha20 } from '@stablelib/xchacha20'
import { randomBytes } from "@noble/hashes/utils";
import { streamXOR as xchacha20 } from "@stablelib/xchacha20";
import { secp256k1 } from "@noble/curves/secp256k1";
import { sha256 } from '@noble/hashes/sha256'
import { sha256 } from "@noble/hashes/sha256";
export enum Nip44Version {
Reserved = 0x00,
XChaCha20 = 0x01
XChaCha20 = 0x01,
}
export class Nip44Encryptor implements MessageEncryptor {
getSharedSecret(privateKey: string, publicKey: string) {
const key = secp256k1.getSharedSecret(privateKey, '02' + publicKey)
const key = secp256k1.getSharedSecret(privateKey, "02" + publicKey);
return sha256(key.slice(1, 33));
}
encryptData(content: string, sharedSecret: Uint8Array) {
const nonce = randomBytes(24)
const plaintext = new TextEncoder().encode(content)
const nonce = randomBytes(24);
const plaintext = new TextEncoder().encode(content);
const ciphertext = xchacha20(sharedSecret, nonce, plaintext, plaintext);
const ctb64 = base64.encode(Uint8Array.from(ciphertext))
const nonceb64 = base64.encode(nonce)
return JSON.stringify({ ciphertext: ctb64, nonce: nonceb64, v: Nip44Version.XChaCha20 })
const ctb64 = base64.encode(Uint8Array.from(ciphertext));
const nonceb64 = base64.encode(nonce);
return JSON.stringify({ ciphertext: ctb64, nonce: nonceb64, v: Nip44Version.XChaCha20 });
}
decryptData(cyphertext: string, sharedSecret: Uint8Array) {
const dt = JSON.parse(cyphertext)
if (dt.v !== 1) throw new Error('NIP44: unknown encryption version')
const dt = JSON.parse(cyphertext);
if (dt.v !== 1) throw new Error("NIP44: unknown encryption version");
const ciphertext = base64.decode(dt.ciphertext)
const nonce = base64.decode(dt.nonce)
const plaintext = xchacha20(sharedSecret, nonce, ciphertext, ciphertext)
const text = new TextDecoder().decode(plaintext)
const ciphertext = base64.decode(dt.ciphertext);
const nonce = base64.decode(dt.nonce);
const plaintext = xchacha20(sharedSecret, nonce, ciphertext, ciphertext);
const text = new TextDecoder().decode(plaintext);
return text;
}
}

View File

@ -12,22 +12,22 @@ import EventKind from "../event-kind";
const NIP46_KIND = 24_133;
interface Nip46Metadata {
name: string
url?: string
description?: string
icons?: Array<string>
name: string;
url?: string;
description?: string;
icons?: Array<string>;
}
interface Nip46Request {
id: string
method: string
params: Array<any>
id: string;
method: string;
params: Array<any>;
}
interface Nip46Response {
id: string
result: any
error: string
id: string;
result: any;
error: string;
}
interface QueueObj {
@ -60,7 +60,7 @@ export class Nip46Signer implements EventSigner {
}
this.#relay = unwrap(u.searchParams.get("relay"));
this.#insideSigner = insideSigner ?? new PrivateKeySigner(secp256k1.utils.randomPrivateKey())
this.#insideSigner = insideSigner ?? new PrivateKeySigner(secp256k1.utils.randomPrivateKey());
}
get relays() {
@ -83,12 +83,19 @@ export class Nip46Signer implements EventSigner {
this.#conn = new Connection(this.#relay, { read: true, write: true });
this.#conn.OnEvent = async (sub, e) => {
await this.#onReply(e);
}
};
this.#conn.OnConnected = async () => {
this.#conn!.QueueReq(["REQ", "reply", {
this.#conn!.QueueReq(
[
"REQ",
"reply",
{
kinds: [NIP46_KIND],
"#p": [this.#localPubkey]
}], () => { });
"#p": [this.#localPubkey],
},
],
() => {}
);
if (isBunker) {
await this.#connect(unwrap(this.#remotePubkey));
@ -96,14 +103,13 @@ export class Nip46Signer implements EventSigner {
} else {
this.#commandQueue.set("connect", {
reject,
resolve
})
}
resolve,
});
}
};
this.#conn.Connect();
this.#didInit = true;
})
});
}
async close() {
@ -161,11 +167,14 @@ export class Nip46Signer implements EventSigner {
this.#log("Recv: %O", reply);
if ("method" in reply && reply.method === "connect") {
this.#remotePubkey = reply.params[0];
await this.#sendCommand({
await this.#sendCommand(
{
id: reply.id,
result: "ack",
error: ""
}, unwrap(this.#remotePubkey));
error: "",
},
unwrap(this.#remotePubkey)
);
id = "connect";
}
const pending = this.#commandQueue.get(id);

View File

@ -57,5 +57,4 @@ export class Nip7Signer implements EventSigner {
}
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).signEvent(ev));
}
}

View File

@ -53,7 +53,7 @@ export interface SystemSnapshot {
}
export interface MessageEncryptor {
getSharedSecret(privateKey: string, publicKey: string): Promise<Uint8Array> | Uint8Array
encryptData(plaintext: string, sharedSecet: Uint8Array): Promise<string> | string
decryptData(cyphertext: string, sharedSecet: Uint8Array): Promise<string> | string
getSharedSecret(privateKey: string, publicKey: string): Promise<Uint8Array> | Uint8Array;
encryptData(plaintext: string, sharedSecet: Uint8Array): Promise<string> | string;
decryptData(cyphertext: string, sharedSecet: Uint8Array): Promise<string> | string;
}

View File

@ -107,4 +107,3 @@ export interface NostrLink {
}
throw new Error("Invalid nostr link");
}

View File

@ -17,7 +17,7 @@ import {
UserRelaysCache,
RelayMetricCache,
db,
UsersRelays
UsersRelays,
} from ".";
/**
@ -67,10 +67,10 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
#relayMetrics: RelayMetricHandler;
constructor(props: {
authHandler?: AuthHandler,
relayCache?: FeedCache<UsersRelays>,
profileCache?: FeedCache<MetadataCache>
relayMetrics?: FeedCache<RelayMetrics>
authHandler?: AuthHandler;
relayCache?: FeedCache<UsersRelays>;
profileCache?: FeedCache<MetadataCache>;
relayMetrics?: FeedCache<RelayMetrics>;
}) {
super();
this.#handleAuth = props.authHandler;
@ -99,11 +99,7 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
*/
async Init() {
db.ready = await db.isAvailable();
const t = [
this.#relayCache.preload(),
this.#profileCache.preload(),
this.#relayMetricsCache.preload()
];
const t = [this.#relayCache.preload(), this.#profileCache.preload(), this.#relayMetricsCache.preload()];
await Promise.all(t);
}
@ -118,8 +114,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
this.#sockets.set(addr, c);
c.OnEvent = (s, e) => this.OnEvent(s, e);
c.OnEose = s => this.OnEndOfStoredEvents(c, s);
c.OnDisconnect = (code) => this.OnRelayDisconnect(c, code);
c.OnConnected = (r) => this.OnRelayConnected(c, r);
c.OnDisconnect = code => this.OnRelayDisconnect(c, code);
c.OnConnected = r => this.OnRelayConnected(c, r);
await c.Connect();
} else {
// update settings if already connected
@ -170,7 +166,7 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
c.OnEvent = (s, e) => this.OnEvent(s, e);
c.OnEose = s => this.OnEndOfStoredEvents(c, s);
c.OnDisconnect = code => this.OnRelayDisconnect(c, code);
c.OnConnected = (r) => this.OnRelayConnected(c, r);
c.OnConnected = r => this.OnRelayConnected(c, r);
await c.Connect();
return c;
}

View File

@ -38,21 +38,21 @@ export type ReqCommand = [cmd: "REQ", id: string, ...filters: Array<ReqFilter>];
* Raw REQ filter object
*/
export interface ReqFilter {
ids?: u256[]
authors?: u256[]
kinds?: number[]
"#e"?: u256[]
"#p"?: u256[]
"#t"?: string[]
"#d"?: string[]
"#r"?: string[]
"#a"?: string[]
"#g"?: string[]
search?: string
since?: number
until?: number
limit?: number
[key: string]: Array<string> | Array<number> | string | number | undefined
ids?: u256[];
authors?: u256[];
kinds?: number[];
"#e"?: u256[];
"#p"?: u256[];
"#t"?: string[];
"#d"?: string[];
"#r"?: string[];
"#a"?: string[];
"#g"?: string[];
search?: string;
since?: number;
until?: number;
limit?: number;
[key: string]: Array<string> | Array<number> | string | number | undefined;
}
/**

View File

@ -249,8 +249,8 @@ export class ReplaceableNoteStore extends HookedNoteStore<Readonly<TaggedRawEven
*/
export class NoteCollection extends KeyedReplaceableNoteStore {
constructor() {
super((e) => {
const legacyReplaceable = [0, 3, 41]
super(e => {
const legacyReplaceable = [0, 3, 41];
if (e.kind >= 30_000 && e.kind < 40_000) {
return `${e.kind}:${e.pubkey}:${findTag(e, "d")}`; // Parameterized replaceable
} else if (e.kind >= 10_000 && e.kind < 20_000) {
@ -263,6 +263,6 @@ export class NoteCollection extends KeyedReplaceableNoteStore {
// unknown kind
return e.id;
}
})
});
}
}

View File

@ -1,13 +1,10 @@
import debug from "debug";
import { unixNowMs, FeedCache } from "@snort/shared";
import { EventKind, HexKey, SystemInterface, TaggedRawEvent, NoteCollection, RequestBuilder } from ".";
import { ProfileCacheExpire } from "./const";
import { mapEventToProfile, MetadataCache } from "./cache";
const MetadataRelays = [
"wss://purplepag.es"
]
const MetadataRelays = ["wss://purplepag.es"];
export class ProfileLoaderService {
#system: SystemInterface;
@ -88,7 +85,8 @@ export class ProfileLoaderService {
.authors([...missing]);
if (this.#missingLastRun.size > 0) {
const fMissing = sub.withFilter()
const fMissing = sub
.withFilter()
.kinds([EventKind.SetMetadata])
.authors([...this.#missingLastRun]);
MetadataRelays.forEach(r => fMissing.relay(r));

View File

@ -3,11 +3,10 @@ import debug from "debug";
import { unixNowMs, unwrap } from "@snort/shared";
import { Connection, ReqFilter, Nips, TaggedRawEvent } from ".";
import { reqFilterEq } from "./utils";
import { NoteStore } from "./note-collection";
import { flatMerge } from "./request-merger";
import { BuiltRawReqFilter } from "./request-builder";
import { expandFilter } from "./request-expander";
import { FlatReqFilter, expandFilter } from "./request-expander";
/**
* Tracing for relay query status
@ -19,6 +18,7 @@ class QueryTrace {
eose?: number;
close?: number;
#wasForceClosed = false;
readonly flatFilters: Array<FlatReqFilter>;
readonly #fnClose: (id: string) => void;
readonly #fnProgress: () => void;
@ -33,6 +33,7 @@ class QueryTrace {
this.start = unixNowMs();
this.#fnClose = fnClose;
this.#fnProgress = fnProgress;
this.flatFilters = filters.flatMap(expandFilter);
}
sentToRelay() {
@ -168,13 +169,7 @@ export class Query implements QueryBase {
}
get flatFilters() {
const f: Array<ReqFilter> = [];
for (const x of this.#tracing.flatMap(a => a.filters)) {
if (!f.some(a => reqFilterEq(a, x))) {
f.push(x);
}
}
return f.flatMap(expandFilter);
return this.#tracing.flatMap(a => a.flatFilters);
}
get feed() {

View File

@ -9,7 +9,5 @@ export class RelayMetricHandler {
this.#cache = cache;
}
onDisconnect(c: Connection, code: number) {
}
onDisconnect(c: Connection, code: number) {}
}

View File

@ -108,10 +108,9 @@ export class RequestBuilder {
const next = this.#builders.flatMap(f => expandFilter(f.filter));
const diff = diffFilters(prev, next);
const ts = (unixNowMs() - start);
const ts = unixNowMs() - start;
this.#log("buildDiff %s %d ms", this.id, ts);
if (diff.changed) {
this.#log(diff);
return splitAllByWriteRelays(relays, diff.added).map(a => {
return {
strategy: RequestStrategy.AuthorsRelays,

View File

@ -1,19 +1,19 @@
import { ReqFilter } from "./nostr";
export interface FlatReqFilter {
keys: number
ids?: string
authors?: string
kinds?: number
"#e"?: string
"#p"?: string
"#t"?: string
"#d"?: string
"#r"?: string
search?: string
since?: number
until?: number
limit?: number
keys: number;
ids?: string;
authors?: string;
kinds?: number;
"#e"?: string;
"#p"?: string;
"#t"?: string;
"#d"?: string;
"#r"?: string;
search?: string;
since?: number;
until?: number;
limit?: number;
}
/**

View File

@ -114,7 +114,7 @@ export function flatMerge(all: Array<FlatReqFilter>): Array<ReqFilter> {
acc[k].push(v);
}
}
})
});
return acc;
}, {} as any) as ReqFilter;
}

View File

@ -28,8 +28,8 @@ export function diffFilters(prev: Array<FlatReqFilter>, next: Array<FlatReqFilte
}
const changed = added.length > 0 || removed.length > 0;
return {
added: changed ? flatMerge(added) : [],
removed: changed ? flatMerge(removed) : [],
added: changed ? added : [],
removed: changed ? removed : [],
changed,
};
}

View File

@ -1,4 +1,3 @@
import { equalProp } from "@snort/shared";
import { FlatReqFilter } from "./request-expander";
import { NostrEvent, ReqFilter } from "./nostr";
@ -11,32 +10,36 @@ export function findTag(e: NostrEvent, tag: string) {
}
export function reqFilterEq(a: FlatReqFilter | ReqFilter, b: FlatReqFilter | ReqFilter): boolean {
return equalProp(a.ids, b.ids)
&& equalProp(a.kinds, b.kinds)
&& equalProp(a.authors, b.authors)
&& equalProp(a.limit, b.limit)
&& equalProp(a.since, b.since)
&& equalProp(a.until, b.until)
&& equalProp(a.search, b.search)
&& equalProp(a["#e"], b["#e"])
&& equalProp(a["#p"], b["#p"])
&& equalProp(a["#t"], b["#t"])
&& equalProp(a["#d"], b["#d"])
&& equalProp(a["#r"], b["#r"]);
return (
equalProp(a.ids, b.ids) &&
equalProp(a.kinds, b.kinds) &&
equalProp(a.authors, b.authors) &&
equalProp(a.limit, b.limit) &&
equalProp(a.since, b.since) &&
equalProp(a.until, b.until) &&
equalProp(a.search, b.search) &&
equalProp(a["#e"], b["#e"]) &&
equalProp(a["#p"], b["#p"]) &&
equalProp(a["#t"], b["#t"]) &&
equalProp(a["#d"], b["#d"]) &&
equalProp(a["#r"], b["#r"])
);
}
export function flatFilterEq(a: FlatReqFilter, b: FlatReqFilter): boolean {
return a.keys === b.keys
&& a.since === b.since
&& a.until === b.until
&& a.limit === b.limit
&& a.search === b.search
&& a.ids === b.ids
&& a.kinds === b.kinds
&& a.authors === b.authors
&& a["#e"] === b["#e"]
&& a["#p"] === b["#p"]
&& a["#t"] === b["#t"]
&& a["#d"] === b["#d"]
&& a["#r"] === b["#r"];
return (
a.keys === b.keys &&
a.since === b.since &&
a.until === b.until &&
a.limit === b.limit &&
a.search === b.search &&
a.ids === b.ids &&
a.kinds === b.kinds &&
a.authors === b.authors &&
a["#e"] === b["#e"] &&
a["#p"] === b["#p"] &&
a["#t"] === b["#t"] &&
a["#d"] === b["#d"] &&
a["#r"] === b["#r"]
);
}

View File

@ -9,54 +9,54 @@ describe("NIP-10", () => {
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
tags: [
["e", "cbf2375078..."],
["e", "977ac5d3b6..."],
["e", "8f99ca1363..."],
]
}
],
};
const b = {
"content": "This is a good point, but your ...",
"id": "434ad4a646...",
content: "This is a good point, but your ...",
id: "434ad4a646...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
tags: [
["e", "cbf2375078..."],
["e", "868187063f..."],
["e", "6834ffc491..."],
]
}
],
};
const c = {
"content": "There is some middle ground ...",
"id": "6834ffc491...",
content: "There is some middle ground ...",
id: "6834ffc491...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
tags: [
["e", "cbf2375078...", "", "root"],
["e", "868187063f...", "", "reply"],
]
}
],
};
expect(EventExt.extractThread(a)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "8f99ca1363...", marker: "reply" },
mentions: [{ key: "e", value: "977ac5d3b6...", marker: "mention" }]
})
mentions: [{ key: "e", value: "977ac5d3b6...", marker: "mention" }],
});
expect(EventExt.extractThread(b)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "6834ffc491...", marker: "reply" },
mentions: [{ key: "e", value: "868187063f...", marker: "mention" }]
})
mentions: [{ key: "e", value: "868187063f...", marker: "mention" }],
});
expect(EventExt.extractThread(c)).toMatchObject({
root: { key: "e", value: "cbf2375078...", relay: "", marker: "root" },
replyTo: { key: "e", value: "868187063f...", relay: "", marker: "reply" },
mentions: []
})
})
})
mentions: [],
});
});
});

View File

@ -1,4 +1,4 @@
import { splitAllByWriteRelays } from "../src/GossipModel"
import { splitAllByWriteRelays } from "../src/GossipModel";
describe("GossipModel", () => {
it("should not output empty", () => {
@ -8,30 +8,26 @@ describe("GossipModel", () => {
return {
pubkey: pk,
created_at: 0,
relays: []
relays: [],
};
}
}
}
const a = [{
"until": 1686651693,
"limit": 200,
"kinds": [
1,
6,
6969
],
"authors": [
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
]
}];
},
};
const a = [
{
until: 1686651693,
limit: 200,
kinds: [1, 6, 6969],
authors: ["3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"],
},
];
const output = splitAllByWriteRelays(Relays, a);
expect(output).toEqual([
{
relay: "",
filters: a
}
])
})
})
filters: a,
},
]);
});
});

View File

@ -1,4 +1,3 @@
import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
import { Nip4WebCryptoEncryptor } from "../src/impl/nip4";
import { Nip44Encryptor } from "../src/impl/nip44";
@ -22,8 +21,8 @@ describe("NIP-04", () => {
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = await dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
})
})
});
});
describe("NIP-44", () => {
it("should encrypt/decrypt", () => {
@ -33,13 +32,13 @@ describe("NIP-44", () => {
const ciphertext = enc.encryptData(msg, sec);
const jObj = JSON.parse(ciphertext);
expect(jObj).toHaveProperty("ciphertext")
expect(jObj).toHaveProperty("nonce")
expect(jObj).toHaveProperty("ciphertext");
expect(jObj).toHaveProperty("nonce");
expect(jObj.v).toBe(1);
const dec = new Nip44Encryptor();
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
})
})
});
});

View File

@ -20,7 +20,7 @@ const DummyCache = {
write: true,
},
},
]
],
};
},
} as RelayCache;
@ -181,7 +181,8 @@ describe("build diff, large follow list", () => {
const start = unixNowMs();
const a = rb.build(DummyCache);
expect(a).toEqual(f.map(a => {
expect(a).toEqual(
f.map(a => {
return {
strategy: RequestStrategy.AuthorsRelays,
relay: `wss://${a}.com/`,
@ -189,15 +190,15 @@ describe("build diff, large follow list", () => {
{
kinds: [1, 6, 10002, 3, 6969],
authors: [a],
}
},
],
}
}));
};
})
);
expect(unixNowMs() - start).toBeLessThan(500);
const start2 = unixNowMs();
const b = rb.buildDiff(DummyCache, rb.buildRaw().flatMap(expandFilter));
expect(b).toEqual([]);
expect(unixNowMs() - start2).toBeLessThan(100);
})
});

View File

@ -108,50 +108,50 @@ describe("flatMerge", () => {
});
});
describe('canMerge', () => {
describe("canMerge", () => {
it("should have 0 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "a",
keys: 1
keys: 1,
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 1 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "b",
keys: 1
keys: 1,
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 10 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "a",
kinds: 1,
keys: 2
keys: 2,
};
expect(canMergeFilters(a, b)).toEqual(false);
});
it("should have 11 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "b",
kinds: 1,
keys: 2
keys: 2,
};
expect(canMergeFilters(a, b)).toEqual(false);
});
@ -160,13 +160,13 @@ describe('canMerge', () => {
since: 1,
until: 100,
kinds: [1],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
const b = {
since: 1,
until: 100,
kinds: [6969],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
expect(canMergeFilters(a, b)).toEqual(true);
});
@ -175,14 +175,14 @@ describe('canMerge', () => {
since: 1,
until: 100,
kinds: [1],
authors: ["f", "kieran", "snort", "c", "d"]
authors: ["f", "kieran", "snort", "c", "d"],
};
const b = {
since: 1,
until: 100,
kinds: [1],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
expect(canMergeFilters(a, b)).toEqual(true);
});
})
});

View File

@ -22,10 +22,7 @@
"depends": []
},
"externalBin": [],
"icon": [
"icons/128x128.png",
"icons/128x128@2x.png"
],
"icon": ["icons/128x128.png", "icons/128x128@2x.png"],
"identifier": "social.snort.app",
"longDescription": "",
"macOS": {