Compare commits

..

1 Commits

Author SHA1 Message Date
fiatjaf
84e4fb1f92 update noble secp256k1 and ensure we always return hex. 2022-02-11 16:20:47 -03:00
9 changed files with 19 additions and 89 deletions

1
.gitignore vendored
View File

@@ -2,4 +2,3 @@ node_modules
dist
yarn.lock
package-lock.json
nostr.js

View File

@@ -70,20 +70,3 @@ pool.addRelay('<url>')
All functions expect bytearrays as hex strings and output bytearrays as hex strings.
For other utils please read the source (for now).
### Using from the browser (if you don't want to use a bundler)
You can import nostr-tools as an ES module. Just add a script tag like this:
```html
<script type="module">
import {generatePrivateKey} from 'https://unpkg.com/nostr-tools/nostr.js'
console.log(generatePrivateKey())
</script>
```
And import whatever function you would import from `"nostr-tools"` in a bundler.
## License
Public domain.

View File

@@ -1,25 +0,0 @@
#!/usr/bin/env node
const esbuild = require('esbuild')
const alias = require('esbuild-plugin-alias')
const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default
const buildOptions = {
entryPoints: ['index.js'],
outfile: 'nostr.js',
bundle: true,
format: 'esm',
plugins: [
alias({
stream: require.resolve('readable-stream')
}),
nodeGlobals({buffer: true})
],
define: {
window: 'self',
global: 'self'
},
loader: {'.js': 'jsx'}
}
esbuild.build(buildOptions).then(() => console.log('build success.'))

View File

@@ -52,7 +52,7 @@ export function verifySignature(event) {
}
export async function signEvent(event, key) {
return Buffer.from(
await secp256k1.schnorr.sign(getEventHash(event), key)
).toString('hex')
return Buffer.from(secp256k1.schnorr.sign(getEventHash(event), key)).toString(
'hex'
)
}

View File

@@ -1,5 +1,4 @@
import * as secp256k1 from '@noble/secp256k1'
import {Buffer} from 'buffer'
export function generatePrivateKey() {
return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex')

View File

@@ -15,7 +15,11 @@ export async function searchDomain(domain, query = '') {
export async function queryName(fullname) {
try {
let [name, domain] = fullname.split('@')
if (!domain) return null
if (!domain) {
domain = name
name = '_'
}
let res = await (
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.23.4",
"version": "0.22.0",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
@@ -30,15 +30,7 @@
"client"
],
"devDependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
"esbuild": "^0.14.38",
"esbuild-plugin-alias": "^0.2.1",
"eslint": "^8.5.0",
"eslint-plugin-babel": "^5.3.1",
"events": "^3.3.0",
"readable-stream": "^3.6.0"
},
"scripts": {
"prepublish": "node build.js"
"eslint-plugin-babel": "^5.3.1"
}
}

20
pool.js
View File

@@ -26,42 +26,32 @@ export function relayPool() {
const activeSubscriptions = {}
const sub = ({cb, filter, beforeSend}, id) => {
if (!id) id = Math.random().toString().slice(2)
const sub = ({cb, filter}, id = Math.random().toString().slice(2)) => {
const subControllers = Object.fromEntries(
Object.values(relays)
.filter(({policy}) => policy.read)
.map(({relay}) => [
relay.url,
relay.sub({cb: event => cb(event, relay.url), filter, beforeSend}, id)
relay.sub({filter, cb: event => cb(event, relay.url)}, id)
])
)
const activeCallback = cb
const activeFilters = filter
const activeBeforeSend = beforeSend
const unsub = () => {
Object.values(subControllers).forEach(sub => sub.unsub())
delete activeSubscriptions[id]
}
const sub = ({
cb = activeCallback,
filter = activeFilters,
beforeSend = activeBeforeSend
}) => {
const sub = ({cb = activeCallback, filter = activeFilters}) => {
Object.entries(subControllers).map(([relayURL, sub]) => [
relayURL,
sub.sub({cb: event => cb(event, relayURL), filter, beforeSend}, id)
sub.sub({cb, filter}, id)
])
return activeSubscriptions[id]
}
const addRelay = relay => {
subControllers[relay.url] = relay.sub(
{cb: event => cb(event, relay.url), filter, beforeSend},
id
)
subControllers[relay.url] = relay.sub({cb, filter}, id)
return activeSubscriptions[id]
}
const removeRelay = relayURL => {

View File

@@ -119,10 +119,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
ws.send(msg)
}
const sub = (
{cb, filter, beforeSend},
channel = Math.random().toString().slice(2)
) => {
const sub = ({cb, filter}, channel = Math.random().toString().slice(2)) => {
var filters = []
if (Array.isArray(filter)) {
filters = filter
@@ -130,25 +127,16 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
filters.push(filter)
}
if (beforeSend) {
const beforeSendResult = beforeSend({filter, relay: url, channel})
filters = beforeSendResult.filter
}
trySend(['REQ', channel, ...filters])
channels[channel] = cb
openSubs[channel] = filters
const activeCallback = cb
const activeFilters = filters
const activeBeforeSend = beforeSend
return {
sub: ({
cb = activeCallback,
filter = activeFilters,
beforeSend = activeBeforeSend
}) => sub({cb, filter, beforeSend}, channel),
sub: ({cb = activeCallback, filter = activeFilters}) =>
sub({cb, filter}, channel),
unsub: () => {
delete openSubs[channel]
delete channels[channel]
@@ -172,7 +160,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
unsub()
clearTimeout(willUnsub)
},
filter: {ids: [event.id]}
filter: {id: event.id}
},
`monitor-${event.id.slice(0, 5)}`
)