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

105 lines
3.2 KiB
TypeScript
Raw Normal View History

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'
import { zeroX, Logger } from '../../utils'
import { Ocean } from '../../squid'
import Web3 from 'web3'
2018-10-29 17:28:40 +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
}
public async signServiceAgreement(
ddo: DDO,
2019-08-16 16:12:42 +02:00
index: number,
serviceAgreementId: string,
agreementConditionsIds: string[],
2019-06-20 00:20:09 +02:00
consumer: Account
): 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
if (!service.templateId) {
2019-06-20 00:20:09 +02:00
throw new Error('TemplateId not found in DDO.')
}
const serviceAgreementHashSignature = await this.createHashSignature(
service.templateId,
2019-02-21 18:14:07 +01:00
serviceAgreementId,
agreementConditionsIds,
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
return zeroX(serviceAgreementHashSignature)
2018-11-12 08:33:19 +01:00
}
public async createHashSignature(
templateId: string,
2019-02-04 18:13:54 +01:00
serviceAgreementId: string,
valueHashes: string[],
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> {
const serviceAgreementHash = this.hashServiceAgreement(
templateId,
2018-11-22 09:22:27 +01:00
serviceAgreementId,
valueHashes,
timelockValues,
2019-06-20 00:20:09 +02:00
timeoutValues
)
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
}
public hashServiceAgreement(
2019-02-15 19:36:30 +01:00
serviceAgreementTemplateId: string,
serviceAgreementId: string,
valueHashes: string[],
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 = [
{ 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
]
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]
)
return timeoutValues
}
2018-10-29 17:28:40 +01:00
}