2019-07-12 16:56:01 +02:00
|
|
|
import SecretStore from '@oceanprotocol/secret-store-client'
|
|
|
|
import SecretStoreConfig from '@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig'
|
2019-07-11 15:55:08 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
import Account from './Account'
|
|
|
|
import { noDidPrefixed } from '../utils'
|
|
|
|
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
2019-02-14 11:26:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SecretStore submodule of Ocean Protocol.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export class OceanSecretStore extends Instantiable {
|
2019-02-14 11:26:12 +01:00
|
|
|
/**
|
|
|
|
* Returns the instance of OceanSecretStore.
|
|
|
|
* @return {Promise<OceanSecretStore>}
|
|
|
|
*/
|
2019-09-09 12:18:54 +02:00
|
|
|
public static async getInstance(config: InstantiableConfig): Promise<OceanSecretStore> {
|
2019-03-21 02:56:58 +01:00
|
|
|
const instance = new OceanSecretStore()
|
|
|
|
instance.setInstanceConfig(config)
|
2019-02-14 11:26:12 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
return instance
|
2019-02-14 11:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt the given text and store the encryption keys using the `did`.
|
|
|
|
* The encrypted text can be decrypted using the same keys identified by the `did`.
|
|
|
|
* @param {string} did Decentralized ID.
|
|
|
|
* @param {string} content Content to be encrypted.
|
|
|
|
* @param {string} publisher Publisher account.
|
|
|
|
* @return {Promise<string>} Encrypted text.
|
|
|
|
*/
|
2019-09-09 12:18:54 +02:00
|
|
|
public async encrypt(did: string, document: any, publisher: Account): Promise<string> {
|
2019-05-09 16:35:32 +02:00
|
|
|
const signature =
|
2019-06-20 00:20:09 +02:00
|
|
|
(await publisher.getToken()) ||
|
2019-09-09 12:18:54 +02:00
|
|
|
(await this.ocean.utils.signature.signText(noDidPrefixed(did), publisher.getId(), publisher.getPassword()))
|
2019-02-14 11:26:12 +01:00
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
return this.ocean.brizo.encrypt(noDidPrefixed(did), signature, document, publisher.getId())
|
2019-02-14 11:26:12 +01:00
|
|
|
}
|
2019-07-11 15:55:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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<string>} Encrypted text.
|
|
|
|
*/
|
2019-09-09 12:18:54 +02:00
|
|
|
public async decrypt(did: string, content: string, consumer?: Account, secretStoreUrl?: string): Promise<any> {
|
|
|
|
return this.getSecretStoreByAccount(consumer, secretStoreUrl).decryptDocument(noDidPrefixed(did), content)
|
2019-07-11 15:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private getSecretStoreByAccount(account: Account, secretStoreUrl?: string) {
|
2019-07-12 16:56:01 +02:00
|
|
|
const config: any = { ...this.config }
|
2019-07-11 15:55:08 +02:00
|
|
|
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 {
|
2019-09-09 12:18:54 +02:00
|
|
|
const { secretStoreUri, parityUri, password, address, threshold } = config
|
2019-07-12 16:56:01 +02:00
|
|
|
config = { secretStoreUri, parityUri, password, address, threshold }
|
2019-07-11 15:55:08 +02:00
|
|
|
|
|
|
|
return new SecretStore(config)
|
|
|
|
}
|
2019-02-14 11:26:12 +01:00
|
|
|
}
|