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

Move signature values generation to template.

This commit is contained in:
Pedro Gutiérrez 2019-03-12 13:49:11 +01:00 committed by Pedro Gutiérrez
parent c213a8d3ca
commit 6a4df9768a
10 changed files with 72 additions and 53 deletions

View File

@ -164,12 +164,13 @@ describe("Register Escrow Access Secret Store Template", () => {
}) })
describe("Short flow", () => { describe("Short flow", () => {
const did = `0x${utils.generateId()}` const did = utils.generateId()
let agreementId let agreementId
it("should register a DID", async () => { it("should register a DID", async () => {
await keeper.didRegistry.registerAttribute(did.replace("0x", ""), checksum, url, publisher.getId()) // This part is executed inside Ocean.assets.create()
await keeper.didRegistry.registerAttribute(did, checksum, url, publisher.getId())
}) })
it("should create a new agreement (short way)", async () => { it("should create a new agreement (short way)", async () => {

View File

@ -1,5 +1,3 @@
import { Condition } from "./Condition"
import { Contract } from "./Contract"
import { MetaData } from "./MetaData" import { MetaData } from "./MetaData"
import { ServiceAgreementTemplate } from "./ServiceAgreementTemplate" import { ServiceAgreementTemplate } from "./ServiceAgreementTemplate"

View File

@ -1,7 +1,7 @@
export interface ServiceAgreementTemplateParameter { export interface ServiceAgreementTemplateParameter {
name: string name: string
type: string type: string
value: string value: string | number
} }
export interface ServiceAgreementTemplateEvent { export interface ServiceAgreementTemplateEvent {

View File

@ -2,7 +2,7 @@ import DIDRegistry from "./contracts/DIDRegistry"
import Dispenser from "./contracts/Dispenser" import Dispenser from "./contracts/Dispenser"
import OceanToken from "./contracts/Token" import OceanToken from "./contracts/Token"
import { Condition, LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from "./contracts/conditions" import { Condition, LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from "./contracts/conditions"
import { EscrowAccessSecretStoreTemplate } from "./contracts/templates" import { AgreementTemplate, EscrowAccessSecretStoreTemplate } from "./contracts/templates"
import { TemplateStoreManager } from "./contracts/managers" import { TemplateStoreManager } from "./contracts/managers"
import Web3Provider from "./Web3Provider" import Web3Provider from "./Web3Provider"
@ -104,6 +104,11 @@ export class Keeper {
.find(condition => condition.getAddress() === address) .find(condition => condition.getAddress() === address)
} }
public getTemplateByName(name: string): AgreementTemplate {
return Object.values(this.templates)
.find(template => template.contractName === name)
}
/** /**
* Returns the network by name. * Returns the network by name.
* @return {Promise<string>} Network name. * @return {Promise<string>} Network name.

View File

@ -9,7 +9,7 @@ export default abstract class ContractBase {
protected static instance = null protected static instance = null
private contract: Contract = null private contract: Contract = null
private contractName: string contractName: string
constructor(contractName) { constructor(contractName) {
this.contractName = contractName this.contractName = contractName

View File

@ -39,6 +39,6 @@ export abstract class Condition extends ContractBase {
} }
abortByTimeOut(agreementId: string, from?: string) { abortByTimeOut(agreementId: string, from?: string) {
return this.sendFrom("requestTokens", [zeroX(agreementId)], from) return this.sendFrom("abortByTimeOut", [zeroX(agreementId)], from)
} }
} }

View File

@ -1,6 +1,7 @@
import ContractBase from "../ContractBase" import ContractBase from "../ContractBase"
import { Condition } from "../conditions/Condition.abstract" import { Condition } from "../conditions/Condition.abstract"
import Keeper from "../../Keeper" import Keeper from "../../Keeper"
import { DDO } from '../../../ddo/DDO'
import { ServiceAgreementTemplate, ServiceAgreementTemplateCondition } from '../../../ddo/ServiceAgreementTemplate' import { ServiceAgreementTemplate, ServiceAgreementTemplateCondition } from '../../../ddo/ServiceAgreementTemplate'
import { zeroX } from "../../../utils" import { zeroX } from "../../../utils"
@ -49,9 +50,10 @@ export abstract class AgreementTemplate extends ContractBase {
const keeper = await Keeper.getInstance() const keeper = await Keeper.getInstance()
return (await this.getConditionTypes()) return (await this.getConditionTypes())
.map(address => keeper.getConditionByAddress(address)) .map(address => keeper.getConditionByAddress(address))
} }
abstract async getServiceAgreementTemplateValuesMap(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}>
abstract getServiceAgreementTemplate(): Promise<ServiceAgreementTemplate> abstract getServiceAgreementTemplate(): Promise<ServiceAgreementTemplate>
public async getServiceAgreementTemplateConditions(): Promise<ServiceAgreementTemplateCondition[]> { public async getServiceAgreementTemplateConditions(): Promise<ServiceAgreementTemplateCondition[]> {

View File

@ -1,7 +1,9 @@
import { AgreementTemplate } from "./AgreementTemplate.abstract" import { AgreementTemplate } from "./AgreementTemplate.abstract"
import { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from '../conditions' import { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from '../conditions'
import DIDRegistry from '../DIDRegistry' import DIDRegistry from '../DIDRegistry'
import { generateId } from '../../../utils/GeneratorHelpers' import Keeper from "../../Keeper"
import { DDO } from '../../../ddo/DDO'
import { generateId, zeroX } from '../../../utils'
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from "./EscrowAccessSecretStoreTemplate.serviceAgreementTemplate" import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from "./EscrowAccessSecretStoreTemplate.serviceAgreementTemplate"
@ -62,7 +64,7 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
const lockRewardCondition = await LockRewardCondition.getInstance() const lockRewardCondition = await LockRewardCondition.getInstance()
const escrowReward = await EscrowReward.getInstance() const escrowReward = await EscrowReward.getInstance()
const agreementId = `0x${generateId()}` const agreementId = zeroX(generateId())
const publisher = await didRegistry.getDIDOwner(did) const publisher = await didRegistry.getDIDOwner(did)
const conditionIdAccess = await accessSecretStoreCondition.generateIdHash(agreementId, did, from) const conditionIdAccess = await accessSecretStoreCondition.generateIdHash(agreementId, did, from)
@ -87,4 +89,33 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
return agreementId return agreementId
} }
async getServiceAgreementTemplateValuesMap(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> {
const keeper = await Keeper.getInstance()
const ddoOwner = ddo.proof && ddo.proof.creator
const amount = ddo.findServiceByType("Metadata").metadata.base.price
let lockCondition
let releaseCondition
try {
lockCondition = await keeper.conditions.lockRewardCondition.hashValues(ddoOwner, amount)
} catch(e) { }
try {
releaseCondition = await keeper.conditions.accessSecretStoreCondition.hashValues(ddo.shortId(), consumer)
} catch(e) { }
return {
rewardAddress: ddoOwner,
amount: amount.toString(),
documentId: ddo.shortId(),
grantee: consumer,
receiver: consumer,
sender: ddoOwner,
lockCondition,
releaseCondition,
}
}
} }

View File

@ -3,7 +3,10 @@ import BrizoProvider from "../brizo/BrizoProvider"
import { generateId } from "../utils/GeneratorHelpers" import { generateId } from "../utils/GeneratorHelpers"
import Account from "./Account" import Account from "./Account"
import DID from "./DID" import DID from "./DID"
import { DDO } from "../ddo/DDO"
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
import { Keeper } from "../keeper/Keeper"
import { zeroX, didPrefixed } from "../utils"
import OceanAgreementsConditions from "./OceanAgreementsConditions" import OceanAgreementsConditions from "./OceanAgreementsConditions"
@ -54,46 +57,22 @@ export default class OceanAgreements {
serviceDefinitionId: string, serviceDefinitionId: string,
consumer: Account, consumer: Account,
): Promise<AgreementPreparionResult> { ): Promise<AgreementPreparionResult> {
const keeper = await Keeper.getInstance()
const d: DID = DID.parse(did as string) const d: DID = DID.parse(did as string)
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
const agreementId: string = generateId() const agreementId: string = generateId()
const valuesMap = await this.getValuesMapForPrepare(ddo, agreementId, consumer.getId()) const templateName = ddo.findServiceByType("Access").serviceAgreementTemplate.contractName
const valuesMap = await keeper
.getTemplateByName(templateName)
.getServiceAgreementTemplateValuesMap(ddo, agreementId, consumer.getId())
const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, valuesMap, consumer) const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, valuesMap, consumer)
return {agreementId, signature} return {agreementId, signature}
} }
// TODO: this map depends on the template, this generation should be reponsability of the template
private async getValuesMapForPrepare(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> {
const keeper = await Keeper.getInstance()
const ddoOwner = ddo.proof && ddo.proof.creator
const amount = ddo.findServiceByType("Metadata").metadata.base.price
let lockCondition
let releaseCondition
try {
lockCondition = await keeper.conditions.lockRewardCondition.hashValues(ddoOwner, amount)
} catch(e) { }
try {
releaseCondition = await keeper.conditions.accessSecretStoreCondition.hashValues(ddo.shortId(), consumer)
} catch(e) { }
return {
rewardAddress: ddoOwner,
amount: amount.toString(),
documentId: ddo.shortId(),
grantee: consumer,
receiver: consumer,
sender: ddoOwner,
lockCondition,
releaseCondition,
}
}
/** /**
* Submit a service agreement to the publisher to create the agreement on-chain. * Submit a service agreement to the publisher to create the agreement on-chain.
* @param {string} did Decentralized ID. * @param {string} did Decentralized ID.
@ -147,16 +126,19 @@ export default class OceanAgreements {
const d: DID = DID.parse(did) const d: DID = DID.parse(did)
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
const serviceAgreement: ServiceAgreement = await ServiceAgreement // TODO: Use Keeper 0.7+
.executeServiceAgreement(
d,
ddo,
agreementId,
serviceDefinitionId,
signature,
consumer,
publisher)
return serviceAgreement // const serviceAgreement: ServiceAgreement = await ServiceAgreement
// .executeServiceAgreement(
// d,
// ddo,
// agreementId,
// serviceDefinitionId,
// signature,
// consumer,
// publisher)
// return serviceAgreement
return undefined
} }
} }

View File

@ -13,11 +13,11 @@ function fillParameterWithDDO(parameter: ServiceAgreementTemplateParameter, ddo:
return ddo.shortId() return ddo.shortId()
} }
return null return ""
} }
const value = getValue(parameter.name.replace(/^_/, "")) const value = getValue(parameter.name.replace(/^_/, ""))
return {...parameter, value} return {...parameter, value: parameter.type.includes("int") ? Number(value) : value}
} }
/** /**