diff --git a/nip04.js b/nip04.js index e89dcee..f56fdbd 100644 --- a/nip04.js +++ b/nip04.js @@ -5,7 +5,7 @@ import * as secp256k1 from '@noble/secp256k1' export function encrypt(privkey, pubkey, text) { const key = secp256k1.getSharedSecret(privkey, '02' + pubkey) - const normalizedKey = getOnlyXFromFullSharedSecret(key) + const normalizedKey = getNormalizedX(key) let iv = Uint8Array.from(randomBytes(16)) var cipher = aes.createCipheriv( @@ -22,7 +22,7 @@ export function encrypt(privkey, pubkey, text) { export function decrypt(privkey, pubkey, ciphertext) { let [cip, iv] = ciphertext.split('?iv=') let key = secp256k1.getSharedSecret(privkey, '02' + pubkey) - let normalizedKey = getOnlyXFromFullSharedSecret(key) + let normalizedKey = getNormalizedX(key) var decipher = aes.createDecipheriv( 'aes-256-cbc', @@ -35,6 +35,8 @@ export function decrypt(privkey, pubkey, ciphertext) { return decryptedMessage } -function getOnlyXFromFullSharedSecret(fullSharedSecretCoordinates) { - return Buffer.from(fullSharedSecretCoordinates).toString('hex').substr(2, 64) +function getNormalizedX(key) { + return typeof key === 'string' + ? key.substr(2) + : Buffer.from(key.slice(1)).toString('hex') } diff --git a/package.json b/package.json index 336d4b5..344f82d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nostr-tools", - "version": "0.21.1", + "version": "0.21.2", "description": "Tools for making a Nostr client.", "repository": { "type": "git",