2023-07-27 11:26:26 -04:00
|
|
|
// from https://github.com/paulmillr/noble-secp256k1/blob/main/index.ts#L803
|
|
|
|
function hexToBytes(hex) {
|
2024-01-28 12:50:44 -05:00
|
|
|
if (typeof hex !== 'string') {
|
|
|
|
throw new TypeError('hexToBytes: expected string, got ' + typeof hex)
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
if (hex.length % 2)
|
|
|
|
throw new Error('hexToBytes: received invalid unpadded hex' + hex.length)
|
|
|
|
const array = new Uint8Array(hex.length / 2)
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
const j = i * 2
|
|
|
|
const hexByte = hex.slice(j, j + 2)
|
|
|
|
const byte = Number.parseInt(hexByte, 16)
|
|
|
|
if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence')
|
|
|
|
array[i] = byte
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode nip19 ('npub') to hex
|
|
|
|
const npub2hexa = (npub) => {
|
|
|
|
let { prefix, words } = bech32.bech32.decode(npub, 90)
|
|
|
|
if (prefix === 'npub') {
|
|
|
|
let data = new Uint8Array(bech32.bech32.fromWords(words))
|
|
|
|
return buffer.Buffer.from(data).toString('hex')
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
2023-07-27 11:26:26 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// encode hex to nip19 ('npub')
|
|
|
|
const hexa2npub = (hex) => {
|
|
|
|
const data = hexToBytes(hex)
|
|
|
|
const words = bech32.bech32.toWords(data)
|
|
|
|
const prefix = 'npub'
|
|
|
|
return bech32.bech32.encode(prefix, words, 90)
|
|
|
|
}
|
2023-07-27 11:26:26 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// parse inserted pubkey
|
|
|
|
const parsePubkey = (pubkey) =>
|
|
|
|
pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey
|
|
|
|
|
|
|
|
// download js file
|
|
|
|
const downloadFile = (data, fileName) => {
|
|
|
|
const prettyJs = 'const data = ' + JSON.stringify(data, null, 2)
|
|
|
|
const tempLink = document.createElement('a')
|
|
|
|
const taBlob = new Blob([prettyJs], { type: 'text/javascript' })
|
|
|
|
tempLink.setAttribute('href', URL.createObjectURL(taBlob))
|
|
|
|
tempLink.setAttribute('download', fileName)
|
|
|
|
tempLink.click()
|
|
|
|
}
|
2023-08-03 14:55:52 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const updateRelayStatus = (relay, status, addToCount, until, relayStatusAndCount) => {
|
|
|
|
if (relayStatusAndCount[relay] == undefined) {
|
|
|
|
relayStatusAndCount[relay] = {}
|
|
|
|
}
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (status)
|
|
|
|
relayStatusAndCount[relay].status = status
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
relayStatusAndCount[relay].until = until
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (relayStatusAndCount[relay].count != undefined)
|
|
|
|
relayStatusAndCount[relay].count = relayStatusAndCount[relay].count + addToCount
|
|
|
|
else
|
|
|
|
relayStatusAndCount[relay].count = addToCount
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
displayRelayStatus(relayStatusAndCount)
|
|
|
|
}
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const displayRelayStatus = (relayStatusAndCount) => {
|
|
|
|
if (Object.keys(relayStatusAndCount).length > 0) {
|
|
|
|
const newText = Object.keys(relayStatusAndCount).map(
|
|
|
|
it => {
|
|
|
|
let untilStr = "";
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (relayStatusAndCount[it].until)
|
|
|
|
untilStr = " <" + new Date(relayStatusAndCount[it].until * 1000).toLocaleDateString("en-US")
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const relayName = it.replace("wss://", "").replace("ws://", "")
|
|
|
|
const line = "<td>" + relayName + "</td><td>" + relayStatusAndCount[it].status + "</td><td>" + untilStr + "</td><td>" + relayStatusAndCount[it].count + "</td>"
|
|
|
|
return "<tr>" +line+ "</tr>"
|
|
|
|
}
|
|
|
|
).join("<br />")
|
|
|
|
$('#checking-relays').html(newText)
|
|
|
|
} else {
|
|
|
|
$('#checking-relays-header').html("")
|
|
|
|
$('#checking-relays').html("")
|
2023-08-03 14:55:52 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
2023-08-03 14:55:52 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// fetch events from relay, returns a promise
|
|
|
|
const fetchFromRelay = async (relay, filters, pubkey, events, relayStatus) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
updateRelayStatus(relay, "Starting", 0, undefined, relayStatus)
|
|
|
|
// open websocket
|
|
|
|
const ws = new WebSocket(relay)
|
2023-08-02 18:25:29 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// prevent hanging forever
|
|
|
|
let myTimeout = setTimeout(() => {
|
|
|
|
ws.close()
|
|
|
|
reject(relay)
|
|
|
|
}, 10_000)
|
|
|
|
|
|
|
|
const subscriptions = Object.fromEntries(filters.map ( (filter, index) => {
|
|
|
|
let id = "my-sub-"+index
|
|
|
|
|
|
|
|
return [
|
|
|
|
id, {
|
|
|
|
id: id,
|
|
|
|
counter: 0,
|
|
|
|
lastEvent: null,
|
|
|
|
done: false,
|
|
|
|
filter: filter,
|
|
|
|
eventIds: new Set()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
|
|
|
|
// subscribe to events filtered by author
|
|
|
|
ws.onopen = () => {
|
|
|
|
clearTimeout(myTimeout)
|
|
|
|
myTimeout = setTimeout(() => {
|
2023-08-02 18:25:29 -04:00
|
|
|
ws.close()
|
2024-01-28 12:43:23 -05:00
|
|
|
reject(relay)
|
2023-08-03 14:55:52 -04:00
|
|
|
}, 10_000)
|
2024-01-28 12:50:44 -05:00
|
|
|
updateRelayStatus(relay, "Downloading", 0, undefined, relayStatus)
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
for (const [key, sub] of Object.entries(subscriptions)) {
|
|
|
|
ws.send(JSON.stringify(['REQ', sub.id, sub.filter]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for messages
|
|
|
|
ws.onmessage = (event) => {
|
|
|
|
const [msgType, subscriptionId, data] = JSON.parse(event.data)
|
|
|
|
// event messages
|
|
|
|
if (msgType === 'EVENT') {
|
2023-08-03 14:55:52 -04:00
|
|
|
clearTimeout(myTimeout)
|
|
|
|
myTimeout = setTimeout(() => {
|
|
|
|
ws.close()
|
2024-01-28 12:43:23 -05:00
|
|
|
reject(relay)
|
2023-08-03 14:55:52 -04:00
|
|
|
}, 10_000)
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
try {
|
|
|
|
const { id } = data
|
2023-08-03 15:54:07 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (!subscriptions[subscriptionId].lastEvent || data.created_at < subscriptions[subscriptionId].lastEvent.created_at)
|
|
|
|
subscriptions[subscriptionId].lastEvent = data
|
2023-08-03 15:54:07 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (data.id in subscriptions[subscriptionId].eventIds) return
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
subscriptions[subscriptionId].eventIds.add(data.id)
|
|
|
|
subscriptions[subscriptionId].counter++
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// don't save/reboradcast kind 3s that are not from the author.
|
|
|
|
// their are too big.
|
|
|
|
if (data.kind == 3 && data.pubkey != pubkey) {
|
|
|
|
return
|
|
|
|
}
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
let until = undefined
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (subscriptions[subscriptionId].lastEvent) {
|
|
|
|
until = subscriptions[subscriptionId].lastEvent.created_at
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
|
|
|
|
updateRelayStatus(relay, undefined, 1, until, relayStatus)
|
|
|
|
|
|
|
|
// prevent duplicated events
|
|
|
|
if (events[id]) return
|
|
|
|
else events[id] = data
|
|
|
|
|
|
|
|
// show how many events were found until this moment
|
|
|
|
$('#events-found').text(`${Object.keys(events).length} events found`)
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err, event)
|
|
|
|
return
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// end of subscription messages
|
|
|
|
if (msgType === 'EOSE') {
|
|
|
|
// Restarting the filter is necessary to go around Max Limits for each relay.
|
|
|
|
if (subscriptions[subscriptionId].counter < 2) {
|
|
|
|
subscriptions[subscriptionId].done = true
|
|
|
|
console.log(relay, subscriptionId, event.data)
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
let alldone = Object.values(subscriptions).every(filter => filter.done === true);
|
|
|
|
if (alldone) {
|
|
|
|
updateRelayStatus(relay, "Done", 0, undefined, relayStatus)
|
|
|
|
ws.close()
|
|
|
|
resolve(relay)
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
} else {
|
|
|
|
//console.log("Limit: ", { ...filters[0], until: lastSub1Event.created_at })
|
|
|
|
subscriptions[subscriptionId].counter = 0
|
|
|
|
ws.send(JSON.stringify(['REQ', subscriptions[subscriptionId].id, { ...subscriptions[subscriptionId].filter, until: subscriptions[subscriptionId].lastEvent.created_at } ]))
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (msgType === 'AUTH') {
|
|
|
|
signNostrAuthEvent(relay, subscriptionId).then(
|
|
|
|
(event) => {
|
|
|
|
if (event)
|
|
|
|
ws.send(JSON.stringify(['EVENT', event]))
|
|
|
|
else {
|
2024-01-28 12:43:23 -05:00
|
|
|
updateRelayStatus(relay, "AUTH Req", 0, undefined, relayStatus)
|
|
|
|
ws.close()
|
|
|
|
reject(relay)
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(reason) => {
|
|
|
|
updateRelayStatus(relay, "AUTH Req", 0, undefined, relayStatus)
|
|
|
|
ws.close()
|
|
|
|
reject(relay)
|
|
|
|
},
|
|
|
|
)
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
|
|
|
ws.onerror = (err) => {
|
|
|
|
updateRelayStatus(relay, "Done", 0, undefined, relayStatus)
|
2023-08-02 18:25:29 -04:00
|
|
|
try {
|
|
|
|
ws.close()
|
2024-01-28 12:50:44 -05:00
|
|
|
reject(relay)
|
|
|
|
} catch {
|
|
|
|
reject(relay)
|
2023-08-02 18:25:29 -04:00
|
|
|
}
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
ws.onclose = (socket, event) => {
|
|
|
|
updateRelayStatus(relay, "Done", 0, undefined, relayStatus)
|
|
|
|
resolve(relay)
|
|
|
|
}
|
|
|
|
} catch (exception) {
|
|
|
|
console.log(exception)
|
|
|
|
updateRelayStatus(relay, "Error", 0, undefined, relayStatus)
|
|
|
|
try {
|
|
|
|
ws.close()
|
|
|
|
} catch (exception) {
|
|
|
|
}
|
|
|
|
|
|
|
|
reject(relay)
|
|
|
|
}
|
|
|
|
})
|
2023-08-03 14:55:52 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// query relays for events published by this pubkey
|
|
|
|
const getEvents = async (filters, pubkey) => {
|
|
|
|
// events hash
|
|
|
|
const events = {}
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// batch processing of 10 relays
|
|
|
|
await processInPool(relays, (relay, poolStatus) => fetchFromRelay(relay, filters, pubkey, events, poolStatus), 10)
|
2023-08-03 14:55:52 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
displayRelayStatus({})
|
2023-09-05 09:25:07 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// return data as an array of events
|
|
|
|
return Object.keys(events).map((id) => events[id])
|
|
|
|
}
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const processInPool = async (items, processItem, poolSize) => {
|
|
|
|
let pool = {};
|
|
|
|
let poolStatus = {}
|
|
|
|
let remaining = [...items]
|
|
|
|
|
|
|
|
while (remaining.length) {
|
|
|
|
let processing = remaining.splice(0, 1)
|
|
|
|
let item = processing[0]
|
|
|
|
pool[item] = processItem(item, poolStatus);
|
|
|
|
|
|
|
|
if (Object.keys(pool).length > poolSize - 1) {
|
|
|
|
try {
|
|
|
|
const resolvedId = await Promise.race(Object.values(pool)); // wait for one Promise to finish
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
delete pool[resolvedId]; // remove that Promise from the pool
|
|
|
|
} catch (resolvedId) {
|
|
|
|
delete pool[resolvedId]; // remove that Promise from the pool
|
|
|
|
}
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
$('#fetching-progress').val(items.length - remaining.length)
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.allSettled(Object.values(pool));
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const sendAllEvents = async (relay, data, relayStatus, ws) => {
|
|
|
|
console.log("Sending:", data.length, " events")
|
|
|
|
for (evnt of data) {
|
|
|
|
ws.send(JSON.stringify(['EVENT', evnt]))
|
2023-09-05 09:25:07 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
2023-08-02 18:25:29 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// send events to a relay, returns a promisse
|
|
|
|
const sendToRelay = async (relay, data, relayStatus) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const ws = new WebSocket(relay)
|
2023-08-03 14:55:52 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
updateRelayStatus(relay, "Starting", 0, undefined, relayStatus)
|
|
|
|
|
|
|
|
// prevent hanging forever
|
|
|
|
let myTimeout = setTimeout(() => {
|
|
|
|
ws.close()
|
|
|
|
reject('timeout')
|
|
|
|
}, 10_000)
|
|
|
|
|
|
|
|
// fetch events from relay
|
|
|
|
ws.onopen = () => {
|
|
|
|
updateRelayStatus(relay, "Sending", 0, undefined, relayStatus)
|
|
|
|
|
|
|
|
clearTimeout(myTimeout)
|
|
|
|
myTimeout = setTimeout(() => {
|
2023-08-02 18:25:29 -04:00
|
|
|
ws.close()
|
|
|
|
reject('timeout')
|
2023-08-03 14:55:52 -04:00
|
|
|
}, 10_000)
|
2023-08-02 18:25:29 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
sendAllEvents(relay, data, relayStatus, ws)
|
|
|
|
}
|
|
|
|
// Listen for messages
|
|
|
|
ws.onmessage = (event) => {
|
|
|
|
clearTimeout(myTimeout)
|
|
|
|
myTimeout = setTimeout(() => {
|
|
|
|
ws.close()
|
|
|
|
reject('timeout')
|
|
|
|
}, 10_000)
|
2023-08-03 18:57:56 -04:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
const [msgType, subscriptionId, inserted] = JSON.parse(event.data)
|
|
|
|
// event messages
|
|
|
|
// end of subscription messages
|
|
|
|
if (msgType === 'OK') {
|
|
|
|
if (inserted == true) {
|
|
|
|
updateRelayStatus(relay, undefined, 1, undefined, relayStatus)
|
2023-09-05 09:25:07 -04:00
|
|
|
} else {
|
2024-01-28 12:43:23 -05:00
|
|
|
console.log(relay, event.data)
|
2023-08-03 18:57:56 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
} else {
|
|
|
|
console.log(relay, event.data)
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
}
|
|
|
|
ws.onerror = (err) => {
|
2024-01-28 12:43:23 -05:00
|
|
|
updateRelayStatus(relay, "Error", 0, undefined, relayStatus)
|
2024-01-28 12:50:44 -05:00
|
|
|
console.log("Error", err)
|
|
|
|
ws.close()
|
|
|
|
reject(err)
|
2023-07-27 11:26:26 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
ws.onclose = (socket, event) => {
|
|
|
|
updateRelayStatus(relay, "Done", 0, undefined, relayStatus)
|
|
|
|
console.log("OnClose", relayStatus)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
} catch (exception) {
|
|
|
|
console.log(exception)
|
|
|
|
updateRelayStatus(relay, "Error", 0, undefined, relayStatus)
|
|
|
|
try {
|
|
|
|
ws.close()
|
|
|
|
} catch (exception) {
|
|
|
|
}
|
|
|
|
reject(exception)
|
2023-08-03 14:55:52 -04:00
|
|
|
}
|
2024-01-28 12:50:44 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// broadcast events to list of relays
|
|
|
|
const broadcastEvents = async (data) => {
|
|
|
|
// batch processing of 10 relays
|
|
|
|
let broadcastFunctions = [...relays]
|
|
|
|
let relayStatus = {}
|
|
|
|
while (broadcastFunctions.length) {
|
|
|
|
let relaysForThisRound = broadcastFunctions.splice(0, 10)
|
|
|
|
$('#broadcasting-progress').val(relays.length - broadcastFunctions.length)
|
|
|
|
await Promise.allSettled( relaysForThisRound.map((relay) => sendToRelay(relay, data, relayStatus)) )
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
displayRelayStatus(relayStatus)
|
|
|
|
}
|
|
|
|
|
2024-01-28 12:43:23 -05:00
|
|
|
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
async function generateNostrEventId(msg) {
|
|
|
|
const digest = [
|
|
|
|
0,
|
|
|
|
msg.pubkey,
|
|
|
|
msg.created_at,
|
|
|
|
msg.kind,
|
|
|
|
msg.tags,
|
|
|
|
msg.content,
|
|
|
|
];
|
|
|
|
const digest_str = JSON.stringify(digest);
|
|
|
|
const hash = await sha256Hex(digest_str);
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sha256Hex(string) {
|
|
|
|
const utf8 = new TextEncoder().encode(string);
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
return crypto.subtle.digest('SHA-256', utf8).then((hashBuffer) => {
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
|
|
const hashHex = hashArray
|
|
|
|
.map((bytes) => bytes.toString(16).padStart(2, '0'))
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
return hashHex;
|
|
|
|
});
|
|
|
|
}
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
async function signNostrAuthEvent(relay, auth_challenge) {
|
|
|
|
try {
|
2024-01-28 12:43:23 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
if (!window.nostr) {
|
|
|
|
throw "Nostr extension not loaded or available"
|
2024-01-28 12:43:23 -05:00
|
|
|
}
|
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
let msg = {
|
|
|
|
kind: 22243, // NIP-42++
|
|
|
|
content: "",
|
|
|
|
tags: [
|
|
|
|
["relay", relay]
|
|
|
|
["challenge", auth_challenge]
|
|
|
|
],
|
|
|
|
};
|
2024-01-28 12:47:10 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// set msg fields
|
|
|
|
msg.created_at = Math.floor((new Date()).getTime() / 1000);
|
|
|
|
msg.pubkey = await window.nostr.getPublicKey();
|
2024-01-28 12:47:10 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// Generate event id
|
|
|
|
msg.id = await generateNostrEventId(msg);
|
2024-01-28 12:47:10 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
// Sign event
|
|
|
|
signed_msg = await window.nostr.signEvent(msg);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Failed to sign message with browser extension", e);
|
|
|
|
return undefined;
|
|
|
|
}
|
2024-01-28 12:47:10 -05:00
|
|
|
|
2024-01-28 12:50:44 -05:00
|
|
|
return signed_msg;
|
|
|
|
}
|