Compare commits

...

4 Commits

2 changed files with 7 additions and 5 deletions

View File

@@ -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(
@@ -22,7 +22,7 @@ export function encrypt(privkey, pubkey, text) {
export function decrypt(privkey, pubkey, ciphertext) { export function decrypt(privkey, pubkey, ciphertext) {
let [cip, iv] = ciphertext.split('?iv=') let [cip, iv] = ciphertext.split('?iv=')
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey) let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
let normalizedKey = getOnlyXFromFullSharedSecret(key) let normalizedKey = getNormalizedX(key)
var decipher = aes.createDecipheriv( var decipher = aes.createDecipheriv(
'aes-256-cbc', 'aes-256-cbc',
@@ -35,6 +35,8 @@ export function decrypt(privkey, pubkey, ciphertext) {
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')
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "0.21.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",