2019-02-28 21:28:57 +01:00
|
|
|
import { AgreementTemplate } from "./AgreementTemplate.abstract"
|
2019-03-14 21:28:51 +01:00
|
|
|
import { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from "../conditions"
|
|
|
|
import DIDRegistry from "../DIDRegistry"
|
|
|
|
import { DDO } from "../../../ddo/DDO"
|
|
|
|
import { generateId, zeroX } from "../../../utils"
|
2019-02-28 21:28:57 +01:00
|
|
|
|
2019-03-07 15:57:43 +01:00
|
|
|
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from "./EscrowAccessSecretStoreTemplate.serviceAgreementTemplate"
|
|
|
|
|
2019-02-28 21:28:57 +01:00
|
|
|
export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
|
|
|
|
|
|
|
|
public static async getInstance(): Promise<EscrowAccessSecretStoreTemplate> {
|
|
|
|
return AgreementTemplate.getInstance("EscrowAccessSecretStoreTemplate", EscrowAccessSecretStoreTemplate)
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:57:43 +01:00
|
|
|
public async getServiceAgreementTemplate() {
|
|
|
|
return escrowAccessSecretStoreTemplateServiceAgreementTemplate
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:05:27 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-02-28 21:28:57 +01:00
|
|
|
public createAgreement(
|
|
|
|
agreementId: string,
|
|
|
|
did: string,
|
|
|
|
conditionIds: string[],
|
|
|
|
timeLocks: number[],
|
|
|
|
timeOuts: number[],
|
|
|
|
accessConsumer: string,
|
|
|
|
from?: string,
|
|
|
|
) {
|
|
|
|
return super.createAgreement(
|
|
|
|
agreementId,
|
|
|
|
did,
|
|
|
|
conditionIds,
|
|
|
|
timeLocks,
|
|
|
|
timeOuts,
|
|
|
|
[accessConsumer],
|
|
|
|
from,
|
|
|
|
)
|
|
|
|
}
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-14 15:47:31 +01:00
|
|
|
public async createAgreementFromDDO(agreementId: string, ddo: DDO, consumer: string, from?: string) {
|
|
|
|
return !!await this.createFullAgreement(
|
|
|
|
ddo.shortId(),
|
|
|
|
ddo.findServiceByType("Metadata").metadata.base.price,
|
|
|
|
consumer,
|
|
|
|
from,
|
|
|
|
agreementId,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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, from)
|
|
|
|
return [accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId]
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:05:27 +01:00
|
|
|
/**
|
|
|
|
* 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-03-14 15:47:31 +01:00
|
|
|
public async createFullAgreement(did: string, amount: number, consumer: string, from?: string, agreementId?: string): Promise<string> {
|
|
|
|
agreementId = agreementId || zeroX(generateId())
|
2019-03-13 13:22:20 +01:00
|
|
|
const {accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId} =
|
|
|
|
await this.createFullAgreementData(agreementId, did, amount, from)
|
2019-03-04 13:05:27 +01:00
|
|
|
|
|
|
|
await this.createAgreement(
|
|
|
|
agreementId,
|
|
|
|
did,
|
2019-03-13 13:22:20 +01:00
|
|
|
[accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId],
|
2019-03-04 13:05:27 +01:00
|
|
|
[0, 0, 0],
|
|
|
|
[0, 0, 0],
|
2019-03-14 15:47:31 +01:00
|
|
|
consumer,
|
2019-03-04 13:05:27 +01:00
|
|
|
from,
|
|
|
|
)
|
|
|
|
|
|
|
|
return agreementId
|
|
|
|
}
|
2019-03-12 13:49:11 +01:00
|
|
|
|
2019-03-13 13:22:20 +01:00
|
|
|
private async createFullAgreementData(agreementId: string, did: string, amount: number, from?: string) {
|
|
|
|
from = await this.getFromAddress(from)
|
2019-03-12 13:49:11 +01:00
|
|
|
|
2019-03-13 13:22:20 +01:00
|
|
|
const didRegistry = await DIDRegistry.getInstance()
|
|
|
|
|
|
|
|
const accessSecretStoreCondition = await AccessSecretStoreCondition.getInstance()
|
|
|
|
const lockRewardCondition = await LockRewardCondition.getInstance()
|
|
|
|
const escrowReward = await EscrowReward.getInstance()
|
2019-03-12 13:49:11 +01:00
|
|
|
|
2019-03-13 13:22:20 +01:00
|
|
|
const publisher = await didRegistry.getDIDOwner(did)
|
2019-03-12 13:49:11 +01:00
|
|
|
|
2019-03-13 13:22:20 +01:00
|
|
|
const lockRewardConditionId = await lockRewardCondition.generateIdHash(agreementId, await escrowReward.getAddress(), amount)
|
|
|
|
const accessSecretStoreConditionId = await accessSecretStoreCondition.generateIdHash(agreementId, did, from)
|
|
|
|
const escrowRewardId = await escrowReward.generateIdHash(
|
|
|
|
agreementId,
|
|
|
|
amount,
|
|
|
|
publisher,
|
2019-03-14 19:02:41 +01:00
|
|
|
from,
|
2019-03-13 13:22:20 +01:00
|
|
|
lockRewardConditionId,
|
|
|
|
accessSecretStoreConditionId,
|
|
|
|
)
|
2019-03-12 13:49:11 +01:00
|
|
|
|
|
|
|
return {
|
2019-03-13 13:22:20 +01:00
|
|
|
lockRewardConditionId,
|
|
|
|
accessSecretStoreConditionId,
|
|
|
|
escrowRewardId,
|
2019-03-12 13:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-28 21:28:57 +01:00
|
|
|
}
|