@ -1,13 +1,16 @@
import { relayPool , generatePrivateKey , getPublicKey } from 'nostr-tools' ;
import { relayPool , generatePrivateKey , getPublicKey , signEvent } from 'nostr-tools' ;
import { elem } from './domutil.js' ;
import { elem } from './domutil.js' ;
const pool = relayPool ( ) ;
const pool = relayPool ( ) ;
// pool.addRelay('wss://nostr.x1ddos.ch', {read: true, write: true});
pool . addRelay ( 'wss://nostr.x1ddos.ch' , { read : true , write : true } ) ;
pool . addRelay ( 'wss://nostr.bitcoiner.social/' , { read : true , write : true } ) ;
// pool.addRelay('wss://nostr.bitcoiner.social/', {read: true, write: true});
pool . addRelay ( 'wss://relay.nostr.info' , { read : true , write : true } ) ;
// pool.addRelay('wss://relay.nostr.info', {read: true, write: true});
pool . addRelay ( 'wss://nostr.openchain.fr' , { read : true , write : true } ) ;
// pool.addRelay('wss://nostr.openchain.fr', {read: true, write: true});
pool . addRelay ( 'wss://relay.damus.io' , { read : true , write : true } ) ;
// pool.addRelay('wss://relay.damus.io', {read: true, write: true});
// read only
// pool.addRelay('wss://nostr.rocks', {read: true, write: false});
// pool.addRelay('wss://nostr-relay.wlvs.space', {read: true, write: false});
// pool.addRelay('wss://nostr-relay.untethr.me', {read: true, write: false});
const feedlist = document . querySelector ( '#feedlist' ) ;
const feedlist = document . querySelector ( '#feedlist' ) ;
@ -18,8 +21,9 @@ const dateTime = new Intl.DateTimeFormat(navigator.language, {
const userList = [ ] ;
const userList = [ ] ;
let max = 0 ;
let max = 0 ;
function onEvent ( evt , relay ) {
function onEvent ( evt , relay ) {
if ( max ++ >= 7 ) {
if ( max ++ >= 23 ) {
return subscription . unsub ( ) ;
return subscription . unsub ( ) ;
}
}
switch ( evt . kind ) {
switch ( evt . kind ) {
@ -42,6 +46,8 @@ function onEvent(evt, relay) {
}
}
}
}
const pubkey = localStorage . getItem ( 'pub_key' )
const subscription = pool . sub ( {
const subscription = pool . sub ( {
cb : onEvent ,
cb : onEvent ,
filter : {
filter : {
@ -49,6 +55,7 @@ const subscription = pool.sub({
'52155da703585f25053830ac39863c80ea6adc57b360472c16c566a412d2bc38' , // quark
'52155da703585f25053830ac39863c80ea6adc57b360472c16c566a412d2bc38' , // quark
'a6057742e73ff93b89587c27a74edf2cdab86904291416e90dc98af1c5f70cfa' , // mosc
'a6057742e73ff93b89587c27a74edf2cdab86904291416e90dc98af1c5f70cfa' , // mosc
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d' , // fiatjaf
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d' , // fiatjaf
pubkey , // me
// '32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245' // jb55
// '32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245' // jb55
]
]
}
}
@ -80,7 +87,7 @@ function renderRecommendServer(evt, relay) {
function rendernArticle ( content ) {
function rendernArticle ( content ) {
const art = elem ( 'article' , { className : 'mbox' } , content ) ;
const art = elem ( 'article' , { className : 'mbox' } , content ) ;
feedlist . a ppend( art ) ;
feedlist . pre pend( art ) ;
}
}
function getMetadata ( evt , relay ) {
function getMetadata ( evt , relay ) {
@ -117,8 +124,35 @@ function setMetadata(userList, relay, evt, content) {
}
}
}
}
// settings
// check pool.status
// publish
const publish = document . querySelector ( '#publish' ) ;
publish . addEventListener ( 'click' , async ( ) => {
const pubkey = localStorage . getItem ( 'pub_key' ) ;
const privatekey = localStorage . getItem ( 'private_key' ) ;
if ( ! pubkey || ! privatekey ) {
return console . warn ( 'no pubkey/privatekey' ) ;
}
const newEvent = {
kind : 1 ,
pubkey ,
content : 'geil' ,
tags : [ ] ,
created _at : Math . floor ( Date . now ( ) * 0.001 ) ,
} ;
const sig = await signEvent ( newEvent , privatekey ) ;
const ev = await pool . publish ( { ... newEvent , sig } , ( status , url ) => {
if ( status === 0 ) {
console . log ( ` publish request sent to ${ url } ` )
}
if ( status === 1 ) {
console . log ( ` event published by ${ url } ` , ev )
}
} ) ;
} ) ;
// settings
const form = document . querySelector ( 'form[name="settings"]' ) ;
const form = document . querySelector ( 'form[name="settings"]' ) ;
const privateKeyInput = form . querySelector ( '#privatekey' ) ;
const privateKeyInput = form . querySelector ( '#privatekey' ) ;
const pubKeyInput = form . querySelector ( '#pubkey' ) ;
const pubKeyInput = form . querySelector ( '#pubkey' ) ;
@ -128,33 +162,33 @@ const importBtn = form.querySelector('button[name="import"]');
const privateTgl = form . querySelector ( 'button[name="privatekey-toggle"]' )
const privateTgl = form . querySelector ( 'button[name="privatekey-toggle"]' )
generateBtn . addEventListener ( 'click' , ( ) => {
generateBtn . addEventListener ( 'click' , ( ) => {
const private K ey = generatePrivateKey ( ) ;
const private k ey = generatePrivateKey ( ) ;
const pub Key = getPublicKey ( privateK ey) ;
const pub key = getPublicKey ( privatek ey) ;
if ( validKeys ( private Key, pubK ey) ) {
if ( validKeys ( private key, pubk ey) ) {
privateKeyInput . value = private K ey;
privateKeyInput . value = private k ey;
pubKeyInput . value = pub K ey;
pubKeyInput . value = pub k ey;
statusMessage . textContent = 'private-key created!' ;
statusMessage . textContent = 'private-key created!' ;
statusMessage . hidden = false ;
statusMessage . hidden = false ;
}
}
} ) ;
} ) ;
importBtn . addEventListener ( 'click' , ( ) => {
importBtn . addEventListener ( 'click' , ( ) => {
const private K ey = privateKeyInput . value ;
const private k ey = privateKeyInput . value ;
const pub K ey = pubKeyInput . value ;
const pub k ey = pubKeyInput . value ;
if ( validKeys ( private Key, pubK ey) ) {
if ( validKeys ( private key, pubk ey) ) {
localStorage . setItem ( 'private Key', privateK ey) ;
localStorage . setItem ( 'private _key', privatek ey) ;
localStorage . setItem ( 'pub Key', pubK ey) ;
localStorage . setItem ( 'pub _key', pubk ey) ;
statusMessage . textContent = ' private-key saved in local storage !';
statusMessage . textContent = ' stored private and public key locally !';
statusMessage . hidden = false ;
statusMessage . hidden = false ;
}
}
} ) ;
} ) ;
form . addEventListener ( 'input' , ( ) => validKeys ( privateKeyInput . value , pubKeyInput . value ) ) ;
form . addEventListener ( 'input' , ( ) => validKeys ( privateKeyInput . value , pubKeyInput . value ) ) ;
function validKeys ( private Key, pubK ey) {
function validKeys ( private key, pubk ey) {
if ( pub Key && privateK ey) {
if ( pub key && privatek ey) {
try {
try {
if ( getPublicKey ( private Key) === pubK ey) {
if ( getPublicKey ( private key) === pubk ey) {
statusMessage . hidden = true ;
statusMessage . hidden = true ;
statusMessage . textContent = 'public-key corresponds to private-key' ;
statusMessage . textContent = 'public-key corresponds to private-key' ;
importBtn . removeAttribute ( 'disabled' ) ;
importBtn . removeAttribute ( 'disabled' ) ;
@ -175,5 +209,5 @@ privateTgl.addEventListener('click', () => {
privateKeyInput . type = privateKeyInput . type === 'text' ? 'password' : 'text' ;
privateKeyInput . type = privateKeyInput . type === 'text' ? 'password' : 'text' ;
} ) ;
} ) ;
privateKeyInput . value = localStorage . getItem ( 'private K ey') ;
privateKeyInput . value = localStorage . getItem ( 'private _k ey') ;
pubKeyInput . value = localStorage . getItem ( 'pub K ey') ;
pubKeyInput . value = localStorage . getItem ( 'pub _k ey') ;