2019-01-21 17:48:40 +01:00
|
|
|
import AquariusProvider from "../aquarius/AquariusProvider"
|
2019-02-14 11:30:03 +01:00
|
|
|
import BrizoProvider from "../brizo/BrizoProvider"
|
2019-01-21 17:48:40 +01:00
|
|
|
import Account from "./Account"
|
2019-02-14 12:37:52 +01:00
|
|
|
import { generateId } from "../utils/GeneratorHelpers"
|
2019-01-21 17:48:40 +01:00
|
|
|
import DID from "./DID"
|
2019-02-14 11:30:03 +01:00
|
|
|
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
2019-02-13 14:32:52 +01:00
|
|
|
|
2019-02-14 11:30:03 +01:00
|
|
|
export interface AgreementPreparionResult {
|
|
|
|
agreementId: string
|
|
|
|
signature: string
|
|
|
|
}
|
2019-01-21 17:48:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Agreements submodule of Ocean Protocol.
|
|
|
|
*/
|
|
|
|
export default class OceanAgreements {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance of OceanAgreements.
|
|
|
|
* @return {Promise<OceanAgreements>}
|
|
|
|
*/
|
|
|
|
public static async getInstance(): Promise<OceanAgreements> {
|
|
|
|
if (!OceanAgreements.instance) {
|
|
|
|
OceanAgreements.instance = new OceanAgreements()
|
|
|
|
}
|
|
|
|
|
|
|
|
return OceanAgreements.instance
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OceanAgreements instance.
|
|
|
|
* @type {OceanAgreements}
|
|
|
|
*/
|
|
|
|
private static instance: OceanAgreements = null
|
|
|
|
|
|
|
|
/**
|
2019-02-13 14:32:52 +01:00
|
|
|
* Creates a consumer signature for the specified asset service.
|
|
|
|
* @param {string} did Decentralized ID.
|
|
|
|
* @param {string} serviceDefinitionId Service definition ID.
|
|
|
|
* @param {Account} consumer Consumer account.
|
2019-02-14 11:30:03 +01:00
|
|
|
* @return {Promise<AgreementPreparionResult>} Agreement ID and signaturee.
|
2019-02-13 14:32:52 +01:00
|
|
|
*/
|
|
|
|
public async prepare(
|
|
|
|
did: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
consumer: Account,
|
2019-02-14 11:30:03 +01:00
|
|
|
): Promise<AgreementPreparionResult> {
|
2019-02-13 14:32:52 +01:00
|
|
|
|
|
|
|
const d: DID = DID.parse(did as string)
|
|
|
|
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
|
2019-02-14 12:37:52 +01:00
|
|
|
const agreementId: string = generateId()
|
2019-02-13 14:32:52 +01:00
|
|
|
|
|
|
|
const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, consumer)
|
|
|
|
|
|
|
|
return {agreementId, signature}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a service agreement to the publisher to create the agreement on-chain.
|
|
|
|
* @param {string} did Decentralized ID.
|
|
|
|
* @param {string} serviceDefinitionId Service definition ID.
|
|
|
|
* @param {Account} consumer Consumer account.
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
public async send(
|
|
|
|
did: string,
|
|
|
|
agreementId: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
signature: string,
|
|
|
|
consumer: Account,
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
|
|
await BrizoProvider
|
|
|
|
.getBrizo()
|
|
|
|
.initializeServiceAgreement(
|
|
|
|
did,
|
|
|
|
agreementId,
|
|
|
|
serviceDefinitionId,
|
|
|
|
signature,
|
|
|
|
consumer.getId(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a service agreement on-chain. This should be called by the publisher of the asset.
|
|
|
|
* Consumer signature will be verified on-chain, but it is recommended to verify the signature
|
|
|
|
* in this method before submitting on-chain.
|
2019-01-21 17:48:40 +01:00
|
|
|
* @param {string} did Decentralized ID.
|
2019-02-13 14:32:52 +01:00
|
|
|
* @param {string} agreementId Service agreement ID.
|
2019-01-21 17:48:40 +01:00
|
|
|
* @param {string} serviceDefinitionId Service definition ID.
|
2019-02-13 14:32:52 +01:00
|
|
|
* @param {string} signature Service agreement signature.
|
2019-01-21 17:48:40 +01:00
|
|
|
* @param {Account} consumer Consumer account.
|
|
|
|
* @param {Account} publisher Publisher account.
|
|
|
|
* @return {Promise<ServiceAgreement>}
|
|
|
|
*/
|
|
|
|
public async create(
|
|
|
|
did: string,
|
2019-02-13 14:32:52 +01:00
|
|
|
agreementId: string,
|
2019-01-21 17:48:40 +01:00
|
|
|
serviceDefinitionId: string,
|
2019-02-13 14:32:52 +01:00
|
|
|
signature: string,
|
2019-01-21 17:48:40 +01:00
|
|
|
consumer: Account,
|
|
|
|
publisher: Account,
|
|
|
|
): Promise<ServiceAgreement> {
|
|
|
|
const d: DID = DID.parse(did)
|
|
|
|
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
|
|
|
|
|
|
|
|
const serviceAgreement: ServiceAgreement = await ServiceAgreement
|
|
|
|
.executeServiceAgreement(
|
|
|
|
d,
|
|
|
|
ddo,
|
2019-02-13 14:32:52 +01:00
|
|
|
agreementId,
|
2019-01-21 17:48:40 +01:00
|
|
|
serviceDefinitionId,
|
2019-02-13 14:32:52 +01:00
|
|
|
signature,
|
2019-01-21 17:48:40 +01:00
|
|
|
consumer,
|
|
|
|
publisher)
|
|
|
|
|
|
|
|
return serviceAgreement
|
|
|
|
}
|
|
|
|
}
|