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

adapted changes from squid discussion

This commit is contained in:
Sebastian Gerske 2018-11-14 15:44:25 +01:00
parent 92aa4047eb
commit ba4e2d4656
7 changed files with 102 additions and 60 deletions

View File

@ -20,9 +20,6 @@ export default class DDO {
public authentication: Authentication[] public authentication: Authentication[]
public service: Service[] public service: Service[]
// @ts-ignore
private assa: string
public constructor(ddo?: { public constructor(ddo?: {
id?: string, id?: string,
publicKey?: any[], publicKey?: any[],
@ -34,4 +31,30 @@ export default class DDO {
this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : [] this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : []
this.service = ddo ? ddo.service ? ddo.service : [] : [] this.service = ddo ? ddo.service ? ddo.service : [] : []
} }
public findServiceById(serviceDefinitionId: string): Service {
if (!serviceDefinitionId) {
throw new Error("serviceDefinitionId not set")
}
const service: Service = this.service.find((s) => {
return s.serviceDefinitionId === serviceDefinitionId
})
return service
}
public findServiceByType(serviceType: string): Service {
if (!serviceType) {
throw new Error("serviceType not set")
}
const service: Service = this.service.find((s) => {
return s.type === serviceType
})
return service
}
} }

View File

@ -14,8 +14,8 @@ import {Logger, Ocean} from "../squid"
}) })
const result: DDO[] = await ocean.searchAssetsByText("Office Humidity") const result: DDO[] = await ocean.searchAssetsByText("Office Humidity")
const names: string[] = result.map((res: DDO): string => { const names: string[] = result.map((ddo: DDO): string => {
const service = res.service.find((srv) => srv.type === "Metadata") const service = ddo.findServiceByType("Metadata")
if (service && service.metadata) { if (service && service.metadata) {
return service.metadata.base.name return service.metadata.base.name
} }

View File

@ -140,29 +140,34 @@ export default class Ocean {
return storedDdo return storedDdo
} }
public async signServiceAgreement(did: string, serviceAgreementId: string, consumer: Account): Promise<string> { public async signServiceAgreement(did: string, serviceDefinitionId: string, consumer: Account): Promise<any> {
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
const id = did.replace("did:op:", "") const id = did.replace("did:op:", "")
const serviceAgreementId: string = IdGenerator.generateId()
try { try {
const serviceAgrementSignature: string = await ServiceAgreement.signServiceAgreement(id, const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(id,
ddo, serviceAgreementId, consumer) ddo, serviceDefinitionId, serviceAgreementId, consumer)
return serviceAgrementSignature return {
serviceAgreementId,
serviceAgreementSignature,
}
} catch (err) { } catch (err) {
Logger.error("Signing ServiceAgreement failed!", err) Logger.error("Signing ServiceAgreement failed!", err)
} }
} }
public async executeServiceAgreement(did: string, serviceAgreementId: string, serviceAgreementSignature: string, public async executeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string,
consumer: Account, publisher: Account): Promise<ServiceAgreement> { serviceAgreementSignature: string, consumer: Account, publisher: Account)
: Promise<ServiceAgreement> {
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
const id = did.replace("did:op:", "") const id = did.replace("did:op:", "")
const serviceAgrement: ServiceAgreement = await ServiceAgreement.executeServiceAgreement(id, const serviceAgrement: ServiceAgreement = await ServiceAgreement.executeServiceAgreement(id,
ddo, serviceAgreementId, serviceAgreementSignature, consumer, publisher) ddo, serviceDefinitionId, serviceAgreementId, serviceAgreementSignature, consumer, publisher)
return serviceAgrement return serviceAgrement
} }

View File

@ -1,5 +1,6 @@
import Condition from "../../ddo/Condition" import Condition from "../../ddo/Condition"
import DDO from "../../ddo/DDO" import DDO from "../../ddo/DDO"
import Service from "../../ddo/Service"
import Keeper from "../../keeper/Keeper" import Keeper from "../../keeper/Keeper"
import Web3Provider from "../../keeper/Web3Provider" import Web3Provider from "../../keeper/Web3Provider"
import ValuePair from "../../models/ValuePair" import ValuePair from "../../models/ValuePair"
@ -8,46 +9,49 @@ import OceanBase from "../OceanBase"
export default class ServiceAgreement extends OceanBase { export default class ServiceAgreement extends OceanBase {
public static async signServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account): public static async signServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string,
serviceAgreementId: string, consumer: Account):
Promise<string> { Promise<string> {
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
const valueHashes = ServiceAgreement.createValueHashes(values) const valueHashes = ServiceAgreement.createValueHashes(values)
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
const service: Service = ddo.findServiceById(serviceDefinitionId)
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId, const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId,
values, valueHashes, timeoutValues, consumer) values, valueHashes, timeoutValues, consumer)
return serviceAgreementHashSignature return serviceAgreementHashSignature
} }
public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string,
serviceAgreementHashSignature: string, consumer: Account, serviceAgreementId: string, serviceAgreementHashSignature: string,
publisher: Account): Promise<ServiceAgreement> { consumer: Account, publisher: Account): Promise<ServiceAgreement> {
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
const valueHashes = ServiceAgreement.createValueHashes(values) const valueHashes = ServiceAgreement.createValueHashes(values)
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo, const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo,
serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature, consumer, publisher) serviceDefinitionId, serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature,
consumer, publisher)
return serviceAgreement return serviceAgreement
} }
private static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, values: ValuePair[], private static async createSAHashSignature(service: Service, serviceAgreementId: string, values: ValuePair[],
valueHashes: string[], timeoutValues: number[], consumer: Account): valueHashes: string[], timeoutValues: number[], consumer: Account):
Promise<string> { Promise<string> {
if (!ddo.service[0].templateId) { if (!service.templateId) {
throw new Error("TemplateId not found in ddo.") throw new Error("TemplateId not found in ddo.")
} }
const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => { const conditionKeys: string[] = service.conditions.map((condition) => {
return condition.conditionKey return condition.conditionKey
}) })
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId, const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(service.templateId,
serviceAgreementId, conditionKeys, valueHashes, timeoutValues) serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
const serviceAgreementHashSignature = const serviceAgreementHashSignature =
@ -56,18 +60,21 @@ export default class ServiceAgreement extends OceanBase {
return serviceAgreementHashSignature return serviceAgreementHashSignature
} }
private static async executeAgreement(ddo: DDO, serviceAgreementId: string, valueHashes: string[], private static async executeAgreement(ddo: DDO, serviceDefinitionId: string, serviceAgreementId: string,
timeoutValues: number[], serviceAgreementHashSignature: string, valueHashes: string[], timeoutValues: number[],
consumer: Account, publisher: Account): Promise<ServiceAgreement> { serviceAgreementHashSignature: string, consumer: Account, publisher: Account)
: Promise<ServiceAgreement> {
const {serviceAgreement} = await Keeper.getInstance() const {serviceAgreement} = await Keeper.getInstance()
if (!ddo.service[0].templateId) { const service: Service = ddo.findServiceById(serviceDefinitionId)
if (!service.templateId) {
throw new Error("TemplateId not found in ddo.") throw new Error("TemplateId not found in ddo.")
} }
const executeAgreementReceipt = await serviceAgreement.executeAgreement( const executeAgreementReceipt = await serviceAgreement.executeAgreement(
ddo.service[0].templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes, service.templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes,
timeoutValues, serviceAgreementId, ddo.id, publisher.getId()) timeoutValues, serviceAgreementId, ddo.id, publisher.getId())
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) { if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {

View File

@ -2,6 +2,7 @@ import Config from "../src/models/Config"
export default { export default {
aquariusUri: "http://localhost:5000", aquariusUri: "http://localhost:5000",
brizoUri: "http://localhost:3000",
nodeUri: "http://localhost:8545", nodeUri: "http://localhost:8545",
parityUri: "http://localhost:9545", parityUri: "http://localhost:9545",
secretStoreUri: "https://secret-store.dev-ocean.com", secretStoreUri: "https://secret-store.dev-ocean.com",

View File

@ -5,8 +5,8 @@ import SearchQuery from "../../src/aquarius/query/SearchQuery"
import ConfigProvider from "../../src/ConfigProvider" import ConfigProvider from "../../src/ConfigProvider"
import DDO from "../../src/ddo/DDO" import DDO from "../../src/ddo/DDO"
import MetaData from "../../src/ddo/MetaData" import MetaData from "../../src/ddo/MetaData"
import Service from "../../src/ddo/Service"
import Account from "../../src/ocean/Account" import Account from "../../src/ocean/Account"
import IdGenerator from "../../src/ocean/IdGenerator"
import Ocean from "../../src/ocean/Ocean" import Ocean from "../../src/ocean/Ocean"
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider" import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
@ -111,15 +111,18 @@ describe("Ocean", () => {
const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher) const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher)
const service: Service = ddo.findServiceByType("Access")
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id,
const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId, service.serviceDefinitionId, consumer)
consumer)
assert(serviceAgreementSignature) assert(serviceAgreementSignature)
assert(serviceAgreementSignature.startsWith("0x")) assert(serviceAgreementSignature.serviceAgreementId)
assert(serviceAgreementSignature.serviceAgreementSignature)
assert(serviceAgreementSignature.serviceAgreementSignature.startsWith("0x"))
}) })
}) })
@ -132,16 +135,17 @@ describe("Ocean", () => {
const consumer = accounts[1] const consumer = accounts[1]
const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher) const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher)
const service: Service = ddo.findServiceByType("Access")
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementId: string = IdGenerator.generateId() const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id,
const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId, service.serviceDefinitionId, consumer)
consumer)
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, serviceAgreementId, const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id,
serviceAgreementSignature, consumer, publisher) service.serviceDefinitionId, signServiceAgreementResult.serviceAgreementId,
signServiceAgreementResult.serviceAgreementSignature, consumer, publisher)
assert(serviceAgreement) assert(serviceAgreement)
}) })

View File

@ -21,7 +21,7 @@ let accounts: Account[]
let publisherAccount: Account let publisherAccount: Account
let consumerAccount: Account let consumerAccount: Account
let serviceDefinition let service: Service
describe("ServiceAgreement", () => { describe("ServiceAgreement", () => {
@ -55,14 +55,12 @@ describe("ServiceAgreement", () => {
} as DDOCondition } as DDOCondition
}) })
serviceDefinition = [ service = {
{ type: "Access",
serviceDefinitionId: IdGenerator.generateId(), serviceDefinitionId: IdGenerator.generateId(),
templateId: serviceAgreementTemplate.getId(), templateId: serviceAgreementTemplate.getId(),
conditions: ddoConditions, conditions: ddoConditions,
} as Service, } as Service
]
}) })
describe("#signServiceAgreement()", () => { describe("#signServiceAgreement()", () => {
@ -70,14 +68,15 @@ describe("ServiceAgreement", () => {
const id: string = IdGenerator.generateId() const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}` const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefinition}) const ddo = new DDO({id: did, service: [service]})
const assetId: string = IdGenerator.generateId() const assetId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
assert(serviceAgreementSignature) assert(serviceAgreementSignature)
assert(serviceAgreementSignature.startsWith("0x")) assert(serviceAgreementSignature.startsWith("0x"))
@ -89,18 +88,19 @@ describe("ServiceAgreement", () => {
const id: string = IdGenerator.generateId() const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}` const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefinition}) const ddo = new DDO({id: did, service: [service]})
const assetId: string = IdGenerator.generateId() const assetId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
const serviceAgreement: ServiceAgreement = const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementSignature, consumerAccount, publisherAccount) serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement) assert(serviceAgreement)
const serviceDefinitionId = serviceAgreement.getId() const serviceDefinitionId = serviceAgreement.getId()
@ -114,19 +114,20 @@ describe("ServiceAgreement", () => {
const id: string = IdGenerator.generateId() const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}` const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefinition}) const ddo = new DDO({id: did, service: [service]})
const assetId: string = IdGenerator.generateId() const assetId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
assert(serviceAgreementSignature) assert(serviceAgreementSignature)
const serviceAgreement: ServiceAgreement = const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementSignature, consumerAccount, publisherAccount) serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement) assert(serviceAgreement)
const status = await serviceAgreement.getStatus() const status = await serviceAgreement.getStatus()
@ -139,19 +140,20 @@ describe("ServiceAgreement", () => {
const id: string = IdGenerator.generateId() const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}` const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefinition}) const ddo = new DDO({id: did, service: [service]})
const assetId: string = IdGenerator.generateId() const assetId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
assert(serviceAgreementSignature) assert(serviceAgreementSignature)
const serviceAgreement: ServiceAgreement = const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementSignature, consumerAccount, publisherAccount) serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement) assert(serviceAgreement)
const fulfilled: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId()) const fulfilled: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId())