1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

refactor value loading, added valuepair business object

This commit is contained in:
Sebastian Gerske 2018-11-05 13:15:33 +01:00
parent 1ad80dde3e
commit 7a3cc1e473
3 changed files with 42 additions and 39 deletions

4
src/models/ValuePair.ts Normal file
View File

@ -0,0 +1,4 @@
export default class ValuePair {
public type: string
public value: any
}

View File

@ -95,7 +95,6 @@ export default class Ocean {
],
})
await AquariusProvider.getAquarius().storeDDO(ddo)
asset.setId(assetId)

View File

@ -2,6 +2,7 @@ import DDO from "../ddo/DDO"
import AccessConditions from "../keeper/contracts/conditions/AccessConditions"
import ServiceAgreementContract from "../keeper/contracts/ServiceAgreement"
import Web3Provider from "../keeper/Web3Provider"
import ValuePair from "../models/ValuePair"
import Account from "./Account"
import OceanBase from "./OceanBase"
@ -11,23 +12,13 @@ export default class ServiceAgreement extends OceanBase {
publisher: Account):
Promise<ServiceAgreement> {
const timeoutValues: number[] = ddo.service[0].conditions.map((condition) => {
return condition.timeout
})
// todo: this should come from ddo
const values = [
{type: "bool", value: true},
{type: "bool", value: false},
{type: "uint", value: 120},
{type: "string", value: serviceAgreementId},
]
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId,
consumer)
const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(ddo,
serviceAgreementId, values, timeoutValues, serviceAgreementHashSignature, consumer, publisher)
serviceAgreementId, timeoutValues, serviceAgreementHashSignature, consumer, publisher)
return serviceAgreement
}
@ -35,23 +26,14 @@ export default class ServiceAgreement extends OceanBase {
public static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, consumer: Account):
Promise<string> {
// todo get from ddo
const values = [
{type: "bool", value: true},
{type: "bool", value: false},
{type: "uint", value: 120},
{type: "string", value: serviceAgreementId},
]
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
const valueHashes = ServiceAgreement.createValueHashes(values)
const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => {
return condition.conditionKey
})
const timeoutValues: number[] = ddo.service[0].conditions.map((condition) => {
return condition.timeout
})
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId,
serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
@ -62,11 +44,11 @@ export default class ServiceAgreement extends OceanBase {
return serviceAgreementHashSignature
}
private static async signServiceAgreement(ddo: DDO, serviceAgreementId: string, values: any[],
timeoutValues: number[], serviceAgreementHashSignature: string,
consumer: Account, publisher: Account):
Promise<ServiceAgreement> {
private static async signServiceAgreement(ddo: DDO, serviceAgreementId: string, timeoutValues: number[],
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 executeAgreementReceipt = await serviceAgreement.executeAgreement(
@ -87,30 +69,48 @@ export default class ServiceAgreement extends OceanBase {
)
}
private static createValueHashes(values: any[]): any[] {
return values.map((value) => {
return ServiceAgreement.hashSingleValue(value.type, value.value)
private static createValueHashes(valuePairs: ValuePair[]): any[] {
return valuePairs.map((valuePair) => {
return ServiceAgreement.hashSingleValue(valuePair)
})
}
private static hashSingleValue(type: string, value: any): string {
const args = {type, value}
return Web3Provider.getWeb3().utils.soliditySha3(args).toString("hex")
private static hashSingleValue(data: ValuePair): string {
return Web3Provider.getWeb3().utils.soliditySha3(data).toString("hex")
}
private static hashServiceAgreement(serviceAgreementTemplateId: string, serviceAgreementId: string,
conditionKeys: string[], valueHashes: string[], timeouts: number[]) {
const args = [
{type: "bytes32", value: serviceAgreementTemplateId},
{type: "bytes32[]", value: conditionKeys},
{type: "bytes32[]", value: valueHashes},
{type: "uint256[]", value: timeouts},
{type: "bytes32", value: serviceAgreementId},
{type: "bytes32", value: serviceAgreementTemplateId} as ValuePair,
{type: "bytes32[]", value: conditionKeys} as ValuePair,
{type: "bytes32[]", value: valueHashes} as ValuePair,
{type: "uint256[]", value: timeouts} as ValuePair,
{type: "bytes32", value: serviceAgreementId} as ValuePair,
]
return Web3Provider.getWeb3().utils.soliditySha3(...args).toString("hex")
}
private static getTimeoutValuesFromDDO(ddo: DDO): number[] {
const timeoutValues: number[] = ddo.service[0].conditions.map((condition) => {
return condition.timeout
})
return timeoutValues
}
private static getValuesFromDDO(ddo: DDO, serviceAgreementId: string): ValuePair[] {
const values: ValuePair[] = [
{type: "bool", value: true} as ValuePair,
{type: "bool", value: false} as ValuePair,
{type: "uint", value: 120} as ValuePair,
{type: "string", value: serviceAgreementId} as ValuePair,
]
return values
}
private constructor(serviceAgreementId: string, ddo: DDO, private publisher: Account, consumer: Account,
state: boolean, status: boolean) {
super(serviceAgreementId)