mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0396db5ed6 | ||
|
|
0c8e7a74f5 | ||
|
|
c66a2acda1 | ||
|
|
6f07c756e5 | ||
|
|
f6bcda8d8d |
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)).toString('hex')
|
||||||
}
|
}
|
||||||
|
|||||||
6
nip05.js
6
nip05.js
@@ -15,6 +15,12 @@ export async function searchDomain(domain, query = '') {
|
|||||||
export async function queryName(fullname) {
|
export async function queryName(fullname) {
|
||||||
try {
|
try {
|
||||||
let [name, domain] = fullname.split('@')
|
let [name, domain] = fullname.split('@')
|
||||||
|
|
||||||
|
if (!domain) {
|
||||||
|
domain = name
|
||||||
|
name = '_'
|
||||||
|
}
|
||||||
|
|
||||||
let res = await (
|
let res = await (
|
||||||
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
).json()
|
).json()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.20.0",
|
"version": "0.21.3",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user