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

Merge pull request #395 from oceanprotocol/feature/freeWhiteList

freeWhiteList
This commit is contained in:
Matthias Kretschmann 2020-04-02 14:13:40 +02:00 committed by GitHub
commit f28640724c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 2 deletions

View File

@ -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<boolean> {

View File

@ -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<string[]>} List of addresses.
*/
public async getFreeWhiteList(did: string): Promise<string[]> {
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<TransactionReceipt> {
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<TransactionReceipt> {
await this.ocean.keeper.didRegistry.revokePermission(
did,
consumer,
account.getId()
)
}
}

View File

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