diff --git a/src/ddo/interfaces/Consumable.ts b/src/ddo/interfaces/Consumable.ts index 04376268..ddfbee69 100644 --- a/src/ddo/interfaces/Consumable.ts +++ b/src/ddo/interfaces/Consumable.ts @@ -1,4 +1,5 @@ export interface Consumable { status: number message: string + result: boolean } diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index bc4eebff..8332155d 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -333,6 +333,7 @@ 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( @@ -340,7 +341,8 @@ export class Assets extends Instantiable { ) 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) { @@ -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 } } /** @@ -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) } @@ -756,21 +759,27 @@ export class Assets extends Instantiable { public async isConsumable(ddo: DDO, consumer?: string): Promise { 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 } } } diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index fdaf59dc..f1365059 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -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 () => { @@ -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 () => { @@ -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) }) })