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

improved way that is exported Service type

This commit is contained in:
Pedro Gutiérrez 2019-02-07 12:10:25 +01:00 committed by Pedro Gutiérrez
parent 3e50ded2e5
commit f5ab0e6d05
9 changed files with 22 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, ServiceMetadata } from "./Service"
import { Service } from "./Service"
/**
* DID Descriptor Object.
@ -73,14 +73,12 @@ export class DDO {
* @param {string} serviceType Service type.
* @return {Service} Service.
*/
public findServiceByType(serviceType: string): Service {
public findServiceByType<T extends string>(serviceType: T): Service<T> {
if (!serviceType) {
throw new Error("serviceType not set")
}
const service: Service = this.service.find((s) => s.type === serviceType)
return service
return <Service<T>>this.service.find((s) => s.type === serviceType)
}
/**
@ -89,7 +87,7 @@ export class DDO {
*/
public getChecksum(): string {
const web3 = Web3Provider.getWeb3()
const {metadata} = <ServiceMetadata>this.findServiceByType("Metadata")
const {metadata} = this.findServiceByType("Metadata")
const {files, name, author, license} = metadata.base
const values = [
@ -128,7 +126,7 @@ export class DDO {
* Generated and adds the checksum.
*/
public addChecksum(): void {
const metadataService = <ServiceMetadata>this.findServiceByType("Metadata")
const metadataService = this.findServiceByType("Metadata")
if (metadataService.metadata.base.checksum) {
Logger.log("Checksum already exists")
return

View File

@ -20,11 +20,14 @@ export interface ServiceMetadata extends ServiceCommon {
export interface ServiceBase extends ServiceCommon {
templateId?: string
serviceEndpoint: string
purchaseEndpoint?: string
description?: string
serviceAgreementContract?: Contract
conditions?: Condition[]
}
export type Service = ServiceBase | ServiceAuthorization | ServiceMetadata
export type Service<T extends string = "default"> =
T extends "Authorization" ? ServiceAuthorization :
T extends "Metadata" ? ServiceMetadata :
T extends "default" ? ServiceBase :
ServiceBase

View File

@ -1,6 +1,5 @@
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import DID from "../ocean/DID"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
@ -62,7 +61,7 @@ async function exec() {
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
const service: Service = ddo.findServiceByType("Access")
const service = ddo.findServiceByType("Access")
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(
ddo.id,

View File

@ -1,6 +1,5 @@
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
import {runner} from "./runner"
@ -60,7 +59,7 @@ async function exec() {
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
const service: Service = ddo.findServiceByType("Access")
const service = ddo.findServiceByType("Access")
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(
ddo.id,

View File

@ -1,6 +1,5 @@
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import DID from "../ocean/DID"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
@ -60,7 +59,7 @@ async function exec() {
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
const service: Service = ddo.findServiceByType("Access")
const service = ddo.findServiceByType("Access")
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(
ddo.id,

View File

@ -1,6 +1,5 @@
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import {Account, Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
@ -62,7 +61,7 @@ async function exec() {
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
const service: Service = ddo.findServiceByType("Access")
const service = ddo.findServiceByType("Access")
try {
await ocean

View File

@ -1,6 +1,5 @@
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import {Account, Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
@ -69,7 +68,7 @@ async function exec() {
const ddo: DDO = await ocean.registerAsset(metaData, publisher)
Logger.log("did", ddo.id)
const accessService: Service = ddo.findServiceByType("Access")
const accessService = ddo.findServiceByType("Access")
await consumer.requestTokens(metaData.base.price)

View File

@ -88,7 +88,7 @@ export default class OceanAssets {
publicKeyBase58: await publisher.getPublicKey(),
},
],
service: [
service: <Service[]>[
{
type: template.templateName,
purchaseEndpoint: brizo.getPurchaseEndpoint(),
@ -138,7 +138,6 @@ export default class OceanAssets {
// Cleaning not needed information
base: {
...metadata.base,
contentUrls: [],
files: undefined,
} as any,
},
@ -185,8 +184,8 @@ export default class OceanAssets {
await ServiceAgreement.signServiceAgreement(
ddo, serviceDefinitionId, serviceAgreementId, consumer)
const accessService: Service = ddo.findServiceByType("Access")
const metadataService: Service = ddo.findServiceByType("Metadata")
const accessService = ddo.findServiceByType("Access")
const metadataService = ddo.findServiceByType("Metadata")
const price = metadataService.metadata.base.price
const balance = await consumer.getOceanBalance()

View File

@ -22,7 +22,7 @@ let publisherAccount: Account
let consumerAccount: Account
let accessService: Service
let metaDataService: Service
let metaDataService: Service<"Metadata">
const did: DID = DID.generate()
@ -49,12 +49,12 @@ describe("ServiceAgreement", () => {
serviceDefinitionId: "0",
templateId: serviceAgreementTemplate.getId(),
conditions,
} as Service
}
metaDataService = {
type: "MetaData",
type: "Metadata",
metadata,
} as Service
}
})
describe("#signServiceAgreement()", () => {