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

added all necessary methods of Ocean.agreements

This commit is contained in:
Pedro Gutiérrez 2019-02-13 14:32:52 +01:00 committed by Pedro Gutiérrez
parent 05efcc3ff1
commit cfda1e6130
6 changed files with 91 additions and 55 deletions

View File

@ -14,7 +14,7 @@ describe("Buy Asset", () => {
let ddo: DDO
let did: DID
let agreementId: string
let serviceAgreementSignatureResult: {agreementId: string, signature: string}
let serviceAgreement: ServiceAgreement
before(async () => {
@ -83,9 +83,11 @@ describe("Buy Asset", () => {
it("should sign the service agreement", async () => {
const accessService = ddo.findServiceByType("Access")
agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer)
serviceAgreementSignatureResult = await ocean.agreements.prepare(ddo.id, accessService.serviceDefinitionId, consumer)
const {agreementId, signature} = serviceAgreementSignatureResult
assert.match(agreementId, /^[a-f0-9]{64}$/, "Service agreement ID seems not valid")
assert.match(signature, /^0x[a-f0-9]{130}$/, "Service agreement signature seems not valid")
})
it("should execute the service agreement", async () => {
@ -94,8 +96,8 @@ describe("Buy Asset", () => {
serviceAgreement = await ocean.agreements.create(
ddo.id,
accessService.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId,
serviceAgreementSignatureResult.serviceAgreementSignature,
serviceAgreementSignatureResult.agreementId,
serviceAgreementSignatureResult.signature,
consumer,
publisher,
)

View File

@ -116,20 +116,20 @@ export default class Ocean {
/**
* Signs a service agreement by DID.
* @deprecated Replace by [Ocean.assets.order]{@link #OceanAssets.order}
* @deprecated Replace by [Ocean.agreement.prepare]{@link #OceanAgreement.prepare}
* @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {Account} consumer Consumer account.
* @return {Promise<any>}
*
*/
@deprecated("OceanAssets.order")
@deprecated("OceanAgreement.prepare")
public async signServiceAgreement(
did: string,
serviceDefinitionId: string,
consumer: Account,
) {
return await this.assets.order(did, serviceDefinitionId, consumer)
return await this.agreements.prepare(did, serviceDefinitionId, consumer)
}
/**

View File

@ -2,6 +2,10 @@ import AquariusProvider from "../aquarius/AquariusProvider"
import Account from "./Account"
import DID from "./DID"
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
import IdGenerator from "./IdGenerator"
import BrizoProvider from "../brizo/BrizoProvider"
export type agreementPreparionResult = {agreementId: string, signature: string}
/**
* Agreements submodule of Ocean Protocol.
@ -27,20 +31,71 @@ export default class OceanAgreements {
private static instance: OceanAgreements = null
/**
* Executes a service agreement.
* Creates a consumer signature for the specified asset service.
* @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {string} serviceAgreementId Service agreement ID.
* @param {string} serviceAgreementSignature Service agreement signature.
* @param {Account} consumer Consumer account.
* @return {Promise<agreementPreparionResult>} Agreement ID and signaturee.
*/
public async prepare(
did: string,
serviceDefinitionId: string,
consumer: Account,
): Promise<agreementPreparionResult> {
const d: DID = DID.parse(did as string)
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
const agreementId: string = IdGenerator.generateId()
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.
* @param {string} did Decentralized ID.
* @param {string} agreementId Service agreement ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {string} signature Service agreement signature.
* @param {Account} consumer Consumer account.
* @param {Account} publisher Publisher account.
* @return {Promise<ServiceAgreement>}
*/
public async create(
did: string,
agreementId: string,
serviceDefinitionId: string,
serviceAgreementId: string,
serviceAgreementSignature: string,
signature: string,
consumer: Account,
publisher: Account,
): Promise<ServiceAgreement> {
@ -51,9 +106,9 @@ export default class OceanAgreements {
.executeServiceAgreement(
d,
ddo,
agreementId,
serviceDefinitionId,
serviceAgreementId,
serviceAgreementSignature,
signature,
consumer,
publisher)

View File

@ -9,7 +9,6 @@ import ContractEvent from "../keeper/Event"
import EventListener from "../keeper/EventListener"
import Keeper from "../keeper/Keeper"
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
import Logger from "../utils/Logger"
import Account from "./Account"
import DID from "./DID"
import IdGenerator from "./IdGenerator"

View File

@ -12,10 +12,12 @@ import OceanBase from "../OceanBase"
export default class ServiceAgreement extends OceanBase {
public static async signServiceAgreement(ddo: DDO,
public static async signServiceAgreement(
ddo: DDO,
serviceDefinitionId: string,
serviceAgreementId: string,
consumer: Account): Promise<string> {
consumer: Account,
): Promise<string> {
if (ConfigProvider.getConfig().verbose) {
Logger.log("Signing SA with serviceAgreementId", serviceAgreementId)
@ -36,13 +38,15 @@ export default class ServiceAgreement extends OceanBase {
return serviceAgreementHashSignature
}
public static async executeServiceAgreement(did: DID,
public static async executeServiceAgreement(
did: DID,
ddo: DDO,
serviceDefinitionId: string,
serviceAgreementId: string,
serviceAgreementHashSignature: string,
consumer: Account,
publisher: Account): Promise<ServiceAgreement> {
publisher: Account,
): Promise<ServiceAgreement> {
if (ConfigProvider.getConfig().verbose) {
Logger.log("Executing SA with serviceAgreementId", serviceAgreementId)

View File

@ -9,7 +9,6 @@ import { DDO } from "../../src/ddo/DDO"
import { Service } from "../../src/ddo/Service"
import Account from "../../src/ocean/Account"
import Ocean from "../../src/ocean/Ocean"
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
import * as signatureHelpers from "../../src/utils/SignatureHelpers"
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
@ -139,36 +138,13 @@ describe("Ocean", () => {
await consumer.requestTokens(metadata.base.price)
const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer)
const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, service.serviceDefinitionId, consumer)
assert(signServiceAgreementResult)
assert(signServiceAgreementResult.serviceAgreementId, "no serviceAgreementId")
assert(signServiceAgreementResult.serviceAgreementSignature, "no serviceAgreementSignature")
assert(signServiceAgreementResult.serviceAgreementSignature.startsWith("0x"))
assert(signServiceAgreementResult.serviceAgreementSignature.length === 132)
})
})
describe("#executeServiceAgreement()", () => {
it("should execute a service agreement", async () => {
const publisher = accounts[0]
const consumer = accounts[1]
const ddo: DDO = await ocean.registerAsset(metadata, publisher)
const service: Service = ddo.findServiceByType("Access")
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer)
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id,
service.serviceDefinitionId, signServiceAgreementResult.serviceAgreementId,
signServiceAgreementResult.serviceAgreementSignature, consumer, publisher)
assert(serviceAgreement)
assert(signServiceAgreementResult.agreementId, "no agreementId")
assert(signServiceAgreementResult.signature, "no signature")
assert(signServiceAgreementResult.signature.startsWith("0x"))
assert(signServiceAgreementResult.signature.length === 132)
})
})
})