From 652d4e7575cbf1cfe9f131ac7739e89e6498da36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Mon, 15 Apr 2019 15:30:02 +0200 Subject: [PATCH] Add owner method on assets module. --- integration/ocean/AssetOwners.test.ts | 31 +++++++++++++++++++++++++++ src/ocean/OceanAssets.ts | 18 ++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 integration/ocean/AssetOwners.test.ts diff --git a/integration/ocean/AssetOwners.test.ts b/integration/ocean/AssetOwners.test.ts new file mode 100644 index 0000000..6aeadaf --- /dev/null +++ b/integration/ocean/AssetOwners.test.ts @@ -0,0 +1,31 @@ +import { assert } from "chai" + +import { config } from "../config" + +import { getMetadata } from "../utils" + +import { Ocean, Account } from "../../src" // @oceanprotocol/squid + +describe("Asset Owners", () => { + let ocean: Ocean + + let publisher: Account + + const metadata = getMetadata() + + before(async () => { + ocean = await Ocean.getInstance(config) + + // Accounts + publisher = (await ocean.accounts.list())[0] + publisher.setPassword(process.env.ACCOUNT_PASSWORD) + }) + + it("should be set correctly the owner of a asset", async () => { + const ddo = await ocean.assets.create(metadata as any, publisher) + + const owner = await ocean.assets.owner(ddo.id) + + assert.equal(owner, publisher.getId()) + }) +}) diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 98cb533..1325b42 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -259,6 +259,24 @@ export class OceanAssets extends Instantiable { return agreementId } + /** + * Returns the owner of a asset. + * @param {string} did Decentralized ID. + * @return {Promise} Returns Agreement ID + */ + public async owner(did: string): Promise { + const ddo = await this.resolve(did) + const checksum = ddo.getChecksum() + const {creator, signatureValue} = ddo.proof + const signer = await this.ocean.utils.signature.verifyText(checksum, signatureValue) + + if (signer.toLowerCase() !== creator.toLowerCase()) { + this.logger.warn(`Owner of ${ddo.id} doesn't match. Expected ${creator} instead of ${signer}.`) + } + + return creator + } + /** * Search over the assets using a query. * @param {SearchQuery} query Query to filter the assets.