mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
some style changes for readability
This commit is contained in:
parent
c5b0c76fbf
commit
eef46c7a1c
@ -22,8 +22,12 @@ export default class Brizo {
|
|||||||
return `${this.url}/api/v1/brizo/services/compute?pubKey=${pubKey}&serviceId=${serviceId}&algo=${algo}&container=${container}"`
|
return `${this.url}/api/v1/brizo/services/compute?pubKey=${pubKey}&serviceId=${serviceId}&algo=${algo}&container=${container}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initializeServiceAgreement(did: string, serviceAgreementId: string, serviceDefinitionId: string,
|
public async initializeServiceAgreement(
|
||||||
signature: string, consumerPublicKey: string): Promise<any> {
|
did: string,
|
||||||
|
serviceAgreementId: string,
|
||||||
|
serviceDefinitionId: string,
|
||||||
|
signature: string,
|
||||||
|
consumerPublicKey: string): Promise<any> {
|
||||||
|
|
||||||
return WebServiceConnectorProvider.getConnector().post(
|
return WebServiceConnectorProvider.getConnector().post(
|
||||||
`${this.url}/api/v1/brizo/services/access/initialize`,
|
`${this.url}/api/v1/brizo/services/access/initialize`,
|
||||||
|
@ -61,9 +61,13 @@ import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
|
|||||||
|
|
||||||
const service: Service = ddo.findServiceByType("Access")
|
const service: Service = ddo.findServiceByType("Access")
|
||||||
|
|
||||||
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, service.serviceDefinitionId,
|
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(
|
||||||
serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementSignature,
|
ddo.id,
|
||||||
consumer, publisher)
|
service.serviceDefinitionId,
|
||||||
|
serviceAgreementSignatureResult.serviceAgreementId,
|
||||||
|
serviceAgreementSignatureResult.serviceAgreementSignature,
|
||||||
|
consumer,
|
||||||
|
publisher)
|
||||||
Logger.log("ServiceAgreement Id:", serviceAgreement.getId())
|
Logger.log("ServiceAgreement Id:", serviceAgreement.getId())
|
||||||
|
|
||||||
})()
|
})()
|
||||||
|
@ -54,15 +54,22 @@ import {Account, Logger, Ocean} from "../squid"
|
|||||||
|
|
||||||
const accessService = ddo.findServiceByType("Access")
|
const accessService = ddo.findServiceByType("Access")
|
||||||
|
|
||||||
const serviceAgreementSignatureResult: any = await ocean.signServiceAgreement(ddo.id,
|
const serviceAgreementSignatureResult: any = await ocean
|
||||||
accessService.serviceDefinitionId, consumer)
|
.signServiceAgreement(
|
||||||
|
ddo.id,
|
||||||
|
accessService.serviceDefinitionId,
|
||||||
|
consumer)
|
||||||
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
|
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
|
||||||
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
|
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
|
||||||
|
|
||||||
const service: Service = ddo.findServiceByType("Access")
|
const service: Service = ddo.findServiceByType("Access")
|
||||||
|
|
||||||
await ocean.initializeServiceAgreement(ddo.id, service.serviceDefinitionId,
|
await ocean
|
||||||
serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementSignature,
|
.initializeServiceAgreement(
|
||||||
consumer)
|
ddo.id,
|
||||||
|
service.serviceDefinitionId,
|
||||||
|
serviceAgreementSignatureResult.serviceAgreementId,
|
||||||
|
serviceAgreementSignatureResult.serviceAgreementSignature,
|
||||||
|
consumer)
|
||||||
|
|
||||||
})()
|
})()
|
||||||
|
@ -74,8 +74,11 @@ import {Account, Logger, Ocean} from "../squid"
|
|||||||
Logger.log("did", ddo.id)
|
Logger.log("did", ddo.id)
|
||||||
|
|
||||||
const accessService: Service = ddo.findServiceByType("Access")
|
const accessService: Service = ddo.findServiceByType("Access")
|
||||||
const serviceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, accessService.serviceDefinitionId,
|
const serviceAgreementResult: any = await ocean
|
||||||
consumer)
|
.signServiceAgreement(
|
||||||
|
ddo.id,
|
||||||
|
accessService.serviceDefinitionId,
|
||||||
|
consumer)
|
||||||
|
|
||||||
Logger.log("ServiceAgreement Id:", serviceAgreementResult.serviceAgreementId)
|
Logger.log("ServiceAgreement Id:", serviceAgreementResult.serviceAgreementId)
|
||||||
Logger.log("ServiceAgreement Signature:", serviceAgreementResult.serviceAgreementSignature)
|
Logger.log("ServiceAgreement Signature:", serviceAgreementResult.serviceAgreementSignature)
|
||||||
|
@ -77,20 +77,21 @@ export default class Ocean {
|
|||||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
|
||||||
|
|
||||||
// create ddo conditions out of the keys
|
// create ddo conditions out of the keys
|
||||||
const ddoConditions: DDOCondition[] = conditions.map((condition: Condition, index: number): DDOCondition => {
|
const ddoConditions: DDOCondition[] = conditions
|
||||||
const events: Event[] = [
|
.map((condition: Condition, index: number): DDOCondition => {
|
||||||
{
|
const events: Event[] = [
|
||||||
name: "PaymentReleased",
|
{
|
||||||
actorType: [
|
name: "PaymentReleased",
|
||||||
"consumer",
|
actorType: [
|
||||||
],
|
"consumer",
|
||||||
handlers: {
|
],
|
||||||
moduleName: "serviceAgreement",
|
handlers: {
|
||||||
functionName: "fulfillAgreement",
|
moduleName: "serviceAgreement",
|
||||||
version: "0.1",
|
functionName: "fulfillAgreement",
|
||||||
} as EventHandlers,
|
version: "0.1",
|
||||||
} as Event,
|
} as EventHandlers,
|
||||||
]
|
} as Event,
|
||||||
|
]
|
||||||
|
|
||||||
const mapParameterValueToName = (name) => {
|
const mapParameterValueToName = (name) => {
|
||||||
|
|
||||||
@ -180,12 +181,19 @@ export default class Ocean {
|
|||||||
|
|
||||||
const storedDdo = await aquarius.storeDDO(ddo)
|
const storedDdo = await aquarius.storeDDO(ddo)
|
||||||
|
|
||||||
await didRegistry.registerAttribute(id, ValueType.DID, "Metadata", serviceEndpoint, publisher.getId())
|
await didRegistry.registerAttribute(
|
||||||
|
id,
|
||||||
|
ValueType.DID,
|
||||||
|
"Metadata",
|
||||||
|
serviceEndpoint,
|
||||||
|
publisher.getId())
|
||||||
|
|
||||||
return storedDdo
|
return storedDdo
|
||||||
}
|
}
|
||||||
|
|
||||||
public async signServiceAgreement(did: string, serviceDefinitionId: string, consumer: Account): Promise<any> {
|
public async signServiceAgreement(did: string,
|
||||||
|
serviceDefinitionId: string,
|
||||||
|
consumer: Account): Promise<any> {
|
||||||
|
|
||||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||||
const id = did.replace("did:op:", "")
|
const id = did.replace("did:op:", "")
|
||||||
@ -204,25 +212,44 @@ export default class Ocean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initializeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string,
|
public async initializeServiceAgreement(did: string,
|
||||||
serviceAgreementSignature: string, consumer: Account) {
|
serviceDefinitionId: string,
|
||||||
const result = await BrizoProvider.getBrizo().initializeServiceAgreement(did, serviceAgreementId,
|
serviceAgreementId: string,
|
||||||
serviceDefinitionId, serviceAgreementSignature, await consumer.getPublicKey())
|
serviceAgreementSignature: string,
|
||||||
|
consumer: Account) {
|
||||||
|
const result = await BrizoProvider
|
||||||
|
.getBrizo()
|
||||||
|
.initializeServiceAgreement(
|
||||||
|
did,
|
||||||
|
serviceAgreementId,
|
||||||
|
serviceDefinitionId,
|
||||||
|
serviceAgreementSignature,
|
||||||
|
await consumer.getPublicKey())
|
||||||
|
|
||||||
Logger.log(result)
|
Logger.log(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async executeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string,
|
public async executeServiceAgreement(did: string,
|
||||||
serviceAgreementSignature: string, consumer: Account, publisher: Account)
|
serviceDefinitionId: string,
|
||||||
: Promise<ServiceAgreement> {
|
serviceAgreementId: string,
|
||||||
|
serviceAgreementSignature: string,
|
||||||
|
consumer: Account,
|
||||||
|
publisher: Account): Promise<ServiceAgreement> {
|
||||||
|
|
||||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||||
const id = did.replace("did:op:", "")
|
const id = did.replace("did:op:", "")
|
||||||
|
|
||||||
const serviceAgrement: ServiceAgreement = await ServiceAgreement.executeServiceAgreement(id,
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement
|
||||||
ddo, serviceDefinitionId, serviceAgreementId, serviceAgreementSignature, consumer, publisher)
|
.executeServiceAgreement(
|
||||||
|
id,
|
||||||
|
ddo,
|
||||||
|
serviceDefinitionId,
|
||||||
|
serviceAgreementId,
|
||||||
|
serviceAgreementSignature,
|
||||||
|
consumer,
|
||||||
|
publisher)
|
||||||
|
|
||||||
return serviceAgrement
|
return serviceAgreement
|
||||||
}
|
}
|
||||||
|
|
||||||
public async searchAssets(query: SearchQuery): Promise<DDO[]> {
|
public async searchAssets(query: SearchQuery): Promise<DDO[]> {
|
||||||
|
@ -10,9 +10,11 @@ import OceanBase from "../OceanBase"
|
|||||||
|
|
||||||
export default class ServiceAgreement extends OceanBase {
|
export default class ServiceAgreement extends OceanBase {
|
||||||
|
|
||||||
public static async signServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string,
|
public static async signServiceAgreement(assetId: string,
|
||||||
serviceAgreementId: string, consumer: Account):
|
ddo: DDO,
|
||||||
Promise<string> {
|
serviceDefinitionId: string,
|
||||||
|
serviceAgreementId: string,
|
||||||
|
consumer: Account): Promise<string> {
|
||||||
|
|
||||||
// Logger.log("signing SA", serviceAgreementId)
|
// Logger.log("signing SA", serviceAgreementId)
|
||||||
|
|
||||||
@ -21,15 +23,25 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
const valueHashes = ServiceAgreement.createValueHashes(values)
|
const valueHashes = ServiceAgreement.createValueHashes(values)
|
||||||
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
|
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
|
||||||
|
|
||||||
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId,
|
const serviceAgreementHashSignature = await ServiceAgreement
|
||||||
values, valueHashes, timeoutValues, consumer)
|
.createSAHashSignature(
|
||||||
|
service,
|
||||||
|
serviceAgreementId,
|
||||||
|
values,
|
||||||
|
valueHashes,
|
||||||
|
timeoutValues,
|
||||||
|
consumer)
|
||||||
|
|
||||||
return serviceAgreementHashSignature
|
return serviceAgreementHashSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string,
|
public static async executeServiceAgreement(assetId: string,
|
||||||
serviceAgreementId: string, serviceAgreementHashSignature: string,
|
ddo: DDO,
|
||||||
consumer: Account, publisher: Account): Promise<ServiceAgreement> {
|
serviceDefinitionId: string,
|
||||||
|
serviceAgreementId: string,
|
||||||
|
serviceAgreementHashSignature: string,
|
||||||
|
consumer: Account,
|
||||||
|
publisher: Account): Promise<ServiceAgreement> {
|
||||||
|
|
||||||
// Logger.log("executing SA", serviceAgreementId)
|
// Logger.log("executing SA", serviceAgreementId)
|
||||||
|
|
||||||
@ -46,8 +58,12 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
return serviceAgreement
|
return serviceAgreement
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async createSAHashSignature(service: Service, serviceAgreementId: string, values: ValuePair[],
|
private static async createSAHashSignature(service: Service,
|
||||||
valueHashes: string[], timeoutValues: number[], consumer: Account):
|
serviceAgreementId: string,
|
||||||
|
values: ValuePair[],
|
||||||
|
valueHashes: string[],
|
||||||
|
timeoutValues: number[],
|
||||||
|
consumer: Account):
|
||||||
Promise<string> {
|
Promise<string> {
|
||||||
|
|
||||||
if (!service.templateId) {
|
if (!service.templateId) {
|
||||||
@ -58,20 +74,28 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
return condition.conditionKey
|
return condition.conditionKey
|
||||||
})
|
})
|
||||||
|
|
||||||
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(service.templateId,
|
const serviceAgreementHash = ServiceAgreement
|
||||||
serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
|
.hashServiceAgreement(
|
||||||
|
service.templateId,
|
||||||
|
serviceAgreementId,
|
||||||
|
conditionKeys,
|
||||||
|
valueHashes,
|
||||||
|
timeoutValues)
|
||||||
|
|
||||||
const serviceAgreementHashSignature =
|
const serviceAgreementHashSignature = await Web3Provider
|
||||||
await Web3Provider.getWeb3().eth.sign(serviceAgreementHash, consumer.getId())
|
.getWeb3().eth.sign(serviceAgreementHash, consumer.getId())
|
||||||
|
|
||||||
return serviceAgreementHashSignature
|
return serviceAgreementHashSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async executeAgreement(ddo: DDO, serviceDefinitionId: string, serviceAgreementId: string,
|
private static async executeAgreement(ddo: DDO,
|
||||||
valueHashes: string[], timeoutValues: number[],
|
serviceDefinitionId: string,
|
||||||
serviceAgreementHashSignature: string, consumerAddress: string,
|
serviceAgreementId: string,
|
||||||
publisher: Account)
|
valueHashes: string[],
|
||||||
: Promise<ServiceAgreement> {
|
timeoutValues: number[],
|
||||||
|
serviceAgreementHashSignature: string,
|
||||||
|
consumerAddress: string,
|
||||||
|
publisher: Account): Promise<ServiceAgreement> {
|
||||||
|
|
||||||
const {serviceAgreement} = await Keeper.getInstance()
|
const {serviceAgreement} = await Keeper.getInstance()
|
||||||
|
|
||||||
@ -81,9 +105,16 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
throw new Error(`TemplateId not found in service "${service.type}" ddo.`)
|
throw new Error(`TemplateId not found in service "${service.type}" ddo.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const executeAgreementReceipt = await serviceAgreement.executeAgreement(
|
const executeAgreementReceipt = await serviceAgreement
|
||||||
service.templateId, serviceAgreementHashSignature, consumerAddress, valueHashes,
|
.executeAgreement(
|
||||||
timeoutValues, serviceAgreementId, ddo.id, publisher.getId())
|
service.templateId,
|
||||||
|
serviceAgreementHashSignature,
|
||||||
|
consumerAddress,
|
||||||
|
valueHashes,
|
||||||
|
timeoutValues,
|
||||||
|
serviceAgreementId,
|
||||||
|
ddo.id,
|
||||||
|
publisher.getId())
|
||||||
|
|
||||||
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {
|
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {
|
||||||
throw new Error("executing service agreement failed.")
|
throw new Error("executing service agreement failed.")
|
||||||
@ -113,9 +144,11 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static hashServiceAgreement(serviceAgreementTemplateId: string, serviceAgreementId: string,
|
private static hashServiceAgreement(serviceAgreementTemplateId: string,
|
||||||
conditionKeys: string[], valueHashes: string[], timeouts: number[])
|
serviceAgreementId: string,
|
||||||
: string {
|
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,
|
||||||
@ -153,8 +186,12 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
private constructor(serviceAgreementId: string, ddo: DDO, private publisher: Account, consumer: Account,
|
private constructor(serviceAgreementId: string,
|
||||||
state: boolean, status: boolean) {
|
ddo: DDO,
|
||||||
|
private publisher: Account,
|
||||||
|
consumer: Account,
|
||||||
|
state: boolean,
|
||||||
|
status: boolean) {
|
||||||
super(serviceAgreementId)
|
super(serviceAgreementId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,10 +51,13 @@ export default class ServiceAgreementTemplate extends OceanBase {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const receipt = await serviceAgreement.setupAgreementTemplate(
|
const receipt = await serviceAgreement
|
||||||
this.template.id, methodReflections, dependencyMatrix,
|
.setupAgreementTemplate(
|
||||||
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
|
this.template.id,
|
||||||
this.template.fulfilmentOperator, templateOwnerAddress)
|
methodReflections,
|
||||||
|
dependencyMatrix,
|
||||||
|
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
|
||||||
|
this.template.fulfilmentOperator, templateOwnerAddress)
|
||||||
|
|
||||||
const {serviceTemplateId, provider} = receipt.events.SetupAgreementTemplate.returnValues
|
const {serviceTemplateId, provider} = receipt.events.SetupAgreementTemplate.returnValues
|
||||||
|
|
||||||
@ -100,8 +103,8 @@ export default class ServiceAgreementTemplate extends OceanBase {
|
|||||||
dependencies: method.dependencies,
|
dependencies: method.dependencies,
|
||||||
dependencyTimeoutFlags: method.dependencyTimeoutFlags,
|
dependencyTimeoutFlags: method.dependencyTimeoutFlags,
|
||||||
isTerminalCondition: method.isTerminalCondition,
|
isTerminalCondition: method.isTerminalCondition,
|
||||||
condtionKey: ServiceAgreementTemplate.generateConditionsKey(this.getId(),
|
condtionKey: ServiceAgreementTemplate
|
||||||
methodReflection),
|
.generateConditionsKey(this.getId(), methodReflection),
|
||||||
} as Condition
|
} as Condition
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user