Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update prop type for encryption related functions #318

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions src/encryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,15 +84,15 @@ 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',
};

expect(() =>
decrypt({
encryptedData: badVersionData,
encryptedData: badVersionData as never,
privateKey: bob.ethereumPrivateKey,
}),
).toThrow('Encryption type/version not supported.');
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
10 changes: 5 additions & 5 deletions src/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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');
Expand Down