nostr-tools 0.18.0 → 0.19.0
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/package.json +1 -1
- package/pool.js +19 -2
package/package.json
CHANGED
package/pool.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {getEventHash, signEvent} from './event'
|
|
1
|
+
import {getEventHash, verifySignature, signEvent} from './event'
|
|
2
2
|
import {relayConnect, normalizeRelayURL} from './relay'
|
|
3
3
|
|
|
4
4
|
export function relayPool() {
|
|
5
5
|
var globalPrivateKey
|
|
6
|
+
var globalSigningFunction
|
|
7
|
+
|
|
6
8
|
const poolPolicy = {
|
|
7
9
|
// setting this to a number will cause events to be published to a random
|
|
8
10
|
// set of relays only, instead of publishing to all relays all the time
|
|
@@ -76,6 +78,9 @@ export function relayPool() {
|
|
|
76
78
|
setPrivateKey(privateKey) {
|
|
77
79
|
globalPrivateKey = privateKey
|
|
78
80
|
},
|
|
81
|
+
registerSigningFunction(fn) {
|
|
82
|
+
globalSigningFunction = fn
|
|
83
|
+
},
|
|
79
84
|
setPolicy(key, value) {
|
|
80
85
|
poolPolicy[key] = value
|
|
81
86
|
},
|
|
@@ -123,9 +128,21 @@ export function relayPool() {
|
|
|
123
128
|
|
|
124
129
|
if (globalPrivateKey) {
|
|
125
130
|
event.sig = await signEvent(event, globalPrivateKey)
|
|
131
|
+
} else if (globalSigningFunction) {
|
|
132
|
+
event.sig = await globalSigningFunction(event)
|
|
133
|
+
if (!event.sig) {
|
|
134
|
+
// abort here
|
|
135
|
+
return
|
|
136
|
+
} else {
|
|
137
|
+
// check
|
|
138
|
+
if (!(await verifySignature(event)))
|
|
139
|
+
throw new Error(
|
|
140
|
+
'signature provided by custom signing function is invalid.'
|
|
141
|
+
)
|
|
142
|
+
}
|
|
126
143
|
} else {
|
|
127
144
|
throw new Error(
|
|
128
|
-
"can't publish unsigned event. either sign this event beforehand or pass a private key while initializing this relay pool so it can be signed automatically."
|
|
145
|
+
"can't publish unsigned event. either sign this event beforehand, provide a signing function or pass a private key while initializing this relay pool so it can be signed automatically."
|
|
129
146
|
)
|
|
130
147
|
}
|
|
131
148
|
}
|