mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c66a2acda1 | ||
|
|
6f07c756e5 | ||
|
|
f6bcda8d8d | ||
|
|
4b666e421b |
13
nip04.js
13
nip04.js
@@ -16,24 +16,25 @@ export function encrypt(privkey, pubkey, text) {
|
||||
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
||||
encryptedMessage += cipher.final('base64')
|
||||
|
||||
return [encryptedMessage, Buffer.from(iv.buffer).toString('base64')]
|
||||
return `${encryptedMessage}?iv=${Buffer.from(iv.buffer).toString('base64')}`
|
||||
}
|
||||
|
||||
export function decrypt(privkey, pubkey, ciphertext, iv) {
|
||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
const normalizedKey = getOnlyXFromFullSharedSecret(key)
|
||||
export function decrypt(privkey, pubkey, ciphertext) {
|
||||
let [cip, iv] = ciphertext.split('?iv=')
|
||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
let normalizedKey = getOnlyXFromFullSharedSecret(key)
|
||||
|
||||
var decipher = aes.createDecipheriv(
|
||||
'aes-256-cbc',
|
||||
Buffer.from(normalizedKey, 'hex'),
|
||||
Buffer.from(iv, 'base64')
|
||||
)
|
||||
let decryptedMessage = decipher.update(ciphertext, 'base64')
|
||||
let decryptedMessage = decipher.update(cip, 'base64')
|
||||
decryptedMessage += decipher.final('utf8')
|
||||
|
||||
return decryptedMessage
|
||||
}
|
||||
|
||||
function getOnlyXFromFullSharedSecret(fullSharedSecretCoordinates) {
|
||||
return fullSharedSecretCoordinates.substr(2, 64)
|
||||
return Buffer.from(fullSharedSecretCoordinates).toString('hex').substr(2, 64)
|
||||
}
|
||||
|
||||
74
nip05.js
74
nip05.js
@@ -1,52 +1,32 @@
|
||||
import {Buffer} from 'buffer'
|
||||
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++
|
||||
import fetch from 'cross-fetch'
|
||||
|
||||
export async function searchDomain(domain, query = '') {
|
||||
try {
|
||||
let response = Buffer.from(await (await fetching).arrayBuffer())
|
||||
let {answers} = dnsPacket.decode(response)
|
||||
if (answers.length === 0) return null
|
||||
return Buffer.from(answers[0].data[0]).toString()
|
||||
} catch (err) {
|
||||
console.log(`error querying DNS for ${domain} on ${host}`, err)
|
||||
let res = await (
|
||||
await fetch(`https://${domain}/.well-known/nostr.json?name=${query}`)
|
||||
).json()
|
||||
|
||||
return res.names
|
||||
} catch (_) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export async function queryName(fullname) {
|
||||
try {
|
||||
let [name, domain] = fullname.split('@')
|
||||
|
||||
if (!domain) {
|
||||
domain = name
|
||||
name = '_'
|
||||
}
|
||||
|
||||
let res = await (
|
||||
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
).json()
|
||||
|
||||
return res.names && res.names[name]
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "0.19.0",
|
||||
"version": "0.21.1",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -12,7 +12,7 @@
|
||||
"browserify-cipher": ">=1",
|
||||
"buffer": ">=5",
|
||||
"create-hash": "^1.2.0",
|
||||
"dns-packet": "^5.2.4",
|
||||
"cross-fetch": "^3.1.4",
|
||||
"micro-bip32": "^0.1.0",
|
||||
"micro-bip39": "^0.1.3",
|
||||
"websocket-polyfill": "^0.0.3"
|
||||
|
||||
Reference in New Issue
Block a user