fix(Nostr Public Post): Posting to multiple Nostr relays

This commit is contained in:
Chris Troutner
2025-08-04 12:18:49 -07:00
parent 880a07819c
commit e547c97a44
2 changed files with 26 additions and 13 deletions
@@ -1,5 +1,5 @@
/* /*
Component for posting nostr information on kind 1. Component for posting nostr information on kind 1.
*/ */
// Global npm libraries // Global npm libraries
@@ -10,6 +10,9 @@ import { finalizeEvent } from 'nostr-tools/pure'
import { Relay } from 'nostr-tools/relay' import { Relay } from 'nostr-tools/relay'
import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency
// Local libraries
import config from '../../../../config'
function PublicPost (props) { function PublicPost (props) {
const [accordionKey, setAccordionKey] = useState('0') const [accordionKey, setAccordionKey] = useState('0')
const [onFetch, setOnFetch] = useState(false) const [onFetch, setOnFetch] = useState(false)
@@ -44,9 +47,6 @@ function PublicPost (props) {
// Convert private key to binary // Convert private key to binary
const privateKeyBin = hexToBytes(nostrKeyPair.privHex) const privateKeyBin = hexToBytes(nostrKeyPair.privHex)
// Relay list
const psf = 'wss://nostr-relay.psfoundation.info'
// BCH address of the user. // BCH address of the user.
const bchAddr = bchWalletState.address const bchAddr = bchWalletState.address
@@ -63,16 +63,24 @@ function PublicPost (props) {
const signedEvent = finalizeEvent(eventTemplate, privateKeyBin) const signedEvent = finalizeEvent(eventTemplate, privateKeyBin)
console.log('signedEvent: ', signedEvent) console.log('signedEvent: ', signedEvent)
// Connect to a relay. // Publish the post to each relay.
const relay = await Relay.connect(psf) config.nostrRelays.map(async (relayUrl) => {
console.log(`connected to ${relay.url}`) try {
// Connect to a relay.
const relay = await Relay.connect(relayUrl)
console.log(`connected to ${relay.url}`)
// Publish the message to the relay. // Publish the message to the relay.
const result = await relay.publish(signedEvent) const result = await relay.publish(signedEvent)
console.log('result: ', result) console.log('result: ', result)
// Close the connection to the relay.
relay.close()
} catch (err) {
console.warn(`Skipping publishing to ${relayUrl} due to error: ${err}`)
}
})
// Close the connection to the relay.
relay.close()
resetForm() resetForm()
setSuccessMsg('Post successfully published!') setSuccessMsg('Post successfully published!')
setOnFetch(false) setOnFetch(false)
+6 -1
View File
@@ -18,7 +18,12 @@ const config = {
// dexServer: 'http://localhost:5700', // dexServer: 'http://localhost:5700',
nostrTopic: 'bch-dex-test-topic-02', nostrTopic: 'bch-dex-test-topic-02',
nostrRelay: 'wss://nostr-relay.psfoundation.info' nostrRelay: 'wss://nostr-relay.psfoundation.info',
nostrRelays: [
'wss://nostr-relay.psfoundation.info',
'wss://nos.lol',
'wss://relay.damus.io'
]
} }