mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b666e421b | ||
|
|
454366f6a2 |
68
nip05.js
68
nip05.js
@@ -1,52 +1,26 @@
|
|||||||
import {Buffer} from 'buffer'
|
import fetch from 'cross-fetch'
|
||||||
import dnsPacket from 'dns-packet'
|
|
||||||
|
|
||||||
const dohProviders = [
|
|
||||||
'cloudflare-dns.com',
|
|
||||||
'fi.doh.dns.snopyta.org',
|
|
||||||
'basic.bravedns.com',
|
|
||||||
'hydra.plan9-ns1.com',
|
|
||||||
'doh.pl.ahadns.net',
|
|
||||||
'dns.flatuslifir.is',
|
|
||||||
'doh.dns.sb',
|
|
||||||
'doh.li'
|
|
||||||
]
|
|
||||||
|
|
||||||
let counter = 0
|
|
||||||
|
|
||||||
export async function keyFromDomain(domain) {
|
|
||||||
let host = dohProviders[counter % dohProviders.length]
|
|
||||||
|
|
||||||
let buf = dnsPacket.encode({
|
|
||||||
type: 'query',
|
|
||||||
id: Math.floor(Math.random() * 65534),
|
|
||||||
flags: dnsPacket.RECURSION_DESIRED,
|
|
||||||
questions: [
|
|
||||||
{
|
|
||||||
type: 'TXT',
|
|
||||||
name: `_nostrkey.${domain}`
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
|
|
||||||
let fetching = fetch(`https://${host}/dns-query`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/dns-message',
|
|
||||||
'Content-Length': Buffer.byteLength(buf)
|
|
||||||
},
|
|
||||||
body: buf
|
|
||||||
})
|
|
||||||
|
|
||||||
counter++
|
|
||||||
|
|
||||||
|
export async function searchDomain(domain, query = '') {
|
||||||
try {
|
try {
|
||||||
let response = Buffer.from(await (await fetching).arrayBuffer())
|
let res = await (
|
||||||
let {answers} = dnsPacket.decode(response)
|
await fetch(`https://${domain}/.well-known/nostr.json?name=${query}`)
|
||||||
if (answers.length === 0) return null
|
).json()
|
||||||
return Buffer.from(answers[0].data[0]).toString()
|
|
||||||
} catch (err) {
|
return res.names
|
||||||
console.log(`error querying DNS for ${domain} on ${host}`, err)
|
} catch (_) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function queryName(fullname) {
|
||||||
|
try {
|
||||||
|
let [name, domain] = fullname.split('@')
|
||||||
|
let res = await (
|
||||||
|
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
|
).json()
|
||||||
|
|
||||||
|
return res.names && res.names[name]
|
||||||
|
} catch (_) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.18.0",
|
"version": "0.20.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
"browserify-cipher": ">=1",
|
"browserify-cipher": ">=1",
|
||||||
"buffer": ">=5",
|
"buffer": ">=5",
|
||||||
"create-hash": "^1.2.0",
|
"create-hash": "^1.2.0",
|
||||||
"dns-packet": "^5.2.4",
|
"cross-fetch": "^3.1.4",
|
||||||
"micro-bip32": "^0.1.0",
|
"micro-bip32": "^0.1.0",
|
||||||
"micro-bip39": "^0.1.3",
|
"micro-bip39": "^0.1.3",
|
||||||
"websocket-polyfill": "^0.0.3"
|
"websocket-polyfill": "^0.0.3"
|
||||||
|
|||||||
21
pool.js
21
pool.js
@@ -1,8 +1,10 @@
|
|||||||
import {getEventHash, signEvent} from './event'
|
import {getEventHash, verifySignature, signEvent} from './event'
|
||||||
import {relayConnect, normalizeRelayURL} from './relay'
|
import {relayConnect, normalizeRelayURL} from './relay'
|
||||||
|
|
||||||
export function relayPool() {
|
export function relayPool() {
|
||||||
var globalPrivateKey
|
var globalPrivateKey
|
||||||
|
var globalSigningFunction
|
||||||
|
|
||||||
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
|
||||||
@@ -76,6 +78,9 @@ export function relayPool() {
|
|||||||
setPrivateKey(privateKey) {
|
setPrivateKey(privateKey) {
|
||||||
globalPrivateKey = privateKey
|
globalPrivateKey = privateKey
|
||||||
},
|
},
|
||||||
|
registerSigningFunction(fn) {
|
||||||
|
globalSigningFunction = fn
|
||||||
|
},
|
||||||
setPolicy(key, value) {
|
setPolicy(key, value) {
|
||||||
poolPolicy[key] = value
|
poolPolicy[key] = value
|
||||||
},
|
},
|
||||||
@@ -123,9 +128,21 @@ export function relayPool() {
|
|||||||
|
|
||||||
if (globalPrivateKey) {
|
if (globalPrivateKey) {
|
||||||
event.sig = await signEvent(event, globalPrivateKey)
|
event.sig = await signEvent(event, globalPrivateKey)
|
||||||
|
} else if (globalSigningFunction) {
|
||||||
|
event.sig = await globalSigningFunction(event)
|
||||||
|
if (!event.sig) {
|
||||||
|
// abort here
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// check
|
||||||
|
if (!(await verifySignature(event)))
|
||||||
|
throw new Error(
|
||||||
|
'signature provided by custom signing function is invalid.'
|
||||||
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"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."
|
"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."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user