diff --git a/integration/ocean/AssetOwners.test.ts b/integration/ocean/AssetOwners.test.ts index ee4ee07..e128e54 100644 --- a/integration/ocean/AssetOwners.test.ts +++ b/integration/ocean/AssetOwners.test.ts @@ -6,7 +6,7 @@ import { getMetadata } from "../utils" import { Ocean, Account } from "../../src" // @oceanprotocol/squid -describe.only("Asset Owners", () => { +describe("Asset Owners", () => { let ocean: Ocean let account1: Account @@ -43,4 +43,21 @@ describe.only("Asset Owners", () => { assert.equal(finalLength - initialLength, 1) }) + + it("should get the assets that can be consumer by a user", async () => { + const {length: initialLength} = await ocean.assets.consumerAssets(account2.getId()) + + const ddo = await ocean.assets.create(metadata as any, account1) + + const {length: finalLength1} = await ocean.assets.consumerAssets(account2.getId()) + assert.equal(finalLength1 - initialLength, 0) + + // Granting access + await account2.requestTokens(metadata.base.price) + await ocean.assets.order(ddo.id, ddo.findServiceByType("Access").serviceDefinitionId, account2) + // Access granted + + const {length: finalLength2} = await ocean.assets.consumerAssets(account2.getId()) + assert.equal(finalLength2 - initialLength, 1) + }) }) diff --git a/src/keeper/contracts/DIDRegistry.ts b/src/keeper/contracts/DIDRegistry.ts index 8ff0185..8627e77 100644 --- a/src/keeper/contracts/DIDRegistry.ts +++ b/src/keeper/contracts/DIDRegistry.ts @@ -28,7 +28,7 @@ export default class DIDRegistry extends ContractBase { } public async getAttributesByOwner(owner: string): Promise { - return (await this.getPastEvents('DIDAttributeRegistered', {_owner: zeroX(owner)})) + return (await this.getPastEvents("DIDAttributeRegistered", {_owner: zeroX(owner)})) .map(({returnValues}) => returnValues._did) .map(didPrefixed) } diff --git a/src/keeper/contracts/conditions/AccessSecretStoreCondition.ts b/src/keeper/contracts/conditions/AccessSecretStoreCondition.ts index a517098..a77654b 100644 --- a/src/keeper/contracts/conditions/AccessSecretStoreCondition.ts +++ b/src/keeper/contracts/conditions/AccessSecretStoreCondition.ts @@ -1,5 +1,5 @@ import { Condition } from "./Condition.abstract" -import { zeroX, didZeroX } from "../../../utils" +import { zeroX, didZeroX, didPrefixed } from "../../../utils" import { InstantiableConfig } from "../../../Instantiable.abstract" export class AccessSecretStoreCondition extends Condition { @@ -19,4 +19,10 @@ export class AccessSecretStoreCondition extends Condition { public checkPermissions(grantee: string, did: string, from?: string) { return this.call("checkPermissions", [grantee, didZeroX(did)].map(zeroX), from) } + + public async getGrantedDidByConsumer(consumer: string): Promise { + return (await this.getPastEvents("Fulfilled", {_grantee: zeroX(consumer)})) + .map(({returnValues}) => returnValues._documentId) + .map(didPrefixed) + } } diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 35374b7..86579e6 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -286,6 +286,15 @@ export class OceanAssets extends Instantiable { return this.ocean.keeper.didRegistry.getAttributesByOwner(owner) } + /** + * Returns the assets of a consumer. + * @param {string} consumer Consumer address. + * @return {Promise} List of DIDs. + */ + public async consumerAssets(consumer: string) { + return this.ocean.keeper.conditions.accessSecretStoreCondition.getGrantedDidByConsumer(consumer) + } + /** * Search over the assets using a query. * @param {SearchQuery} query Query to filter the assets.