From ac76380ba980c9f178dd73cf63a0a08a4b938200 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 30 Jan 2020 15:03:31 +0100 Subject: [PATCH] prototype DDO updating for transferOwnership --- src/aquarius/Aquarius.ts | 34 ++++++++++++++++++++++++++++++++++ src/ocean/OceanAssets.ts | 18 ++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index cd52a2f..2a97301 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -215,6 +215,40 @@ export class Aquarius { return this.retrieveDDO(undefined, metadataServiceEndpoint) } + /** + * Updates a DDO. + * @param {DID | string} did DID of the asset to update. + * @param {DDO} newDdo New DDO of the asset. + * @return {Promise} Updated DDO of the asset. + */ + public async updateDDO(did: DID | string, newDdo: DDO): Promise { + did = did && DID.parse(did) + const fullUrl = `${this.url}${apiPath}/${did.getDid()}` + const result = await this.fetch + .put(fullUrl, DDO.serialize(newDdo)) + .then((response: any) => { + if (response.ok) { + return response.json() + } + this.logger.log( + 'updateDDO failed:', + response.status, + response.statusText, + did + ) + return null as DDO + }) + .then((response: DDO) => { + return new DDO(response) as DDO + }) + .catch(error => { + this.logger.error('Error updating metadata: ', error) + return null as DDO + }) + + return result + } + public getServiceEndpoint(did: DID) { return `${this.url}/api/v1/aquarius/assets/ddo/did:op:${did.getId()}` } diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 537fd8b..a6f95be 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -418,8 +418,22 @@ export class OceanAssets extends Instantiable { did: string, newOwner: string ): Promise { - const owner = await this.ocean.assets.owner(did) - return this.ocean.keeper.didRegistry.transferDIDOwnership(did, newOwner, owner) + const oldOwner = await this.ocean.assets.owner(did) + const oldDdo = await this.ocean.aquarius.retrieveDDO(did) + + // update owner on-chain + const txReceipt = this.ocean.keeper.didRegistry.transferDIDOwnership( + did, + newOwner, + oldOwner + ) + + // update owner off-chain in DDO + const newDdo = oldDdo + newDdo.publicKey[0].owner = newOwner + await this.ocean.aquarius.updateDDO(did, newDdo) + + return txReceipt } /**