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

using a SecretStore with custom config

This commit is contained in:
Pedro Gutiérrez 2019-02-15 23:40:55 +01:00 committed by Pedro Gutiérrez
parent 407aba7450
commit a6d2ce8fde
4 changed files with 33 additions and 13 deletions

View File

@ -15,6 +15,7 @@ import IdGenerator from "./IdGenerator"
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
import Access from "./ServiceAgreements/Templates/Access"
import ConfigProvider from "../ConfigProvider"
/**
* Assets submodule of Ocean Protocol.
@ -56,6 +57,7 @@ export default class OceanAssets {
* @return {Promise<DDO>}
*/
public async create(metadata: MetaData, publisher: Account, services: Service[] = []): Promise<DDO> {
const {secretStoreUri} = ConfigProvider.getConfig()
const {didRegistry} = await Keeper.getInstance()
const aquarius = AquariusProvider.getAquarius()
const brizo = BrizoProvider.getBrizo()
@ -64,8 +66,11 @@ export default class OceanAssets {
const authorizationService = (services.find(({type}) => type === "Authorization") || {}) as ServiceAuthorization
const secretStoreUrl = authorizationService.service === "SecretStore" && authorizationService.serviceEndpoint
const secretStoreConfig = {
secretStoreUri: secretStoreUrl,
}
const encryptedFiles = await SecretStoreProvider.getSecretStore(secretStoreUrl).encryptDocument(did.getId(), metadata.base.files)
const encryptedFiles = await SecretStoreProvider.getSecretStore(secretStoreConfig).encryptDocument(did.getId(), metadata.base.files)
const template = new Access()
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
@ -121,6 +126,12 @@ export default class OceanAssets {
serviceEndpoint: brizo.getComputeEndpoint(publisher.getId(), String(serviceDefinitionIdCount), "xxx", "xxx"),
serviceDefinitionId: String(serviceDefinitionIdCount++),
},
{
type: "Authorization",
services: 'SecretStore',
serviceEndpoint: secretStoreUri,
serviceDefinitionId: String(serviceDefinitionIdCount++),
},
{
type: "Metadata",
serviceEndpoint,
@ -148,7 +159,11 @@ export default class OceanAssets {
},
...services
.map((_) => ({..._, serviceDefinitionId: String(serviceDefinitionIdCount++)})),
] as Service[],
]
// Remove duplications
.reverse()
.filter(({type}, i, list) => list.findIndex(({type: t}) => t === type) === i)
.reverse() as Service[],
})
ddo.addChecksum()
@ -156,8 +171,6 @@ export default class OceanAssets {
const storedDdo = await aquarius.storeDDO(ddo)
// Logger.log(JSON.stringify(storedDdo, null, 2))
await didRegistry.registerAttribute(
did.getId(),
ddo.getChecksum(),

View File

@ -1,4 +1,5 @@
import SecretStore from "@oceanprotocol/secret-store-client"
import SecretStoreConfig from "@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig"
import ConfigProvider from "../ConfigProvider"
export default class SecretStoreProvider {
@ -8,24 +9,30 @@ export default class SecretStoreProvider {
SecretStoreProvider.secretStore = secretStore
}
public static getSecretStore(url?: string): SecretStore {
if (!url) {
public static getSecretStore(config?: Partial<SecretStoreConfig>): SecretStore {
config = {...config}
// Cleaning undefined parameters
Object.keys(config)
.forEach(key => config[key] || delete config[key])
if (!Object.keys(config).length) {
if (!SecretStoreProvider.secretStore) {
SecretStoreProvider.secretStore = new SecretStore(ConfigProvider.getConfig())
}
return SecretStoreProvider.secretStore
} else {
if (!SecretStoreProvider.secretStoreByUrl.get(url)) {
SecretStoreProvider.secretStoreByUrl.set(url,
const configRef = JSON.stringify(config)
if (!SecretStoreProvider.secretStoreByUrl.get(configRef)) {
SecretStoreProvider.secretStoreByUrl.set(configRef,
new SecretStore({
...ConfigProvider.getConfig(),
secretStoreUri: url,
...config,
}),
)
}
return SecretStoreProvider.secretStoreByUrl.get(url)
return SecretStoreProvider.secretStoreByUrl.get(configRef)
}
}

View File

@ -194,7 +194,7 @@ describe("DDO", () => {
it("should create an predefined ddo", async () => {
const service: Partial<Service> = {
const service: Partial<Service> & any = {
serviceEndpoint: "http://",
description: "nice service",
}
@ -205,7 +205,7 @@ describe("DDO", () => {
assert(ddo)
assert(ddo.service.length === 1)
assert(ddo.service[0].description === service.description)
assert((ddo.service[0] as any).description === service.description)
assert(ddo.authentication.length === 0)
assert(ddo.publicKey.length === 0)

View File

@ -21,7 +21,7 @@ let accounts: Account[]
let publisherAccount: Account
let consumerAccount: Account
let accessService: Service
let accessService: Service<"Access">
let metaDataService: Service<"Metadata">
const did: DID = DID.generate()