From 2832832849e2da56b914c2c5c4c5e3f21c902470 Mon Sep 17 00:00:00 2001 From: promet99 <54811538+promet99@users.noreply.github.com> Date: Sat, 1 Jul 2023 23:07:47 +0900 Subject: [PATCH 1/2] fix: update prop type for function encrypt --- src/encryption.test.ts | 20 ++++++++++---------- src/encryption.ts | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/encryption.test.ts b/src/encryption.test.ts index 779bc2e9..3a393681 100644 --- a/src/encryption.test.ts +++ b/src/encryption.test.ts @@ -17,11 +17,11 @@ describe('encryption', function () { const secretMessage = 'My name is Satoshi Buterin'; const encryptedData = { - version: 'x25519-xsalsa20-poly1305', + version: 'x25519-xsalsa20-poly1305' as const, nonce: '1dvWO7uOnBnO7iNDJ9kO9pTasLuKNlej', ephemPublicKey: 'FBH1/pAEHOOW14Lu3FWkgV3qOEcuL78Zy+qW1RwzMXQ=', ciphertext: 'f8kBcl/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy', - }; + } as const; it("getting bob's encryptionPublicKey", async function () { const result = getEncryptionPublicKey(bob.ethereumPrivateKey); @@ -84,7 +84,7 @@ describe('encryption', function () { it('decryption failed because version is wrong or missing', function () { const badVersionData = { - version: 'x256k1-aes256cbc', + version: 'x256k1-aes256cbc' as const, nonce: '1dvWO7uOnBnO7iNDJ9kO9pTasLuKNlej', ephemPublicKey: 'FBH1/pAEHOOW14Lu3FWkgV3qOEcuL78Zy+qW1RwzMXQ=', ciphertext: 'f8kBcl/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy', @@ -92,7 +92,7 @@ describe('encryption', function () { expect(() => decrypt({ - encryptedData: badVersionData, + encryptedData: badVersionData as never, privateKey: bob.ethereumPrivateKey, }), ).toThrow('Encryption type/version not supported.'); @@ -101,7 +101,7 @@ describe('encryption', function () { it('decryption failed because nonce is wrong or missing', function () { // encrypted data const badNonceData = { - version: 'x25519-xsalsa20-poly1305', + version: 'x25519-xsalsa20-poly1305' as const, nonce: '', ephemPublicKey: 'FBH1/pAEHOOW14Lu3FWkgV3qOEcuL78Zy+qW1RwzMXQ=', ciphertext: 'f8kBcl/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy', @@ -118,7 +118,7 @@ describe('encryption', function () { it('decryption failed because ephemPublicKey is wrong or missing', function () { // encrypted data const badEphemData = { - version: 'x25519-xsalsa20-poly1305', + version: 'x25519-xsalsa20-poly1305' as const, nonce: '1dvWO7uOnBnO7iNDJ9kO9pTasLuKNlej', ephemPublicKey: 'FFFF/pAEHOOW14Lu3FWkgV3qOEcuL78Zy+qW1RwzMXQ=', ciphertext: 'f8kBcl/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy', @@ -135,7 +135,7 @@ describe('encryption', function () { it('decryption failed because cyphertext is wrong or missing', function () { // encrypted data const badEphemData = { - version: 'x25519-xsalsa20-poly1305', + version: 'x25519-xsalsa20-poly1305' as const, nonce: '1dvWO7uOnBnO7iNDJ9kO9pTasLuKNlej', ephemPublicKey: 'FBH1/pAEHOOW14Lu3FWkgV3qOEcuL78Zy+qW1RwzMXQ=', ciphertext: 'ffffff/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy', @@ -165,7 +165,7 @@ describe('encryption', function () { expect(() => encrypt({ publicKey: undefined as any, - data: secretMessage, + data: secretMessage as never, version: 'x25519-xsalsa20-poly1305', }), ).toThrow('Missing publicKey parameter'); @@ -175,7 +175,7 @@ describe('encryption', function () { expect(() => encrypt({ publicKey: bob.encryptionPublicKey, - data: null, + data: null as never, version: 'x25519-xsalsa20-poly1305', }), ).toThrow('Missing data parameter'); @@ -185,7 +185,7 @@ describe('encryption', function () { expect(() => encrypt({ publicKey: bob.encryptionPublicKey, - data: undefined, + data: undefined as never, version: 'x25519-xsalsa20-poly1305', }), ).toThrow('Missing data parameter'); diff --git a/src/encryption.ts b/src/encryption.ts index 6a0b92e4..0491e7d2 100644 --- a/src/encryption.ts +++ b/src/encryption.ts @@ -4,7 +4,7 @@ import * as naclUtil from 'tweetnacl-util'; import { isNullish } from './utils'; export type EthEncryptedData = { - version: string; + version: 'x25519-xsalsa20-poly1305'; nonce: string; ephemPublicKey: string; ciphertext: string; @@ -25,8 +25,8 @@ export function encrypt({ version, }: { publicKey: string; - data: unknown; - version: string; + data: string; + version: 'x25519-xsalsa20-poly1305'; }): EthEncryptedData { if (isNullish(publicKey)) { throw new Error('Missing publicKey parameter'); @@ -65,7 +65,7 @@ export function encrypt({ // handle encrypted data const output = { - version: 'x25519-xsalsa20-poly1305', + version: 'x25519-xsalsa20-poly1305' as const, nonce: naclUtil.encodeBase64(nonce), ephemPublicKey: naclUtil.encodeBase64(ephemeralKeyPair.publicKey), ciphertext: naclUtil.encodeBase64(encryptedMessage), @@ -98,7 +98,7 @@ export function encryptSafely({ }: { publicKey: string; data: unknown; - version: string; + version: 'x25519-xsalsa20-poly1305'; }): EthEncryptedData { if (isNullish(publicKey)) { throw new Error('Missing publicKey parameter'); From a12e6e2533369a52f6f3e6d12a40f238505a8b56 Mon Sep 17 00:00:00 2001 From: promet99 <54811538+promet99@users.noreply.github.com> Date: Sat, 1 Jul 2023 23:55:45 +0900 Subject: [PATCH 2/2] misc: update doc command on readme, and .eslintrc to ignore generated docs --- .eslintrc.js | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 88ec884e..1a18a489 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -27,5 +27,5 @@ module.exports = { 'id-length': 'off', 'no-param-reassign': 'off', }, - ignorePatterns: ['!.eslintrc.js', 'test/*.js', 'dist'], + ignorePatterns: ['!.eslintrc.js', 'test/*.js', 'dist', 'docs'], }; diff --git a/README.md b/README.md index 6bd99d76..d66f22e1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and ### Documentation -The API documentation can be generated with the command `yarn docs`, which saves it in the `./docs` directory. Open the `./docs/index.html` file to browse the documentation. +The API documentation can be generated with the command `yarn build:docs`, which saves it in the `./docs` directory. Open the `./docs/index.html` file to browse the documentation. ### Release & Publishing