]> zoso.dev Git - libnemo.git/commitdiff
Version 1.4.2
authorMiro Metsänheimo <miro@metsanheimo.fi>
Sun, 24 Apr 2022 19:59:00 +0000 (22:59 +0300)
committerMiro Metsänheimo <miro@metsanheimo.fi>
Sun, 24 Apr 2022 19:59:00 +0000 (22:59 +0300)
* Fix encrypting and decrypting emojis

README.md
lib/box.ts
lib/util/convert.ts
package-lock.json
package.json
test/test.js

index 98a65cdaf2132069d53427c114f6f1df3a2634f5..f2a84033a4d8355290fced5af44876c357aa2178 100644 (file)
--- a/README.md
+++ b/README.md
@@ -33,7 +33,7 @@ npm install nanocurrency-web
 ### In web
 
 ```html
-<script src="https://unpkg.com/nanocurrency-web@1.4.1" type="text/javascript"></script>
+<script src="https://unpkg.com/nanocurrency-web@1.4.2" type="text/javascript"></script>
 <script type="text/javascript">
     NanocurrencyWeb.wallet.generate(...);
 </script>
index 25c4c16c900920f2ab21973e9ab7469bdec633e0..3389466d949e060e473235b11a35ae2c32de5041 100644 (file)
@@ -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)
        }
 
 }
index a6634c2a430055e6aead37e4193b0e4769276880..4bf819803d862266ea33fa2a045e1bed076f600b 100644 (file)
@@ -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
         *
index 4b5408f4f3cf11dcf9c8fee45a2e51cd5424eef5..b4cb72f4d920b6b3ca4cf419b87123400351cc11 100644 (file)
@@ -1,6 +1,6 @@
 {
        "name": "nanocurrency-web",
-       "version": "1.4.1",
+       "version": "1.4.2",
        "lockfileVersion": 1,
        "requires": true,
        "dependencies": {
index 5b39c7e13a9904f70a8dec4035cc6a54da96f87f..0d2c630da198529c36ae0b2fbd7619d6d9d124d1 100644 (file)
@@ -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 <miro@metsanheimo.fi>",
        "license": "MIT",
index e7f3168f9a9e9a2dd6b34d3ff22aacc5951703e4..1f03febfdb1096d39c2544d410a3e5b051160fe3 100644 (file)
@@ -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()
        })