order of functions

This commit is contained in:
Vitor Pamplona 2024-01-28 12:50:44 -05:00
parent 23a501f0e3
commit 0d8073db51

View File

@ -14,40 +14,40 @@ function hexToBytes(hex) {
array[i] = byte array[i] = byte
} }
return array return array
} }
// decode nip19 ('npub') to hex // decode nip19 ('npub') to hex
const npub2hexa = (npub) => { const npub2hexa = (npub) => {
let { prefix, words } = bech32.bech32.decode(npub, 90) let { prefix, words } = bech32.bech32.decode(npub, 90)
if (prefix === 'npub') { if (prefix === 'npub') {
let data = new Uint8Array(bech32.bech32.fromWords(words)) let data = new Uint8Array(bech32.bech32.fromWords(words))
return buffer.Buffer.from(data).toString('hex') return buffer.Buffer.from(data).toString('hex')
} }
} }
// encode hex to nip19 ('npub') // encode hex to nip19 ('npub')
const hexa2npub = (hex) => { const hexa2npub = (hex) => {
const data = hexToBytes(hex) const data = hexToBytes(hex)
const words = bech32.bech32.toWords(data) const words = bech32.bech32.toWords(data)
const prefix = 'npub' const prefix = 'npub'
return bech32.bech32.encode(prefix, words, 90) return bech32.bech32.encode(prefix, words, 90)
} }
// parse inserted pubkey // parse inserted pubkey
const parsePubkey = (pubkey) => const parsePubkey = (pubkey) =>
pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey
// download js file // download js file
const downloadFile = (data, fileName) => { const downloadFile = (data, fileName) => {
const prettyJs = 'const data = ' + JSON.stringify(data, null, 2) const prettyJs = 'const data = ' + JSON.stringify(data, null, 2)
const tempLink = document.createElement('a') const tempLink = document.createElement('a')
const taBlob = new Blob([prettyJs], { type: 'text/javascript' }) const taBlob = new Blob([prettyJs], { type: 'text/javascript' })
tempLink.setAttribute('href', URL.createObjectURL(taBlob)) tempLink.setAttribute('href', URL.createObjectURL(taBlob))
tempLink.setAttribute('download', fileName) tempLink.setAttribute('download', fileName)
tempLink.click() tempLink.click()
} }
const updateRelayStatus = (relay, status, addToCount, until, relayStatusAndCount) => { const updateRelayStatus = (relay, status, addToCount, until, relayStatusAndCount) => {
if (relayStatusAndCount[relay] == undefined) { if (relayStatusAndCount[relay] == undefined) {
relayStatusAndCount[relay] = {} relayStatusAndCount[relay] = {}
} }
@ -63,9 +63,9 @@ function hexToBytes(hex) {
relayStatusAndCount[relay].count = addToCount relayStatusAndCount[relay].count = addToCount
displayRelayStatus(relayStatusAndCount) displayRelayStatus(relayStatusAndCount)
} }
const displayRelayStatus = (relayStatusAndCount) => { const displayRelayStatus = (relayStatusAndCount) => {
if (Object.keys(relayStatusAndCount).length > 0) { if (Object.keys(relayStatusAndCount).length > 0) {
const newText = Object.keys(relayStatusAndCount).map( const newText = Object.keys(relayStatusAndCount).map(
it => { it => {
@ -84,10 +84,10 @@ function hexToBytes(hex) {
$('#checking-relays-header').html("") $('#checking-relays-header').html("")
$('#checking-relays').html("") $('#checking-relays').html("")
} }
} }
// fetch events from relay, returns a promise // fetch events from relay, returns a promise
const fetchFromRelay = async (relay, filters, pubkey, events, relayStatus) => const fetchFromRelay = async (relay, filters, pubkey, events, relayStatus) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
try { try {
updateRelayStatus(relay, "Starting", 0, undefined, relayStatus) updateRelayStatus(relay, "Starting", 0, undefined, relayStatus)
@ -241,8 +241,8 @@ function hexToBytes(hex) {
} }
}) })
// query relays for events published by this pubkey // query relays for events published by this pubkey
const getEvents = async (filters, pubkey) => { const getEvents = async (filters, pubkey) => {
// events hash // events hash
const events = {} const events = {}
@ -253,9 +253,9 @@ function hexToBytes(hex) {
// return data as an array of events // return data as an array of events
return Object.keys(events).map((id) => events[id]) return Object.keys(events).map((id) => events[id])
} }
const processInPool = async (items, processItem, poolSize) => { const processInPool = async (items, processItem, poolSize) => {
let pool = {}; let pool = {};
let poolStatus = {} let poolStatus = {}
let remaining = [...items] let remaining = [...items]
@ -281,15 +281,15 @@ function hexToBytes(hex) {
await Promise.allSettled(Object.values(pool)); await Promise.allSettled(Object.values(pool));
} }
const sendAllEvents = async (relay, data, relayStatus, ws) => { const sendAllEvents = async (relay, data, relayStatus, ws) => {
console.log("Sending:", data.length, " events") console.log("Sending:", data.length, " events")
for (evnt of data) { for (evnt of data) {
ws.send(JSON.stringify(['EVENT', evnt])) ws.send(JSON.stringify(['EVENT', evnt]))
} }
} }
// send events to a relay, returns a promisse // send events to a relay, returns a promisse
const sendToRelay = async (relay, data, relayStatus) => const sendToRelay = async (relay, data, relayStatus) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
try { try {
const ws = new WebSocket(relay) const ws = new WebSocket(relay)
@ -357,8 +357,8 @@ function hexToBytes(hex) {
} }
}) })
// broadcast events to list of relays // broadcast events to list of relays
const broadcastEvents = async (data) => { const broadcastEvents = async (data) => {
// batch processing of 10 relays // batch processing of 10 relays
let broadcastFunctions = [...relays] let broadcastFunctions = [...relays]
let relayStatus = {} let relayStatus = {}
@ -369,9 +369,39 @@ function hexToBytes(hex) {
} }
displayRelayStatus(relayStatus) displayRelayStatus(relayStatus)
} }
async function signNostrAuthEvent(relay, auth_challenge) {
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);
return hash;
}
function sha256Hex(string) {
const utf8 = new TextEncoder().encode(string);
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;
});
}
async function signNostrAuthEvent(relay, auth_challenge) {
try { try {
if (!window.nostr) { if (!window.nostr) {
@ -403,32 +433,4 @@ function hexToBytes(hex) {
} }
return signed_msg; return signed_msg;
} }
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);
return hash;
}
function sha256Hex(string) {
const utf8 = new TextEncoder().encode(string);
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;
});
}