1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

added Ocean.secretStore module

This commit is contained in:
Pedro Gutiérrez 2019-02-14 11:26:12 +01:00 committed by Pedro Gutiérrez
parent 42b7c0cced
commit 3fd8f995f6
6 changed files with 147 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import deprecated from "deprecated-decorator"
import OceanAccounts from "./OceanAccounts" import OceanAccounts from "./OceanAccounts"
import OceanAgreements from "./OceanAgreements" import OceanAgreements from "./OceanAgreements"
import OceanAssets from "./OceanAssets" import OceanAssets from "./OceanAssets"
import OceanSecretStore from "./OceanSecretStore"
import AquariusProvider from "../aquarius/AquariusProvider" import AquariusProvider from "../aquarius/AquariusProvider"
import { SearchQuery } from "../aquarius/query/SearchQuery" import { SearchQuery } from "../aquarius/query/SearchQuery"
@ -38,6 +39,7 @@ export default class Ocean {
Ocean.instance.accounts = await OceanAccounts.getInstance() Ocean.instance.accounts = await OceanAccounts.getInstance()
Ocean.instance.assets = await OceanAssets.getInstance() Ocean.instance.assets = await OceanAssets.getInstance()
Ocean.instance.agreements = await OceanAgreements.getInstance() Ocean.instance.agreements = await OceanAgreements.getInstance()
Ocean.instance.secretStore = await OceanSecretStore.getInstance()
} }
return Ocean.instance return Ocean.instance
@ -67,6 +69,12 @@ export default class Ocean {
*/ */
public agreements: OceanAgreements public agreements: OceanAgreements
/**
* Ocean secretStore submodule
* @type {OceanSecretStore}
*/
public secretStore: OceanSecretStore
private constructor() { private constructor() {
} }
@ -106,7 +114,7 @@ export default class Ocean {
* Registers a new DDO. * Registers a new DDO.
* @deprecated Replace by [Ocean.assets.create]{@link #OceanAssets.create} * @deprecated Replace by [Ocean.assets.create]{@link #OceanAssets.create}
* @param {MetaData} metadata DDO metadata. * @param {MetaData} metadata DDO metadata.
* @param {Account} publisher Publicher account. * @param {Account} publisher Publisher account.
* @return {Promise<DDO>} * @return {Promise<DDO>}
*/ */
@deprecated("OceanAssets.create") @deprecated("OceanAssets.create")

View File

@ -9,9 +9,9 @@ import Keeper from "../keeper/Keeper"
import SecretStoreProvider from "../secretstore/SecretStoreProvider" import SecretStoreProvider from "../secretstore/SecretStoreProvider"
import Account from "./Account" import Account from "./Account"
import DID from "./DID" import DID from "./DID"
import OceanAgreements from "./OceanAgreements"
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate" import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
import Access from "./ServiceAgreements/Templates/Access" import Access from "./ServiceAgreements/Templates/Access"
import OceanAgreements from "./OceanAgreements"
/** /**
* Assets submodule of Ocean Protocol. * Assets submodule of Ocean Protocol.
@ -49,7 +49,7 @@ export default class OceanAssets {
/** /**
* Creates a new DDO. * Creates a new DDO.
* @param {MetaData} metadata DDO metadata. * @param {MetaData} metadata DDO metadata.
* @param {Account} publisher Publicher account. * @param {Account} publisher Publisher account.
* @return {Promise<DDO>} * @return {Promise<DDO>}
*/ */
public async create(metadata: MetaData, publisher: Account, services: Service[] = []): Promise<DDO> { public async create(metadata: MetaData, publisher: Account, services: Service[] = []): Promise<DDO> {

View File

@ -0,0 +1,64 @@
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: string, publisher: Account): Promise<string> {
console.warn("TODO")
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<string> {
console.warn("TODO")
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)
}
}

View File

@ -1,6 +1,7 @@
import SecretStore from "@oceanprotocol/secret-store-client" import SecretStore from "@oceanprotocol/secret-store-client"
import SecretStoreConfig from "@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig" import SecretStoreConfig from "@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig"
import ConfigProvider from "../ConfigProvider" import ConfigProvider from "../ConfigProvider"
import Config from "../models/Config"
export default class SecretStoreProvider { export default class SecretStoreProvider {

View File

@ -0,0 +1,69 @@
import { assert, expect, spy, use } from "chai"
import * as spies from "chai-spies"
import ConfigProvider from "../../src/ConfigProvider"
import Account from "../../src/ocean/Account"
import Ocean from "../../src/ocean/Ocean"
import OceanSecretStore from "../../src/ocean/OceanSecretStore"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
import config from "../config"
use(spies)
describe("OceanSecretStore", () => {
let oceanSecretStore: OceanSecretStore
let accounts: Account[]
const did = `did:op:${"a".repeat(64)}`
before(async () => {
oceanSecretStore = await OceanSecretStore.getInstance()
ConfigProvider.setConfig(config)
const ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
})
afterEach(() => {
spy.restore()
})
describe("#getInstance()", () => {
it("should get an instance of OceanSecretStore", async () => {
const oceanSecretStore: OceanSecretStore = await OceanSecretStore.getInstance()
assert.instanceOf(oceanSecretStore, OceanSecretStore, "No returned OceanSecretStore instance")
})
})
describe("#encrypt()", () => {
it("should encrypt a content", async () => {
const secretStoreToSpy = SecretStoreProvider.getSecretStore()
const secretStoreEncryptSpy = spy.on(secretStoreToSpy, "encryptDocument", () => "encryptedResult")
const secretStoreProviderGetInstanceSpy = spy.on(SecretStoreProvider, "getSecretStore", () => secretStoreToSpy)
const result = await oceanSecretStore.encrypt(did, "test", accounts[0])
expect(secretStoreProviderGetInstanceSpy).to.have.been.called.with({address: accounts[0].getId()})
expect(secretStoreEncryptSpy).to.have.been.called.with(did, "test")
assert.equal(result, "encryptedResult", "Result doesn't match")
})
})
describe("#decrypt()", () => {
it("should decrypt a content", async () => {
const secretStoreToSpy = SecretStoreProvider.getSecretStore()
const secretStoreEncryptSpy = spy.on(secretStoreToSpy, "decryptDocument", () => "decryptedResult")
const secretStoreProviderGetInstanceSpy = spy.on(SecretStoreProvider, "getSecretStore", () => secretStoreToSpy)
const result = await oceanSecretStore.decrypt(did, "encryptedContent", accounts[0])
expect(secretStoreProviderGetInstanceSpy).to.have.been.called.with({address: accounts[0].getId()})
expect(secretStoreEncryptSpy).to.have.been.called.with(did, "encryptedContent")
assert.equal(result, "decryptedResult", "Result doesn't match")
})
})
})

View File

@ -161,6 +161,8 @@ describe("ServiceAgreement", () => {
assert(serviceAgreement) assert(serviceAgreement)
// get funds // get funds
// TODO: remove small delay to prevent failtures
await new Promise((resolve) => setTimeout(resolve, 100))
await consumerAccount.requestTokens(metaDataService.metadata.base.price) await consumerAccount.requestTokens(metaDataService.metadata.base.price)
const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price, const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price,