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