From 8b531fa570ca7cfadf13d7acec3fdde013b6e78e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Miro=20Mets=C3=A4nheimo?= Date: Sun, 24 Apr 2022 22:59:00 +0300 Subject: [PATCH] Version 1.4.2 * Fix encrypting and decrypting emojis --- README.md | 2 +- lib/box.ts | 4 ++-- lib/util/convert.ts | 27 ++++++++++++++++++++++----- package-lock.json | 2 +- package.json | 2 +- test/test.js | 2 +- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 98a65cd..f2a8403 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ npm install nanocurrency-web ### In web ```html - + diff --git a/lib/box.ts b/lib/box.ts index 25c4c16..3389466 100644 --- a/lib/box.ts +++ b/lib/box.ts @@ -24,7 +24,7 @@ export default class Box { const nonce = Convert.hex2ab(lib.WordArray.random(this.NONCE_LENGTH).toString()) const encrypted = new Curve25519().box( - Convert.str2bin(message), + Convert.decodeUTF8(message), nonce, Convert.hex2ab(convertedPublicKey), Convert.hex2ab(convertedPrivateKey), @@ -63,7 +63,7 @@ export default class Box { throw new Error('Could not decrypt message') } - return Convert.bin2str(decrypted) + return Convert.encodeUTF8(decrypted) } } diff --git a/lib/util/convert.ts b/lib/util/convert.ts index a6634c2..4bf8198 100644 --- a/lib/util/convert.ts +++ b/lib/util/convert.ts @@ -30,17 +30,34 @@ export default class Convert { * Convert a byte array to a UTF-8 encoded string * * @param {Uint8Array} arr Byte array - * @return {String} UTF-8 encoded string + * @return {string} UTF-8 encoded string */ - static bin2str = (arr: Uint8Array) => { - let i, s = [] - for (i = 0; i < arr.length; i++) { + static encodeUTF8 = (arr: Uint8Array): string => { + const s = [] + for (let i = 0; i < arr.length; i++) { s.push(String.fromCharCode(arr[i])) } - return decodeURIComponent(escape(s.join(''))) } + /** + * Convert a UTF-8 encoded string to a byte array + * + * @param {string} str UTF-8 encoded string + * @return {Uint8Array} Byte array + */ + static decodeUTF8 = (str: string): Uint8Array => { + if (typeof str !== 'string') { + throw new TypeError('expected string') + } + const d = unescape(encodeURIComponent(str)) + const b = new Uint8Array(d.length) + for (let i = 0; i < d.length; i++) { + b[i] = d.charCodeAt(i) + } + return b + } + /** * Convert Array of 8 bytes (int64) to hex string * diff --git a/package-lock.json b/package-lock.json index 4b5408f..b4cb72f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nanocurrency-web", - "version": "1.4.1", + "version": "1.4.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5b39c7e..0d2c630 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nanocurrency-web", - "version": "1.4.1", + "version": "1.4.2", "description": "Toolkit for Nano cryptocurrency client side offline integrations", "author": "Miro Metsänheimo ", "license": "MIT", diff --git a/test/test.js b/test/test.js index e7f3168..1f03feb 100644 --- a/test/test.js +++ b/test/test.js @@ -332,7 +332,7 @@ describe('Signer tests', () => { describe('Box tests', () => { before(() => { - this.message = 'The quick brown fox jumps over the lazy dog' + this.message = 'The quick brown fox jumps over the lazy dog 🔥' this.bob = wallet.generate() this.alice = wallet.generateLegacy() }) -- 2.34.1