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

complete Escrow template flow

This commit is contained in:
Pedro Gutiérrez 2019-03-01 13:31:58 +01:00 committed by Pedro Gutiérrez
parent c9b383223b
commit f7cd71ac33
2 changed files with 65 additions and 16 deletions

View File

@ -2,7 +2,7 @@ import { assert } from 'chai'
import { config } from "../config"
import { Ocean, templates, conditions, generateId, Keeper } from '../../src' // @oceanprotocol/squid
import { Ocean, templates, conditions, generateId, Keeper, Account } from '../../src' // @oceanprotocol/squid
const { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } = conditions
const { EscrowAccessSecretStoreTemplate } = templates
@ -13,13 +13,13 @@ describe("Register Escrow Access Secret Store Template", () => {
const agreementId = `0x${generateId()}`
const escrowAmount = 12
const did = `0x${"a".repeat(64)}`
const did = `0x${generateId()}`
const url = 'https://example.com/did/ocean/test-attr-example.txt'
const checksum = "b".repeat(32)
let templateManagerOwner: string
let sender: string
let receiver: string
let templateManagerOwner: Account
let publisher: Account
let consumer: Account
let accessSecretStoreCondition: conditions.AccessSecretStoreCondition
let lockRewardCondition: conditions.LockRewardCondition
@ -38,9 +38,9 @@ describe("Register Escrow Access Secret Store Template", () => {
template = await EscrowAccessSecretStoreTemplate.getInstance()
// Accounts
templateManagerOwner = (await ocean.accounts.list())[0].getId()
sender = (await ocean.accounts.list())[1].getId()
receiver = (await ocean.accounts.list())[2].getId()
templateManagerOwner = (await ocean.accounts.list())[0]
publisher = (await ocean.accounts.list())[1]
consumer = (await ocean.accounts.list())[2]
// Conditions
accessSecretStoreCondition = await AccessSecretStoreCondition.getInstance()
@ -49,21 +49,32 @@ describe("Register Escrow Access Secret Store Template", () => {
})
it("should propose the template", async () => {
await keeper.templateStoreManager.proposeTemplate(template.getAddress(), receiver, true)
await keeper.templateStoreManager.proposeTemplate(template.getAddress(), consumer.getId(), true)
// TODO: Use a event to detect template mined
await new Promise(_ => setTimeout(_, 6 * 1000))
})
it("should approve the template", async () => {
await keeper.templateStoreManager.approveTemplate(template.getAddress(), templateManagerOwner, true)
await keeper.templateStoreManager.approveTemplate(template.getAddress(), templateManagerOwner.getId(), true)
// TODO: Use a event to detect template mined
await new Promise(_ => setTimeout(_, 6 * 1000))
})
it("should register a DID", async () => {
await keeper.didRegistry.registerAttribute(did.replace("0x", ""), checksum, url, publisher.getId())
})
it("should generate the condition IDs", async () => {
conditionIdAccess = await accessSecretStoreCondition.generateId(agreementId, await accessSecretStoreCondition.hashValues(did, receiver))
conditionIdAccess = await accessSecretStoreCondition.generateIdHash(agreementId, did, consumer.getId())
conditionIdLock = await lockRewardCondition.generateIdHash(agreementId, await escrowReward.getAddress(), escrowAmount)
conditionIdEscrow = await escrowReward.generateId(agreementId, await escrowReward.hashValues(escrowAmount, receiver, sender, conditionIdLock, conditionIdAccess))
conditionIdEscrow = await escrowReward.generateIdHash(
agreementId,
escrowAmount,
consumer.getId(),
publisher.getId(),
conditionIdLock,
conditionIdAccess,
)
})
it("should have conditions types", async () => {
@ -93,17 +104,51 @@ describe("Register Escrow Access Secret Store Template", () => {
})
it("should create a new agreement", async () => {
await keeper.didRegistry.registerAttribute(did.replace("0x", ""), checksum, url, sender)
const agreement = await template.createAgreement(
agreementId,
did,
[conditionIdAccess, conditionIdLock, conditionIdEscrow],
[0, 0, 0],
[0, 0, 0],
receiver,
consumer.getId(),
)
assert.isTrue(agreement.status)
})
it("should fulfill LockRewardCondition", async () => {
await consumer.requestTokens(escrowAmount)
await keeper.token.approve(lockRewardCondition.getAddress(), escrowAmount, consumer.getId())
const fulfill = await lockRewardCondition.fulfill(agreementId, escrowReward.getAddress(), escrowAmount, consumer.getId())
assert.isDefined(fulfill.events.Fulfilled, "Not Fulfilled event.")
})
it("should fulfill AccessSecretStoreCondition", async () => {
const fulfill = await accessSecretStoreCondition.fulfill(agreementId, did, consumer.getId(), publisher.getId())
assert.isDefined(fulfill.events.Fulfilled, "Not Fulfilled event.")
})
it("should fulfill EscrowReward", async () => {
const fulfill = await escrowReward.fulfill(
agreementId,
escrowAmount,
consumer.getId(),
publisher.getId(),
conditionIdLock,
conditionIdAccess,
consumer.getId(),
)
assert.isDefined(fulfill.events.Fulfilled, "Not Fulfilled event.")
})
it("should grant the access to the consumer", async () => {
const accessGranted = await accessSecretStoreCondition.checkPermissions(consumer.getId(), did)
assert.isTrue(accessGranted, "Consumer has not been granted.")
})
})

View File

@ -3,7 +3,7 @@ import { Condition } from "./Condition.abstract"
export class AccessSecretStoreCondition extends Condition {
public static async getInstance(): Promise<AccessSecretStoreCondition> {
return Condition.getInstance("AccessSecretStoreCondition", AccessSecretStoreCondition)
return Condition.getInstance("AccessSecretStoreCondition", AccessSecretStoreCondition) as any
}
hashValues(did: string, grantee: string) {
@ -13,4 +13,8 @@ export class AccessSecretStoreCondition extends Condition {
fulfill(agreementId: string, did: string, grantee: string, from?: string) {
return super.fulfill(agreementId, [did, grantee], from)
}
checkPermissions(grantee: string, did: string, from?: string) {
return this.call<boolean>("checkPermissions", [grantee, did], from)
}
}