Compare commits

..

6 Commits

Author SHA1 Message Date
fiatjaf
74a0d5454a guard against some nonexisting arrays of event listeners. 2022-12-23 15:18:23 -03:00
fiatjaf
c0d1e41424 always recompute the hash when signing.
fixes https://github.com/fiatjaf/nostr-tools/issues/59
2022-12-23 15:06:21 -03:00
fiatjaf
f7e510e1c8 nip05 regex name check. 2022-12-23 15:04:24 -03:00
fiatjaf
c08bdac7a7 catch usage of global fetch for nodejs.
fixes https://github.com/fiatjaf/nostr-tools/issues/53
2022-12-23 11:36:37 -03:00
rkfg
c5b64404f6 Add limit to filter 2022-12-23 11:29:38 -03:00
adamritter
c7b26fdba2 Don't expose external API to hex representation of mnemoic 2022-12-23 11:01:10 -03:00
6 changed files with 23 additions and 17 deletions

View File

@@ -64,6 +64,6 @@ export function verifySignature(
export async function signEvent(event: Event, key: string): Promise<string> {
return secp256k1.utils.bytesToHex(
await secp256k1.schnorr.sign(event.id || getEventHash(event), key)
await secp256k1.schnorr.sign(getEventHash(event), key)
)
}

View File

@@ -6,6 +6,7 @@ export type Filter = {
authors?: string[]
since?: number
until?: number
limit?: number
[key: `#${string}`]: string[]
}

View File

@@ -1,6 +1,10 @@
import {ProfilePointer} from './nip19'
var _fetch = fetch
var _fetch: any
try {
_fetch = fetch
} catch {}
export function useFetchImplementation(fetchImplementation: any) {
_fetch = fetchImplementation
@@ -32,6 +36,8 @@ export async function queryProfile(
name = '_'
}
if (!name.match(/^[a-z0-9-_]+$/)) return null
let res = await (
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
).json()

View File

@@ -7,17 +7,13 @@ import {
} from '@scure/bip39'
import {HDKey} from '@scure/bip32'
export function privateKeyFromSeed(seed: string): string {
let root = HDKey.fromMasterSeed(secp256k1.utils.hexToBytes(seed))
export function privateKeyFromSeedWords(mnemonic: string): string {
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic))
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
if (!privateKey) throw new Error('could not derive private key')
return secp256k1.utils.bytesToHex(privateKey)
}
export function seedFromWords(mnemonic: string): string {
return secp256k1.utils.bytesToHex(mnemonicToSeedSync(mnemonic))
}
export function generateSeedWords(): string {
return generateMnemonic(wordlist)
}

View File

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

View File

@@ -98,13 +98,13 @@ export function relayInit(url: string): Relay {
matchFilters(openSubs[id].filters, event)
) {
openSubs[id]
subListeners[id]?.event.forEach(cb => cb(event))
;(subListeners[id]?.event || []).forEach(cb => cb(event))
}
return
case 'EOSE': {
if (data.length !== 2) return // ignore empty or malformed EOSE
let id = data[1]
subListeners[id]?.eose.forEach(cb => cb())
;(subListeners[id]?.eose || []).forEach(cb => cb())
return
}
case 'OK': {
@@ -174,8 +174,9 @@ export function relayInit(url: string): Relay {
subListeners[subid][type].push(cb)
},
off: (type: 'event' | 'eose', cb: any): void => {
let idx = subListeners[subid][type].indexOf(cb)
if (idx >= 0) subListeners[subid][type].splice(idx, 1)
let listeners = subListeners[subid]
let idx = listeners[type].indexOf(cb)
if (idx >= 0) listeners[type].splice(idx, 1)
}
}
}
@@ -221,14 +222,14 @@ export function relayInit(url: string): Relay {
id: `monitor-${id.slice(0, 5)}`
})
let willUnsub = setTimeout(() => {
pubListeners[id].failed.forEach(cb =>
;(pubListeners[id]?.failed || []).forEach(cb =>
cb('event not seen after 5 seconds')
)
monitor.unsub()
}, 5000)
monitor.on('event', () => {
clearTimeout(willUnsub)
pubListeners[id].seen.forEach(cb => cb())
;(pubListeners[id]?.seen || []).forEach(cb => cb())
})
}
@@ -247,8 +248,10 @@ export function relayInit(url: string): Relay {
}
},
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
let idx = pubListeners[id][type].indexOf(cb)
if (idx >= 0) pubListeners[id][type].splice(idx, 1)
let listeners = pubListeners[id]
if (!listeners) return
let idx = listeners[type].indexOf(cb)
if (idx >= 0) listeners[type].splice(idx, 1)
}
}
},