Skip to content

Commit

Permalink
snort/system
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Sep 15, 2023
1 parent 3ca21fb commit 5104448
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@fontsource/lato": "^5.0.11",
"@heroicons/react": "^2.0.18",
"@noble/hashes": "1.3.1",
"@snort/system": "^1.0.21",
"@types/lodash": "^4.14.197",
"bech32": "^2.0.0",
"classnames": "^2.3.2",
Expand Down
9 changes: 5 additions & 4 deletions src/js/nostr/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EventDB from '@/nostr/EventDB';
import getRelayPool from '@/nostr/relayPool';

import Events from '../nostr/Events';
import Snort from '../nostr/SnortSystem';
import localState from '../state/LocalState.ts';

import IndexedDB from './IndexedDB';
Expand Down Expand Up @@ -43,8 +44,6 @@ const PubSub = {
subscribe(

Check failure on line 44 in src/js/nostr/PubSub.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎····filter:·Filter,⏎····callback?:·(event:·Event)·=>·void,⏎··` with `filter:·Filter,·callback?:·(event:·Event)·=>·void`

Check failure on line 44 in src/js/nostr/PubSub.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎····filter:·Filter,⏎····callback?:·(event:·Event)·=>·void,⏎··` with `filter:·Filter,·callback?:·(event:·Event)·=>·void`
filter: Filter,
callback?: (event: Event) => void,
sinceLastOpened = false,
mergeSubscriptions = true,
): Unsubscribe {
let currentSubscriptionId;
if (callback) {
Expand All @@ -67,10 +66,12 @@ const PubSub = {
});
}

const unsubRelays = this.subscribeRelayPool(filter, sinceLastOpened, mergeSubscriptions);
Snort.subscribe(filter, (event) => {
console.log('snort returned', event);
Events.handle(event);
});

return () => {
unsubRelays?.();
if (currentSubscriptionId) {
this.subscriptions.delete(currentSubscriptionId);
}
Expand Down
52 changes: 52 additions & 0 deletions src/js/nostr/SnortSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
EventPublisher,
NoopStore,
NostrSystem,
RequestBuilder,
UserRelaysCache,
} from '@snort/system';
import { Event, Filter } from 'nostr-tools';

// Provided in-memory / indexedDb cache for relays
// You can also implement your own with "RelayCache" interface
const RelaysCache = new UserRelaysCache();

// example auth handler using NIP-07
const AuthHandler = async (challenge: string, relay: string) => {
const pub = await EventPublisher.nip7();
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
});

const connect = System.ConnectToRelay('wss://relay.snort.social', { read: true, write: false });

const publish = async (event: Event) => {

Check failure on line 30 in src/js/nostr/SnortSystem.ts

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used

Check failure on line 30 in src/js/nostr/SnortSystem.ts

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used
await connect;
};

const subscribe = async (filter: Filter, callback: (event: Event) => void) => {
await connect;
const rb = new RequestBuilder('no id');

rb.withBareFilter(filter);

const q = System.Query<NoopStore>(NoopStore, rb);
// basic usage using "onEvent", fired for every event added to the store
q.feed.onEvent((events) => {
events.forEach((event) => {
callback(event);
});
});
};

export default {
publish,
subscribe,
};
97 changes: 94 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,14 @@
dependencies:
"@noble/hashes" "1.3.1"

"@noble/[email protected]", "@noble/hashes@~1.3.0", "@noble/hashes@~1.3.1":
"@noble/curves@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
dependencies:
"@noble/hashes" "1.3.2"

"@noble/[email protected]", "@noble/[email protected]", "@noble/hashes@^1.3.2", "@noble/hashes@~1.3.0", "@noble/hashes@~1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==
Expand Down Expand Up @@ -1378,7 +1385,7 @@
estree-walker "^2.0.1"
picomatch "^2.2.2"

"@scure/[email protected]", "@scure/base@~1.1.0":
"@scure/[email protected]", "@scure/base@^1.1.2", "@scure/base@~1.1.0":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==
Expand All @@ -1405,6 +1412,68 @@
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
integrity sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==

"@snort/shared@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@snort/shared/-/shared-1.0.6.tgz#52a40b6c25a5bfcd54a1a150a2a836fa6b37250c"
integrity sha512-p4BRIALNTV7truHGaF+rKoLF5OB2QY1EZ+NPO5iccDdLhQxPSloIkrFxk5Le+yuX2AYtNd8Z/HTxViU8reYCXA==
dependencies:
"@noble/curves" "^1.2.0"
"@noble/hashes" "^1.3.2"
"@scure/base" "^1.1.2"
debug "^4.3.4"
dexie "^3.2.4"
light-bolt11-decoder "^3.0.0"

"@snort/system@^1.0.21":
version "1.0.21"
resolved "https://registry.yarnpkg.com/@snort/system/-/system-1.0.21.tgz#b158bf631101f8af18528c6224c1edc58e51709a"
integrity sha512-hXv2ZvHded1Ot7s6MQarC7GQ/jHVT4sSQIHrOTq4vsqlGmT56bx+KP33Rdw6//k+YhyljmAUMYSoYtDl17LjLg==
dependencies:
"@noble/curves" "^1.2.0"
"@noble/hashes" "^1.3.2"
"@scure/base" "^1.1.2"
"@snort/shared" "^1.0.6"
"@stablelib/xchacha20" "^1.0.1"
debug "^4.3.4"
dexie "^3.2.4"
isomorphic-ws "^5.0.0"
uuid "^9.0.0"
ws "^8.14.0"

"@stablelib/binary@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/binary/-/binary-1.0.1.tgz#c5900b94368baf00f811da5bdb1610963dfddf7f"
integrity sha512-ClJWvmL6UBM/wjkvv/7m5VP3GMr9t0osr4yVgLZsLCOz4hGN9gIAFEqnJ0TsSMAN+n840nf2cHZnA5/KFqHC7Q==
dependencies:
"@stablelib/int" "^1.0.1"

"@stablelib/chacha@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/chacha/-/chacha-1.0.1.tgz#deccfac95083e30600c3f92803a3a1a4fa761371"
integrity sha512-Pmlrswzr0pBzDofdFuVe1q7KdsHKhhU24e8gkEwnTGOmlC7PADzLVxGdn2PoNVBBabdg0l/IfLKg6sHAbTQugg==
dependencies:
"@stablelib/binary" "^1.0.1"
"@stablelib/wipe" "^1.0.1"

"@stablelib/int@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/int/-/int-1.0.1.tgz#75928cc25d59d73d75ae361f02128588c15fd008"
integrity sha512-byr69X/sDtDiIjIV6m4roLVWnNNlRGzsvxw+agj8CIEazqWGOQp2dTYgQhtyVXV9wpO6WyXRQUzLV/JRNumT2w==

"@stablelib/wipe@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/wipe/-/wipe-1.0.1.tgz#d21401f1d59ade56a62e139462a97f104ed19a36"
integrity sha512-WfqfX/eXGiAd3RJe4VU2snh/ZPwtSjLG4ynQ/vYzvghTh7dHFcI1wl+nrkWG6lGhukOxOsUHfv8dUXr58D0ayg==

"@stablelib/xchacha20@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/xchacha20/-/xchacha20-1.0.1.tgz#e98808d1f7d8b20e3ff37c71a3062a2a955d9a8c"
integrity sha512-1YkiZnFF4veUwBVhDnDYwo6EHeKzQK4FnLiO7ezCl/zu64uG0bCCAUROJaBkaLH+5BEsO3W7BTXTguMbSLlWSw==
dependencies:
"@stablelib/binary" "^1.0.1"
"@stablelib/chacha" "^1.0.1"
"@stablelib/wipe" "^1.0.1"

"@surma/rollup-plugin-off-main-thread@^2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@surma/rollup-plugin-off-main-thread/-/rollup-plugin-off-main-thread-2.2.3.tgz#ee34985952ca21558ab0d952f00298ad2190c053"
Expand Down Expand Up @@ -3977,7 +4046,19 @@ nostr-relaypool@^0.6.28:
nostr-tools "^1.10.0"
safe-stable-stringify "^2.4.2"

nostr-tools@^1.10.0, nostr-tools@^1.14.2:
nostr-tools@^1.10.0:
version "1.15.0"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.15.0.tgz#92e487654d1f6994923176aedf3b27fb617f7a78"
integrity sha512-Dh7LVAUqaSiSs61QddsWluLVWpMwyMGaVlbhDYEy03ZwnBBzm10pz+mQZSdVV88/B3a5843gHZ4dIBUeS5upoA==
dependencies:
"@noble/ciphers" "^0.2.0"
"@noble/curves" "1.1.0"
"@noble/hashes" "1.3.1"
"@scure/base" "1.1.1"
"@scure/bip32" "1.3.1"
"@scure/bip39" "1.2.1"

nostr-tools@^1.14.2:
version "1.14.2"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.14.2.tgz#161c9401467725e87c07fcf1c9924d31b12fd45c"
integrity sha512-QEe8+tMDW0632eNDcQ+EG1edmsCXLV4WPiWLDcdT3uoE+GM15pVcy18sKwbN7SmgO4GKFEqQ49k45eANC6++SQ==
Expand Down Expand Up @@ -5229,6 +5310,11 @@ util-deprecate@^1.0.2:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

uuid@^9.0.0:
version "9.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

v8-to-istanbul@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-9.1.0.tgz#1b83ed4e397f58c85c266a570fc2558b5feb9265"
Expand Down Expand Up @@ -5590,6 +5676,11 @@ ws@^8.13.0:
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==

ws@^8.14.0:
version "8.14.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.14.1.tgz#4b9586b4f70f9e6534c7bb1d3dc0baa8b8cf01e0"
integrity sha512-4OOseMUq8AzRBI/7SLMUwO+FEDnguetSk7KMb1sHwvF2w2Wv5Hoj0nlifx8vtGsftE/jWHojPy8sMMzYLJ2G/A==

xml-name-validator@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-4.0.0.tgz#79a006e2e63149a8600f15430f0a4725d1524835"
Expand Down

0 comments on commit 5104448

Please sign in to comment.