1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge branch 'main' into feature/issue777-add-ddo-param-when-did-exists

This commit is contained in:
Bogdan Fazakas 2021-06-07 11:13:03 +03:00
commit c9c983aaed
11 changed files with 1141 additions and 354 deletions

View File

@ -4,8 +4,54 @@ All notable changes to this project will be documented in this file. Dates are d
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
#### [v0.15.1](https://github.com/oceanprotocol/ocean.js/compare/v0.15.0...v0.15.1)
- Bump microbundle from 0.13.0 to 0.13.1 [`#820`](https://github.com/oceanprotocol/ocean.js/pull/820)
- Bump @types/node from 15.9.0 to 15.12.0 [`#829`](https://github.com/oceanprotocol/ocean.js/pull/829)
- Bump @typescript-eslint/eslint-plugin from 4.25.0 to 4.26.0 [`#824`](https://github.com/oceanprotocol/ocean.js/pull/824)
- Bump @typescript-eslint/parser from 4.25.0 to 4.26.0 [`#823`](https://github.com/oceanprotocol/ocean.js/pull/823)
- Bump @types/node from 15.6.1 to 15.9.0 [`#826`](https://github.com/oceanprotocol/ocean.js/pull/826)
- Add order checks for credentials [`#827`](https://github.com/oceanprotocol/ocean.js/pull/827)
- Feature/credentials support in asset [`#787`](https://github.com/oceanprotocol/ocean.js/pull/787)
- add "valid" boolean in File.ts [`#825`](https://github.com/oceanprotocol/ocean.js/pull/825)
- Add ocean.assets.isConsumable function [`#786`](https://github.com/oceanprotocol/ocean.js/pull/786)
- fix ddo encrypt for browsers [`#822`](https://github.com/oceanprotocol/ocean.js/pull/822)
- add type 'free' to BestPrice [`#821`](https://github.com/oceanprotocol/ocean.js/pull/821)
- fix version [`6735e8a`](https://github.com/oceanprotocol/ocean.js/commit/6735e8a33f8a6e2fb513d5412a0a1db70d8b59ff)
#### [v0.15.0](https://github.com/oceanprotocol/ocean.js/compare/v0.14.9...v0.15.0)
> 28 May 2021
- Bump typescript from 4.2.4 to 4.3.2 [`#819`](https://github.com/oceanprotocol/ocean.js/pull/819)
- Bump @truffle/hdwallet-provider from 1.3.0 to 1.4.0 [`#816`](https://github.com/oceanprotocol/ocean.js/pull/816)
- Bump mocha from 8.3.2 to 8.4.0 [`#817`](https://github.com/oceanprotocol/ocean.js/pull/817)
- Fix/ Order method should throw error messages [`#803`](https://github.com/oceanprotocol/ocean.js/pull/803)
- Bump @oceanprotocol/contracts from 0.6.2 to 0.6.3 [`#814`](https://github.com/oceanprotocol/ocean.js/pull/814)
- Bump release-it from 14.6.1 to 14.7.0 [`#815`](https://github.com/oceanprotocol/ocean.js/pull/815)
- Bump auto-changelog from 2.2.1 to 2.3.0 [`#813`](https://github.com/oceanprotocol/ocean.js/pull/813)
- Bump @types/node from 15.0.2 to 15.6.1 [`#810`](https://github.com/oceanprotocol/ocean.js/pull/810)
- remove all doo price tests [`#805`](https://github.com/oceanprotocol/ocean.js/pull/805)
- Bump ts-node from 9.1.1 to 10.0.0 [`#811`](https://github.com/oceanprotocol/ocean.js/pull/811)
- Bump @typescript-eslint/parser from 4.22.1 to 4.25.0 [`#812`](https://github.com/oceanprotocol/ocean.js/pull/812)
- Bump web3-eth-contract from 1.3.5 to 1.3.6 [`#802`](https://github.com/oceanprotocol/ocean.js/pull/802)
- Bump prettier from 2.2.1 to 2.3.0 [`#793`](https://github.com/oceanprotocol/ocean.js/pull/793)
- Bump @types/chai from 4.2.17 to 4.2.18 [`#797`](https://github.com/oceanprotocol/ocean.js/pull/797)
- Bump @typescript-eslint/eslint-plugin from 4.22.1 to 4.25.0 [`#809`](https://github.com/oceanprotocol/ocean.js/pull/809)
- Bump eslint from 7.25.0 to 7.27.0 [`#808`](https://github.com/oceanprotocol/ocean.js/pull/808)
- building package-lock.json with npm v7 [`#804`](https://github.com/oceanprotocol/ocean.js/pull/804)
- Bump mocha from 8.3.2 to 8.4.0 [`#801`](https://github.com/oceanprotocol/ocean.js/pull/801)
- Bump release-it from 14.6.1 to 14.6.2 [`#795`](https://github.com/oceanprotocol/ocean.js/pull/795)
- Bump lodash from 4.17.19 to 4.17.21 [`#799`](https://github.com/oceanprotocol/ocean.js/pull/799)
- Bump @truffle/hdwallet-provider from 1.3.0 to 1.4.0 [`#800`](https://github.com/oceanprotocol/ocean.js/pull/800)
- building package.json with npm v7 [`5aa8d7d`](https://github.com/oceanprotocol/ocean.js/commit/5aa8d7d10113a829f5c6a62bc21bf46d6e03e2ac)
- Release 0.15.0 [`ffcdfb2`](https://github.com/oceanprotocol/ocean.js/commit/ffcdfb299da05a8b2ecd3e449a0aeeaf5f1cc875)
- fix ddo encrypt for browsers [`b85d85b`](https://github.com/oceanprotocol/ocean.js/commit/b85d85b569731a3b68d7b2c4913a15e5bf1395db)
#### [v0.14.9](https://github.com/oceanprotocol/ocean.js/compare/v0.14.8...v0.14.9)
> 11 May 2021
- Feature/dispenser [`#790`](https://github.com/oceanprotocol/ocean.js/pull/790)
- Bump handlebars from 4.7.6 to 4.7.7 [`#791`](https://github.com/oceanprotocol/ocean.js/pull/791)
- Bump @types/node from 15.0.1 to 15.0.2 [`#785`](https://github.com/oceanprotocol/ocean.js/pull/785)
@ -13,7 +59,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Bump @typescript-eslint/eslint-plugin from 4.22.0 to 4.22.1 [`#783`](https://github.com/oceanprotocol/ocean.js/pull/783)
- Revert "creating interface for the dispenser contract" [`53b401d`](https://github.com/oceanprotocol/ocean.js/commit/53b401dcd059234197e0119e7eca2fecc8d19109)
- creating interface for the dispenser contract [`413b17e`](https://github.com/oceanprotocol/ocean.js/commit/413b17e668a4d92e2ab9d43092b604bc2b2f2914)
- Revert "instantiate Dispenser" [`603e512`](https://github.com/oceanprotocol/ocean.js/commit/603e512f8ac085f630555016fcb4095b766c6136)
- Release 0.14.9 [`804a6a6`](https://github.com/oceanprotocol/ocean.js/commit/804a6a62facbe9f6b110d4d864cd63424555316b)
#### [v0.14.8](https://github.com/oceanprotocol/ocean.js/compare/v0.14.7...v0.14.8)
@ -828,8 +874,8 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
> 30 September 2020
- add stats/history [`#289`](https://github.com/oceanprotocol/ocean.js/pull/289)
- add stats [`b815fbf`](https://github.com/oceanprotocol/ocean.js/commit/b815fbf0a135e0825674b45b64f1d911c03e6a59)
- add stats [`c6a9f7d`](https://github.com/oceanprotocol/ocean.js/commit/c6a9f7d69b8ac60f349e62a3dbc161b835b4fb41)
- add stats [`b815fbf`](https://github.com/oceanprotocol/ocean.js/commit/b815fbf0a135e0825674b45b64f1d911c03e6a59)
- small refactor [`6f645d6`](https://github.com/oceanprotocol/ocean.js/commit/6f645d6e8fd134a69b56e862e9652a830308df12)
#### [v0.3.2](https://github.com/oceanprotocol/ocean.js/compare/v0.3.1...v0.3.2)
@ -1202,4 +1248,4 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- minor rendering fix [`#11`](https://github.com/oceanprotocol/ocean.js/pull/11)
- cleaning [`cd9f629`](https://github.com/oceanprotocol/ocean.js/commit/cd9f6295ed08110936f9a092527bae5d0a974bbb)
- remove test output files [`28d1775`](https://github.com/oceanprotocol/ocean.js/commit/28d1775593ab78bf404708244c49af8f912f3117)
- first cut, highly WIP [`67af4d1`](https://github.com/oceanprotocol/ocean.js/commit/67af4d1d595fc105cc544ff019199c6cef9f76e5)
- add ownerAssets [`d8dadf5`](https://github.com/oceanprotocol/ocean.js/commit/d8dadf5495d77e2f73d45454347c13e185bed7d4)

866
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "@oceanprotocol/lib",
"source": "./src/index.ts",
"version": "0.14.9",
"version": "0.15.1",
"description": "JavaScript client library for Ocean Protocol",
"main": "./dist/node/lib.js",
"exports": "./dist/node/lib.js",
@ -56,7 +56,7 @@
"node-abort-controller": "^2.0.0",
"save-file": "^2.3.1",
"uuid": "^8.3.2",
"web3": "^1.3.6",
"web3": "^1.3.5",
"web3-eth-contract": "^1.3.6"
},
"devDependencies": {

View File

@ -7,6 +7,7 @@ import Web3Provider from '../datatokens/Web3Provider'
import { BestPrice } from './interfaces/BestPrice'
import { DataTokenInfo } from './interfaces/DataTokenInfo'
import { PurgatoryData } from './interfaces/PurgatoryData'
import { Credentials } from './interfaces/Credentials'
/**
* DID Descriptor Object.
* Contains all the data related to an asset.
@ -62,6 +63,8 @@ export class DDO {
public dataTokenInfo?: DataTokenInfo
public credentials?: Credentials
public constructor(ddo: Partial<DDO> = {}) {
Object.assign(this, ddo, {
created: (ddo && ddo.created) || new Date().toISOString().replace(/\.[0-9]{3}/, '')

View File

@ -0,0 +1,16 @@
export enum CredentialType {
address = 'address',
credential3Box = 'credential3Box'
}
export type CredentialAction = 'allow' | 'deny'
export interface Credential {
type: CredentialType
value: string[]
}
export interface Credentials {
allow?: Credential[]
deny?: Credential[]
}

View File

@ -61,4 +61,10 @@ export interface File {
* @example "zip"
*/
compression?: string
/**
* File availability (check fileinfo connectivity)
* @type {boolean}
*/
valid?: boolean
}

View File

@ -13,3 +13,4 @@ export * from './PublicKey'
export * from './Service'
export * from './ServicePrices'
export * from './PurgatoryData'
export * from './Credentials'

View File

@ -13,6 +13,12 @@ import { Provider } from '../provider/Provider'
import { isAddress } from 'web3-utils'
import { MetadataMain } from '../ddo/interfaces'
import { TransactionReceipt } from 'web3-core'
import {
CredentialType,
CredentialAction,
Credentials
} from '../ddo/interfaces/Credentials'
import { updateCredentialDetail, removeCredentialDetail } from './AssetsCredential'
import { Consumable } from '../ddo/interfaces/Consumable'
export enum CreateProgressStep {
@ -289,6 +295,71 @@ export class Assets extends Instantiable {
return ddo
}
/**
* Update Credentials attribute in DDO
* @param {ddo} DDO
* @param {credentialType} CredentialType e.g. address / credentail3Box
* @param {allowList} string[] List of allow credential
* @param {denyList} string[] List of deny credential
* @return {Promise<DDO>} Updated DDO
*/
public async updateCredentials(
ddo: DDO,
credentialType: CredentialType,
allowList: string[],
denyList: string[]
): Promise<DDO> {
let newDDo
if (allowList && allowList.length > 0) {
newDDo = updateCredentialDetail(ddo, credentialType, allowList, 'allow')
} else {
newDDo = removeCredentialDetail(ddo, credentialType, 'allow')
}
if (denyList && denyList.length > 0) {
newDDo = updateCredentialDetail(ddo, credentialType, denyList, 'deny')
} else {
newDDo = removeCredentialDetail(ddo, credentialType, 'deny')
}
return newDDo
}
/**
* check if a credential can consume a dataset
* @param {ddo} DDO
* @param {credentialType} CredentialType e.g. address / credentail3Box
* @param {value} string credential
* @return {Consumable} allowed 0 = OK , 2 - Credential not in allow list, 3 - Credential in deny list
*/
public checkCredential(
ddo: DDO,
credentialType: CredentialType,
value: string
): Consumable {
let status = 0
let message = 'All good'
if (ddo.credentials) {
if (ddo.credentials.allow && ddo.credentials.allow.length > 0) {
const allowList = ddo.credentials.allow.find(
(credentail) => credentail.type === credentialType
)
if (allowList && !allowList.value.includes(value)) {
status = 2
message = 'Credential missing from allow list'
}
}
if (ddo.credentials.deny && ddo.credentials.deny.length > 0) {
const denyList = ddo.credentials.deny.find(
(credentail) => credentail.type === credentialType
)
if (denyList && denyList.value.includes(value)) {
status = 3
message = 'Credential found on deny list'
}
}
}
return { status, message }
}
/**
* Publish DDO on chain.
* @param {ddo} DDO
@ -499,9 +570,18 @@ export class Assets extends Instantiable {
searchPreviousOrders = true
): Promise<string> {
let service: Service
<<<<<<< HEAD
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
const consumable = await this.isConsumable(ddo)
if (consumable.status > 0) return null
=======
const ddo = await this.resolve(did)
const consumable = await this.isConsumable(ddo, consumerAddress)
if (consumable.status > 0) {
throw new Error(`Order asset failed, ` + consumable.message)
}
>>>>>>> main
if (!consumerAddress) consumerAddress = payerAddress
if (serviceIndex === -1) {
@ -681,10 +761,13 @@ export class Assets extends Instantiable {
/**
*
* @param {DDO} ddo
* @param {consumer} string
* @return {Promise<Consumable>}
*/
public async isConsumable(ddo: DDO): Promise<Consumable> {
if (!ddo) return null
public async isConsumable(ddo: DDO, consumer?: string): Promise<Consumable> {
let status = 0
let message = 'All good'
if (!ddo) return { status, message }
const metadata = ddo.findServiceByType('metadata')
if (metadata.attributes.status?.isOrderDisabled)
@ -692,15 +775,13 @@ export class Assets extends Instantiable {
status: 1,
message: 'Ordering this asset has been temporarily disabled by the publisher.'
}
/*
// To do: call helper method check credential
// return: 4, Credential missing from allow list
// return: 5, Credential found on deny list
*/
return {
status: 0,
message: 'All good'
if (consumer) {
;({ status, message } = this.checkCredential(ddo, CredentialType.address, consumer))
}
/*
// return: 2, Credential missing from allow list
// return: 3, Credential found on deny list
*/
return { status, message }
}
}

View File

@ -0,0 +1,161 @@
import { DDO } from '../ddo/DDO'
import {
Credentials,
Credential,
CredentialType,
CredentialAction
} from '../ddo/interfaces/Credentials'
/**
* checks if a credential list exists for a specific action
* @param {credentials} Credentials list of crentials from ddo
* @param {credentialType} CredentialType e.g. address / credential3Box
* @param {credentialAction} CredentialAction allow or deny
* @return {boolean}
*/
export function checkCredentialExist(
credentials: Credentials,
credentialType: CredentialType,
credentialAction: CredentialAction
): boolean {
let isExist = false
if (credentialAction === 'allow') {
if (credentials && credentials.allow) {
const allowList = credentials.allow.find(
(credential) => credential.type === credentialType
)
isExist = allowList && allowList.value.length > 0
}
return isExist
} else {
if (credentials && credentials.deny) {
const dennyList = credentials.deny.find(
(credential) => credential.type === credentialType
)
isExist = dennyList && dennyList.value.length > 0
}
return isExist
}
}
/**
* Removes all credentials of a certain type for a specific action
* @param {ddo} DDO
* @param {credentialType} CredentialType e.g. address / credential3Box
* @param {credentialAction} CredentialAction allow or deny
* @return {DDO}
*/
export function removeCredentialDetail(
ddo: DDO,
credentialType: CredentialType,
credentialAction: CredentialAction
): DDO {
const exists = checkCredentialExist(ddo.credentials, credentialType, credentialAction)
if (credentialAction === 'allow') {
if (exists) {
ddo.credentials.allow = ddo.credentials.allow.filter(
(credential) => credential.type !== credentialType
)
}
if (ddo.credentials && !ddo.credentials.allow) {
ddo.credentials = {
deny: ddo.credentials && ddo.credentials.deny
}
}
} else {
if (exists) {
ddo.credentials.deny = ddo.credentials.deny.filter(
(credential) => credential.type !== credentialType
)
}
if (ddo.credentials && !ddo.credentials.deny) {
ddo.credentials = {
allow: ddo.credentials && ddo.credentials.allow
}
}
}
return ddo
}
/**
* Updates credentials of a certain type for a specific action
* @param {ddo} DDO
* @param {credentialType} CredentialType e.g. address / credential3Box
* @param {list} string[] list of values
* @param {credentialAction} CredentialAction allow or deny
* @return {DDO}
*/
export function updateCredentialDetail(
ddo: DDO,
credentialType: CredentialType,
list: string[],
credentialAction: CredentialAction
): DDO {
const exists = checkCredentialExist(ddo.credentials, credentialType, credentialAction)
if (credentialAction === 'allow') {
if (exists) {
ddo.credentials.allow.find((credential) => {
if (credential.type === credentialType) {
credential.value = list
}
})
} else {
ddo = addCredentialDetail(ddo, credentialType, list, credentialAction)
}
} else {
if (exists) {
ddo.credentials.deny.find((credential) => {
if (credential.type === credentialType) {
credential.value = list
}
})
} else {
ddo = addCredentialDetail(ddo, credentialType, list, credentialAction)
}
}
return ddo
}
/**
* Adds values to credentials of a certain type for a specific action
* @param {ddo} DDO
* @param {credentialType} CredentialType e.g. address / credential3Box
* @param {list} string[] list of values
* @param {credentialAction} CredentialAction allow or deny
* @return {DDO}
*/
export function addCredentialDetail(
ddo: DDO,
credentialType: CredentialType,
list: string[],
credentialAction: CredentialAction
) {
const newCredentialDetail: Credential = {
type: credentialType,
value: list
}
if (credentialAction === 'allow') {
if (ddo.credentials && ddo.credentials.allow) {
ddo.credentials.allow.push(newCredentialDetail)
} else {
const newCredentials: Credentials = {
allow: [newCredentialDetail],
deny: ddo.credentials && ddo.credentials.deny
}
ddo.credentials = newCredentials
}
} else {
if (ddo.credentials && ddo.credentials.deny) {
ddo.credentials.deny.push(newCredentialDetail)
} else {
const newCredential: Credentials = {
allow: ddo.credentials && ddo.credentials.allow,
deny: [newCredentialDetail]
}
ddo.credentials = newCredential
}
}
return ddo
}

View File

@ -5,7 +5,14 @@ import spies from 'chai-spies'
import Web3 from 'web3'
import { AbiItem } from 'web3-utils/types'
import { DataTokens } from '../../src/datatokens/Datatokens'
import { Account, EditableMetadata, Service, ServiceAccess, DID } from '../../src/lib'
import {
Account,
EditableMetadata,
Service,
ServiceAccess,
DID,
CredentialType
} from '../../src/lib'
import { noDidPrefixed } from '../../src/utils/'
import { Ocean } from '../../src/ocean/Ocean'
import { ConfigHelper } from '../../src/utils/ConfigHelper'
@ -39,12 +46,16 @@ use(spies)
describe('Marketplace flow', () => {
let owner: Account
let alice: Account
let bob: Account
let charlie: Account
let ddo
let ddoWithPool
let ddoWithBadUrl
let ddoEncrypted
let alice: Account
let ddoWithCredentialsAllowList
let ddoWithCredentialsDenyList
let ddoWithCredentials
let asset
let assetWithPool
let assetWithBadUrl
@ -63,6 +74,8 @@ describe('Marketplace flow', () => {
let data
let blob
let poolLastPrice
let allowList: any
let denyList: any
const marketplaceAllowance = '20'
const tokenAmount = '10000'
@ -83,9 +96,12 @@ describe('Marketplace flow', () => {
alice = (await ocean.accounts.list())[1]
bob = (await ocean.accounts.list())[2]
marketplace = (await ocean.accounts.list())[3]
charlie = (await ocean.accounts.list())[4]
data = { t: 1, url: config.metadataCacheUri }
blob = JSON.stringify(data)
await contracts.deployContracts(owner.getId())
allowList = [alice.getId(), bob.getId()]
denyList = [charlie.getId()]
})
it('Alice publishes a datatoken contract', async () => {
@ -222,7 +238,7 @@ describe('Marketplace flow', () => {
assert(ddo.dataToken === tokenAddress)
const storeTx = await ocean.onChainMetadata.publish(ddo.id, ddo, alice.getId())
assert(storeTx)
await waitForAqua(ocean, ddo.id)
ddoWithBadUrl = await ocean.assets.create(
assetWithBadUrl,
alice,
@ -236,7 +252,7 @@ describe('Marketplace flow', () => {
alice.getId()
)
assert(storeTxWithBadUrl)
await waitForAqua(ocean, ddoWithBadUrl.id)
ddoWithPool = await ocean.assets.create(
assetWithPool,
alice,
@ -250,10 +266,8 @@ describe('Marketplace flow', () => {
alice.getId()
)
assert(storeTxWithPool)
await waitForAqua(ocean, ddoWithPool.id)
})
it('Alice publishes an encrypted dataset', async () => {
// publish an encrypted dataset
ddoEncrypted = await ocean.assets.create(
assetWithEncrypt,
alice,
@ -261,15 +275,62 @@ describe('Marketplace flow', () => {
tokenAddressEncrypted
)
assert(ddoEncrypted.dataToken === tokenAddressEncrypted)
const storeTx = await ocean.onChainMetadata.publish(
const storeTxEncrypted = await ocean.onChainMetadata.publish(
ddoEncrypted.id,
ddoEncrypted,
alice.getId(),
true
)
assert(storeTx)
assert(storeTxEncrypted)
// create assets with credentials allow & deny list
ddoWithCredentialsAllowList = await ocean.assets.create(
asset,
alice,
[service1],
null
)
const storeTxWithCredentialsAllowList = await ocean.onChainMetadata.publish(
ddoWithCredentialsAllowList.id,
ddoWithCredentialsAllowList,
alice.getId(),
false
)
assert(storeTxWithCredentialsAllowList)
ddoWithCredentialsDenyList = await ocean.assets.create(asset, alice, [service1], null)
const storeTxWithCredentialsDenyList = await ocean.onChainMetadata.publish(
ddoWithCredentialsDenyList.id,
ddoWithCredentialsDenyList,
alice.getId(),
false
)
assert(storeTxWithCredentialsDenyList)
ddoWithCredentials = await ocean.assets.create(asset, alice, [service1], null)
ddoWithCredentials = await ocean.assets.updateCredentials(
ddoWithCredentials,
CredentialType.address,
allowList,
denyList
)
const storeTxWithCredentials = await ocean.onChainMetadata.publish(
ddoWithCredentials.id,
ddoWithCredentials,
alice.getId(),
false
)
assert(storeTxWithCredentials)
// wait for all this assets to be published
await waitForAqua(ocean, ddo.id)
await waitForAqua(ocean, ddoWithBadUrl.id)
await waitForAqua(ocean, ddoWithPool.id)
await waitForAqua(ocean, ddoEncrypted.id)
await waitForAqua(ocean, ddoWithCredentialsAllowList.id)
await waitForAqua(ocean, ddoWithCredentialsDenyList.id)
await waitForAqua(ocean, ddoWithCredentials.id)
})
it('Marketplace should resolve asset using DID', async () => {
await ocean.assets.resolve(ddo.id).then((newDDO) => {
assert(newDDO.id === ddo.id)
@ -381,6 +442,30 @@ describe('Marketplace flow', () => {
assert(assets.results.length > 0)
})
it('Alice adds allow credentials for a dataset and deny credentials for another', async () => {
const resolvedDDO = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
const newDdo = await ocean.assets.updateCredentials(
resolvedDDO,
CredentialType.address,
allowList,
[]
)
const txid = await ocean.onChainMetadata.update(newDdo.id, newDdo, alice.getId())
assert(txid !== null)
})
it('Alice adds deny credentials for a dataset', async () => {
const resolvedDDO = await ocean.assets.resolve(ddoWithCredentialsDenyList.id)
const newDdo = await ocean.assets.updateCredentials(
resolvedDDO,
CredentialType.address,
[],
denyList
)
const txid = await ocean.onChainMetadata.update(newDdo.id, newDdo, alice.getId())
assert(txid !== null)
})
it('Alice updates metadata and removes sample links', async () => {
const newMetaData: EditableMetadata = {
description: 'new description no links',
@ -586,4 +671,34 @@ describe('Marketplace flow', () => {
)
assert(balance.toString() === balanceAfterOrder.toString())
})
it('Bob should be able to consume an asset with allow list, because he is on that list', async () => {
const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
let consumable = await ocean.assets.isConsumable(ddoWithAllowList, bob.getId())
assert(consumable.status === 0)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0)
})
it('Bob should be able to consume an asset with deny list, because he is not on that list', async () => {
const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id)
let consumable = await ocean.assets.isConsumable(ddoWithDenyList, bob.getId())
assert(consumable.status === 0)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0)
})
it('Charlie should not be able to consume an asset with allow list, because he is not on that list', async () => {
const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
let consumable = await ocean.assets.isConsumable(ddoWithAllowList, charlie.getId())
assert(consumable.status === 2)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3)
})
it('Charlie should not be able to consume an asset with deny list, because he is on that list', async () => {
const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id)
let consumable = await ocean.assets.isConsumable(ddoWithDenyList, charlie.getId())
assert(consumable.status === 3)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3)
})
})

View File

@ -3,19 +3,40 @@ import spies from 'chai-spies'
import { SearchQuery, MetadataCache } from '../../../src/metadatacache/MetadataCache'
import { Ocean } from '../../../src/ocean/Ocean'
import config from '../config'
import { DDO } from '../../../src/lib'
import Web3 from 'web3'
import { Account, DDO, CredentialType, ConfigHelper } from '../../../src/lib'
import { responsify, getSearchResults } from '../helpers'
const web3 = new Web3('http://127.0.0.1:8545')
use(spies)
describe('Assets', () => {
let ocean: Ocean
let metadataCache: MetadataCache
let asset: any
let owner: Account
let alice: Account
let bob: Account
let charlie: Account
let ddo: DDO
let ddoWithAddressAnd3Box: DDO
const addressType = CredentialType.address
const threeBoxType = CredentialType.credential3Box
let walletA: string
let walletB: string
let walletC: string
const threeBoxValue = 'did:3:bafyre'
beforeEach(async () => {
const config = new ConfigHelper().getConfig('development')
config.web3Provider = web3
ocean = await Ocean.getInstance(config)
metadataCache = ocean.metadataCache // eslint-disable-line prefer-destructuring
owner = (await ocean.accounts.list())[0]
alice = (await ocean.accounts.list())[1]
walletA = alice.getId()
bob = (await ocean.accounts.list())[2]
walletB = bob.getId()
charlie = (await ocean.accounts.list())[3]
walletC = charlie.getId()
})
afterEach(() => {
@ -57,4 +78,125 @@ describe('Assets', () => {
assert.isDefined(assets.results[0].findServiceById)
})
})
it('Generates metadata', async () => {
asset = {
main: {
type: 'dataset',
name: 'test-dataset',
dateCreated: new Date(Date.now()).toISOString().split('.')[0] + 'Z', // remove milliseconds
author: 'oceanprotocol-team',
license: 'MIT',
files: [
{
url: 'https://s3.amazonaws.com/testfiles.oceanprotocol.com/info.0.json',
checksum: 'efb2c764274b745f5fc37f97c6b0e761',
contentLength: '4535431',
contentType: 'text/csv',
encoding: 'UTF-8',
compression: 'zip'
}
]
}
}
})
it('Alice creates a DDO', async () => {
const price = '10' // in datatoken
const publishedDate = new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0
const service1 = await ocean.assets.createAccessServiceAttributes(
alice,
price,
publishedDate,
timeout
)
ddo = await ocean.assets.create(asset, alice, [service1], null)
})
it('should add allow credential', async () => {
assert(ocean.assets.checkCredential(ddo, addressType, walletA).status === 0)
assert(ocean.assets.checkCredential(ddo, addressType, walletC).status === 0)
const allowWalletAddressList = [walletA, walletB]
console.error(JSON.stringify(ddo.credentials))
const newDdo = await ocean.assets.updateCredentials(
ddo,
addressType,
allowWalletAddressList,
[]
)
assert(newDdo.credentials.allow.length === 1)
assert(ocean.assets.checkCredential(ddo, addressType, walletA).status === 0)
assert(ocean.assets.checkCredential(ddo, addressType, walletC).status === 2)
})
it('should append allow credential', async () => {
const allowWalletAddressList = [walletA, walletB]
const allow3BoxList = [threeBoxValue]
ddoWithAddressAnd3Box = await ocean.assets.updateCredentials(
ddo,
addressType,
allowWalletAddressList,
[]
)
ddoWithAddressAnd3Box = await ocean.assets.updateCredentials(
ddo,
threeBoxType,
allow3BoxList,
[]
)
assert(ddoWithAddressAnd3Box.credentials.allow.length === 2)
})
it('should add deny credential', async () => {
const denyWalletAddressList = [walletC]
const newDdo = await ocean.assets.updateCredentials(
ddo,
addressType,
[],
denyWalletAddressList
)
assert(newDdo.credentials.deny.length === 1)
assert(ocean.assets.checkCredential(ddo, addressType, walletA).status === 0)
assert(ocean.assets.checkCredential(ddo, addressType, walletC).status === 3)
})
it('should append deny credential', async () => {
const denyWalletAddressList = [walletC]
const deny3BoxList = [threeBoxValue]
let newDdo = await ocean.assets.updateCredentials(
ddo,
addressType,
[],
denyWalletAddressList
)
newDdo = await ocean.assets.updateCredentials(ddo, threeBoxType, [], deny3BoxList)
assert(newDdo.credentials.deny.length === 2)
})
it('should only remove allow credential by credential type', async () => {
const allowWalletAddressList = [walletA, walletB]
const allow3BoxList = [threeBoxValue]
let newDdo = await ocean.assets.updateCredentials(
ddo,
addressType,
allowWalletAddressList,
[]
)
newDdo = await ocean.assets.updateCredentials(
ddoWithAddressAnd3Box,
threeBoxType,
allow3BoxList,
[]
)
assert(newDdo.credentials.allow.length === 2)
newDdo = await ocean.assets.updateCredentials(
ddoWithAddressAnd3Box,
threeBoxType,
[],
[]
)
assert(newDdo.credentials.allow.length === 1)
assert(ocean.assets.checkCredential(ddo, addressType, walletA).status === 0)
assert(ocean.assets.checkCredential(ddo, addressType, walletC).status === 2)
})
})