2019-06-20 00:20:09 +02:00
|
|
|
import { assert } from 'chai'
|
|
|
|
import { config } from '../config'
|
|
|
|
import { getMetadata } from '../utils'
|
2020-03-30 13:57:14 +02:00
|
|
|
import { Ocean, Account, EditableMetaData } from '../../../src' // @oceanprotocol/squid
|
2020-05-09 21:14:57 +02:00
|
|
|
import { ServiceComputePrivacy } from '../../../src/ddo/Service'
|
2019-04-15 15:30:02 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('Asset Owners', () => {
|
2019-04-15 15:30:02 +02:00
|
|
|
let ocean: Ocean
|
|
|
|
|
2019-04-16 13:27:37 +02:00
|
|
|
let account1: Account
|
|
|
|
let account2: Account
|
2020-03-25 23:34:34 +01:00
|
|
|
let consumer1: Account
|
|
|
|
let consumer2: Account
|
2019-04-15 15:30:02 +02:00
|
|
|
|
2019-06-26 20:57:50 +02:00
|
|
|
let metadata = getMetadata()
|
2019-04-15 15:30:02 +02:00
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
|
|
|
|
// Accounts
|
2019-06-24 13:06:38 +02:00
|
|
|
;[account1, account2] = await ocean.accounts.list()
|
2020-03-25 23:34:34 +01:00
|
|
|
consumer1 = (await ocean.accounts.list())[3]
|
|
|
|
consumer2 = (await ocean.accounts.list())[4]
|
2019-06-26 20:57:50 +02:00
|
|
|
|
|
|
|
if (!ocean.keeper.dispenser) {
|
|
|
|
metadata = getMetadata(0)
|
|
|
|
}
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should set the owner of an asset', async () => {
|
2019-04-16 13:27:37 +02:00
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
2019-04-15 15:30:02 +02:00
|
|
|
const owner = await ocean.assets.owner(ddo.id)
|
|
|
|
|
2019-04-16 13:27:37 +02:00
|
|
|
assert.equal(owner, account1.getId())
|
|
|
|
})
|
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should set the provider of an asset', async () => {
|
2019-08-30 10:50:30 +02:00
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
const isProvider = await ocean.keeper.didRegistry.isDIDProvider(
|
|
|
|
ddo.id,
|
|
|
|
config.brizoAddress
|
|
|
|
)
|
2019-08-30 10:50:30 +02:00
|
|
|
|
|
|
|
assert.isTrue(isProvider)
|
|
|
|
})
|
|
|
|
|
2019-09-26 14:59:05 +02:00
|
|
|
it('should be added correctly a permission on an asset', async () => {
|
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
assert.isFalse(
|
|
|
|
await ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())
|
|
|
|
)
|
2019-09-26 14:59:05 +02:00
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
await ocean.keeper.didRegistry.grantPermission(
|
|
|
|
ddo.id,
|
|
|
|
account2.getId(),
|
|
|
|
account1.getId()
|
|
|
|
)
|
2019-09-26 14:59:05 +02:00
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
assert.isTrue(
|
|
|
|
await ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())
|
|
|
|
)
|
2019-09-26 14:59:05 +02:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should get the assets owned by a user', async () => {
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: initialLength } = await ocean.assets.ownerAssets(account2.getId())
|
2019-04-16 13:27:37 +02:00
|
|
|
|
|
|
|
await ocean.assets.create(metadata as any, account1)
|
|
|
|
await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
|
|
|
await ocean.assets.create(metadata as any, account2)
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: finalLength } = await ocean.assets.ownerAssets(account2.getId())
|
2019-04-16 13:27:37 +02:00
|
|
|
|
|
|
|
assert.equal(finalLength - initialLength, 1)
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|
2019-04-16 14:03:16 +02:00
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should get the assets that can be consumed by a user', async () => {
|
2019-11-15 00:00:10 +01:00
|
|
|
const { length: initialLength } = await ocean.assets.consumerAssets(
|
|
|
|
account2.getId()
|
|
|
|
)
|
2020-03-25 13:07:51 +01:00
|
|
|
|
2019-04-16 14:03:16 +02:00
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
2019-11-15 00:00:10 +01:00
|
|
|
const { length: finalLength1 } = await ocean.assets.consumerAssets(
|
|
|
|
account2.getId()
|
|
|
|
)
|
2020-03-30 14:09:23 +02:00
|
|
|
|
2019-04-16 14:03:16 +02:00
|
|
|
assert.equal(finalLength1 - initialLength, 0)
|
|
|
|
// Granting access
|
2019-06-26 20:57:50 +02:00
|
|
|
try {
|
2019-11-15 00:00:10 +01:00
|
|
|
await account2.requestTokens(
|
2020-03-06 11:35:31 +01:00
|
|
|
parseInt(
|
|
|
|
(
|
|
|
|
+metadata.main.price *
|
|
|
|
10 ** -(await ocean.keeper.token.decimals())
|
|
|
|
).toString()
|
|
|
|
)
|
2019-11-15 00:00:10 +01:00
|
|
|
)
|
2020-03-30 14:09:23 +02:00
|
|
|
} catch {}
|
2020-01-27 19:14:12 +01:00
|
|
|
await ocean.assets.order(ddo.id, account2)
|
2019-04-16 14:03:16 +02:00
|
|
|
// Access granted
|
2019-11-15 00:00:10 +01:00
|
|
|
const { length: finalLength2 } = await ocean.assets.consumerAssets(
|
|
|
|
account2.getId()
|
|
|
|
)
|
2019-04-16 14:03:16 +02:00
|
|
|
assert.equal(finalLength2 - initialLength, 1)
|
|
|
|
})
|
2019-11-13 16:48:53 +01:00
|
|
|
|
|
|
|
it('should be able to transfer ownership', async () => {
|
|
|
|
const { id } = await ocean.assets.create(metadata as any, account1)
|
|
|
|
// transfer
|
2020-03-25 10:21:03 +01:00
|
|
|
await ocean.assets.transferOwnership(id, account2.getId(), account1)
|
2019-11-13 16:48:53 +01:00
|
|
|
const newOwner = await ocean.keeper.didRegistry.getDIDOwner(id)
|
|
|
|
assert.equal(newOwner, account2.getId())
|
2020-03-25 10:21:03 +01:00
|
|
|
// check aquarius
|
|
|
|
const aquariusOwner = await ocean.assets.owner(id)
|
|
|
|
assert.equal(aquariusOwner, account2.getId())
|
2019-11-13 16:48:53 +01:00
|
|
|
})
|
2020-03-30 13:57:14 +02:00
|
|
|
|
|
|
|
it('should be able to update metadata', async () => {
|
|
|
|
const { id } = await ocean.assets.create(metadata as any, account1)
|
2020-03-30 16:53:36 +02:00
|
|
|
const links = [
|
|
|
|
{
|
|
|
|
name: 'Sample1',
|
|
|
|
url: 'http://www.example.com',
|
|
|
|
type: 'sample'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Sample2',
|
|
|
|
url: 'http://www.example.net',
|
|
|
|
type: 'sample'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
const newPrices = [
|
|
|
|
{
|
|
|
|
serviceIndex: 0,
|
|
|
|
price: '31000000000000000000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
serviceIndex: 2,
|
|
|
|
price: '31000000000000000000'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const newMetaData = {
|
|
|
|
title: 'New title',
|
|
|
|
description: 'New description',
|
|
|
|
links: links,
|
|
|
|
servicePrices: newPrices
|
|
|
|
}
|
2020-03-30 14:09:23 +02:00
|
|
|
await ocean.assets.editMetadata(id, newMetaData, account1)
|
|
|
|
|
2020-03-30 13:57:14 +02:00
|
|
|
const newDDO = await ocean.assets.resolve(id)
|
2020-03-30 14:09:23 +02:00
|
|
|
|
|
|
|
assert.equal(newDDO.service[0].attributes.main.name, 'New title')
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[0].attributes.additionalInformation.description,
|
|
|
|
'New description'
|
|
|
|
)
|
2020-03-30 16:53:36 +02:00
|
|
|
assert.equal(newDDO.service[0].attributes.main.price, '31000000000000000000')
|
|
|
|
assert.equal(newDDO.service[2].attributes.main.price, '31000000000000000000')
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[0].attributes.additionalInformation.links[0].name,
|
|
|
|
'Sample1'
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[0].attributes.additionalInformation.links[0].url,
|
|
|
|
'http://www.example.com'
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[0].attributes.additionalInformation.links[1].name,
|
|
|
|
'Sample2'
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[0].attributes.additionalInformation.links[1].url,
|
|
|
|
'http://www.example.net'
|
|
|
|
)
|
2020-03-30 13:57:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to retire metadata', async () => {
|
|
|
|
const { id } = await ocean.assets.create(metadata as any, account1)
|
2020-03-30 14:09:23 +02:00
|
|
|
|
|
|
|
await ocean.assets.retire(id, account1)
|
|
|
|
|
2020-03-30 13:57:14 +02:00
|
|
|
const newDDO = await ocean.assets.resolve(id)
|
2020-03-30 14:09:23 +02:00
|
|
|
|
2020-03-30 13:57:14 +02:00
|
|
|
assert.equal(newDDO, null)
|
|
|
|
})
|
2020-04-09 15:48:22 +02:00
|
|
|
|
2020-03-25 23:34:34 +01:00
|
|
|
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()))
|
|
|
|
})
|
2020-05-09 21:14:57 +02:00
|
|
|
|
|
|
|
it('should be able to update computePrivacy', async () => {
|
|
|
|
const origComputePrivacy = {
|
|
|
|
allowRawAlgorithm: true,
|
|
|
|
allowNetworkAccess: true,
|
|
|
|
trustedAlgorithms: []
|
|
|
|
}
|
|
|
|
|
|
|
|
const newComputePrivacy = {
|
|
|
|
allowRawAlgorithm: false,
|
|
|
|
allowNetworkAccess: false,
|
|
|
|
trustedAlgorithms: ['did:op:123', 'did:op:1234']
|
|
|
|
}
|
|
|
|
const computeService = await ocean.compute.createComputeServiceAttributes(
|
|
|
|
account1,
|
|
|
|
'0',
|
|
|
|
'2020-03-10T10:00:00Z',
|
|
|
|
origComputePrivacy as ServiceComputePrivacy
|
|
|
|
)
|
|
|
|
const { id } = await ocean.assets.create(metadata as any, account1, [
|
|
|
|
computeService
|
|
|
|
])
|
|
|
|
|
|
|
|
const oldDDO = await ocean.assets.resolve(id)
|
|
|
|
let serviceIndex = null
|
|
|
|
|
|
|
|
for (let index = 0; index < oldDDO.service.length; index++) {
|
2020-05-09 21:20:40 +02:00
|
|
|
if (oldDDO.service[index].type === 'compute') serviceIndex = index
|
2020-05-09 21:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await ocean.assets.updateComputePrivacy(
|
|
|
|
id,
|
|
|
|
serviceIndex,
|
|
|
|
newComputePrivacy as ServiceComputePrivacy,
|
|
|
|
account1
|
|
|
|
)
|
|
|
|
|
|
|
|
const newDDO = await ocean.assets.resolve(id)
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[serviceIndex].attributes.main.privacy.allowRawAlgorithm,
|
|
|
|
newComputePrivacy.allowRawAlgorithm
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
newDDO.service[serviceIndex].attributes.main.privacy.allowNetworkAccess,
|
|
|
|
newComputePrivacy.allowNetworkAccess
|
|
|
|
)
|
2020-05-10 10:08:09 +02:00
|
|
|
assert.deepEqual(
|
2020-05-09 21:14:57 +02:00
|
|
|
newDDO.service[serviceIndex].attributes.main.privacy.trustedAlgorithms,
|
|
|
|
newComputePrivacy.trustedAlgorithms
|
|
|
|
)
|
|
|
|
})
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|