squid-js/src/keeper/contracts/templates/EscrowAccessSecretStoreTemp...

176 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { AgreementTemplate } from './AgreementTemplate.abstract'
import { DDO } from '../../../ddo/DDO'
import { generateId, zeroX } from '../../../utils'
import { InstantiableConfig } from '../../../Instantiable.abstract'
2019-06-20 00:20:09 +02:00
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from './EscrowAccessSecretStoreTemplate.serviceAgreementTemplate'
export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
2019-06-20 00:20:09 +02:00
public static async getInstance(
config: InstantiableConfig
): Promise<EscrowAccessSecretStoreTemplate> {
return AgreementTemplate.getInstance(
config,
'EscrowAccessSecretStoreTemplate',
EscrowAccessSecretStoreTemplate
)
}
public async getServiceAgreementTemplate() {
return escrowAccessSecretStoreTemplateServiceAgreementTemplate
}
/**
* Create a agreement using EscrowAccessSecretStoreTemplate.
* @param {string} agreementId Generated agreement ID.
* @param {string} did Asset DID.
* @param {string[]} conditionIds List of conditions IDs.
* @param {number[]} timeLocks Timelocks.
* @param {number[]} timeOuts Timeouts.
* @param {string} accessConsumer Consumer address.
* @param {string} from Action sender.
* @param {any} Transaction receipt.
*/
public createAgreement(
agreementId: string,
did: string,
conditionIds: string[],
timeLocks: number[],
timeOuts: number[],
accessConsumer: string,
2019-06-20 00:20:09 +02:00
from?: string
) {
return super.createAgreement(
agreementId,
did,
conditionIds,
timeLocks,
timeOuts,
[accessConsumer],
2019-06-20 00:20:09 +02:00
from
)
}
2019-06-20 00:20:09 +02:00
public async createAgreementFromDDO(
agreementId: string,
ddo: DDO,
consumer: string,
from?: string
) {
return !!(await this.createFullAgreement(
ddo.shortId(),
2019-06-20 00:20:09 +02:00
ddo.findServiceByType('Metadata').metadata.base.price,
consumer,
from,
2019-06-20 00:20:09 +02:00
agreementId
))
}
2019-06-20 00:20:09 +02:00
public async getAgreementIdsFromDDO(
agreementId: string,
ddo: DDO,
consumer: string,
from?: string
) {
const {
accessSecretStoreConditionId,
lockRewardConditionId,
escrowRewardId
} = await this.createFullAgreementData(
agreementId,
ddo.shortId(),
ddo.findServiceByType('Metadata').metadata.base.price,
consumer
)
return [
accessSecretStoreConditionId,
lockRewardConditionId,
escrowRewardId
]
}
/**
* Create a agreement using EscrowAccessSecretStoreTemplate using only the most important information.
* @param {string} did Asset DID.
* @param {number} amount Asset price.
* @param {string} from Consumer address.
* @return {Promise<string>} Agreement ID.
*/
2019-05-29 15:52:35 +02:00
public async createFullAgreement(
did: string,
amount: number | string,
consumer: string,
from?: string,
2019-06-20 00:20:09 +02:00
agreementId: string = generateId()
2019-05-29 15:52:35 +02:00
): Promise<string> {
2019-06-20 00:20:09 +02:00
const {
accessSecretStoreConditionId,
lockRewardConditionId,
escrowRewardId
} = await this.createFullAgreementData(
agreementId,
did,
amount,
consumer
)
await this.createAgreement(
agreementId,
did,
2019-06-20 00:20:09 +02:00
[
accessSecretStoreConditionId,
lockRewardConditionId,
escrowRewardId
],
[0, 0, 0],
[0, 0, 0],
consumer,
2019-06-20 00:20:09 +02:00
from
)
2019-03-15 15:23:28 +01:00
return zeroX(agreementId)
}
2019-06-20 00:20:09 +02:00
private async createFullAgreementData(
agreementId: string,
did: string,
amount: number | string,
consumer: string
) {
const { didRegistry, conditions } = this.ocean.keeper
2019-06-20 00:20:09 +02:00
const {
accessSecretStoreCondition,
lockRewardCondition,
escrowReward
} = conditions
const publisher = await didRegistry.getDIDOwner(did)
2019-06-20 00:20:09 +02:00
const lockRewardConditionId = await lockRewardCondition.generateIdHash(
agreementId,
await escrowReward.getAddress(),
amount
)
const accessSecretStoreConditionId = await accessSecretStoreCondition.generateIdHash(
agreementId,
did,
consumer
)
const escrowRewardId = await escrowReward.generateIdHash(
agreementId,
String(amount),
publisher,
2019-03-15 15:23:28 +01:00
consumer,
lockRewardConditionId,
2019-06-20 00:20:09 +02:00
accessSecretStoreConditionId
)
return {
lockRewardConditionId,
accessSecretStoreConditionId,
2019-06-20 00:20:09 +02:00
escrowRewardId
}
}
}