Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Ocean.assets.isConsumable return interface and message #869

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ddo/interfaces/Consumable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Consumable {
status: number
message: string
result: boolean
}
29 changes: 19 additions & 10 deletions src/ocean/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,16 @@ export class Assets extends Instantiable {
): Consumable {
let status = 0
let message = 'All good'
let result = true
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'
message = 'Access is denied, your wallet address is not found on allow list'
result = false
}
}
if (ddo.credentials.deny && ddo.credentials.deny.length > 0) {
Expand All @@ -349,11 +351,12 @@ export class Assets extends Instantiable {
)
if (denyList && denyList.value.includes(value)) {
status = 3
message = 'Credential found on deny list'
message = 'Access is denied, your wallet address is found on deny list'
result = false
}
}
}
return { status, message }
return { status, message, result }
}

/**
Expand Down Expand Up @@ -568,7 +571,7 @@ export class Assets extends Instantiable {
let service: Service
const { ddo } = await assetResolve(asset, this.ocean)
const consumable = await this.isConsumable(ddo, consumerAddress)
if (consumable.status > 0) {
if (!consumable.result) {
throw new Error(`Order asset failed, ` + consumable.message)
}

Expand Down Expand Up @@ -756,21 +759,27 @@ export class Assets extends Instantiable {
public async isConsumable(ddo: DDO, consumer?: string): Promise<Consumable> {
let status = 0
let message = 'All good'
if (!ddo) return { status, message }
let result = true
if (!ddo) return { status, message, result }
const metadata = ddo.findServiceByType('metadata')

if (metadata.attributes.status?.isOrderDisabled)
return {
status: 1,
message: 'Ordering this asset has been temporarily disabled by the publisher.'
message: 'Ordering this asset has been temporarily disabled by the publisher.',
result: false
}
if (consumer) {
;({ status, message } = this.checkCredential(ddo, CredentialType.address, consumer))
;({ status, message, result } = this.checkCredential(
ddo,
CredentialType.address,
consumer
))
}
/*
// return: 2, Credential missing from allow list
// return: 3, Credential found on deny list
// return: 2, Access is denied, your wallet address is not found on allow list
// return: 3, Access is denied, your wallet address is found on deny list
*/
return { status, message }
return { status, message, result }
}
}
10 changes: 10 additions & 0 deletions test/integration/Marketplaceflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ describe('Marketplace flow', () => {
const response = await ocean.assets.isConsumable(ddo)
assert(response !== null)
assert(response.status === 0)
assert(response.result === true)
})

it('Alice should update her asset and set isOrderDisabled = true', async () => {
Expand All @@ -569,6 +570,7 @@ describe('Marketplace flow', () => {
const response = await ocean.assets.isConsumable(ddo)
assert(response !== null)
assert(response.status === 1)
assert(response.result === false)
})

it('Alice should create a FRE pricing for her asset', async () => {
Expand Down Expand Up @@ -676,29 +678,37 @@ describe('Marketplace flow', () => {
const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
let consumable = await ocean.assets.isConsumable(ddoWithAllowList, bob.getId())
assert(consumable.status === 0)
assert(consumable.result === true)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0)
assert(consumable.result === true)
})

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)
assert(consumable.result === true)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0)
assert(consumable.result === true)
})
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)
assert(consumable.result === false)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3)
assert(consumable.result === false)
})
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)
assert(consumable.result === false)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3)
assert(consumable.result === false)
})
})