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

improved Service typing

This commit is contained in:
Pedro Gutiérrez 2019-02-15 19:36:30 +01:00 committed by Pedro Gutiérrez
parent 0a0786a835
commit 407aba7450
5 changed files with 38 additions and 27 deletions

View File

@ -4,7 +4,7 @@ import * as signatureHelpers from "../utils/SignatureHelpers"
import { Authentication } from "./Authentication" import { Authentication } from "./Authentication"
import { Proof } from "./Proof" import { Proof } from "./Proof"
import { PublicKey } from "./PublicKey" import { PublicKey } from "./PublicKey"
import { Service } from "./Service" import { Service, ServiceType } from "./Service"
/** /**
* DID Descriptor Object. * DID Descriptor Object.
@ -46,7 +46,7 @@ export class DDO {
public proof: Proof public proof: Proof
public constructor(ddo?: Partial<DDO>) { public constructor(ddo?: Partial<DDO>) {
this.created = (ddo && ddo.created) || new Date(Date.now()).toISOString().replace(/\.[0-9]{3}/, "") this.created = (ddo && ddo.created) || new Date().toISOString().replace(/\.[0-9]{3}/, "")
this.authentication = (ddo && ddo.authentication) || [] this.authentication = (ddo && ddo.authentication) || []
this.id = (ddo && ddo.id) || null this.id = (ddo && ddo.id) || null
this.publicKey = (ddo && ddo.publicKey) || [] this.publicKey = (ddo && ddo.publicKey) || []
@ -58,14 +58,14 @@ export class DDO {
* @param {string} serviceDefinitionId Service ID. * @param {string} serviceDefinitionId Service ID.
* @return {Service} Service. * @return {Service} Service.
*/ */
public findServiceById(serviceDefinitionId: string): Service { public findServiceById<T extends ServiceType>(serviceDefinitionId: string): Service<T> {
if (!serviceDefinitionId) { if (!serviceDefinitionId) {
throw new Error("serviceDefinitionId not set") throw new Error("serviceDefinitionId not set")
} }
const service: Service = this.service.find((s) => s.serviceDefinitionId === serviceDefinitionId) const service = this.service.find((s) => s.serviceDefinitionId === serviceDefinitionId)
return service return service as Service<T>
} }
/** /**
@ -73,7 +73,7 @@ export class DDO {
* @param {string} serviceType Service type. * @param {string} serviceType Service type.
* @return {Service} Service. * @return {Service} Service.
*/ */
public findServiceByType<T extends string>(serviceType: T): Service<T> { public findServiceByType<T extends ServiceType>(serviceType: T): Service<T> {
if (!serviceType) { if (!serviceType) {
throw new Error("serviceType not set") throw new Error("serviceType not set")
} }

View File

@ -2,10 +2,12 @@ import { Condition } from "./Condition"
import { Contract } from "./Contract" import { Contract } from "./Contract"
import { MetaData } from "./MetaData" import { MetaData } from "./MetaData"
export type ServiceType = "Authorization" | "Metadata" | "Access" | "Compute" | "FitchainCompute"
export interface ServiceCommon { export interface ServiceCommon {
type: string type: ServiceType
serviceEndpoint?: string
serviceDefinitionId?: string serviceDefinitionId?: string
serviceEndpoint?: string
} }
export interface ServiceAuthorization extends ServiceCommon { export interface ServiceAuthorization extends ServiceCommon {
@ -18,7 +20,8 @@ export interface ServiceMetadata extends ServiceCommon {
metadata: MetaData metadata: MetaData
} }
export interface ServiceBase extends ServiceCommon { export interface ServiceAccess extends ServiceCommon {
type: "Access"
templateId?: string templateId?: string
purchaseEndpoint?: string purchaseEndpoint?: string
description?: string description?: string
@ -26,8 +29,14 @@ export interface ServiceBase extends ServiceCommon {
conditions?: Condition[] conditions?: Condition[]
} }
export type Service<T extends string = "default"> = export interface ServiceCompute extends ServiceCommon {
templateId?: string
}
export type Service<T extends ServiceType | "default" = "default"> =
T extends "Authorization" ? ServiceAuthorization : T extends "Authorization" ? ServiceAuthorization :
T extends "Metadata" ? ServiceMetadata : T extends "Metadata" ? ServiceMetadata :
T extends "default" ? ServiceBase : T extends "Access" ? ServiceAccess :
ServiceBase T extends "Compute" ? ServiceCompute :
T extends "default" ? ServiceCommon :
ServiceCommon

View File

@ -170,8 +170,8 @@ export default class Ocean {
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 accessService: Service = ddo.findServiceByType("Access") const accessService = ddo.findServiceByType("Access")
const metadataService: Service = ddo.findServiceByType("Metadata") const metadataService = ddo.findServiceByType("Metadata")
const accessEvent: ContractEvent = EventListener.subscribe( const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName, accessService.conditions[1].contractName,

View File

@ -1,7 +1,7 @@
import ConfigProvider from "../../ConfigProvider" import ConfigProvider from "../../ConfigProvider"
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 { ServiceAccess } 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"
@ -23,7 +23,7 @@ export default class ServiceAgreement extends OceanBase {
Logger.log("Signing SA with serviceAgreementId", serviceAgreementId) Logger.log("Signing SA with serviceAgreementId", serviceAgreementId)
} }
const service: Service = ddo.findServiceById(serviceDefinitionId) const service = ddo.findServiceById<"Access">(serviceDefinitionId)
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId) const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
const valueHashes: string[] = ServiceAgreement.createValueHashes(values) const valueHashes: string[] = ServiceAgreement.createValueHashes(values)
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
@ -52,7 +52,7 @@ export default class ServiceAgreement extends OceanBase {
Logger.log("Executing SA with serviceAgreementId", serviceAgreementId) Logger.log("Executing SA with serviceAgreementId", serviceAgreementId)
} }
const service: Service = ddo.findServiceById(serviceDefinitionId) const service = ddo.findServiceById<"Access">(serviceDefinitionId)
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId) const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
const valueHashes: string[] = ServiceAgreement.createValueHashes(values) const valueHashes: string[] = ServiceAgreement.createValueHashes(values)
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
@ -66,7 +66,7 @@ export default class ServiceAgreement extends OceanBase {
} }
private static async createSAHashSignature( private static async createSAHashSignature(
service: Service, service: ServiceAccess,
serviceAgreementId: string, serviceAgreementId: string,
valueHashes: string[], valueHashes: string[],
timeoutValues: number[], timeoutValues: number[],
@ -118,7 +118,7 @@ export default class ServiceAgreement extends OceanBase {
const {serviceAgreement} = await Keeper.getInstance() const {serviceAgreement} = await Keeper.getInstance()
const service: Service = ddo.findServiceById(serviceDefinitionId) const service = ddo.findServiceById<"Access">(serviceDefinitionId)
if (!service.templateId) { if (!service.templateId) {
throw new Error(`TemplateId not found in service "${service.type}" ddo.`) throw new Error(`TemplateId not found in service "${service.type}" ddo.`)
@ -177,11 +177,14 @@ export default class ServiceAgreement extends OceanBase {
return hash return hash
} }
private static hashServiceAgreement(serviceAgreementTemplateId: string, private static hashServiceAgreement(
serviceAgreementTemplateId: string,
serviceAgreementId: string, serviceAgreementId: string,
conditionKeys: string[], conditionKeys: string[],
valueHashes: string[], valueHashes: string[],
timeouts: number[]): string { timeouts: number[],
): string {
const args = [ const args = [
{type: "bytes32", value: serviceAgreementTemplateId} as ValuePair, {type: "bytes32", value: serviceAgreementTemplateId} as ValuePair,
{type: "bytes32[]", value: conditionKeys} as ValuePair, {type: "bytes32[]", value: conditionKeys} as ValuePair,
@ -193,7 +196,7 @@ export default class ServiceAgreement extends OceanBase {
return Web3Provider.getWeb3().utils.soliditySha3(...args).toString("hex") return Web3Provider.getWeb3().utils.soliditySha3(...args).toString("hex")
} }
private static getTimeoutValuesFromService(service: Service): number[] { private static getTimeoutValuesFromService(service: ServiceAccess): number[] {
const timeoutValues: number[] = service.conditions.map((condition: Condition) => { const timeoutValues: number[] = service.conditions.map((condition: Condition) => {
return condition.timeout return condition.timeout
}) })
@ -201,7 +204,7 @@ export default class ServiceAgreement extends OceanBase {
return timeoutValues return timeoutValues
} }
private static getValuesFromService(service: Service, serviceAgreementId: string): ValuePair[][] { private static getValuesFromService(service: ServiceAccess, serviceAgreementId: string): ValuePair[][] {
const values: ValuePair[][] = [] const values: ValuePair[][] = []

View File

@ -7,7 +7,6 @@
"es7" "es7"
], ],
"declaration": true, "declaration": true,
"noUnusedLocals": true,
"module": "commonjs", "module": "commonjs",
"noImplicitAny": false, "noImplicitAny": false,
"removeComments": true, "removeComments": true,