diff --git a/src/keeper/contracts/DIDRegistry.ts b/src/keeper/contracts/DIDRegistry.ts index 026491d..5d98049 100644 --- a/src/keeper/contracts/DIDRegistry.ts +++ b/src/keeper/contracts/DIDRegistry.ts @@ -75,7 +75,10 @@ export default class DIDRegistry extends ContractBase { } public async revokePermission(did: string, grantee: string, ownerAddress: string) { - return this.send('revokePermission', ownerAddress, [zeroX(did), zeroX(grantee)]) + return this.send('revokePermission', ownerAddress, [ + didZeroX(did), + zeroX(grantee) + ]) } public async getPermission(did: string, grantee: string): Promise { diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index b171ca0..2515855 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -5,7 +5,7 @@ import { MetaData } from '../ddo/MetaData' import { Service, ServiceAccess } from '../ddo/Service' import Account from './Account' import DID from './DID' -import { fillConditionsWithDDO, SubscribablePromise } from '../utils' +import { fillConditionsWithDDO, SubscribablePromise, didZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { OrderProgressStep } from './utils/ServiceUtils' @@ -416,4 +416,67 @@ export class OceanAssets extends Instantiable { } } } + + /** + * Get FreeWhiteList for a DID + * @param {string} did Asset DID. + * @return {Promise} List of addresses. + */ + public async getFreeWhiteList(did: string): Promise { + const events = await this.ocean.keeper.didRegistry.getPastEvents( + 'DIDPermissionGranted', + { + _did: didZeroX(did) + } + ) + const list = events.map(({ returnValues }) => returnValues._grantee) + const filteredList = [] + for (let index = 0; index < list.length; index++) { + const address = list[index] + const hasPermission = await this.ocean.keeper.didRegistry.getPermission( + did, + address + ) + if (hasPermission) filteredList.push(address) + } + return filteredList + } + + /** + * Add consumer to FreeWhiteList for a DID + * @param {string} did Asset DID. + * @param {string} consumer Ethereum address to add to the list. + * @param {Account} account Ethereum account of DID owner + * @return None + */ + public async addConsumerToFreeWhiteList( + did: string, + consumer: string, + account: Account + ): Promise { + await this.ocean.keeper.didRegistry.grantPermission( + did, + consumer, + account.getId() + ) + } + + /** + * Remove consumer from DID's FreeWhiteList + * @param {string} did Asset DID. + * @param {string} consumer Ethereum address to add to the list. + * @param {Account} account Ethereum account of DID owner + * @return None + */ + public async removeConsumerFromFreeWhiteList( + did: string, + consumer: string, + account: Account + ): Promise { + await this.ocean.keeper.didRegistry.revokePermission( + did, + consumer, + account.getId() + ) + } } diff --git a/test/integration/ocean/AssetOwners.test.ts b/test/integration/ocean/AssetOwners.test.ts index 33297cb..215c197 100644 --- a/test/integration/ocean/AssetOwners.test.ts +++ b/test/integration/ocean/AssetOwners.test.ts @@ -8,6 +8,8 @@ describe('Asset Owners', () => { let account1: Account let account2: Account + let consumer1: Account + let consumer2: Account let metadata = getMetadata() @@ -16,6 +18,8 @@ describe('Asset Owners', () => { // Accounts ;[account1, account2] = await ocean.accounts.list() + consumer1 = (await ocean.accounts.list())[3] + consumer2 = (await ocean.accounts.list())[4] if (!ocean.keeper.dispenser) { metadata = getMetadata(0) @@ -113,4 +117,21 @@ describe('Asset Owners', () => { assert.equal(newOwner, account2.getId()) }) + + it('should add and remove correctly an address to/from FreeWhiteList on an asset', async () => { + const ddo = await ocean.assets.create(metadata as any, account1) + await ocean.assets.addConsumerToFreeWhiteList(ddo.id, consumer1.getId(), account1) + await ocean.assets.addConsumerToFreeWhiteList(ddo.id, consumer2.getId(), account1) + const list = await ocean.assets.getFreeWhiteList(ddo.id) + assert.notEqual(-1, list.indexOf(consumer1.getId())) + assert.notEqual(-1, list.indexOf(consumer2.getId())) + await ocean.assets.removeConsumerFromFreeWhiteList( + ddo.id, + consumer1.getId(), + account1 + ) + const remList = await ocean.assets.getFreeWhiteList(ddo.id) + assert.equal(-1, remList.indexOf(consumer1.getId())) + assert.notEqual(-1, remList.indexOf(consumer2.getId())) + }) })