Compare commits

...

6 Commits

Author SHA1 Message Date
fiatjaf
3d6f9a41e0 prevent blocking waiting times on publish (unless "wait" is set in the pool policy). 2022-01-12 17:39:24 -03:00
fiatjaf
e3631ba806 fix and update nip06. 2022-01-06 21:46:34 -03:00
fiatjaf
89f11e214d fix filter matching for tags. 2022-01-02 19:46:19 -03:00
fiatjaf
bb09e25512 fix tag in matchFilter for kinds and ids. 2022-01-01 21:18:37 -03:00
fiatjaf
1b5c314436 nip-01 update: everything as arrays on filters. 2022-01-01 20:49:05 -03:00
fiatjaf
2230f32d11 use randomBytes from @noble/hashes. 2022-01-01 14:59:12 -03:00
5 changed files with 55 additions and 38 deletions

View File

@@ -1,19 +1,23 @@
export function matchFilter(filter, event) { export function matchFilter(filter, event) {
if (filter.id && event.id !== filter.id) return false if (filter.ids && filter.ids.indexOf(event.id) === -1) return false
if (typeof filter.kind === 'number' && event.kind !== filter.kind) return false if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1) if (filter.authors && filter.authors.indexOf(event.pubkey) === -1)
return false return false
for (let f in filter) {
if (f[0] === '#') {
if ( if (
filter['#e'] && filter[f] &&
!event.tags.find(([t, v]) => t === 'e' && v === filter['#e']) !event.tags.find(
([t, v]) => t === f.slice(1) && filter[f].indexOf(v) !== -1
)
) )
return false return false
if ( }
filter['#p'] && }
!event.tags.find(([t, v]) => t === 'p' && v === filter['#p'])
) if (filter.since && event.created_at < filter.since) return false
return false if (filter.until && event.created_at >= filter.until) return false
if (filter.since && event.created_at <= filter.since) return false
return true return true
} }

View File

@@ -1,6 +1,6 @@
import aes from 'browserify-cipher' import aes from 'browserify-cipher'
import {Buffer} from 'buffer' import {Buffer} from 'buffer'
import randomBytes from 'randombytes' import {randomBytes} from '@noble/hashes/utils'
import * as secp256k1 from '@noble/secp256k1' import * as secp256k1 from '@noble/secp256k1'
export function encrypt(privkey, pubkey, text) { export function encrypt(privkey, pubkey, text) {

View File

@@ -8,13 +8,13 @@ import {HDKey} from 'micro-bip32'
export function privateKeyFromSeed(seed) { export function privateKeyFromSeed(seed) {
let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex')) let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
return Buffer.from(root.derive(`m/44'/1237'/0'/0'`).privateKey).toString( return Buffer.from(root.derive(`m/44'/1237'/0'/0/0`).privateKey).toString(
'hex' 'hex'
) )
} }
export function seedFromWords(mnemonic) { export function seedFromWords(mnemonic) {
return Buffer.from(mnemonicToSeedSync(mnemonic, wordlist)).toString('hex') return Buffer.from(mnemonicToSeedSync(mnemonic)).toString('hex')
} }
export function generateSeedWords() { export function generateSeedWords() {

View File

@@ -1,12 +1,13 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "0.15.1", "version": "0.18.0",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/fiatjaf/nostr-tools.git" "url": "https://github.com/fiatjaf/nostr-tools.git"
}, },
"dependencies": { "dependencies": {
"@noble/hashes": "^0.5.7",
"@noble/secp256k1": "^1.3.0", "@noble/secp256k1": "^1.3.0",
"browserify-cipher": ">=1", "browserify-cipher": ">=1",
"buffer": ">=5", "buffer": ">=5",
@@ -14,7 +15,6 @@
"dns-packet": "^5.2.4", "dns-packet": "^5.2.4",
"micro-bip32": "^0.1.0", "micro-bip32": "^0.1.0",
"micro-bip39": "^0.1.3", "micro-bip39": "^0.1.3",
"randombytes": ">=2",
"websocket-polyfill": "^0.0.3" "websocket-polyfill": "^0.0.3"
}, },
"keywords": [ "keywords": [

21
pool.js
View File

@@ -6,7 +6,11 @@ export function relayPool() {
const poolPolicy = { const poolPolicy = {
// setting this to a number will cause events to be published to a random // setting this to a number will cause events to be published to a random
// set of relays only, instead of publishing to all relays all the time // set of relays only, instead of publishing to all relays all the time
randomChoice: null randomChoice: null,
// setting this to true will cause .publish() calls to wait until the event has
// been published -- or at least attempted to be published -- to all relays
wait: false
} }
const relays = {} const relays = {}
const noticeCallbacks = [] const noticeCallbacks = []
@@ -111,7 +115,7 @@ export function relayPool() {
let index = noticeCallbacks.indexOf(cb) let index = noticeCallbacks.indexOf(cb)
if (index !== -1) noticeCallbacks.splice(index, 1) if (index !== -1) noticeCallbacks.splice(index, 1)
}, },
async publish(event, statusCallback = (status, relayURL) => {}) { async publish(event, statusCallback) {
event.id = getEventHash(event) event.id = getEventHash(event)
if (!event.sig) { if (!event.sig) {
@@ -136,6 +140,7 @@ export function relayPool() {
let successes = 0 let successes = 0
if (poolPolicy.wait) {
for (let i = 0; i < writeable.length; i++) { for (let i = 0; i < writeable.length; i++) {
let {relay} = writeable[i] let {relay} = writeable[i]
@@ -143,11 +148,11 @@ export function relayPool() {
await new Promise(async (resolve, reject) => { await new Promise(async (resolve, reject) => {
try { try {
await relay.publish(event, status => { await relay.publish(event, status => {
statusCallback(status, relay.url) if (statusCallback) statusCallback(status, relay.url)
resolve() resolve()
}) })
} catch (err) { } catch (err) {
statusCallback(-1, relay.url) if (statusCallback) statusCallback(-1, relay.url)
} }
}) })
@@ -159,6 +164,14 @@ export function relayPool() {
/***/ /***/
} }
} }
} else {
writeable.forEach(async ({relay}) => {
let callback = statusCallback
? status => statusCallback(status, relay.url)
: null
relay.publish(event, callback)
})
}
return event return event
} }