diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index e082394..151dc85 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -1,8 +1,10 @@ import { URL } from 'whatwg-url' import { DDO } from '../ddo/DDO' import DID from '../ocean/DID' +import { EditableMetaData } from '../ddo/MetaData' import { Logger } from '../utils' import { WebServiceConnector } from '../ocean/utils/WebServiceConnector' +import { escrowAccessServiceAgreementTemplate } from '../keeper/contracts/templates/EscrowAccess.serviceAgreementTemplate' const apiPath = '/api/v1/aquarius/assets/ddo' @@ -260,6 +262,95 @@ export class Aquarius { return result } + + public async editMetadata( + did: DID | string, + newMetadata: EditableMetaData, + updated: string, + signature: string + ): Promise { + did = did && DID.parse(did) + const fullUrl = `${this.url}${apiPath}/metadata/update/${did.getDid()}` + let data = Object() + if (newMetadata.description != null) + data.description = newMetadata.description + if (newMetadata.title != null) + data.title = newMetadata.title + if (newMetadata.servicePrices != null) + data.servicePrices = newMetadata.servicePrices + if (newMetadata.links != null) { + data.links = [] + for (const [name, url] of Object.entries(newMetadata.links)) { + let asample = Object() + asample.name = name + asample.url = url + asample.type = 'sample' + data.links.push(asample) + } + } + data.updated = updated + data.signature = signature + const result = await this.fetch + .put( + fullUrl, + JSON.stringify(data) + ) + .then((response: any) => { + if (response.ok) { + return response.text + } + this.logger.log( + 'transferownership failed:', + response.status, + response.statusText + ) + return null + }) + + .catch(error => { + this.logger.error('Error transfering ownership metadata: ', error) + return null + }) + + return result + } + + public async retire( + did: DID | string, + updated: string, + signature: string + ): Promise { + did = did && DID.parse(did) + const fullUrl = `${this.url}${apiPath}/${did.getDid()}` + const result = await this.fetch + .delete( + fullUrl, + JSON.stringify({ + signature: signature, + updated: updated + }) + ) + .then((response: any) => { + if (response.ok) { + return response.text + } + this.logger.log( + 'transferownership failed:', + response.status, + response.statusText + ) + return null + }) + + .catch(error => { + this.logger.error('Error transfering ownership metadata: ', error) + return null + }) + + return result + } + + public getServiceEndpoint(did: DID) { return `${this.url}/api/v1/aquarius/assets/ddo/did:op:${did.getId()}` } diff --git a/src/ddo/MetaData.ts b/src/ddo/MetaData.ts index 34aceb7..01aa38e 100644 --- a/src/ddo/MetaData.ts +++ b/src/ddo/MetaData.ts @@ -276,3 +276,15 @@ export interface MetaData { additionalInformation?: AdditionalInformation curation?: Curation } + +export interface ServicePrices{ + serviceIndex: number, + price: string +} + +export interface EditableMetaData{ + description?: string, + title?: string, + links?: { [name: string]: string }[], + servicePrices?: ServicePrices[] +} diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 719d362..0a52d84 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -1,7 +1,7 @@ import { TransactionReceipt } from 'web3-core' import { SearchQuery } from '../aquarius/Aquarius' import { DDO } from '../ddo/DDO' -import { MetaData } from '../ddo/MetaData' +import { MetaData, EditableMetaData } from '../ddo/MetaData' import { Service, ServiceAccess } from '../ddo/Service' import Account from './Account' import DID from './DID' @@ -384,6 +384,45 @@ export class OceanAssets extends Instantiable { return txReceipt } + public async editMetadata( + did: string, + newMetadata: EditableMetaData, + account: Account + ): Promise { + const oldDdo = await this.ocean.aquarius.retrieveDDO(did) + // get a signature + const signature = await this.ocean.utils.signature.signForAquarius( + oldDdo.updated, + account + ) + let result=null + if (signature != null) + result=await this.ocean.aquarius.editMetadata( + did, + newMetadata, + oldDdo.updated, + signature + ) + + return result + } + + public async retire( + did: string, + account: Account + ): Promise { + const oldDdo = await this.ocean.aquarius.retrieveDDO(did) + // get a signature + const signature = await this.ocean.utils.signature.signForAquarius( + oldDdo.updated, + account + ) + let result=null + if (signature != null) + result = await this.ocean.aquarius.retire(did, oldDdo.updated, signature) + return result + } + /** * Returns the assets of a consumer. * @param {string} consumer Consumer address. diff --git a/src/ocean/utils/WebServiceConnector.ts b/src/ocean/utils/WebServiceConnector.ts index d34e6fe..89cc4d3 100644 --- a/src/ocean/utils/WebServiceConnector.ts +++ b/src/ocean/utils/WebServiceConnector.ts @@ -43,13 +43,25 @@ export class WebServiceConnector { }) } - public delete(url: string): Promise { - return this.fetch(url, { - method: 'DELETE', - headers: { - 'Content-type': 'application/json' - } - }) + public delete(url: string, payload?: BodyInit): Promise { + if (payload != null) { + return this.fetch(url, { + method: 'DELETE', + body: payload, + headers: { + 'Content-type': 'application/json' + } + }) + } + else { + return this.fetch(url, { + method: 'DELETE', + headers: { + 'Content-type': 'application/json' + } + }) + } + } public async downloadFile(