nostr-tools 0.22.1 → 0.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,3 +70,20 @@ pool.addRelay('<url>')
70
70
  All functions expect bytearrays as hex strings and output bytearrays as hex strings.
71
71
 
72
72
  For other utils please read the source (for now).
73
+
74
+ ### Using from the browser (if you don't want to use a bundler)
75
+
76
+ You can import nostr-tools as an ES module. Just add a script tag like this:
77
+
78
+ ```html
79
+ <script type="module">
80
+ import {generatePrivateKey} from 'https://unpkg.com/nostr-tools/nostr.js'
81
+ console.log(generatePrivateKey())
82
+ </script>
83
+ ```
84
+
85
+ And import whatever function you would import from `"nostr-tools"` in a bundler.
86
+
87
+ ## License
88
+
89
+ Public domain.
package/build.js ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+
3
+ const esbuild = require('esbuild')
4
+ const alias = require('esbuild-plugin-alias')
5
+ const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default
6
+
7
+ const buildOptions = {
8
+ entryPoints: ['index.js'],
9
+ outfile: 'nostr.js',
10
+ bundle: true,
11
+ format: 'esm',
12
+ plugins: [
13
+ alias({
14
+ stream: require.resolve('readable-stream')
15
+ }),
16
+ nodeGlobals({buffer: true})
17
+ ],
18
+ define: {
19
+ window: 'self',
20
+ global: 'self'
21
+ },
22
+ loader: {'.js': 'jsx'}
23
+ }
24
+
25
+ esbuild.build(buildOptions).then(() => console.log('build success.'))
package/keys.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as secp256k1 from '@noble/secp256k1'
2
+ import {Buffer} from 'buffer'
2
3
 
3
4
  export function generatePrivateKey() {
4
5
  return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex')
package/nip05.js CHANGED
@@ -15,11 +15,7 @@ export async function searchDomain(domain, query = '') {
15
15
  export async function queryName(fullname) {
16
16
  try {
17
17
  let [name, domain] = fullname.split('@')
18
-
19
- if (!domain) {
20
- domain = name
21
- name = '_'
22
- }
18
+ if (!domain) return null
23
19
 
24
20
  let res = await (
25
21
  await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-tools",
3
- "version": "0.22.1",
3
+ "version": "0.23.1",
4
4
  "description": "Tools for making a Nostr client.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,15 @@
30
30
  "client"
31
31
  ],
32
32
  "devDependencies": {
33
+ "@esbuild-plugins/node-globals-polyfill": "^0.1.1",
34
+ "esbuild": "^0.14.38",
35
+ "esbuild-plugin-alias": "^0.2.1",
33
36
  "eslint": "^8.5.0",
34
- "eslint-plugin-babel": "^5.3.1"
37
+ "eslint-plugin-babel": "^5.3.1",
38
+ "events": "^3.3.0",
39
+ "readable-stream": "^3.6.0"
40
+ },
41
+ "scripts": {
42
+ "prepublish": "node build.js"
35
43
  }
36
44
  }
package/pool.js CHANGED
@@ -26,27 +26,35 @@ export function relayPool() {
26
26
 
27
27
  const activeSubscriptions = {}
28
28
 
29
- const sub = ({cb, filter}, id = Math.random().toString().slice(2)) => {
29
+ const sub = (
30
+ {cb, filter, beforeSend},
31
+ id = Math.random().toString().slice(2)
32
+ ) => {
30
33
  const subControllers = Object.fromEntries(
31
34
  Object.values(relays)
32
35
  .filter(({policy}) => policy.read)
33
36
  .map(({relay}) => [
34
37
  relay.url,
35
- relay.sub({filter, cb: event => cb(event, relay.url)}, id)
38
+ relay.sub({filter, cb: event => cb(event, relay.url), beforeSend}, id)
36
39
  ])
37
40
  )
38
41
 
39
42
  const activeCallback = cb
40
43
  const activeFilters = filter
44
+ const activeBeforeSend = beforeSend
41
45
 
42
46
  const unsub = () => {
43
47
  Object.values(subControllers).forEach(sub => sub.unsub())
44
48
  delete activeSubscriptions[id]
45
49
  }
46
- const sub = ({cb = activeCallback, filter = activeFilters}) => {
50
+ const sub = ({
51
+ cb = activeCallback,
52
+ filter = activeFilters,
53
+ beforeSend = activeBeforeSend
54
+ }) => {
47
55
  Object.entries(subControllers).map(([relayURL, sub]) => [
48
56
  relayURL,
49
- sub.sub({cb, filter}, id)
57
+ sub.sub({cb, filter, beforeSend}, id)
50
58
  ])
51
59
  return activeSubscriptions[id]
52
60
  }
package/relay.js CHANGED
@@ -119,7 +119,10 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
119
119
  ws.send(msg)
120
120
  }
121
121
 
122
- const sub = ({cb, filter}, channel = Math.random().toString().slice(2)) => {
122
+ const sub = (
123
+ {cb, filter, beforeSend},
124
+ channel = Math.random().toString().slice(2)
125
+ ) => {
123
126
  var filters = []
124
127
  if (Array.isArray(filter)) {
125
128
  filters = filter
@@ -127,16 +130,25 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
127
130
  filters.push(filter)
128
131
  }
129
132
 
133
+ if (beforeSend) {
134
+ const beforeSendResult = beforeSend({filter, relay: url, channel})
135
+ filters = beforeSendResult.filter
136
+ }
137
+
130
138
  trySend(['REQ', channel, ...filters])
131
139
  channels[channel] = cb
132
140
  openSubs[channel] = filters
133
141
 
134
142
  const activeCallback = cb
135
143
  const activeFilters = filters
144
+ const activeBeforeSend = beforeSend
136
145
 
137
146
  return {
138
- sub: ({cb = activeCallback, filter = activeFilters}) =>
139
- sub({cb, filter}, channel),
147
+ sub: ({
148
+ cb = activeCallback,
149
+ filter = activeFilters,
150
+ beforeSend = activeBeforeSend
151
+ }) => sub({cb, filter, beforeSend}, channel),
140
152
  unsub: () => {
141
153
  delete openSubs[channel]
142
154
  delete channels[channel]
@@ -160,7 +172,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
160
172
  unsub()
161
173
  clearTimeout(willUnsub)
162
174
  },
163
- filter: {id: event.id}
175
+ filter: {ids: [event.id]}
164
176
  },
165
177
  `monitor-${event.id.slice(0, 5)}`
166
178
  )