diff --git a/.circleci/config.yml b/.circleci/config.yml index 19e63151b7..d55cf778f4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -275,6 +275,26 @@ jobs: root: *working_directory paths: - packages/epk-signature/coverage/ + test-epk-decryption: + docker: + - *node_image + working_directory: *working_directory + steps: + - attach_workspace: + at: *working_directory + - run: + name: 'Build epk-decryption' + command: 'yarn workspace @requestnetwork/epk-decryption run build' + - run: + name: 'Lint epk-decryption' + command: 'yarn workspace @requestnetwork/epk-decryption run lint' + - run: + name: 'Test epk-decryption' + command: 'yarn workspace @requestnetwork/epk-decryption run test' + - persist_to_workspace: + root: *working_directory + paths: + - packages/epk-decryption/coverage/ test-web3-signature: docker: - *node_image @@ -438,6 +458,9 @@ workflows: - test-epk-signature: requires: - build + - test-epk-decryption: + requires: + - build - test-web3-signature: requires: - build @@ -463,5 +486,6 @@ workflows: - test-advanced-logic - test-epk-signature - test-web3-signature + - test-epk-decryption - test-request-node - test-utils diff --git a/README.md b/README.md index 39ac19bd3f..54f7e3a884 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Join the [Request Hub][requesthub-slack-url] to get in touch with us. | [`@requestnetwork/data-format`](/packages/data-format) | [![npm](https://img.shields.io/npm/v/@requestnetwork/data-format.svg)](https://www.npmjs.com/package/@requestnetwork/data-format) | Standards for data stored on Request, like invoices format | | [`@requestnetwork/epk-signature`](/packages/epk-signature) | [![npm](https://img.shields.io/npm/v/@requestnetwork/epk-signature.svg)](https://www.npmjs.com/package/@requestnetwork/epk-signature) | Sign requests using Ethereum private keys | | [`@requestnetwork/ethereum-storage`](/packages/ethereum-storage) | [![npm](https://img.shields.io/npm/v/@requestnetwork/ethereum-storage.svg)](https://www.npmjs.com/package/@requestnetwork/ethereum-storage) | Storage of Request data on Ethereum and IPFS | +| [`@requestnetwork/epk-decryption`](/packages/epk-decryption) | [![npm](https://img.shields.io/npm/v/@requestnetwork/epk-decryption.svg)](https://www.npmjs.com/package/@requestnetwork/epk-decryption) | Decrypt encrypted requests using Ethereum private keys | | [`@requestnetwork/request-logic`](/packages/request-logic) | [![npm](https://img.shields.io/npm/v/@requestnetwork/request-logic.svg)](https://www.npmjs.com/package/@requestnetwork/request-logic) | The Request business logic: properties and actions of requests | | [`@requestnetwork/request-node`](/packages/request-node) | [![npm](https://img.shields.io/npm/v/@requestnetwork/request-node.svg)](https://www.npmjs.com/package/@requestnetwork/request-node) | Web server that allows easy access to Request system | | [`@requestnetwork/transaction-manager`](/packages/transaction-manager) | [![npm](https://img.shields.io/npm/v/@requestnetwork/transaction-manager.svg)](https://www.npmjs.com/package/@requestnetwork/transaction-manager) | Creates transactions to be sent to Data Access | diff --git a/package.json b/package.json index d17cbd99ee..fd48e8a37a 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "build": "lerna run build", "clean": "lerna run clean", - "build:tsc": "tsc -b packages/request-client.js packages/data-access packages/integration-test packages/ethereum-storage packages/request-logic packages/request-node packages/types packages/utils packages/types packages/advanced-logic packages/transaction-manager packages/epk-signature packages/data-format", + "build:tsc": "tsc -b packages/request-client.js packages/data-access packages/integration-test packages/ethereum-storage packages/request-logic packages/request-node packages/types packages/utils packages/types packages/advanced-logic packages/transaction-manager packages/epk-signature packages/epk-decryption packages/data-format", "lint": "lerna run lint", "lerna": "lerna", "packageJsonLint": "npmPkgJsonLint ./packages", diff --git a/packages/epk-decryption/.huskyrc.json b/packages/epk-decryption/.huskyrc.json new file mode 100644 index 0000000000..8c29c632a7 --- /dev/null +++ b/packages/epk-decryption/.huskyrc.json @@ -0,0 +1,5 @@ +{ + "hooks": { + "pre-commit": "yarn run lint-staged" + } +} diff --git a/packages/epk-decryption/.lintstagedrc.json b/packages/epk-decryption/.lintstagedrc.json new file mode 100644 index 0000000000..09124f6125 --- /dev/null +++ b/packages/epk-decryption/.lintstagedrc.json @@ -0,0 +1,3 @@ +{ + "src/**/*.ts": ["tslint --project . --fix", "prettier --single-quote --write", "git add"] +} diff --git a/packages/epk-decryption/.nycrc b/packages/epk-decryption/.nycrc new file mode 100644 index 0000000000..8f593ebeb3 --- /dev/null +++ b/packages/epk-decryption/.nycrc @@ -0,0 +1,19 @@ +{ + "extension": [ + ".ts" + ], + "include": [ + "src/*.ts", + "src/**/*.ts" + ], + "require": [ + "ts-node/register" + ], + "reporter": [ + "text-summary", + "json", + "html" + ], + "sourceMap":true, + "all": true +} diff --git a/packages/epk-decryption/README.md b/packages/epk-decryption/README.md new file mode 100644 index 0000000000..08ef26eb94 --- /dev/null +++ b/packages/epk-decryption/README.md @@ -0,0 +1,63 @@ +# @requestnetwork/epk-decryption + +Ethereum Private Key Decryption Provider. + +`@requestnetwork/epk-decryption` is a typescript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork). + +Implementation of the decryption provider from the private keys. +The decryption provider is used to make decryption in the Request Network Protocol (e.g.: see [Transaction Manager](/packages/transaction-manager)). + +It uses the Request Network Protocol concept of `Identity` described in the [request logic specification](/packages/request-logic/specs/request-logic-specification-v2.0.0.md). + +## Installation + +```bash +npm install @requestnetwork/epk-decryption +``` + +## Usage + +```javascript +import { EncryptionTypes, IdentityTypes } from '@requestnetwork/types' + +import EthereumPrivateKeyDecryptionProvider from '@requestnetwork/epk-decryption' + +const decryptionParametersExample: EncryptionTypes.IDecryptionParameters = { + key: '0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3', + method: EncryptionTypes.METHOD.ECIES, +}; + +// Identity from the previous signature parameter +const identityExample: IdentityTypes.IIdentity = { + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: '0x627306090abab3a6e1400e9345bc60c78a8bef57' +}; + +// Construct the provider with a +const decryptionProvider = new EthereumPrivateKeyDecryptionProvider(decryptionParametersExample); + +// can list the identity usable +const listOfAvailableIdentity = decryptionProvider.getAllRegisteredIdentities(); // [identityExample] + +// can decrypt data with identity +const dataToDecrypt = "02...."; +const decryptedData = await decryptionProvider.decrypt(dataToDecrypt, identityExample); // "Decrypted data..." + +// can add a new decryption parameters +decryptionProvider.addDecryptionParameters({method: EncryptionTypes.METHOD.ECIES, key: ...}); + +// can remove a decryption parameters from its identity +decryptionProvider.removeRegisteredIdentity({type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, value: ...}); + +// can remove all decryption parameters +decryptionProvider.clearAllRegisteredIdentities(); +``` + +## Contributing + +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. +[Read the contributing guide](/CONTRIBUTING.md) + +## License + +[MIT](/LICENSE) diff --git a/packages/epk-decryption/package.json b/packages/epk-decryption/package.json new file mode 100644 index 0000000000..f3c4da038a --- /dev/null +++ b/packages/epk-decryption/package.json @@ -0,0 +1,66 @@ +{ + "name": "@requestnetwork/epk-decryption", + "version": "0.1.0", + "publishConfig": { + "access": "public" + }, + "description": "Decryption provider using ethereum private keys.", + "keywords": [ + "requestnetwork", + "decryption-provider" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/RequestNetwork/requestNetwork.git" + }, + "homepage": "https://github.com/RequestNetwork/requestNetwork/tree/master/packages/epk-decryption#readme", + "bugs": { + "url": "https://github.com/RequestNetwork/requestNetwork/issues" + }, + "license": "MIT", + "engines": { + "node": ">=8.0.0" + }, + "main": "dist/index.js", + "types": "dist/index.d.ts", + "directories": { + "lib": "src", + "test": "test" + }, + "files": [ + "dist" + ], + "scripts": { + "build": "tsc -b", + "clean": "shx rm -rf dist", + "lint": "tslint --project . && eslint \"src/**/*.ts\"", + "lint-staged": "lint-staged", + "test": "nyc mocha --require ts-node/register --require source-map-support/register \"test/**/*.ts\"", + "test:watch": "nyc mocha --watch --watch-extensions ts --require ts-node/register --require source-map-support/register \"test/**/*.ts\"" + }, + "dependencies": { + "@requestnetwork/types": "0.4.0", + "@requestnetwork/utils": "0.4.0" + }, + "devDependencies": { + "@types/chai": "4.1.7", + "@types/mocha": "5.2.6", + "@typescript-eslint/parser": "1.2.0", + "chai": "4.2.0", + "chai-as-promised": "7.1.1", + "eslint": "5.13.0", + "eslint-plugin-spellcheck": "0.0.11", + "eslint-plugin-typescript": "0.14.0", + "husky": "2.4.0", + "lint-staged": "8.1.3", + "mocha": "5.2.0", + "npm-run-all": "4.1.5", + "nyc": "13.2.0", + "prettier": "1.16.4", + "shx": "0.3.2", + "source-map-support": "0.5.10", + "ts-node": "8.0.2", + "tslint": "5.12.1", + "typescript": "3.3.1" + } +} diff --git a/packages/epk-decryption/src/ethereum-private-key-decryption-provider.ts b/packages/epk-decryption/src/ethereum-private-key-decryption-provider.ts new file mode 100644 index 0000000000..8ec26958ab --- /dev/null +++ b/packages/epk-decryption/src/ethereum-private-key-decryption-provider.ts @@ -0,0 +1,124 @@ +import { DecryptionProviderTypes, EncryptionTypes, IdentityTypes } from '@requestnetwork/types'; + +import Utils from '@requestnetwork/utils'; + +/** Type of the dictionary of decryptionParameters (private keys) indexed by ethereum address */ +type IDecryptionParametersDictionary = Map; + +/** + * Implementation of the decryption provider from private key + * Allows to decrypt() with "ethereumAddress" identities thanks to their private key given in constructor() or addDecryptionParameters() + */ +export default class EthereumPrivateKeyDecryptionProvider + implements DecryptionProviderTypes.IDecryptionProvider { + /** list of supported encryption method */ + public supportedMethods: EncryptionTypes.METHOD[] = [EncryptionTypes.METHOD.ECIES]; + /** list of supported identity types */ + public supportedIdentityTypes: IdentityTypes.TYPE[] = [IdentityTypes.TYPE.ETHEREUM_ADDRESS]; + + /** Dictionary containing all the private keys indexed by address */ + private decryptionParametersDictionary: IDecryptionParametersDictionary; + + constructor(decryptionParameters?: EncryptionTypes.IDecryptionParameters) { + this.decryptionParametersDictionary = new Map(); + if (decryptionParameters) { + this.addDecryptionParameters(decryptionParameters); + } + } + + /** + * Decrypts data + * + * @param data the encrypted data + * @param identity identity to decrypt with + * + * @returns the data decrypted + */ + public async decrypt(data: string, identity: IdentityTypes.IIdentity): Promise { + if (!Utils.multiFormat.isEciesEncryption(data)) { + throw Error(`The data must be encrypted with ${EncryptionTypes.METHOD.ECIES}`); + } + + if (!this.supportedIdentityTypes.includes(identity.type)) { + throw Error(`Identity type not supported ${identity.type}`); + } + + // toLowerCase to avoid mismatch because of case + const decryptionParameters: + | EncryptionTypes.IDecryptionParameters + | undefined = this.decryptionParametersDictionary.get(identity.value.toLowerCase()); + + if (!decryptionParameters) { + throw Error(`private key unknown for the identity: ${identity.value}`); + } + + return Utils.encryption.decrypt(data, decryptionParameters); + } + + /** + * Adds a new private key in the provider + * + * @param decryptionParameters decryption parameters to add + * + * @returns identity from the decryption parameter added + */ + public addDecryptionParameters( + decryptionParameters: EncryptionTypes.IDecryptionParameters, + ): IdentityTypes.IIdentity { + if (!this.supportedMethods.includes(decryptionParameters.method)) { + throw Error(`Encryption method not supported ${decryptionParameters.method}`); + } + + // compute the address from private key + // toLowerCase to avoid mismatch because of case + const address = Utils.crypto.EcUtils.getAddressFromPrivateKey( + decryptionParameters.key, + ).toLowerCase(); + + this.decryptionParametersDictionary.set(address, decryptionParameters); + + return { + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: address, + }; + } + + /** + * Removes a private key from the provider + * + * @param identity identity to remove the private key + * + * @returns void + */ + public removeRegisteredIdentity(identity: IdentityTypes.IIdentity): void { + // Check the type of the identity to be sure that the value used to delete will be the right type + if (!this.supportedIdentityTypes.includes(identity.type)) { + throw Error(`Identity type not supported ${identity.type}`); + } + + this.decryptionParametersDictionary.delete(identity.value); + } + + /** + * Removes all private keys from the provider + * + * @param identity identity to remove the private key + * + * @returns void + */ + public clearAllRegisteredIdentities(): void { + this.decryptionParametersDictionary.clear(); + } + + /** + * Gets all the identities available to decrypt with + * + * @returns all the identities registered + */ + public getAllRegisteredIdentities(): IdentityTypes.IIdentity[] { + return Array.from(this.decryptionParametersDictionary.keys(), address => ({ + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: address, + })); + } +} diff --git a/packages/epk-decryption/src/index.ts b/packages/epk-decryption/src/index.ts new file mode 100644 index 0000000000..f1e387583a --- /dev/null +++ b/packages/epk-decryption/src/index.ts @@ -0,0 +1,3 @@ +export { + default as EthereumPrivateKeyDecryptionProvider, +} from './ethereum-private-key-decryption-provider'; diff --git a/packages/epk-decryption/test/ethereum-private-key-decryption-provider-test.ts b/packages/epk-decryption/test/ethereum-private-key-decryption-provider-test.ts new file mode 100644 index 0000000000..42b60e4426 --- /dev/null +++ b/packages/epk-decryption/test/ethereum-private-key-decryption-provider-test.ts @@ -0,0 +1,227 @@ +import 'mocha'; + +import { EncryptionTypes, IdentityTypes } from '@requestnetwork/types'; +import Utils from '@requestnetwork/utils'; + +import EthereumPrivateKeyDecryptionProvider from '../src/ethereum-private-key-decryption-provider'; + +import * as chai from 'chai'; +import * as chaiAsPromised from 'chai-as-promised'; + +chai.use(chaiAsPromised); +const expect = chai.expect; + +export const id1Raw = { + address: '0xaf083f77f1ffd54218d91491afd06c9296eac3ce', + decryptionParams: { + key: '0x04674d2e53e0e14653487d7323cc5f0a7959c83067f5654cafe4094bde90fa8a', + method: EncryptionTypes.METHOD.ECIES, + }, + encryptionParams: { + key: + '299708c07399c9b28e9870c4e643742f65c94683f35d1b3fc05d0478344ee0cc5a6a5e23f78b5ff8c93a04254232b32350c8672d2873677060d5095184dad422', + method: EncryptionTypes.METHOD.ECIES, + }, + identity: { + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: '0xaf083f77f1ffd54218d91491afd06c9296eac3ce', + }, + privateKey: '0x04674d2e53e0e14653487d7323cc5f0a7959c83067f5654cafe4094bde90fa8a', + publicKey: + '299708c07399c9b28e9870c4e643742f65c94683f35d1b3fc05d0478344ee0cc5a6a5e23f78b5ff8c93a04254232b32350c8672d2873677060d5095184dad422', +}; + +export const id2Raw = { + address: '0x818b6337657a23f58581715fc610577292e521d0', + decryptionParams: { + key: '0x4025da5692759add08f98f4b056c41c71916a671cedc7584a80d73adc7fb43c0', + method: EncryptionTypes.METHOD.ECIES, + }, + encryptionParams: { + key: + 'cf4a1d0bbef8bf0e3fa479a9def565af1b22ea6266294061bfb430701b54a83699e3d47bf52e9f0224dcc29a02721810f1f624f1f70ea3cc5f1fb752cfed379d', + method: EncryptionTypes.METHOD.ECIES, + }, + identity: { + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: '0x818b6337657a23f58581715fc610577292e521d0', + }, + privateKey: '0x4025da5692759add08f98f4b056c41c71916a671cedc7584a80d73adc7fb43c0', + publicKey: + 'cf4a1d0bbef8bf0e3fa479a9def565af1b22ea6266294061bfb430701b54a83699e3d47bf52e9f0224dcc29a02721810f1f624f1f70ea3cc5f1fb752cfed379d', +}; + +const decryptedDataExpected = JSON.stringify({ + attribut1: 'VALUE', + attribut2: 'Value', +}); + +/* tslint:disable:no-unused-expression */ +describe('ethereum-private-key-decryption-provider', () => { + describe('constructor', () => { + it('can construct', async () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + expect( + encryptionProvider.supportedIdentityTypes, + 'encryptionProvider.supportedIdentityTypes is wrong', + ).to.be.deep.equal([IdentityTypes.TYPE.ETHEREUM_ADDRESS]); + expect( + encryptionProvider.supportedMethods, + 'encryptionProvider.supportedMethods is wrong', + ).to.be.deep.equal([EncryptionTypes.METHOD.ECIES]); + + expect( + encryptionProvider.getAllRegisteredIdentities(), + 'getAllRegisteredIdentities is wrong', + ).to.be.deep.equal([id1Raw.identity]); + }); + it('cannot construct with decryption parameter not supported', async () => { + expect( + () => + new EthereumPrivateKeyDecryptionProvider({ + key: '0x0', + method: 'not_supported', + } as any), + 'should have thrown', + ).to.throw('Encryption method not supported not_supported'); + }); + it('cannot construct with decryption parameter value not valid', async () => { + expect( + () => + new EthereumPrivateKeyDecryptionProvider({ + key: '0x0', + method: EncryptionTypes.METHOD.ECIES, + }), + 'should have thrown', + ).to.throw('The private key must be a string representing 32 bytes'); + }); + }); + + describe('addDecryptionParameters', () => { + it('can addDecryptionParameters', () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const identityAdded: IdentityTypes.IIdentity = encryptionProvider.addDecryptionParameters( + id2Raw.decryptionParams, + ); + expect(identityAdded, 'identityAdded is wrong').to.deep.equal(id2Raw.identity); + + expect( + encryptionProvider.getAllRegisteredIdentities(), + 'getAllRegisteredIdentities is wrong', + ).to.be.deep.equal([id1Raw.identity, id2Raw.identity]); + }); + + it('cannot addDecryptionParameters if method not supported', () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const arbitraryParams: any = { + method: 'unknown method', + privateKey: '0x000', + }; + expect(() => { + encryptionProvider.addDecryptionParameters(arbitraryParams); + }, 'should throw').to.throw('Encryption method not supported unknown method'); + }); + }); + describe('removeDecryptionParameters', () => { + it('can removeDecryptionParameters', () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + encryptionProvider.addDecryptionParameters(id2Raw.decryptionParams); + + encryptionProvider.removeRegisteredIdentity(id2Raw.identity); + + expect( + encryptionProvider.getAllRegisteredIdentities(), + 'getAllRegisteredIdentities is wrong', + ).to.be.deep.equal([id1Raw.identity]); + }); + + it('cannot removeDecryptionParameters if method not supported', () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const arbitraryIdentity: any = { + type: 'unknown type', + value: '0x000', + }; + expect(() => { + encryptionProvider.removeRegisteredIdentity(arbitraryIdentity); + }, 'should throw').to.throw('Identity type not supported unknown type'); + }); + }); + + describe('clearAllDecryptionParameters', () => { + it('can clearAllDecryptionParameters', () => { + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + encryptionProvider.addDecryptionParameters(id2Raw.decryptionParams); + + encryptionProvider.clearAllRegisteredIdentities(); + + expect( + encryptionProvider.getAllRegisteredIdentities(), + 'getAllRegisteredIdentities is wrong', + ).to.be.deep.equal([]); + }); + }); + + describe('decrypt', () => { + it('can decrypt', async () => { + const encryptedData = await Utils.encryption.encrypt( + decryptedDataExpected, + id1Raw.encryptionParams, + ); + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const decryptedData: string = await encryptionProvider.decrypt( + encryptedData, + id1Raw.identity, + ); + + expect(decryptedData, 'decryptedData is wrong').to.be.deep.equal(decryptedDataExpected); + }); + + it('cannot decrypt if encryption not supported', async () => { + const encryptedData = '04000000'; + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + await expect( + encryptionProvider.decrypt(encryptedData, id1Raw.identity), + 'should throw', + ).to.eventually.be.rejectedWith( + `The data must be encrypted with ${EncryptionTypes.METHOD.ECIES}`, + ); + }); + + it('cannot decrypt if identity not supported', async () => { + const encryptedData = await Utils.encryption.encrypt( + decryptedDataExpected, + id1Raw.encryptionParams, + ); + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const arbitraryIdentity: any = { type: 'unknown type', value: '0x000' }; + await expect( + encryptionProvider.decrypt(encryptedData, arbitraryIdentity), + 'should throw', + ).to.eventually.be.rejectedWith('Identity type not supported unknown type'); + }); + + it('cannot decrypt if private key of the identity not given', async () => { + const encryptedData = await Utils.encryption.encrypt( + decryptedDataExpected, + id1Raw.encryptionParams, + ); + const encryptionProvider = new EthereumPrivateKeyDecryptionProvider(id1Raw.decryptionParams); + + const arbitraryIdentity: IdentityTypes.IIdentity = { + type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, + value: '0x000', + }; + await expect( + encryptionProvider.decrypt(encryptedData, arbitraryIdentity), + 'should throw', + ).to.eventually.be.rejectedWith('private key unknown for the identity: 0x000'); + }); + }); +}); diff --git a/packages/epk-decryption/tsconfig.json b/packages/epk-decryption/tsconfig.json new file mode 100644 index 0000000000..39dc27900e --- /dev/null +++ b/packages/epk-decryption/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "include": ["./src/**/*"], + "references": [{ "path": "../types" }, { "path": "../utils" }] +} diff --git a/packages/epk-decryption/tslint.json b/packages/epk-decryption/tslint.json new file mode 100644 index 0000000000..0946f20963 --- /dev/null +++ b/packages/epk-decryption/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/packages/types/src/decryption-provider-types.ts b/packages/types/src/decryption-provider-types.ts new file mode 100644 index 0000000000..1afc635e74 --- /dev/null +++ b/packages/types/src/decryption-provider-types.ts @@ -0,0 +1,10 @@ +import * as Encryption from './encryption-types'; +import * as Identity from './identity-types'; + +/** Decryption provider interface */ +export interface IDecryptionProvider { + supportedMethods: Encryption.METHOD[]; + supportedIdentityTypes: Identity.TYPE[]; + + decrypt: (data: string, signer: Identity.IIdentity) => Promise; +} diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index b4d5f54a39..eb5966d744 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -1,5 +1,6 @@ import * as AdvancedLogicTypes from './advanced-logic-types'; import * as DataAccessTypes from './data-access-types'; +import * as DecryptionProviderTypes from './decryption-provider-types'; import * as EncryptionTypes from './encryption-types'; import * as ExtensionTypes from './extension-types'; import * as IdentityTypes from './identity-types'; @@ -14,6 +15,7 @@ import * as TransactionTypes from './transaction-types'; export { AdvancedLogicTypes, LogTypes, + DecryptionProviderTypes, EncryptionTypes, ExtensionTypes, RequestLogicTypes, diff --git a/packages/utils/src/encryption.ts b/packages/utils/src/encryption.ts index 9edbd21fca..ce99d1861c 100644 --- a/packages/utils/src/encryption.ts +++ b/packages/utils/src/encryption.ts @@ -75,7 +75,7 @@ async function decrypt( if (decryptionParams.method !== EncryptionTypes.METHOD.ECIES) { throw new Error(`decryptionParams.method should be ${EncryptionTypes.METHOD.ECIES}`); } - return Crypto.EcUtils.decrypt(decryptionParams.key, encryptedData.slice(2)); + return Crypto.EcUtils.decrypt(decryptionParams.key, multiFormat.removePadding(encryptedData)); } if (multiFormat.isAes256cbcEncryption(encryptedData)) {