1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/ocean/OceanSecretStore.ts
2019-02-26 00:18:07 +01:00

63 lines
2.2 KiB
TypeScript

import SecretStoreProvider from "../secretstore/SecretStoreProvider"
import Account from "./Account"
/**
* SecretStore submodule of Ocean Protocol.
*/
export default class OceanSecretStore {
/**
* Returns the instance of OceanSecretStore.
* @return {Promise<OceanSecretStore>}
*/
public static async getInstance(): Promise<OceanSecretStore> {
if (!OceanSecretStore.instance) {
OceanSecretStore.instance = new OceanSecretStore()
}
return OceanSecretStore.instance
}
/**
* OceanSecretStore instance.
* @type {OceanSecretStore}
*/
private static instance: OceanSecretStore = null
/**
* 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.
*/
public async encrypt(did: string, content: any, publisher: Account): Promise<string> {
return await this.getSecretStoreByAccount(publisher)
// TODO did to id
.encryptDocument(did, content)
}
/**
* 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.
*/
public async decrypt(did: string, content: string, consumer: Account): Promise<any> {
return await this.getSecretStoreByAccount(consumer)
// TODO did to id
.decryptDocument(did, content)
}
private getSecretStoreByAccount(account: Account) {
const config: any = {address: account.getId()}
if (account.getPassword()) {
config.password = account.getPassword()
}
return SecretStoreProvider.getSecretStore(config)
}
}