2019-06-20 00:20:09 +02:00
|
|
|
import { ServiceAgreementTemplateCondition } from '../../ddo/ServiceAgreementTemplate'
|
|
|
|
import { DDO } from '../../ddo/DDO'
|
|
|
|
import { ServiceAccess } from '../../ddo/Service'
|
|
|
|
import Account from '../Account'
|
2020-01-15 10:33:42 +01:00
|
|
|
import { zeroX, Logger } from '../../utils'
|
|
|
|
import { Ocean } from '../../squid'
|
|
|
|
import Web3 from 'web3'
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2020-01-15 10:33:42 +01:00
|
|
|
export class ServiceAgreement {
|
|
|
|
private ocean: Ocean
|
|
|
|
private logger: Logger
|
|
|
|
private web3: Web3
|
|
|
|
constructor(ocean: Ocean, logger: Logger, web3: Web3) {
|
|
|
|
this.ocean = ocean
|
|
|
|
this.logger = logger
|
|
|
|
this.web3 = web3
|
2019-03-28 12:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async signServiceAgreement(
|
2019-02-13 14:32:52 +01:00
|
|
|
ddo: DDO,
|
2019-08-16 16:12:42 +02:00
|
|
|
index: number,
|
2019-02-13 14:32:52 +01:00
|
|
|
serviceAgreementId: string,
|
2019-03-13 13:22:20 +01:00
|
|
|
agreementConditionsIds: string[],
|
2019-06-20 00:20:09 +02:00
|
|
|
consumer: Account
|
2019-02-13 14:32:52 +01:00
|
|
|
): Promise<string> {
|
2019-08-16 16:12:42 +02:00
|
|
|
const service = ddo.findServiceById<'access'>(index)
|
2019-11-15 00:00:10 +01:00
|
|
|
const timelockValues: number[] = this.getTimeValuesFromService(
|
|
|
|
service,
|
|
|
|
'timelock'
|
|
|
|
)
|
2019-09-09 12:18:54 +02:00
|
|
|
const timeoutValues: number[] = this.getTimeValuesFromService(service, 'timeout')
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2019-03-11 22:52:53 +01:00
|
|
|
if (!service.templateId) {
|
2019-06-20 00:20:09 +02:00
|
|
|
throw new Error('TemplateId not found in DDO.')
|
2019-03-11 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 12:20:22 +01:00
|
|
|
const serviceAgreementHashSignature = await this.createHashSignature(
|
2019-03-11 22:52:53 +01:00
|
|
|
service.templateId,
|
2019-02-21 18:14:07 +01:00
|
|
|
serviceAgreementId,
|
2019-03-13 13:22:20 +01:00
|
|
|
agreementConditionsIds,
|
2019-03-11 22:52:53 +01:00
|
|
|
timelockValues,
|
2019-02-21 18:14:07 +01:00
|
|
|
timeoutValues,
|
2019-06-20 00:20:09 +02:00
|
|
|
consumer
|
2019-02-21 18:14:07 +01:00
|
|
|
)
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
this.logger.debug('SA hash signature:', serviceAgreementHashSignature)
|
2018-12-11 17:41:10 +01:00
|
|
|
|
2019-07-03 13:08:00 +02:00
|
|
|
return zeroX(serviceAgreementHashSignature)
|
2018-11-12 08:33:19 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 12:20:22 +01:00
|
|
|
public async createHashSignature(
|
2019-03-11 22:52:53 +01:00
|
|
|
templateId: string,
|
2019-02-04 18:13:54 +01:00
|
|
|
serviceAgreementId: string,
|
|
|
|
valueHashes: string[],
|
2019-03-11 22:52:53 +01:00
|
|
|
timelockValues: number[],
|
2019-02-04 18:13:54 +01:00
|
|
|
timeoutValues: number[],
|
2019-06-20 00:20:09 +02:00
|
|
|
consumer: Account
|
2019-01-30 15:35:31 +01:00
|
|
|
): Promise<string> {
|
2019-03-28 12:20:22 +01:00
|
|
|
const serviceAgreementHash = this.hashServiceAgreement(
|
2019-03-11 22:52:53 +01:00
|
|
|
templateId,
|
2018-11-22 09:22:27 +01:00
|
|
|
serviceAgreementId,
|
|
|
|
valueHashes,
|
2019-03-11 22:52:53 +01:00
|
|
|
timelockValues,
|
2019-06-20 00:20:09 +02:00
|
|
|
timeoutValues
|
2019-03-11 22:52:53 +01:00
|
|
|
)
|
2018-11-05 10:01:58 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const serviceAgreementHashSignature = await this.ocean.utils.signature.signText(
|
|
|
|
serviceAgreementHash,
|
|
|
|
consumer.getId(),
|
|
|
|
consumer.getPassword()
|
|
|
|
)
|
2018-11-05 10:01:58 +01:00
|
|
|
|
|
|
|
return serviceAgreementHashSignature
|
|
|
|
}
|
|
|
|
|
2019-03-28 12:20:22 +01:00
|
|
|
public hashServiceAgreement(
|
2019-02-15 19:36:30 +01:00
|
|
|
serviceAgreementTemplateId: string,
|
|
|
|
serviceAgreementId: string,
|
|
|
|
valueHashes: string[],
|
2019-03-11 22:52:53 +01:00
|
|
|
timelocks: number[],
|
2019-06-20 00:20:09 +02:00
|
|
|
timeouts: number[]
|
2019-02-15 19:36:30 +01:00
|
|
|
): string {
|
2019-11-11 12:27:18 +01:00
|
|
|
const args: any = [
|
2020-01-20 11:44:13 +01:00
|
|
|
{ type: 'bytes32', value: zeroX(serviceAgreementTemplateId) },
|
2019-06-20 00:20:09 +02:00
|
|
|
{ type: 'bytes32[]', value: valueHashes.map(zeroX) },
|
|
|
|
{ type: 'uint256[]', value: timelocks },
|
|
|
|
{ type: 'uint256[]', value: timeouts },
|
|
|
|
{ type: 'bytes32', value: zeroX(serviceAgreementId) }
|
2018-10-29 17:28:40 +01:00
|
|
|
]
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2019-11-11 12:27:18 +01:00
|
|
|
return this.web3.utils.soliditySha3(...args)
|
2018-10-29 17:28:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
private getTimeValuesFromService(
|
|
|
|
service: ServiceAccess,
|
|
|
|
type: 'timeout' | 'timelock'
|
|
|
|
): number[] {
|
2019-09-13 13:38:23 +02:00
|
|
|
const timeoutValues: number[] = service.attributes.serviceAgreementTemplate.conditions.map(
|
2019-06-20 00:20:09 +02:00
|
|
|
(condition: ServiceAgreementTemplateCondition) => condition[type]
|
|
|
|
)
|
2018-11-05 13:15:33 +01:00
|
|
|
|
|
|
|
return timeoutValues
|
|
|
|
}
|
2018-10-29 17:28:40 +01:00
|
|
|
}
|