nostr-tools 0.20.0 → 0.21.2
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/nip04.js +11 -8
- package/nip05.js +6 -0
- package/package.json +1 -1
package/nip04.js
CHANGED
|
@@ -5,7 +5,7 @@ import * as secp256k1 from '@noble/secp256k1'
|
|
|
5
5
|
|
|
6
6
|
export function encrypt(privkey, pubkey, text) {
|
|
7
7
|
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
|
8
|
-
const normalizedKey =
|
|
8
|
+
const normalizedKey = getNormalizedX(key)
|
|
9
9
|
|
|
10
10
|
let iv = Uint8Array.from(randomBytes(16))
|
|
11
11
|
var cipher = aes.createCipheriv(
|
|
@@ -16,24 +16,27 @@ export function encrypt(privkey, pubkey, text) {
|
|
|
16
16
|
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
|
17
17
|
encryptedMessage += cipher.final('base64')
|
|
18
18
|
|
|
19
|
-
return
|
|
19
|
+
return `${encryptedMessage}?iv=${Buffer.from(iv.buffer).toString('base64')}`
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function decrypt(privkey, pubkey, ciphertext
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
export function decrypt(privkey, pubkey, ciphertext) {
|
|
23
|
+
let [cip, iv] = ciphertext.split('?iv=')
|
|
24
|
+
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
|
25
|
+
let normalizedKey = getNormalizedX(key)
|
|
25
26
|
|
|
26
27
|
var decipher = aes.createDecipheriv(
|
|
27
28
|
'aes-256-cbc',
|
|
28
29
|
Buffer.from(normalizedKey, 'hex'),
|
|
29
30
|
Buffer.from(iv, 'base64')
|
|
30
31
|
)
|
|
31
|
-
let decryptedMessage = decipher.update(
|
|
32
|
+
let decryptedMessage = decipher.update(cip, 'base64')
|
|
32
33
|
decryptedMessage += decipher.final('utf8')
|
|
33
34
|
|
|
34
35
|
return decryptedMessage
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
function
|
|
38
|
-
return
|
|
38
|
+
function getNormalizedX(key) {
|
|
39
|
+
return typeof key === 'string'
|
|
40
|
+
? key.substr(2)
|
|
41
|
+
: Buffer.from(key.slice(1)).toString('hex')
|
|
39
42
|
}
|
package/nip05.js
CHANGED
|
@@ -15,6 +15,12 @@ export async function searchDomain(domain, query = '') {
|
|
|
15
15
|
export async function queryName(fullname) {
|
|
16
16
|
try {
|
|
17
17
|
let [name, domain] = fullname.split('@')
|
|
18
|
+
|
|
19
|
+
if (!domain) {
|
|
20
|
+
domain = name
|
|
21
|
+
name = '_'
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
let res = await (
|
|
19
25
|
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
|
20
26
|
).json()
|