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 service: Service[]
// @ts-ignore
private assa: string
public constructor(ddo?: {
id?: string,
publicKey?: any[],
@ -34,4 +31,30 @@ export default class DDO {
this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : []
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 names: string[] = result.map((res: DDO): string => {
const service = res.service.find((srv) => srv.type === "Metadata")
const names: string[] = result.map((ddo: DDO): string => {
const service = ddo.findServiceByType("Metadata")
if (service && service.metadata) {
return service.metadata.base.name
}

View File

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

View File

@ -1,5 +1,6 @@
import Condition from "../../ddo/Condition"
import DDO from "../../ddo/DDO"
import Service from "../../ddo/Service"
import Keeper from "../../keeper/Keeper"
import Web3Provider from "../../keeper/Web3Provider"
import ValuePair from "../../models/ValuePair"
@ -8,46 +9,49 @@ import OceanBase from "../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> {
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
const valueHashes = ServiceAgreement.createValueHashes(values)
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)
return serviceAgreementHashSignature
}
public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string,
serviceAgreementHashSignature: string, consumer: Account,
publisher: Account): Promise<ServiceAgreement> {
public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string,
serviceAgreementId: string, serviceAgreementHashSignature: string,
consumer: Account, publisher: Account): Promise<ServiceAgreement> {
const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId)
const valueHashes = ServiceAgreement.createValueHashes(values)
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo)
const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo,
serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature, consumer, publisher)
serviceDefinitionId, serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature,
consumer, publisher)
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):
Promise<string> {
if (!ddo.service[0].templateId) {
if (!service.templateId) {
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
})
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId,
const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(service.templateId,
serviceAgreementId, conditionKeys, valueHashes, timeoutValues)
const serviceAgreementHashSignature =
@ -56,18 +60,21 @@ export default class ServiceAgreement extends OceanBase {
return serviceAgreementHashSignature
}
private static async executeAgreement(ddo: DDO, serviceAgreementId: string, valueHashes: string[],
timeoutValues: number[], serviceAgreementHashSignature: string,
consumer: Account, publisher: Account): Promise<ServiceAgreement> {
private static async executeAgreement(ddo: DDO, serviceDefinitionId: string, serviceAgreementId: string,
valueHashes: string[], timeoutValues: number[],
serviceAgreementHashSignature: string, consumer: Account, publisher: Account)
: Promise<ServiceAgreement> {
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.")
}
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())
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {

View File

@ -2,6 +2,7 @@ import Config from "../src/models/Config"
export default {
aquariusUri: "http://localhost:5000",
brizoUri: "http://localhost:3000",
nodeUri: "http://localhost:8545",
parityUri: "http://localhost:9545",
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 DDO from "../../src/ddo/DDO"
import MetaData from "../../src/ddo/MetaData"
import Service from "../../src/ddo/Service"
import Account from "../../src/ocean/Account"
import IdGenerator from "../../src/ocean/IdGenerator"
import Ocean from "../../src/ocean/Ocean"
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
@ -111,15 +111,18 @@ describe("Ocean", () => {
const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher)
const service: Service = ddo.findServiceByType("Access")
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId,
consumer)
const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer)
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 ddo: DDO = await ocean.registerAsset(new MetaData(), publisher)
const service: Service = ddo.findServiceByType("Access")
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId,
consumer)
const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer)
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, serviceAgreementId,
serviceAgreementSignature, consumer, publisher)
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id,
service.serviceDefinitionId, signServiceAgreementResult.serviceAgreementId,
signServiceAgreementResult.serviceAgreementSignature, consumer, publisher)
assert(serviceAgreement)
})

View File

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