nostr-oer-client/send.js
2024-08-12 19:38:09 +02:00

39 lines
1.1 KiB
JavaScript

import { bytesToHex, hexToBytes } from '@noble/hashes/utils' // already an installed dependency
import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure'
import { Relay } from 'nostr-tools/relay'
import WebSocket from 'ws';
// Provide WebSocket implementation to nostr-tools
global.WebSocket = WebSocket;
// Generate keys (or use existing ones)
//let secretKey = generateSecretKey()
//let pk = getPublicKey(secretKey)
//let sk = bytesToHex(secretKey)
let sk = 'e8913a0b983c8678e3cce83a45ace14f6656bd06ff98803a38d72a5567e7e105';
// Create event template
let eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'Hello world! Welcome to SC24!',
}
// Finalize and sign the event
const signedEvent = finalizeEvent(eventTemplate, sk)
// Connect to a relay and publish
async function publishMessage() {
const relay = await Relay.connect('ws://localhost:7000')
console.log(`connected to ${relay.url}`)
await relay.publish(signedEvent)
console.log('Event published!')
relay.close()
}
publishMessage()