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:
parent
c9b383223b
commit
f7cd71ac33
@ -2,7 +2,7 @@ import { assert } from 'chai'
|
|||||||
|
|
||||||
import { config } from "../config"
|
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 { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } = conditions
|
||||||
const { EscrowAccessSecretStoreTemplate } = templates
|
const { EscrowAccessSecretStoreTemplate } = templates
|
||||||
@ -13,13 +13,13 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
|
|
||||||
const agreementId = `0x${generateId()}`
|
const agreementId = `0x${generateId()}`
|
||||||
const escrowAmount = 12
|
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 url = 'https://example.com/did/ocean/test-attr-example.txt'
|
||||||
const checksum = "b".repeat(32)
|
const checksum = "b".repeat(32)
|
||||||
|
|
||||||
let templateManagerOwner: string
|
let templateManagerOwner: Account
|
||||||
let sender: string
|
let publisher: Account
|
||||||
let receiver: string
|
let consumer: Account
|
||||||
|
|
||||||
let accessSecretStoreCondition: conditions.AccessSecretStoreCondition
|
let accessSecretStoreCondition: conditions.AccessSecretStoreCondition
|
||||||
let lockRewardCondition: conditions.LockRewardCondition
|
let lockRewardCondition: conditions.LockRewardCondition
|
||||||
@ -38,9 +38,9 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
template = await EscrowAccessSecretStoreTemplate.getInstance()
|
template = await EscrowAccessSecretStoreTemplate.getInstance()
|
||||||
|
|
||||||
// Accounts
|
// Accounts
|
||||||
templateManagerOwner = (await ocean.accounts.list())[0].getId()
|
templateManagerOwner = (await ocean.accounts.list())[0]
|
||||||
sender = (await ocean.accounts.list())[1].getId()
|
publisher = (await ocean.accounts.list())[1]
|
||||||
receiver = (await ocean.accounts.list())[2].getId()
|
consumer = (await ocean.accounts.list())[2]
|
||||||
|
|
||||||
// Conditions
|
// Conditions
|
||||||
accessSecretStoreCondition = await AccessSecretStoreCondition.getInstance()
|
accessSecretStoreCondition = await AccessSecretStoreCondition.getInstance()
|
||||||
@ -49,21 +49,32 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should propose the template", async () => {
|
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
|
// TODO: Use a event to detect template mined
|
||||||
await new Promise(_ => setTimeout(_, 6 * 1000))
|
await new Promise(_ => setTimeout(_, 6 * 1000))
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should approve the template", async () => {
|
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
|
// TODO: Use a event to detect template mined
|
||||||
await new Promise(_ => setTimeout(_, 6 * 1000))
|
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 () => {
|
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)
|
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 () => {
|
it("should have conditions types", async () => {
|
||||||
@ -93,17 +104,51 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should create a new agreement", async () => {
|
it("should create a new agreement", async () => {
|
||||||
await keeper.didRegistry.registerAttribute(did.replace("0x", ""), checksum, url, sender)
|
|
||||||
|
|
||||||
const agreement = await template.createAgreement(
|
const agreement = await template.createAgreement(
|
||||||
agreementId,
|
agreementId,
|
||||||
did,
|
did,
|
||||||
[conditionIdAccess, conditionIdLock, conditionIdEscrow],
|
[conditionIdAccess, conditionIdLock, conditionIdEscrow],
|
||||||
[0, 0, 0],
|
[0, 0, 0],
|
||||||
[0, 0, 0],
|
[0, 0, 0],
|
||||||
receiver,
|
consumer.getId(),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.isTrue(agreement.status)
|
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.")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -3,7 +3,7 @@ import { Condition } from "./Condition.abstract"
|
|||||||
export class AccessSecretStoreCondition extends Condition {
|
export class AccessSecretStoreCondition extends Condition {
|
||||||
|
|
||||||
public static async getInstance(): Promise<AccessSecretStoreCondition> {
|
public static async getInstance(): Promise<AccessSecretStoreCondition> {
|
||||||
return Condition.getInstance("AccessSecretStoreCondition", AccessSecretStoreCondition)
|
return Condition.getInstance("AccessSecretStoreCondition", AccessSecretStoreCondition) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
hashValues(did: string, grantee: string) {
|
hashValues(did: string, grantee: string) {
|
||||||
@ -13,4 +13,8 @@ export class AccessSecretStoreCondition extends Condition {
|
|||||||
fulfill(agreementId: string, did: string, grantee: string, from?: string) {
|
fulfill(agreementId: string, did: string, grantee: string, from?: string) {
|
||||||
return super.fulfill(agreementId, [did, grantee], from)
|
return super.fulfill(agreementId, [did, grantee], from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkPermissions(grantee: string, did: string, from?: string) {
|
||||||
|
return this.call<boolean>("checkPermissions", [grantee, did], from)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user