|
// Use a standard NoStr client, but this is not required because the NoStr event is super easy to construct |
|
import { generateSecretKey, getPublicKey, finalizeEvent, verifyEvent } from "nostr-tools/pure"; |
|
import { bytesToHex, hexToBytes } from "@noble/hashes/utils"; |
|
|
|
// Hardcode sender SECP256k1 private key, each sender should has different unique private key as identity |
|
const senderSecretKey = hexToBytes("c885648cc3e4c94fe00b74111247d15ebe35640f7973d8b9f839ced49e3706d5") // generateSecretKey() |
|
|
|
// Hardcode recipient SECP256k1 public key, so you can subscribe new messages by the recipient |
|
const recipient = "6f7bb11c04d792784c9dfcb4246e9afc0d6a7eae549531c2fce51adf09b2887e" |
|
let event = finalizeEvent({ |
|
kind: 1573, |
|
created_at: Math.floor(Date.now() / 1000), |
|
tags: [["s", "0"], ["p", recipient]], |
|
// Content must be string, size limit in 2KiB, Blob and Json can serialize first |
|
content: JSON.stringify({ |
|
foo: "bar", |
|
hello: "world", |
|
}), |
|
}, senderSecretKey) |
|
|
|
// A brief preview for the NoStr event |
|
console.log(event); |
|
|
|
// This is one of our preview Relay federation |
|
const rpcHost = "https://dev-relay.dephy.dev" |
|
// Publish the message |
|
const resp = await fetch(`${rpcHost}/events`, { |
|
method: "POST", |
|
headers: { |
|
"Content-Type": "application/json", |
|
}, |
|
body: JSON.stringify(event), |
|
}) |
|
// Response status should be 200 if everything good, otherwise 400 |
|
console.log(resp.status) |
|
// If response status not 400, the body will contains an error message |
|
console.log(await resp.text()) |