mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
use timeouts from template
This commit is contained in:
parent
7a3cc1e473
commit
e1b4293125
@ -1,6 +1,8 @@
|
|||||||
|
import Parameter from "./Parameter"
|
||||||
|
|
||||||
export default class Condition {
|
export default class Condition {
|
||||||
public name: string = "lockPayment"
|
public name: string = "lockPayment"
|
||||||
public timeout: number = 0
|
public timeout: number = 0
|
||||||
public conditionKey: string
|
public conditionKey: string
|
||||||
public parameters: any
|
public parameters: Parameter[]
|
||||||
}
|
}
|
||||||
|
5
src/ddo/Parameter.ts
Normal file
5
src/ddo/Parameter.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export default class Parameter {
|
||||||
|
public name: string
|
||||||
|
public type: string
|
||||||
|
public value: any
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import DDO from "../ddo/DDO"
|
import DDO from "../ddo/DDO"
|
||||||
import ServiceAgreement from "../ocean/ServiceAgreement"
|
import ServiceAgreement from "../ocean/ServiceAgreements/ServiceAgreement"
|
||||||
import {Account, Asset, Logger, Ocean} from "../squid"
|
import {Account, Asset, Logger, Ocean} from "../squid"
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import DDO from "../ddo/DDO"
|
import DDO from "../ddo/DDO"
|
||||||
import ServiceAgreement from "../ocean/ServiceAgreement"
|
import ServiceAgreement from "../ocean/ServiceAgreements/ServiceAgreement"
|
||||||
import {Account, Asset, Logger, Ocean} from "../squid"
|
import {Account, Asset, Logger, Ocean} from "../squid"
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
@ -12,6 +12,7 @@ export default class ContractReflector {
|
|||||||
methodName: parts[1],
|
methodName: parts[1],
|
||||||
address: contract.getAddress(),
|
address: contract.getAddress(),
|
||||||
signature: contract.getSignatureOfMethod(parts[1]),
|
signature: contract.getSignatureOfMethod(parts[1]),
|
||||||
|
inputs: contract.getInputsOfMethod(parts[1]),
|
||||||
} as MethodReflection
|
} as MethodReflection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,20 +43,15 @@ export default abstract class ContractBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getSignatureOfMethod(methodName: string): string {
|
public getSignatureOfMethod(methodName: string): string {
|
||||||
|
const foundMethod = this.searchMethod(methodName)
|
||||||
const foundMethod = this.contract.options.jsonInterface.find((method) => {
|
|
||||||
if (method.name === methodName) {
|
|
||||||
return method
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!foundMethod) {
|
|
||||||
throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return foundMethod.signature
|
return foundMethod.signature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getInputsOfMethod(methodName: string): any[] {
|
||||||
|
const foundMethod = this.searchMethod(methodName)
|
||||||
|
return foundMethod.inputs
|
||||||
|
}
|
||||||
|
|
||||||
protected async init() {
|
protected async init() {
|
||||||
this.contract = await ContractHandler.get(this.contractName)
|
this.contract = await ContractHandler.get(this.contractName)
|
||||||
}
|
}
|
||||||
@ -95,4 +90,16 @@ export default abstract class ContractBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private searchMethod(methodName): any {
|
||||||
|
const foundMethod = this.contract.options.jsonInterface.find((method) => {
|
||||||
|
if (method.name === methodName) {
|
||||||
|
return method
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (!foundMethod) {
|
||||||
|
throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`)
|
||||||
|
}
|
||||||
|
return foundMethod
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,10 @@ export default class AccessConditions extends ContractBase {
|
|||||||
return accessConditions
|
return accessConditions
|
||||||
}
|
}
|
||||||
|
|
||||||
public async grantAccess(serviceDefinitionId: any, assetId: any, documentKeyId: any, publisherAddress: string)
|
public async grantAccess(serviceAgreementId: any, assetId: any, documentKeyId: any, publisherAddress: string)
|
||||||
: Promise<Receipt> {
|
: Promise<Receipt> {
|
||||||
return this.send("grantAccess", publisherAddress, [
|
return this.send("grantAccess", publisherAddress, [
|
||||||
serviceDefinitionId, "0x" + assetId, "0x" + documentKeyId,
|
serviceAgreementId, "0x" + assetId, "0x" + documentKeyId,
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,4 +3,5 @@ export default class MethodReflection {
|
|||||||
public methodName: string
|
public methodName: string
|
||||||
public address: string
|
public address: string
|
||||||
public signature: string
|
public signature: string
|
||||||
|
public inputs: any[]
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import AquariusProvider from "../aquarius/AquariusProvider"
|
|||||||
import Account from "./Account"
|
import Account from "./Account"
|
||||||
import IdGenerator from "./IdGenerator"
|
import IdGenerator from "./IdGenerator"
|
||||||
import OceanBase from "./OceanBase"
|
import OceanBase from "./OceanBase"
|
||||||
import ServiceAgreement from "./ServiceAgreement"
|
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
||||||
|
|
||||||
export default class Asset extends OceanBase {
|
export default class Asset extends OceanBase {
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ export default class Asset extends OceanBase {
|
|||||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||||
|
|
||||||
const serviceAgreementId: string = IdGenerator.generateId()
|
const serviceAgreementId: string = IdGenerator.generateId()
|
||||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.createServiceAgreement(this.getId(),
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(this.getId(),
|
||||||
ddo, serviceAgreementId, consumer, this.publisher)
|
ddo, serviceAgreementId, consumer, this.publisher)
|
||||||
|
|
||||||
return serviceAgreement
|
return serviceAgreement
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
import AquariusProvider from "../aquarius/AquariusProvider"
|
import AquariusProvider from "../aquarius/AquariusProvider"
|
||||||
import SearchQuery from "../aquarius/query/SearchQuery"
|
import SearchQuery from "../aquarius/query/SearchQuery"
|
||||||
import ConfigProvider from "../ConfigProvider"
|
import ConfigProvider from "../ConfigProvider"
|
||||||
import Condition from "../ddo/Condition"
|
import DDOCondition from "../ddo/Condition"
|
||||||
import DDO from "../ddo/DDO"
|
import DDO from "../ddo/DDO"
|
||||||
|
import Parameter from "../ddo/Parameter"
|
||||||
import Service from "../ddo/Service"
|
import Service from "../ddo/Service"
|
||||||
import Keeper from "../keeper/Keeper"
|
import Keeper from "../keeper/Keeper"
|
||||||
import Web3Provider from "../keeper/Web3Provider"
|
import Web3Provider from "../keeper/Web3Provider"
|
||||||
import Config from "../models/Config"
|
import Config from "../models/Config"
|
||||||
|
import ValuePair from "../models/ValuePair"
|
||||||
import Account from "./Account"
|
import Account from "./Account"
|
||||||
import Asset from "./Asset"
|
import Asset from "./Asset"
|
||||||
import IdGenerator from "./IdGenerator"
|
import IdGenerator from "./IdGenerator"
|
||||||
import ServiceAgreementTemplate from "./ServiceAgreementTemplate"
|
import Condition from "./ServiceAgreements/Condition"
|
||||||
|
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
|
||||||
|
import DefaultTemplate from "./ServiceAgreements/Templates/Default"
|
||||||
|
|
||||||
export default class Ocean {
|
export default class Ocean {
|
||||||
|
|
||||||
@ -46,35 +50,28 @@ export default class Ocean {
|
|||||||
const assetId: string = IdGenerator.generateId()
|
const assetId: string = IdGenerator.generateId()
|
||||||
const did: string = `did:op:${assetId}`
|
const did: string = `did:op:${assetId}`
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
const serviceName = "Access"
|
const serviceName = "Access"
|
||||||
const saTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(serviceName, methods, dependencyMatrix,
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(serviceName, DefaultTemplate.methods,
|
||||||
asset.publisher)
|
asset.publisher)
|
||||||
|
|
||||||
// get condition keys from template
|
// get condition keys from template
|
||||||
const conditionKeys: string[] = saTemplate.getConditionKeys()
|
const conditions: Condition[] = serviceAgreementTemplate.getConditions()
|
||||||
|
|
||||||
// create ddo conditions out of the keys
|
// create ddo conditions out of the keys
|
||||||
const conditions: Condition[] = conditionKeys.map((conditionKey, i): Condition => {
|
const ddoConditions: DDOCondition[] = conditions.map((condition: Condition): DDOCondition => {
|
||||||
return {
|
return {
|
||||||
name: methods[i].split(".")[1],
|
name: condition.methodReflection.methodName,
|
||||||
timeout: 100,
|
timeout: 100,
|
||||||
conditionKey: conditionKey,
|
conditionKey: condition.condtionKey,
|
||||||
parameters: {
|
parameters: condition.methodReflection.inputs.map((input: ValuePair) => {
|
||||||
// todo wtf?
|
return {
|
||||||
assetId: "bytes32",
|
...input,
|
||||||
price: "integer"
|
value: "xxx",
|
||||||
},
|
} as Parameter
|
||||||
} as Condition
|
}),
|
||||||
|
|
||||||
|
} as DDOCondition
|
||||||
})
|
})
|
||||||
|
|
||||||
// create ddo itself
|
// create ddo itself
|
||||||
@ -89,8 +86,8 @@ export default class Ocean {
|
|||||||
// the id of the service agreement?
|
// the id of the service agreement?
|
||||||
serviceDefinitionId: "0x" + IdGenerator.generateId(),
|
serviceDefinitionId: "0x" + IdGenerator.generateId(),
|
||||||
// the id of the service agreement template
|
// the id of the service agreement template
|
||||||
templateId: saTemplate.getId(),
|
templateId: serviceAgreementTemplate.getId(),
|
||||||
conditions,
|
conditions: ddoConditions,
|
||||||
} as Service,
|
} as Service,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
@ -1,70 +0,0 @@
|
|||||||
import ContractReflector from "../keeper/ContractReflector"
|
|
||||||
import ServiceAgreement from "../keeper/contracts/ServiceAgreement"
|
|
||||||
import Web3Provider from "../keeper/Web3Provider"
|
|
||||||
import MethodReflection from "../models/MethodReflection"
|
|
||||||
import Account from "./Account"
|
|
||||||
import OceanBase from "./OceanBase"
|
|
||||||
|
|
||||||
export default class ServiceAgreementTemplate extends OceanBase {
|
|
||||||
|
|
||||||
public static async registerServiceAgreementsTemplate(serviceName: string, methods: string[],
|
|
||||||
dependencyMatrix: number[], templateOwner: Account):
|
|
||||||
Promise<ServiceAgreementTemplate> {
|
|
||||||
|
|
||||||
const methodReflections: MethodReflection[] =
|
|
||||||
await Promise.all(methods.map(async (method) => {
|
|
||||||
const methodReflection = await
|
|
||||||
ContractReflector.reflectContractMethod(method)
|
|
||||||
return methodReflection
|
|
||||||
}))
|
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
|
||||||
|
|
||||||
const receipt = await serviceAgreement.setupAgreementTemplate(
|
|
||||||
methodReflections, dependencyMatrix,
|
|
||||||
Web3Provider.getWeb3().utils.fromAscii(serviceName),
|
|
||||||
templateOwner.getId())
|
|
||||||
|
|
||||||
const serviceAgreementTemplateId =
|
|
||||||
receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId
|
|
||||||
|
|
||||||
return new ServiceAgreementTemplate(
|
|
||||||
serviceAgreementTemplateId,
|
|
||||||
ServiceAgreementTemplate.generateConditionsKeys(serviceAgreementTemplateId, methodReflections),
|
|
||||||
templateOwner)
|
|
||||||
}
|
|
||||||
|
|
||||||
private static generateConditionsKeys(serviceAgreementTemplateId: string, methodReflections: MethodReflection[]):
|
|
||||||
string[] {
|
|
||||||
const conditions = []
|
|
||||||
for (const methodReflection of methodReflections) {
|
|
||||||
const values = [
|
|
||||||
{type: "bytes32", value: serviceAgreementTemplateId},
|
|
||||||
{type: "address", value: methodReflection.address},
|
|
||||||
{type: "bytes4", value: methodReflection.signature},
|
|
||||||
]
|
|
||||||
conditions.push(Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex"))
|
|
||||||
}
|
|
||||||
return conditions
|
|
||||||
}
|
|
||||||
|
|
||||||
private constructor(serviceAgreementTemplateId, private conditionKeys: string[], private owner: Account) {
|
|
||||||
super(serviceAgreementTemplateId)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets the status of a service agreement template
|
|
||||||
*/
|
|
||||||
public async getStatus(): Promise<boolean> {
|
|
||||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
|
||||||
return serviceAgreement.getTemplateStatus(this.getId())
|
|
||||||
}
|
|
||||||
|
|
||||||
public getOwner(): Account {
|
|
||||||
return this.owner
|
|
||||||
}
|
|
||||||
|
|
||||||
public getConditionKeys(): string[] {
|
|
||||||
return this.conditionKeys
|
|
||||||
}
|
|
||||||
}
|
|
6
src/ocean/ServiceAgreements/Condition.ts
Normal file
6
src/ocean/ServiceAgreements/Condition.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import MethodReflection from "../../models/MethodReflection"
|
||||||
|
|
||||||
|
export default class Condition {
|
||||||
|
public methodReflection: MethodReflection
|
||||||
|
public condtionKey
|
||||||
|
}
|
5
src/ocean/ServiceAgreements/Method.ts
Normal file
5
src/ocean/ServiceAgreements/Method.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export default class Method {
|
||||||
|
public path: string
|
||||||
|
public dependency: number
|
||||||
|
public timeout: number
|
||||||
|
}
|
@ -1,40 +1,38 @@
|
|||||||
import DDO from "../ddo/DDO"
|
import DDO from "../../ddo/DDO"
|
||||||
import AccessConditions from "../keeper/contracts/conditions/AccessConditions"
|
import AccessConditions from "../../keeper/contracts/conditions/AccessConditions"
|
||||||
import ServiceAgreementContract from "../keeper/contracts/ServiceAgreement"
|
import ServiceAgreementContract from "../../keeper/contracts/ServiceAgreement"
|
||||||
import Web3Provider from "../keeper/Web3Provider"
|
import Web3Provider from "../../keeper/Web3Provider"
|
||||||
import ValuePair from "../models/ValuePair"
|
import ValuePair from "../../models/ValuePair"
|
||||||
import Account from "./Account"
|
import Account from "../Account"
|
||||||
import OceanBase from "./OceanBase"
|
import OceanBase from "../OceanBase"
|
||||||
|
|
||||||
export default class ServiceAgreement extends OceanBase {
|
export default class ServiceAgreement extends OceanBase {
|
||||||
|
|
||||||
public static async createServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account,
|
public static async signServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account,
|
||||||
publisher: Account):
|
publisher: Account):
|
||||||
Promise<ServiceAgreement> {
|
Promise<ServiceAgreement> {
|
||||||
|
|
||||||
|
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
|
||||||
|
const valueHashes = ServiceAgreement.createValueHashes(values)
|
||||||
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
|
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
|
||||||
|
|
||||||
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId,
|
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId,
|
||||||
consumer)
|
values, valueHashes, timeoutValues, consumer)
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(ddo,
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo,
|
||||||
serviceAgreementId, timeoutValues, serviceAgreementHashSignature, consumer, publisher)
|
serviceAgreementId, values, valueHashes, timeoutValues, serviceAgreementHashSignature, consumer, publisher)
|
||||||
|
|
||||||
return serviceAgreement
|
return serviceAgreement
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, consumer: Account):
|
public static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, values: ValuePair[],
|
||||||
|
valueHashes: string[], timeoutValues: number[], consumer: Account):
|
||||||
Promise<string> {
|
Promise<string> {
|
||||||
|
|
||||||
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
|
|
||||||
const valueHashes = ServiceAgreement.createValueHashes(values)
|
|
||||||
|
|
||||||
const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => {
|
const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => {
|
||||||
return condition.conditionKey
|
return condition.conditionKey
|
||||||
})
|
})
|
||||||
|
|
||||||
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
|
|
||||||
|
|
||||||
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId,
|
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId,
|
||||||
serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
|
serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
|
||||||
|
|
||||||
@ -44,12 +42,11 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
return serviceAgreementHashSignature
|
return serviceAgreementHashSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async signServiceAgreement(ddo: DDO, serviceAgreementId: string, timeoutValues: number[],
|
private static async executeAgreement(ddo: DDO, serviceAgreementId: string, values: ValuePair[],
|
||||||
serviceAgreementHashSignature: string, consumer: Account,
|
valueHashes: string[], timeoutValues: number[],
|
||||||
publisher: Account): Promise<ServiceAgreement> {
|
serviceAgreementHashSignature: string, consumer: Account,
|
||||||
|
publisher: Account): Promise<ServiceAgreement> {
|
||||||
|
|
||||||
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
|
|
||||||
const valueHashes = ServiceAgreement.createValueHashes(values)
|
|
||||||
const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance()
|
const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance()
|
||||||
const executeAgreementReceipt = await serviceAgreement.executeAgreement(
|
const executeAgreementReceipt = await serviceAgreement.executeAgreement(
|
||||||
ddo.service[0].templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes,
|
ddo.service[0].templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes,
|
||||||
@ -80,7 +77,8 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static hashServiceAgreement(serviceAgreementTemplateId: string, serviceAgreementId: string,
|
private static hashServiceAgreement(serviceAgreementTemplateId: string, serviceAgreementId: string,
|
||||||
conditionKeys: string[], valueHashes: string[], timeouts: number[]) {
|
conditionKeys: string[], valueHashes: string[], timeouts: number[])
|
||||||
|
: string {
|
||||||
const args = [
|
const args = [
|
||||||
{type: "bytes32", value: serviceAgreementTemplateId} as ValuePair,
|
{type: "bytes32", value: serviceAgreementTemplateId} as ValuePair,
|
||||||
{type: "bytes32[]", value: conditionKeys} as ValuePair,
|
{type: "bytes32[]", value: conditionKeys} as ValuePair,
|
||||||
@ -104,6 +102,7 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
const values: ValuePair[] = [
|
const values: ValuePair[] = [
|
||||||
{type: "bool", value: true} as ValuePair,
|
{type: "bool", value: true} as ValuePair,
|
||||||
{type: "bool", value: false} as ValuePair,
|
{type: "bool", value: false} as ValuePair,
|
||||||
|
{type: "bool", value: false} as ValuePair,
|
||||||
{type: "uint", value: 120} as ValuePair,
|
{type: "uint", value: 120} as ValuePair,
|
||||||
{type: "string", value: serviceAgreementId} as ValuePair,
|
{type: "string", value: serviceAgreementId} as ValuePair,
|
||||||
]
|
]
|
86
src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts
Normal file
86
src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
import ContractReflector from "../../keeper/ContractReflector"
|
||||||
|
import ServiceAgreement from "../../keeper/contracts/ServiceAgreement"
|
||||||
|
import Web3Provider from "../../keeper/Web3Provider"
|
||||||
|
import MethodReflection from "../../models/MethodReflection"
|
||||||
|
import ValuePair from "../../models/ValuePair"
|
||||||
|
import Logger from "../../utils/Logger"
|
||||||
|
import Account from "../Account"
|
||||||
|
import OceanBase from "../OceanBase"
|
||||||
|
import Condition from "./Condition"
|
||||||
|
import Method from "./Method"
|
||||||
|
|
||||||
|
export default class ServiceAgreementTemplate extends OceanBase {
|
||||||
|
|
||||||
|
public static async registerServiceAgreementsTemplate(serviceName: string, methods: Method[],
|
||||||
|
templateOwner: Account)
|
||||||
|
: Promise<ServiceAgreementTemplate> {
|
||||||
|
|
||||||
|
const methodReflections: MethodReflection[] =
|
||||||
|
await Promise.all(methods.map(async (method: Method) => {
|
||||||
|
const methodReflection = await
|
||||||
|
ContractReflector.reflectContractMethod(method.path)
|
||||||
|
return methodReflection
|
||||||
|
}))
|
||||||
|
|
||||||
|
const dependencyMatrix: number[] =
|
||||||
|
await Promise.all(methods.map(async (method: Method) => {
|
||||||
|
// tslint:disable
|
||||||
|
return method.dependency | method.timeout
|
||||||
|
}))
|
||||||
|
|
||||||
|
Logger.log(dependencyMatrix)
|
||||||
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
||||||
|
|
||||||
|
const receipt = await serviceAgreement.setupAgreementTemplate(
|
||||||
|
methodReflections, dependencyMatrix,
|
||||||
|
Web3Provider.getWeb3().utils.fromAscii(serviceName),
|
||||||
|
templateOwner.getId())
|
||||||
|
|
||||||
|
const serviceAgreementTemplateId =
|
||||||
|
receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId
|
||||||
|
|
||||||
|
const conditions: Condition[] = methodReflections.map((methodReflection) => {
|
||||||
|
return {
|
||||||
|
methodReflection,
|
||||||
|
condtionKey: ServiceAgreementTemplate.generateConditionsKey(serviceAgreementTemplateId,
|
||||||
|
methodReflection),
|
||||||
|
} as Condition
|
||||||
|
})
|
||||||
|
|
||||||
|
return new ServiceAgreementTemplate(
|
||||||
|
serviceAgreementTemplateId,
|
||||||
|
conditions,
|
||||||
|
templateOwner)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static generateConditionsKey(serviceAgreementTemplateId: string, methodReflection: MethodReflection)
|
||||||
|
: string {
|
||||||
|
const values = [
|
||||||
|
{type: "bytes32", value: serviceAgreementTemplateId} as ValuePair,
|
||||||
|
{type: "address", value: methodReflection.address} as ValuePair,
|
||||||
|
{type: "bytes4", value: methodReflection.signature} as ValuePair,
|
||||||
|
]
|
||||||
|
return Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex")
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructor(serviceAgreementTemplateId, private conditions: Condition[],
|
||||||
|
private owner: Account) {
|
||||||
|
super(serviceAgreementTemplateId)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the status of a service agreement template
|
||||||
|
*/
|
||||||
|
public async getStatus(): Promise<boolean> {
|
||||||
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
||||||
|
return serviceAgreement.getTemplateStatus(this.getId())
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwner(): Account {
|
||||||
|
return this.owner
|
||||||
|
}
|
||||||
|
|
||||||
|
public getConditions(): Condition[] {
|
||||||
|
return this.conditions
|
||||||
|
}
|
||||||
|
}
|
26
src/ocean/ServiceAgreements/Templates/Default.ts
Normal file
26
src/ocean/ServiceAgreements/Templates/Default.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import Method from "../Method"
|
||||||
|
|
||||||
|
const methods: Method[] = [
|
||||||
|
{
|
||||||
|
path: "PaymentConditions.lockPayment",
|
||||||
|
dependency: 0,
|
||||||
|
timeout: 10,
|
||||||
|
} as Method,
|
||||||
|
{
|
||||||
|
path: "AccessConditions.grantAccess",
|
||||||
|
dependency: 1,
|
||||||
|
timeout: 500,
|
||||||
|
} as Method,
|
||||||
|
{
|
||||||
|
path: "PaymentConditions.releasePayment",
|
||||||
|
dependency: 4,
|
||||||
|
timeout: 17,
|
||||||
|
} as Method,
|
||||||
|
{
|
||||||
|
path: "PaymentConditions.refundPayment",
|
||||||
|
dependency: 1,
|
||||||
|
timeout: 40,
|
||||||
|
} as Method,
|
||||||
|
]
|
||||||
|
|
||||||
|
export default {methods}
|
@ -7,7 +7,7 @@ import ContractHandler from "../../src/keeper/ContractHandler"
|
|||||||
import Account from "../../src/ocean/Account"
|
import Account from "../../src/ocean/Account"
|
||||||
import Asset from "../../src/ocean/Asset"
|
import Asset from "../../src/ocean/Asset"
|
||||||
import Ocean from "../../src/ocean/Ocean"
|
import Ocean from "../../src/ocean/Ocean"
|
||||||
import ServiceAgreement from "../../src/ocean/ServiceAgreement"
|
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
||||||
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
|
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
|
||||||
import config from "../config"
|
import config from "../config"
|
||||||
import AquariusMock from "../mocks/Aquarius.mock"
|
import AquariusMock from "../mocks/Aquarius.mock"
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
import {assert} from "chai"
|
import {assert} from "chai"
|
||||||
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
|
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
|
||||||
import ConfigProvider from "../../src/ConfigProvider"
|
import ConfigProvider from "../../src/ConfigProvider"
|
||||||
import Condition from "../../src/ddo/Condition"
|
import DDOCondition from "../../src/ddo/Condition"
|
||||||
import DDO from "../../src/ddo/DDO"
|
import DDO from "../../src/ddo/DDO"
|
||||||
|
import Parameter from "../../src/ddo/Parameter"
|
||||||
import Service from "../../src/ddo/Service"
|
import Service from "../../src/ddo/Service"
|
||||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||||
import Account from "../../src/ocean/Account"
|
import Account from "../../src/ocean/Account"
|
||||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||||
import Ocean from "../../src/ocean/Ocean"
|
import Ocean from "../../src/ocean/Ocean"
|
||||||
import ServiceAgreement from "../../src/ocean/ServiceAgreement"
|
import Condition from "../../src/ocean/ServiceAgreements/Condition"
|
||||||
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate"
|
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
||||||
|
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
||||||
|
import DefaultTemplate from "../../src/ocean/ServiceAgreements/Templates/Default"
|
||||||
import config from "../config"
|
import config from "../config"
|
||||||
import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
|
import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
|
||||||
|
|
||||||
@ -35,43 +38,35 @@ describe("ServiceAgreement", () => {
|
|||||||
consumerAccount = accounts[2]
|
consumerAccount = accounts[2]
|
||||||
|
|
||||||
const resourceName = "superb car data"
|
const resourceName = "superb car data"
|
||||||
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
|
|
||||||
|
|
||||||
testServiceAgreementTemplate =
|
testServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods,
|
||||||
dependencyMatrix, templateOwnerAccount)
|
templateOwnerAccount)
|
||||||
|
|
||||||
// get condition keys from template
|
// get condition keys from template
|
||||||
const conditionKeys: string[] = testServiceAgreementTemplate.getConditionKeys()
|
const conditions: Condition[] = testServiceAgreementTemplate.getConditions()
|
||||||
|
|
||||||
// create ddo conditions out of the keys
|
// create ddo conditions out of the keys
|
||||||
const conditions: Condition[] = conditionKeys.map((conditionKey, i): Condition => {
|
const ddoConditions: DDOCondition[] = conditions.map((condition): DDOCondition => {
|
||||||
return {
|
return {
|
||||||
name: methods[i].split(".")[1],
|
name: condition.methodReflection.methodName,
|
||||||
timeout: 100,
|
timeout: 100,
|
||||||
conditionKey: conditionKey,
|
conditionKey: condition.condtionKey,
|
||||||
parameters: {
|
parameters: condition.methodReflection.inputs.map((input) => {
|
||||||
// todo wtf?
|
return {
|
||||||
assetId: "bytes32",
|
...input,
|
||||||
price: "integer"
|
value: "xx",
|
||||||
},
|
}as Parameter
|
||||||
} as Condition
|
}),
|
||||||
|
} as DDOCondition
|
||||||
})
|
})
|
||||||
|
|
||||||
serviceDefintion = [
|
serviceDefintion = [
|
||||||
{
|
{
|
||||||
serviceDefinitionId: IdGenerator.generateId(),
|
serviceDefinitionId: IdGenerator.generateId(),
|
||||||
templateId: testServiceAgreementTemplate.getId(),
|
templateId: testServiceAgreementTemplate.getId(),
|
||||||
conditions,
|
conditions: ddoConditions,
|
||||||
} as Service
|
} as Service,
|
||||||
]
|
]
|
||||||
|
|
||||||
})
|
})
|
||||||
@ -88,7 +83,7 @@ describe("ServiceAgreement", () => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
||||||
publisherAccount)
|
publisherAccount)
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
|
|
||||||
@ -110,7 +105,7 @@ describe("ServiceAgreement", () => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
||||||
publisherAccount)
|
publisherAccount)
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
|
|
||||||
@ -131,7 +126,7 @@ describe("ServiceAgreement", () => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
||||||
const serviceAgreement: ServiceAgreement =
|
const serviceAgreement: ServiceAgreement =
|
||||||
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
|
||||||
publisherAccount)
|
publisherAccount)
|
||||||
assert(serviceAgreement)
|
assert(serviceAgreement)
|
||||||
|
|
||||||
|
@ -3,19 +3,12 @@ import ConfigProvider from "../../src/ConfigProvider"
|
|||||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||||
import Account from "../../src/ocean/Account"
|
import Account from "../../src/ocean/Account"
|
||||||
import Ocean from "../../src/ocean/Ocean"
|
import Ocean from "../../src/ocean/Ocean"
|
||||||
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate"
|
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
||||||
|
import DefaultTemplate from "../../src/ocean/ServiceAgreements/Templates/Default"
|
||||||
import config from "../config"
|
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", () => {
|
||||||
|
|
||||||
@ -32,8 +25,8 @@ describe("ServiceAgreementTemplate", () => {
|
|||||||
const templateOwner = accounts[0]
|
const templateOwner = accounts[0]
|
||||||
const resourceName = "consume"
|
const resourceName = "consume"
|
||||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods,
|
||||||
dependencyMatrix, templateOwner)
|
templateOwner)
|
||||||
|
|
||||||
assert(serviceAgreementTemplate)
|
assert(serviceAgreementTemplate)
|
||||||
assert(serviceAgreementTemplate.getId())
|
assert(serviceAgreementTemplate.getId())
|
||||||
@ -48,8 +41,8 @@ describe("ServiceAgreementTemplate", () => {
|
|||||||
const resourceName = "consume"
|
const resourceName = "consume"
|
||||||
|
|
||||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
|
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods,
|
||||||
dependencyMatrix, publisherAccount)
|
publisherAccount)
|
||||||
assert(serviceAgreementTemplate)
|
assert(serviceAgreementTemplate)
|
||||||
|
|
||||||
const templateStatus = await serviceAgreementTemplate.getStatus()
|
const templateStatus = await serviceAgreementTemplate.getStatus()
|
||||||
|
Loading…
Reference in New Issue
Block a user