mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6786641b1d | ||
|
|
0396db5ed6 | ||
|
|
0c8e7a74f5 | ||
|
|
c66a2acda1 | ||
|
|
6f07c756e5 | ||
|
|
f6bcda8d8d | ||
|
|
4b666e421b |
19
nip04.js
19
nip04.js
@@ -5,7 +5,7 @@ import * as secp256k1 from '@noble/secp256k1'
|
|||||||
|
|
||||||
export function encrypt(privkey, pubkey, text) {
|
export function encrypt(privkey, pubkey, text) {
|
||||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
const normalizedKey = getOnlyXFromFullSharedSecret(key)
|
const normalizedKey = getNormalizedX(key)
|
||||||
|
|
||||||
let iv = Uint8Array.from(randomBytes(16))
|
let iv = Uint8Array.from(randomBytes(16))
|
||||||
var cipher = aes.createCipheriv(
|
var cipher = aes.createCipheriv(
|
||||||
@@ -16,24 +16,27 @@ export function encrypt(privkey, pubkey, text) {
|
|||||||
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
||||||
encryptedMessage += cipher.final('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) {
|
export function decrypt(privkey, pubkey, ciphertext) {
|
||||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
let [cip, iv] = ciphertext.split('?iv=')
|
||||||
const normalizedKey = getOnlyXFromFullSharedSecret(key)
|
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
|
let normalizedKey = getNormalizedX(key)
|
||||||
|
|
||||||
var decipher = aes.createDecipheriv(
|
var decipher = aes.createDecipheriv(
|
||||||
'aes-256-cbc',
|
'aes-256-cbc',
|
||||||
Buffer.from(normalizedKey, 'hex'),
|
Buffer.from(normalizedKey, 'hex'),
|
||||||
Buffer.from(iv, 'base64')
|
Buffer.from(iv, 'base64')
|
||||||
)
|
)
|
||||||
let decryptedMessage = decipher.update(ciphertext, 'base64')
|
let decryptedMessage = decipher.update(cip, 'base64')
|
||||||
decryptedMessage += decipher.final('utf8')
|
decryptedMessage += decipher.final('utf8')
|
||||||
|
|
||||||
return decryptedMessage
|
return decryptedMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOnlyXFromFullSharedSecret(fullSharedSecretCoordinates) {
|
function getNormalizedX(key) {
|
||||||
return fullSharedSecretCoordinates.substr(2, 64)
|
return typeof key === 'string'
|
||||||
|
? key.substr(2, 64)
|
||||||
|
: Buffer.from(key.slice(1, 33)).toString('hex')
|
||||||
}
|
}
|
||||||
|
|||||||
74
nip05.js
74
nip05.js
@@ -1,52 +1,32 @@
|
|||||||
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('@')
|
||||||
|
|
||||||
|
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
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.19.0",
|
"version": "0.21.4",
|
||||||
"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"
|
||||||
|
|||||||
Reference in New Issue
Block a user