Compare commits

...

2 Commits

Author SHA1 Message Date
fiatjaf
ce214ebbab small tweaks on relayConnect. 2021-12-30 15:02:05 -03:00
fiatjaf
800beb37f1 cut out the first byte of pubkeys. 2021-12-29 15:15:53 -03:00
3 changed files with 23 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ export function generatePrivateKey() {
} }
export function getPublicKey(privateKey) { export function getPublicKey(privateKey) {
return Buffer.from( return Buffer.from(pointFromScalar(Buffer.from(privateKey, 'hex'), true))
pointFromScalar(Buffer.from(privateKey, 'hex'), true) .toString('hex')
).toString('hex') .slice(2)
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "0.12.2", "version": "0.12.4",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
return [host, ...qs].join('?') return [host, ...qs].join('?')
} }
export function relayConnect(url, onNotice) { export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
url = normalizeRelayURL(url) url = normalizeRelayURL(url)
var ws, resolveOpen, untilOpen, wasClosed var ws, resolveOpen, untilOpen, wasClosed
@@ -46,8 +46,9 @@ export function relayConnect(url, onNotice) {
} }
} }
} }
ws.onerror = () => { ws.onerror = (err) => {
console.log('error connecting to relay', url) console.log('error connecting to relay', url)
onError(err)
} }
ws.onclose = () => { ws.onclose = () => {
resetOpenState() resetOpenState()
@@ -146,22 +147,26 @@ export function relayConnect(url, onNotice) {
return { return {
url, url,
sub, sub,
async publish(event, statusCallback = status => {}) { async publish(event, statusCallback) {
try { try {
await trySend(['EVENT', event]) await trySend(['EVENT', event])
statusCallback(0) if (statusCallback) {
let {unsub} = sub( statusCallback(0)
{ let {unsub} = sub(
cb: () => { {
statusCallback(1) cb: () => {
statusCallback(1)
unsub()
clearTimeout(willUnsub)
},
filter: {id: event.id}
}, },
filter: {id: event.id} `monitor-${event.id.slice(0, 5)}`
}, )
`monitor-${event.id.slice(0, 5)}` let willUnsub = setTimeout(unsub, 5000)
) }
setTimeout(unsub, 5000)
} catch (err) { } catch (err) {
statusCallback(-1) if (statusCallback) statusCallback(-1)
} }
}, },
close() { close() {