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 { Proof } from "./Proof"
import { PublicKey } from "./PublicKey"
import { Service } from "./Service"
import { Service, ServiceType } from "./Service"
/**
* DID Descriptor Object.
@ -46,7 +46,7 @@ export class DDO {
public proof: Proof
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.id = (ddo && ddo.id) || null
this.publicKey = (ddo && ddo.publicKey) || []
@ -58,14 +58,14 @@ export class DDO {
* @param {string} serviceDefinitionId Service ID.
* @return {Service} Service.
*/
public findServiceById(serviceDefinitionId: string): Service {
public findServiceById<T extends ServiceType>(serviceDefinitionId: string): Service<T> {
if (!serviceDefinitionId) {
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.
* @return {Service} Service.
*/
public findServiceByType<T extends string>(serviceType: T): Service<T> {
public findServiceByType<T extends ServiceType>(serviceType: T): Service<T> {
if (!serviceType) {
throw new Error("serviceType not set")
}

View File

@ -2,10 +2,12 @@ import { Condition } from "./Condition"
import { Contract } from "./Contract"
import { MetaData } from "./MetaData"
export type ServiceType = "Authorization" | "Metadata" | "Access" | "Compute" | "FitchainCompute"
export interface ServiceCommon {
type: string
serviceEndpoint?: string
type: ServiceType
serviceDefinitionId?: string
serviceEndpoint?: string
}
export interface ServiceAuthorization extends ServiceCommon {
@ -18,7 +20,8 @@ export interface ServiceMetadata extends ServiceCommon {
metadata: MetaData
}
export interface ServiceBase extends ServiceCommon {
export interface ServiceAccess extends ServiceCommon {
type: "Access"
templateId?: string
purchaseEndpoint?: string
description?: string
@ -26,8 +29,14 @@ export interface ServiceBase extends ServiceCommon {
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 "Metadata" ? ServiceMetadata :
T extends "default" ? ServiceBase :
ServiceBase
T extends "Access" ? ServiceAccess :
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 ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
const accessService: Service = ddo.findServiceByType("Access")
const metadataService: Service = ddo.findServiceByType("Metadata")
const accessService = ddo.findServiceByType("Access")
const metadataService = ddo.findServiceByType("Metadata")
const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName,

View File

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

View File

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