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
}
return array
}
}
// decode nip19 ('npub') to hex
const npub2hexa = (npub) => {
// 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')
}
}
}
// encode hex to nip19 ('npub')
const hexa2npub = (hex) => {
// 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)
}
}
// parse inserted pubkey
const parsePubkey = (pubkey) =>
// parse inserted pubkey
const parsePubkey = (pubkey) =>
pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey
// download js file
const downloadFile = (data, fileName) => {
// 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()
}
}
const updateRelayStatus = (relay, status, addToCount, until, relayStatusAndCount) => {
const updateRelayStatus = (relay, status, addToCount, until, relayStatusAndCount) => {
if (relayStatusAndCount[relay] == undefined) {
relayStatusAndCount[relay] = {}
}
@ -63,9 +63,9 @@ function hexToBytes(hex) {
relayStatusAndCount[relay].count = addToCount
displayRelayStatus(relayStatusAndCount)
}
}
const displayRelayStatus = (relayStatusAndCount) => {
const displayRelayStatus = (relayStatusAndCount) => {
if (Object.keys(relayStatusAndCount).length > 0) {
const newText = Object.keys(relayStatusAndCount).map(
it => {
@ -84,10 +84,10 @@ function hexToBytes(hex) {
$('#checking-relays-header').html("")
$('#checking-relays').html("")
}
}
}
// fetch events from relay, returns a promise
const fetchFromRelay = async (relay, filters, pubkey, events, relayStatus) =>
// 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)
@ -241,8 +241,8 @@ function hexToBytes(hex) {
}
})
// query relays for events published by this pubkey
const getEvents = async (filters, pubkey) => {
// query relays for events published by this pubkey
const getEvents = async (filters, pubkey) => {
// events hash
const events = {}
@ -253,9 +253,9 @@ function hexToBytes(hex) {
// return data as an array of events
return Object.keys(events).map((id) => events[id])
}
}
const processInPool = async (items, processItem, poolSize) => {
const processInPool = async (items, processItem, poolSize) => {
let pool = {};
let poolStatus = {}
let remaining = [...items]
@ -281,15 +281,15 @@ function hexToBytes(hex) {
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")
for (evnt of data) {
ws.send(JSON.stringify(['EVENT', evnt]))
}
}
}
// send events to a relay, returns a promisse
const sendToRelay = async (relay, data, relayStatus) =>
// send events to a relay, returns a promisse
const sendToRelay = async (relay, data, relayStatus) =>
new Promise((resolve, reject) => {
try {
const ws = new WebSocket(relay)
@ -357,8 +357,8 @@ function hexToBytes(hex) {
}
})
// broadcast events to list of relays
const broadcastEvents = async (data) => {
// broadcast events to list of relays
const broadcastEvents = async (data) => {
// batch processing of 10 relays
let broadcastFunctions = [...relays]
let relayStatus = {}
@ -369,9 +369,39 @@ function hexToBytes(hex) {
}
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 {
if (!window.nostr) {
@ -403,32 +433,4 @@ function hexToBytes(hex) {
}
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;
});
}
}