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) await AquariusProvider.getAquarius().storeDDO(ddo)
asset.setId(assetId) asset.setId(assetId)

View File

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