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/OceanAgreements.ts

132 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-02-14 12:37:52 +01:00
import { generateId } from "../utils/GeneratorHelpers"
2019-02-21 18:14:07 +01:00
import Account from "./Account"
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"
import { zeroX, didPrefixed } from "../utils"
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
import { OceanAgreementsConditions } from "./OceanAgreementsConditions"
export interface AgreementPrepareResult {
2019-02-14 11:30:03 +01:00
agreementId: string
signature: string
}
2019-01-21 17:48:40 +01:00
/**
* Agreements submodule of Ocean Protocol.
*/
export class OceanAgreements extends Instantiable {
2019-01-21 17:48:40 +01:00
/**
* Returns the instance of OceanAgreements.
* @return {Promise<OceanAgreements>}
*/
public static async getInstance(config: InstantiableConfig): Promise<OceanAgreements> {
const instance = new OceanAgreements()
instance.setInstanceConfig(config)
instance.conditions = await OceanAgreementsConditions.getInstance(config)
2019-01-21 17:48:40 +01:00
return instance
2019-01-21 17:48:40 +01:00
}
/**
* Agreements Conditions submodule.
* @type {OceanAgreementsConditions}
*/
public conditions: OceanAgreementsConditions
2019-01-21 17:48:40 +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.
* @return {Promise<AgreementPrepareResult>} Agreement ID and signaturee.
*/
public async prepare(
did: string,
serviceDefinitionId: string,
consumer: Account,
): Promise<AgreementPrepareResult> {
const d: DID = DID.parse(did as string)
const ddo = await this.ocean.aquarius.retrieveDDO(d)
2019-02-14 12:37:52 +01:00
const agreementId: string = generateId()
const templateName = ddo.findServiceByType("Access").serviceAgreementTemplate.contractName
const agreementConditionsIds = await this.ocean.keeper
.getTemplateByName(templateName)
.getAgreementIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId())
const signature = await ServiceAgreement.signServiceAgreement(
this.web3,
ddo,
serviceDefinitionId,
agreementId,
agreementConditionsIds,
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> {
const result = await this.ocean.brizo
.initializeServiceAgreement(
didPrefixed(did),
zeroX(agreementId),
serviceDefinitionId,
zeroX(signature),
consumer.getId(),
)
2019-02-21 17:58:54 +01:00
if (!result.ok) {
throw new Error("Error on initialize agreement: " + await result.text())
}
}
/**
* 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.
* @param {string} agreementId Service agreement ID.
2019-01-21 17:48:40 +01:00
* @param {string} serviceDefinitionId Service definition ID.
* @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<boolean>}
2019-01-21 17:48:40 +01:00
*/
public async create(
did: string,
agreementId: string,
2019-01-21 17:48:40 +01:00
serviceDefinitionId: string,
signature: string,
2019-01-21 17:48:40 +01:00
consumer: Account,
publisher: Account,
) {
2019-01-21 17:48:40 +01:00
const d: DID = DID.parse(did)
const ddo = await this.ocean.aquarius.retrieveDDO(d)
2019-01-21 17:48:40 +01:00
const templateName = ddo.findServiceById<"Access">(serviceDefinitionId).serviceAgreementTemplate.contractName
await this.ocean.keeper
.getTemplateByName(templateName)
.createAgreementFromDDO(agreementId, ddo, consumer.getId(), publisher.getId())
2019-01-21 17:48:40 +01:00
return true
2019-01-21 17:48:40 +01:00
}
}