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

Add consumerAssets method to assets module.

This commit is contained in:
Pedro Gutiérrez 2019-04-16 14:03:16 +02:00 committed by Pedro Gutiérrez
parent 47ced8af1b
commit b36e3a4f06
4 changed files with 35 additions and 3 deletions

View File

@ -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)
})
})

View File

@ -28,7 +28,7 @@ export default class DIDRegistry extends ContractBase {
}
public async getAttributesByOwner(owner: string): Promise<string[]> {
return (await this.getPastEvents('DIDAttributeRegistered', {_owner: zeroX(owner)}))
return (await this.getPastEvents("DIDAttributeRegistered", {_owner: zeroX(owner)}))
.map(({returnValues}) => returnValues._did)
.map(didPrefixed)
}

View File

@ -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<boolean>("checkPermissions", [grantee, didZeroX(did)].map(zeroX), from)
}
public async getGrantedDidByConsumer(consumer: string): Promise<string[]> {
return (await this.getPastEvents("Fulfilled", {_grantee: zeroX(consumer)}))
.map(({returnValues}) => returnValues._documentId)
.map(didPrefixed)
}
}

View File

@ -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<string[]>} 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.