mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
make templates and service agreements configurable
This commit is contained in:
parent
cf86f5af0d
commit
076c2eb788
@ -8,26 +8,35 @@ import ServiceAgreementTemplate from "./ServiceAgreementTemplate"
|
|||||||
|
|
||||||
export default class ServiceAgreement extends OceanBase {
|
export default class ServiceAgreement extends OceanBase {
|
||||||
|
|
||||||
public static async signServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate, publisher: Account,
|
public static async createServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate, assetId: string,
|
||||||
did: string, assetId: string, consumer: Account):
|
did: string, consumer: Account, publisher: Account) {
|
||||||
Promise<ServiceAgreement> {
|
|
||||||
|
|
||||||
|
// todo: this should come from ddo
|
||||||
const serviceAgreementId = IdGenerator.generateId()
|
const serviceAgreementId = IdGenerator.generateId()
|
||||||
|
const timeoutValues = [0, 0, 0, 500] // timeout 500 blocks @ condition 4
|
||||||
const valueHashes = [
|
const values = [
|
||||||
ServiceAgreement.hashSingleValue("bool", true),
|
{type: "bool", value: true},
|
||||||
ServiceAgreement.hashSingleValue("bool", false),
|
{type: "bool", value: false},
|
||||||
ServiceAgreement.hashSingleValue("uint", 120),
|
{type: "uint", value: 120},
|
||||||
// assetId
|
{type: "string", value: assetId},
|
||||||
ServiceAgreement.hashSingleValue("string", assetId),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
const timeoutValues = [0, 0, 0, 500] // timeout 500 blocks @ condition 4
|
const saHashSig = await ServiceAgreement.createSAHashSignature(serviceAgreementTemplate, serviceAgreementId,
|
||||||
|
values, timeoutValues, consumer)
|
||||||
|
|
||||||
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(serviceAgreementTemplate, valueHashes,
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(serviceAgreementTemplate,
|
||||||
timeoutValues, serviceAgreementId)
|
serviceAgreementId, assetId, did, values, timeoutValues, saHashSig, consumer, publisher)
|
||||||
const serviceAgreementHashSignature =
|
|
||||||
await Web3Provider.getWeb3().eth.sign(serviceAgreementHash, consumer.getId())
|
return serviceAgreement
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async signServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate,
|
||||||
|
serviceAgreementId: string, assetId: string, did: string, values: any[],
|
||||||
|
timeoutValues: number[], serviceAgreementHashSignature: string,
|
||||||
|
consumer: Account, publisher: Account):
|
||||||
|
Promise<ServiceAgreement> {
|
||||||
|
|
||||||
|
const valueHashes = ServiceAgreement.createValueHashes(values)
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance()
|
const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance()
|
||||||
|
|
||||||
@ -49,11 +58,31 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static hashSingleValue(type: string, value: any): string {
|
private static createValueHashes(values: any[]): any[] {
|
||||||
|
return values.map((value) => {
|
||||||
|
return ServiceAgreement.hashSingleValue(value.type, value.value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private static hashSingleValue(type: string, value: any): string {
|
||||||
const args = {type, value}
|
const args = {type, value}
|
||||||
return Web3Provider.getWeb3().utils.soliditySha3(args).toString("hex")
|
return Web3Provider.getWeb3().utils.soliditySha3(args).toString("hex")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async createSAHashSignature(serviceAgreementTemplate: ServiceAgreementTemplate,
|
||||||
|
serviceAgreementId: string, values: any[], timeoutValues: number[],
|
||||||
|
consumer: Account): Promise<string> {
|
||||||
|
|
||||||
|
const valueHashes = ServiceAgreement.createValueHashes(values)
|
||||||
|
|
||||||
|
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(serviceAgreementTemplate, valueHashes,
|
||||||
|
timeoutValues, serviceAgreementId)
|
||||||
|
const serviceAgreementHashSignature =
|
||||||
|
await Web3Provider.getWeb3().eth.sign(serviceAgreementHash, consumer.getId())
|
||||||
|
|
||||||
|
return serviceAgreementHashSignature
|
||||||
|
}
|
||||||
|
|
||||||
private static hashServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate, valueHashes: string[],
|
private static hashServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate, valueHashes: string[],
|
||||||
timeouts: number[], serviceAgreementId: string) {
|
timeouts: number[], serviceAgreementId: string) {
|
||||||
const args = [
|
const args = [
|
||||||
|
@ -7,18 +7,16 @@ import OceanBase from "./OceanBase"
|
|||||||
|
|
||||||
export default class ServiceAgreementTemplate extends OceanBase {
|
export default class ServiceAgreementTemplate extends OceanBase {
|
||||||
|
|
||||||
public static async registerServiceAgreementsTemplate(serviceName: string, templateOwner: Account):
|
public static async registerServiceAgreementsTemplate(serviceName: string, methods: string[],
|
||||||
|
dependencyMatrix: number[], templateOwner: Account):
|
||||||
Promise<ServiceAgreementTemplate> {
|
Promise<ServiceAgreementTemplate> {
|
||||||
|
|
||||||
const methodReflections: MethodReflection[] = [
|
const methodReflections: MethodReflection[] =
|
||||||
await ContractReflector.reflectContractMethod("PaymentConditions.lockPayment"),
|
await Promise.all(methods.map(async (method) => {
|
||||||
await ContractReflector.reflectContractMethod("AccessConditions.grantAccess"),
|
const methodReflection = await
|
||||||
await ContractReflector.reflectContractMethod("PaymentConditions.releasePayment"),
|
ContractReflector.reflectContractMethod(method)
|
||||||
await ContractReflector.reflectContractMethod("PaymentConditions.refundPayment"),
|
return methodReflection
|
||||||
]
|
}))
|
||||||
|
|
||||||
// tslint:disable
|
|
||||||
const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit
|
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
||||||
|
|
||||||
|
@ -29,20 +29,29 @@ describe("ServiceAgreement", () => {
|
|||||||
consumerAccount = accounts[2]
|
consumerAccount = accounts[2]
|
||||||
|
|
||||||
const resourceName = "superb car data"
|
const resourceName = "superb car data"
|
||||||
testServiceAgreementTemplate =
|
const methods: string[] = [
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, templateOwnerAccount)
|
"PaymentConditions.lockPayment",
|
||||||
|
"AccessConditions.grantAccess",
|
||||||
|
"PaymentConditions.releasePayment",
|
||||||
|
"PaymentConditions.refundPayment",
|
||||||
|
]
|
||||||
|
// tslint:disable
|
||||||
|
const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit
|
||||||
|
|
||||||
|
testServiceAgreementTemplate =
|
||||||
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
||||||
|
dependencyMatrix, templateOwnerAccount)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("#executeServiceAgreement()", () => {
|
describe("#createServiceAgreement()", () => {
|
||||||
it("should execute an service agreement", async () => {
|
it("should execute an service agreement", async () => {
|
||||||
|
|
||||||
const did: string = IdGenerator.generateId()
|
const did: string = IdGenerator.generateId()
|
||||||
const assetId: string = IdGenerator.generateId()
|
const assetId: string = IdGenerator.generateId()
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.signServiceAgreement(testServiceAgreementTemplate, publisherAccount,
|
await ServiceAgreement.createServiceAgreement(testServiceAgreementTemplate, assetId, did,
|
||||||
did, assetId, consumerAccount)
|
consumerAccount, publisherAccount)
|
||||||
|
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
const id = serviceAgreement.getId()
|
const id = serviceAgreement.getId()
|
||||||
@ -58,8 +67,8 @@ describe("ServiceAgreement", () => {
|
|||||||
const assetId: string = IdGenerator.generateId()
|
const assetId: string = IdGenerator.generateId()
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.signServiceAgreement(testServiceAgreementTemplate, publisherAccount,
|
await ServiceAgreement.createServiceAgreement(testServiceAgreementTemplate, assetId, did,
|
||||||
did, assetId, consumerAccount)
|
consumerAccount, publisherAccount)
|
||||||
|
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
const status = await serviceAgreement.getStatus()
|
const status = await serviceAgreement.getStatus()
|
||||||
@ -73,13 +82,9 @@ describe("ServiceAgreement", () => {
|
|||||||
const did: string = IdGenerator.generateId()
|
const did: string = IdGenerator.generateId()
|
||||||
const assetId: string = IdGenerator.generateId()
|
const assetId: string = IdGenerator.generateId()
|
||||||
|
|
||||||
const resourceName = "nice service"
|
|
||||||
const serviceAgreementTemplate =
|
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, templateOwnerAccount)
|
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.signServiceAgreement(serviceAgreementTemplate, publisherAccount,
|
await ServiceAgreement.createServiceAgreement(testServiceAgreementTemplate, assetId, did,
|
||||||
did, assetId, consumerAccount)
|
consumerAccount, publisherAccount)
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
|
|
||||||
const fulfilled: boolean = await serviceAgreement.grantAccess(did, IdGenerator.generateId())
|
const fulfilled: boolean = await serviceAgreement.grantAccess(did, IdGenerator.generateId())
|
||||||
|
@ -8,6 +8,14 @@ import config from "../config"
|
|||||||
|
|
||||||
let ocean: Ocean
|
let ocean: Ocean
|
||||||
let accounts: Account[]
|
let accounts: Account[]
|
||||||
|
const methods: string[] = [
|
||||||
|
"PaymentConditions.lockPayment",
|
||||||
|
"AccessConditions.grantAccess",
|
||||||
|
"PaymentConditions.releasePayment",
|
||||||
|
"PaymentConditions.refundPayment",
|
||||||
|
]
|
||||||
|
// tslint:disable
|
||||||
|
const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit
|
||||||
|
|
||||||
describe("ServiceAgreementTemplate", () => {
|
describe("ServiceAgreementTemplate", () => {
|
||||||
|
|
||||||
@ -22,9 +30,11 @@ describe("ServiceAgreementTemplate", () => {
|
|||||||
it("should setup an agreement template", async () => {
|
it("should setup an agreement template", async () => {
|
||||||
|
|
||||||
const templateOwner = accounts[0]
|
const templateOwner = accounts[0]
|
||||||
const resourceName = "test data"
|
const resourceName = "consume"
|
||||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, templateOwner)
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
||||||
|
dependencyMatrix, templateOwner)
|
||||||
|
|
||||||
assert(serviceAgreementTemplate)
|
assert(serviceAgreementTemplate)
|
||||||
assert(serviceAgreementTemplate.getId())
|
assert(serviceAgreementTemplate.getId())
|
||||||
assert(serviceAgreementTemplate.getOwner().getId() === templateOwner.getId())
|
assert(serviceAgreementTemplate.getOwner().getId() === templateOwner.getId())
|
||||||
@ -35,9 +45,12 @@ describe("ServiceAgreementTemplate", () => {
|
|||||||
it("should get the status of a newly deployed agreement template", async () => {
|
it("should get the status of a newly deployed agreement template", async () => {
|
||||||
|
|
||||||
const publisherAccount = accounts[0]
|
const publisherAccount = accounts[0]
|
||||||
const resourceName = "template status"
|
const resourceName = "consume"
|
||||||
|
|
||||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, publisherAccount)
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
||||||
|
dependencyMatrix, publisherAccount)
|
||||||
|
assert(serviceAgreementTemplate)
|
||||||
|
|
||||||
const templateStatus = await serviceAgreementTemplate.getStatus()
|
const templateStatus = await serviceAgreementTemplate.getStatus()
|
||||||
assert(templateStatus === true)
|
assert(templateStatus === true)
|
||||||
|
Loading…
Reference in New Issue
Block a user