-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
svelte demo has feature parity with vanilla-js demoo
- Loading branch information
Showing
8 changed files
with
216 additions
and
90 deletions.
There are no files selected for viewing
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { SimplePool } from "nostr-tools"; | ||
import { get } from 'svelte/store'; | ||
import { finalizeEvent, verifyEvent } from "nostr-tools"; | ||
import { relaySettings, user, events, activeRelays, usub } from './stores/index'; | ||
import { verifyPow } from './utils.js'; | ||
|
||
let pubs; | ||
|
||
export const pool = new SimplePool(); | ||
|
||
export const publishEvent = async (ev) => { | ||
console.log(ev) | ||
const pow = verifyPow(ev); | ||
console.log('Publishing event:', ev); | ||
try { | ||
const {isAnon, secret} = get(user); | ||
if(isAnon) { | ||
ev = finalizeEvent(ev, secret); | ||
} | ||
else { | ||
ev = await window.nostr.signEvent(ev) | ||
} | ||
let isGood = verifyEvent(ev); | ||
if (!isGood) throw new Error('Event is not valid'); | ||
const currentActiveRelays = get(activeRelays); | ||
pubs = pool.publish(currentActiveRelays, ev); | ||
await Promise.allSettled(pubs); | ||
console.log('Event published successfully.'); | ||
} catch (error) { | ||
console.error('Error publishing event:', error); | ||
} | ||
}; | ||
|
||
function setMyRelays(relays) { | ||
console.log(`Setting my relays: ${relays}`); | ||
relaySettings.update(r => ({ | ||
...r, | ||
myRelays: Array.from(new Set([...r.myRelays, ...relays])) | ||
})); | ||
} | ||
|
||
export function onK0(event){ | ||
let profile | ||
try { | ||
profile = JSON.parse(event.content) | ||
let photo | ||
if(profile?.photo) photo = profile.photo | ||
else if(profile?.picture) photo = profile.picture | ||
else if(profile?.avatar) photo = profile.avatar | ||
user.update( u => ({...u, photo, name: profile.name }) ) | ||
} | ||
catch(e){ | ||
console.error('Error parsing K0 content:', e) | ||
} | ||
console.log('K0 profile:', profile) | ||
events.update( e => ({...e, k0: event}) ) | ||
} | ||
|
||
export function onK3(event){ | ||
let relays = [] | ||
try{ | ||
relays = Object.keys(JSON.parse(event.content)) | ||
} | ||
catch(e){ | ||
console.error('Error parsing K3 content:', e) | ||
console.warn('K3 content:', event.content) | ||
} | ||
|
||
console.log('K3 relays:', relays) | ||
setMyRelays(relays) | ||
events.update( e => ({...e, k3: event}) ) | ||
} | ||
|
||
export function onK10002(event){ | ||
const relays = event.tags.filter( t => t[0] === 'r' ).map( r => r[1] ) | ||
console.log('K10002 relays:', relays) | ||
setMyRelays(relays?.length? relays : []) | ||
events.update( e => ({...e, k10002: event}) ) | ||
} | ||
|
||
export function onevent(event){ | ||
switch(event.kind){ | ||
case 0: return onK0(event) | ||
case 3: return onK3(event) | ||
case 10002: return onK10002(event) | ||
} | ||
} | ||
|
||
export function oneose(){ | ||
try { | ||
const _usub = get(usub) | ||
_usub.close() | ||
} | ||
catch(e){ | ||
console.warn('Error closing subscription:', e) | ||
} | ||
} | ||
|
||
export function onclose( resolve ){ | ||
user.update( u => ({...u, isAnon: false}) ) | ||
resolve() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { user } from './user'; | ||
export { miningState } from './mining-state'; | ||
export { relaySettings } from './relay-settings'; | ||
export { contentState } from './content-state'; | ||
export * from './user'; | ||
export * from './mining-state'; | ||
export * from './relay-settings'; | ||
export * from './content-state'; | ||
export * from './relays'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { derived, readable, writable } from "svelte/store"; | ||
import { relaySettings } from "./relay-settings"; | ||
|
||
const POW_RELAYS = [ | ||
'wss://nostr.bitcoiner.social', | ||
'wss://nostr.mom', | ||
'wss://nos.lol', | ||
'wss://powrelay.xyz', | ||
'wss://labour.fiatjaf.com/', | ||
'wss://nostr.lu.ke', | ||
'wss://140.f7z.io' | ||
] | ||
|
||
export const usub = new writable(null) | ||
|
||
export const activeRelays = derived( | ||
relaySettings, | ||
($relaySettings) => { | ||
let relays = []; | ||
if ($relaySettings.myRelays && $relaySettings.myRelays.length > 0) { | ||
relays.push(...$relaySettings.myRelays); | ||
} | ||
if ($relaySettings.powRelaysEnabled) { | ||
relays.push(...POW_RELAYS); | ||
} | ||
return relays; | ||
} | ||
); | ||
|
||
export const powRelays = new readable(POW_RELAYS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { getEventHash } from "nostr-tools"; | ||
|
||
export const getPow = (hex) => { | ||
let count = 0; | ||
|
||
for (let i = 0; i < hex.length; i++) { | ||
const nibble = parseInt(hex[i], 16); | ||
if (nibble === 0) { | ||
count += 4; | ||
} else { | ||
count += Math.clz32(nibble) - 28; | ||
break; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
export const verifyPow = (event) => { | ||
console.log(event) | ||
const hash = getEventHash(event); | ||
const count = getPow(hash); | ||
const nonceTag = event.tags.find(tag => tag[0] === 'nonce'); | ||
if (!nonceTag || nonceTag.length < 3) { | ||
return 0; | ||
} | ||
const targetDifficulty = parseInt(nonceTag[2], 10); | ||
return Math.min(count, targetDifficulty); | ||
} |