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 { Authentication } from "./Authentication"
import { Proof } from "./Proof" import { Proof } from "./Proof"
import { PublicKey } from "./PublicKey" import { PublicKey } from "./PublicKey"
import { Service, ServiceMetadata } from "./Service" import { Service } from "./Service"
/** /**
* DID Descriptor Object. * DID Descriptor Object.
@ -73,14 +73,12 @@ export class DDO {
* @param {string} serviceType Service type. * @param {string} serviceType Service type.
* @return {Service} Service. * @return {Service} Service.
*/ */
public findServiceByType(serviceType: string): Service { public findServiceByType<T extends string>(serviceType: T): Service<T> {
if (!serviceType) { if (!serviceType) {
throw new Error("serviceType not set") throw new Error("serviceType not set")
} }
const service: Service = this.service.find((s) => s.type === serviceType) return <Service<T>>this.service.find((s) => s.type === serviceType)
return service
} }
/** /**
@ -89,7 +87,7 @@ export class DDO {
*/ */
public getChecksum(): string { public getChecksum(): string {
const web3 = Web3Provider.getWeb3() const web3 = Web3Provider.getWeb3()
const {metadata} = <ServiceMetadata>this.findServiceByType("Metadata") const {metadata} = this.findServiceByType("Metadata")
const {files, name, author, license} = metadata.base const {files, name, author, license} = metadata.base
const values = [ const values = [
@ -128,7 +126,7 @@ export class DDO {
* Generated and adds the checksum. * Generated and adds the checksum.
*/ */
public addChecksum(): void { public addChecksum(): void {
const metadataService = <ServiceMetadata>this.findServiceByType("Metadata") const metadataService = this.findServiceByType("Metadata")
if (metadataService.metadata.base.checksum) { if (metadataService.metadata.base.checksum) {
Logger.log("Checksum already exists") Logger.log("Checksum already exists")
return return

View File

@ -20,11 +20,14 @@ export interface ServiceMetadata extends ServiceCommon {
export interface ServiceBase extends ServiceCommon { export interface ServiceBase extends ServiceCommon {
templateId?: string templateId?: string
serviceEndpoint: string
purchaseEndpoint?: string purchaseEndpoint?: string
description?: string description?: string
serviceAgreementContract?: Contract serviceAgreementContract?: Contract
conditions?: Condition[] 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 { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData" import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import DID from "../ocean/DID" import DID from "../ocean/DID"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid" import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config" import config from "./config"
@ -62,7 +61,7 @@ async function exec() {
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId) Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature) Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
const service: Service = ddo.findServiceByType("Access") const service = ddo.findServiceByType("Access")
const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement( const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(
ddo.id, ddo.id,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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