diff --git a/package-lock.json b/package-lock.json index 78af9e1..bae11c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -173,6 +173,14 @@ "resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.10.3.tgz", "integrity": "sha512-Nvnk9nNWMmfXz38bHRHHNgc8MGmFPZTkXPBWbEXR9+maJq5/kRFx8OckroKqmYXX4QBEh5frwd77omNOL2MUNw==" }, + "@oceanprotocol/secret-store-client": { + "version": "0.0.15", + "resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.15.tgz", + "integrity": "sha512-5yNNA+qdjut9/nMiFKJd4D4io+GhzdfvdqVd5YMkgT9RV1qDosGj6NVsKArYay6g+tQH7pCJ6Y1FiAbhaaFB9g==", + "requires": { + "node-fetch": "^2.6.0" + } + }, "@octokit/endpoint": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-4.2.2.tgz", @@ -3797,8 +3805,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3819,14 +3826,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3841,20 +3846,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3971,8 +3973,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3984,7 +3985,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3999,7 +3999,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4007,14 +4006,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4033,7 +4030,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4114,8 +4110,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -4127,7 +4122,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -4213,8 +4207,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -4250,7 +4243,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4270,7 +4262,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4314,14 +4305,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -9724,7 +9713,7 @@ "requires": { "underscore": "1.8.3", "web3-core-helpers": "1.0.0-beta.37", - "websocket": "git://github.com/frozeman/WebSocket-Node.git#browserifyCompatible" + "websocket": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2" } }, "web3-shh": { diff --git a/package.json b/package.json index 4cd7eae..e0fa7a5 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ }, "dependencies": { "@oceanprotocol/keeper-contracts": "^0.10.3", + "@oceanprotocol/secret-store-client": "0.0.15", "bignumber.js": "^8.1.1", "deprecated-decorator": "^0.1.6", "node-fetch": "^2.6.0", diff --git a/src/models/Config.ts b/src/models/Config.ts index 085ba5f..083b6ff 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -53,6 +53,10 @@ export class Config { * @type {number} */ public authTokenExpiration?: number + + // Parity config + public parityUri?: string + public threshold?: number } export default Config diff --git a/src/ocean/OceanSecretStore.ts b/src/ocean/OceanSecretStore.ts index d6cfafe..f0efab9 100644 --- a/src/ocean/OceanSecretStore.ts +++ b/src/ocean/OceanSecretStore.ts @@ -1,3 +1,6 @@ +import SecretStore from "@oceanprotocol/secret-store-client" +import SecretStoreConfig from "@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig" + import Account from './Account' import { noDidPrefixed } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' @@ -47,4 +50,38 @@ export class OceanSecretStore extends Instantiable { publisher.getId() ) } + + /** + * Decrypt an encrypted text using the stored encryption keys associated with the `did`. + * Decryption requires that the account owner has access permissions for this `did` + * @param {string} did Decentralized ID. + * @param {string} content Content to be encrypted. + * @param {string} consumer cONSUMER account. + * @return {Promise} Encrypted text. + */ + public async decrypt(did: string, content: string, consumer?: Account, secretStoreUrl?: string): Promise { + return await this.getSecretStoreByAccount(consumer, secretStoreUrl) + .decryptDocument(noDidPrefixed(did), content) + } + + private getSecretStoreByAccount(account: Account, secretStoreUrl?: string) { + const config: any = {...this.config} + if (account) { + config.address = account.getId() + } + if (account && account.getPassword()) { + config.password = account.getPassword() + } + if (secretStoreUrl) { + config.secretStoreUri = secretStoreUrl + } + return this.getSecretStore(config) + } + + private getSecretStore(config: SecretStoreConfig): SecretStore { + const {secretStoreUri, parityUri, password, address, threshold} = config + config = {secretStoreUri, parityUri, password, address, threshold} + + return new SecretStore(config) + } }