mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
enhanced did handling
This commit is contained in:
parent
a0648b4b84
commit
6ea480fd47
@ -1,6 +1,7 @@
|
||||
import {URL} from "whatwg-url"
|
||||
import DDO from "../ddo/DDO"
|
||||
import Config from "../models/Config"
|
||||
import DID from "../ocean/DID"
|
||||
import Logger from "../utils/Logger"
|
||||
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
|
||||
import SearchQuery from "./query/SearchQuery"
|
||||
@ -114,8 +115,8 @@ export default class Aquarius {
|
||||
return result
|
||||
}
|
||||
|
||||
public async retrieveDDO(did: string): Promise<DDO> {
|
||||
const fullUrl = `${this.url}${apiPath}/${did}`
|
||||
public async retrieveDDO(did: DID): Promise<DDO> {
|
||||
const fullUrl = `${this.url}${apiPath}/${did.getDid()}`
|
||||
const result = await WebServiceConnectorProvider.getConnector()
|
||||
.get(fullUrl)
|
||||
.then((response: any) => {
|
||||
@ -136,7 +137,7 @@ export default class Aquarius {
|
||||
return result
|
||||
}
|
||||
|
||||
public getServiceEndpoint(did) {
|
||||
return `${this.url}/api/v1/aquarius/assets/metadata/${did}`
|
||||
public getServiceEndpoint(did: DID) {
|
||||
return `${this.url}/api/v1/aquarius/assets/metadata/${did.getId()}`
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ import DDO from "../ddo/DDO"
|
||||
import MetaData from "../ddo/MetaData"
|
||||
import MetaDataBase from "../ddo/MetaDataBase"
|
||||
import Service from "../ddo/Service"
|
||||
import DID from "../ocean/DID"
|
||||
import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
|
||||
import config from "./config"
|
||||
|
||||
@ -43,7 +44,7 @@ import config from "./config"
|
||||
|
||||
const ddo: DDO = await ocean.registerAsset(metaData, publisher)
|
||||
Logger.log("did", ddo.id)
|
||||
const assetId = ddo.id.replace("did:op:", "")
|
||||
const did: DID = DID.parse(ddo.id)
|
||||
|
||||
const accessService = ddo.findServiceByType("Access")
|
||||
|
||||
@ -65,6 +66,6 @@ import config from "./config"
|
||||
publisher)
|
||||
Logger.log("ServiceAgreement Id:", serviceAgreement.getId())
|
||||
|
||||
const paid = await serviceAgreement.payAsset(assetId, metaData.base.price, consumer)
|
||||
const paid = await serviceAgreement.payAsset(did.getId(), metaData.base.price, consumer)
|
||||
Logger.log(`Asset paid: ${paid}`)
|
||||
})()
|
||||
|
@ -2,6 +2,7 @@ import DDO from "../ddo/DDO"
|
||||
import MetaData from "../ddo/MetaData"
|
||||
import MetaDataBase from "../ddo/MetaDataBase"
|
||||
import Service from "../ddo/Service"
|
||||
import DID from "../ocean/DID"
|
||||
import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
|
||||
import config from "./config"
|
||||
|
||||
@ -43,7 +44,7 @@ import config from "./config"
|
||||
|
||||
const ddo: DDO = await ocean.registerAsset(metaData, publisher)
|
||||
Logger.log("did", ddo.id)
|
||||
const assetId = ddo.id.replace("did:op:", "")
|
||||
const did: DID = DID.parse(ddo.id)
|
||||
|
||||
const accessService = ddo.findServiceByType("Access")
|
||||
|
||||
@ -65,9 +66,9 @@ import config from "./config"
|
||||
|
||||
await consumer.requestTokens(metaData.base.price)
|
||||
|
||||
const paid = await serviceAgreement.payAsset(assetId, metaData.base.price, consumer)
|
||||
const paid = await serviceAgreement.payAsset(did.getId(), metaData.base.price, consumer)
|
||||
Logger.log(`Asset paid: ${paid}`)
|
||||
|
||||
const accessGranted = await serviceAgreement.grantAccess(assetId, assetId, publisher)
|
||||
const accessGranted = await serviceAgreement.grantAccess(did.getId(), did.getId(), publisher)
|
||||
Logger.log(`Asset access granted: ${accessGranted}`)
|
||||
})()
|
||||
|
@ -1,5 +1,6 @@
|
||||
import {Receipt} from "web3-utils"
|
||||
import MethodReflection from "../../models/MethodReflection"
|
||||
import DID from "../../ocean/DID"
|
||||
import ContractBase from "./ContractBase"
|
||||
|
||||
export default class ServiceAgreement extends ContractBase {
|
||||
@ -44,12 +45,12 @@ export default class ServiceAgreement extends ContractBase {
|
||||
|
||||
public async executeAgreement(serviceAgreementTemplateId: string, serviceAgreementSignatureHash: string,
|
||||
consumerAddress: string, valueHashes: string[], timeoutValues: number[],
|
||||
serviceAgreementId: string, did: string, publisherAddress: string):
|
||||
serviceAgreementId: string, did: DID, publisherAddress: string):
|
||||
Promise<Receipt> {
|
||||
|
||||
return this.send("executeAgreement", publisherAddress, [
|
||||
serviceAgreementTemplateId, serviceAgreementSignatureHash, consumerAddress, valueHashes,
|
||||
timeoutValues, "0x" + serviceAgreementId, "0x" + did.replace("did:op:", ""),
|
||||
timeoutValues, "0x" + serviceAgreementId, "0x" + did.getId(),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
41
src/ocean/DID.ts
Normal file
41
src/ocean/DID.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import IdGenerator from "./IdGenerator"
|
||||
|
||||
const prefix = "did:op:"
|
||||
|
||||
export default class DID {
|
||||
|
||||
public static parse(didString: string): DID {
|
||||
let did: DID
|
||||
if (didString.startsWith(prefix)) {
|
||||
const id = didString.split(prefix)[1]
|
||||
if (!id.startsWith("0x")) {
|
||||
did = new DID(id)
|
||||
}
|
||||
}
|
||||
|
||||
if (!did) {
|
||||
throw new Error(`Parsing DID failed, ${didString}`)
|
||||
}
|
||||
|
||||
return did
|
||||
}
|
||||
|
||||
public static generate(): DID {
|
||||
return new DID(IdGenerator.generateId())
|
||||
}
|
||||
|
||||
private id: string
|
||||
|
||||
private constructor(id: string) {
|
||||
|
||||
this.id = id
|
||||
}
|
||||
|
||||
public getDid(): string {
|
||||
return `${prefix}${this.id}`
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.id
|
||||
}
|
||||
}
|
@ -18,14 +18,15 @@ import Config from "../models/Config"
|
||||
import ValueType from "../models/ValueType"
|
||||
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
|
||||
import Logger from "../utils/Logger"
|
||||
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
|
||||
import Account from "./Account"
|
||||
import DID from "./DID"
|
||||
import IdGenerator from "./IdGenerator"
|
||||
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
||||
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
|
||||
import Access from "./ServiceAgreements/Templates/Access"
|
||||
|
||||
import EventListener from "../keeper/EventListener"
|
||||
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
|
||||
|
||||
export default class Ocean {
|
||||
|
||||
@ -55,9 +56,10 @@ export default class Ocean {
|
||||
return ethAccounts.map((address: string) => new Account(address))
|
||||
}
|
||||
|
||||
public async resolveDID(did): Promise<DDO> {
|
||||
public async resolveDID(did: string): Promise<DDO> {
|
||||
|
||||
return AquariusProvider.getAquarius().retrieveDDO(did)
|
||||
const d: DID = DID.parse(did)
|
||||
return AquariusProvider.getAquarius().retrieveDDO(d)
|
||||
}
|
||||
|
||||
public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> {
|
||||
@ -66,19 +68,19 @@ export default class Ocean {
|
||||
const aquarius = AquariusProvider.getAquarius()
|
||||
const brizo = BrizoProvider.getBrizo()
|
||||
|
||||
const assetId: string = IdGenerator.generateId()
|
||||
const did: string = `did:op:${assetId}`
|
||||
const did: DID = DID.generate()
|
||||
const accessServiceDefinitionId: string = "0"
|
||||
const computeServiceDefintionId: string = "1"
|
||||
const metadataServiceDefinitionId: string = "2"
|
||||
|
||||
metadata.base.contentUrls =
|
||||
[await SecretStoreProvider.getSecretStore().encryptDocument(assetId, metadata.base.contentUrls)]
|
||||
[await SecretStoreProvider.getSecretStore()
|
||||
.encryptDocument(did.getId(), metadata.base.contentUrls)]
|
||||
|
||||
const template = new Access()
|
||||
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
||||
|
||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, assetId)
|
||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, did.getId())
|
||||
|
||||
const serviceEndpoint = aquarius.getServiceEndpoint(did)
|
||||
|
||||
@ -86,14 +88,14 @@ export default class Ocean {
|
||||
const ddo: DDO = new DDO({
|
||||
authentication: [{
|
||||
type: "RsaSignatureAuthentication2018",
|
||||
publicKey: did + "#keys-1",
|
||||
publicKey: did.getDid() + "#keys-1",
|
||||
} as Authentication],
|
||||
id: did,
|
||||
id: did.getDid(),
|
||||
publicKey: [
|
||||
{
|
||||
id: did + "#keys-1",
|
||||
id: did.getDid() + "#keys-1",
|
||||
type: "Ed25519VerificationKey2018",
|
||||
owner: did,
|
||||
owner: did.getDid(),
|
||||
publicKeyBase58: await publisher.getPublicKey(),
|
||||
} as PublicKey,
|
||||
],
|
||||
@ -143,7 +145,7 @@ export default class Ocean {
|
||||
// Logger.log(JSON.stringify(storedDdo, null, 2))
|
||||
|
||||
await didRegistry.registerAttribute(
|
||||
assetId,
|
||||
did.getId(),
|
||||
ValueType.URL,
|
||||
"Metadata",
|
||||
serviceEndpoint,
|
||||
@ -156,12 +158,12 @@ export default class Ocean {
|
||||
serviceDefinitionId: string,
|
||||
consumer: Account): Promise<any> {
|
||||
|
||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||
const id = did.replace("did:op:", "")
|
||||
const d: DID = DID.parse(did as string)
|
||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
try {
|
||||
const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(id,
|
||||
const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(
|
||||
ddo, serviceDefinitionId, serviceAgreementId, consumer)
|
||||
|
||||
const accessService: Service = ddo.findServiceByType("Access")
|
||||
@ -183,7 +185,7 @@ export default class Ocean {
|
||||
|
||||
const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.serviceAgreementId)
|
||||
await sa.payAsset(
|
||||
id,
|
||||
d.getId(),
|
||||
metadataService.metadata.base.price,
|
||||
consumer,
|
||||
)
|
||||
@ -207,8 +209,8 @@ export default class Ocean {
|
||||
cb,
|
||||
consumer: Account) {
|
||||
|
||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||
const id = did.replace("did:op:", "")
|
||||
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")
|
||||
@ -222,7 +224,7 @@ export default class Ocean {
|
||||
const webConnector = WebServiceConnectorProvider.getConnector()
|
||||
const contentUrls = await SecretStoreProvider
|
||||
.getSecretStore()
|
||||
.decryptDocument(id, metadataService.metadata.base.contentUrls[0])
|
||||
.decryptDocument(d.getId(), metadataService.metadata.base.contentUrls[0])
|
||||
const serviceUrl: string = accessService.serviceEndpoint
|
||||
Logger.log("Consuming asset files using service url: ", serviceUrl)
|
||||
const files = []
|
||||
@ -261,12 +263,12 @@ export default class Ocean {
|
||||
consumer: Account,
|
||||
publisher: Account): Promise<ServiceAgreement> {
|
||||
|
||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
|
||||
const id = did.replace("did:op:", "")
|
||||
const d: DID = DID.parse(did)
|
||||
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
|
||||
|
||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement
|
||||
.executeServiceAgreement(
|
||||
id,
|
||||
d,
|
||||
ddo,
|
||||
serviceDefinitionId,
|
||||
serviceAgreementId,
|
||||
|
@ -7,12 +7,12 @@ import Web3Provider from "../../keeper/Web3Provider"
|
||||
import ValuePair from "../../models/ValuePair"
|
||||
import Logger from "../../utils/Logger"
|
||||
import Account from "../Account"
|
||||
import DID from "../DID"
|
||||
import OceanBase from "../OceanBase"
|
||||
|
||||
export default class ServiceAgreement extends OceanBase {
|
||||
|
||||
public static async signServiceAgreement(assetId: string,
|
||||
ddo: DDO,
|
||||
public static async signServiceAgreement(ddo: DDO,
|
||||
serviceDefinitionId: string,
|
||||
serviceAgreementId: string,
|
||||
consumer: Account): Promise<string> {
|
||||
@ -36,7 +36,7 @@ export default class ServiceAgreement extends OceanBase {
|
||||
return serviceAgreementHashSignature
|
||||
}
|
||||
|
||||
public static async executeServiceAgreement(assetId: string,
|
||||
public static async executeServiceAgreement(did: DID,
|
||||
ddo: DDO,
|
||||
serviceDefinitionId: string,
|
||||
serviceAgreementId: string,
|
||||
@ -54,7 +54,7 @@ export default class ServiceAgreement extends OceanBase {
|
||||
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
|
||||
|
||||
// todo get consumer from ddo
|
||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo,
|
||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(did, ddo,
|
||||
serviceDefinitionId, serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature,
|
||||
consumer.getId(), publisher)
|
||||
|
||||
@ -92,7 +92,8 @@ export default class ServiceAgreement extends OceanBase {
|
||||
return serviceAgreementHashSignature
|
||||
}
|
||||
|
||||
private static async executeAgreement(ddo: DDO,
|
||||
private static async executeAgreement(did: DID,
|
||||
ddo: DDO,
|
||||
serviceDefinitionId: string,
|
||||
serviceAgreementId: string,
|
||||
valueHashes: string[],
|
||||
@ -123,7 +124,7 @@ export default class ServiceAgreement extends OceanBase {
|
||||
valueHashes,
|
||||
timeoutValues,
|
||||
serviceAgreementId,
|
||||
ddo.id,
|
||||
did,
|
||||
publisher.getId())
|
||||
|
||||
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {
|
||||
|
@ -2,7 +2,7 @@ import * as assert from "assert"
|
||||
import Aquarius from "../../src/aquarius/Aquarius"
|
||||
import SearchQuery from "../../src/aquarius/query/SearchQuery"
|
||||
import DDO from "../../src/ddo/DDO"
|
||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||
import DID from "../../src/ocean/DID"
|
||||
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
|
||||
import config from "../config"
|
||||
import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
|
||||
@ -85,9 +85,9 @@ describe("Aquarius", () => {
|
||||
|
||||
it("should store a ddo", async () => {
|
||||
|
||||
const did: string = `did:op:${IdGenerator.generateId()}`
|
||||
const did: DID = DID.generate()
|
||||
const ddo: DDO = new DDO({
|
||||
id: did,
|
||||
id: did.getId(),
|
||||
})
|
||||
|
||||
// @ts-ignore
|
||||
@ -103,9 +103,9 @@ describe("Aquarius", () => {
|
||||
|
||||
it("should store a ddo", async () => {
|
||||
|
||||
const did: string = `did:op:${IdGenerator.generateId()}`
|
||||
const did: DID = DID.generate()
|
||||
const ddo: DDO = new DDO({
|
||||
id: did,
|
||||
id: did.getId(),
|
||||
})
|
||||
|
||||
// @ts-ignore
|
||||
@ -114,12 +114,12 @@ describe("Aquarius", () => {
|
||||
const storageResult: DDO = await aquarius.storeDDO(ddo)
|
||||
assert(storageResult)
|
||||
|
||||
assert(storageResult.id === did)
|
||||
assert(storageResult.id === did.getId())
|
||||
|
||||
const restrieveResult: DDO = await aquarius.retrieveDDO(did)
|
||||
assert(restrieveResult)
|
||||
|
||||
assert(restrieveResult.id === did)
|
||||
assert(restrieveResult.id === did.getId())
|
||||
assert(restrieveResult.id === storageResult.id)
|
||||
})
|
||||
})
|
||||
|
@ -1,5 +1,6 @@
|
||||
import Aquarius from "../../src/aquarius/Aquarius"
|
||||
import DDO from "../../src/ddo/DDO"
|
||||
import DID from "../../src/ocean/DID"
|
||||
|
||||
const ddoStore: Map<string, any> = new Map<string, any>()
|
||||
|
||||
@ -14,7 +15,7 @@ export default class AquariusMock extends Aquarius {
|
||||
return ddo
|
||||
}
|
||||
|
||||
public async retrieveDDO(did: string): Promise<DDO> {
|
||||
return ddoStore.get(did)
|
||||
public async retrieveDDO(did: DID): Promise<DDO> {
|
||||
return ddoStore.get(did.getDid())
|
||||
}
|
||||
}
|
||||
|
66
test/ocean/DID.test.ts
Normal file
66
test/ocean/DID.test.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import * as assert from "assert"
|
||||
import DID from "../../src/ocean/DID"
|
||||
|
||||
describe("DID", () => {
|
||||
|
||||
describe("#generate()", () => {
|
||||
it("should generate a new did", () => {
|
||||
const did: DID = DID.generate()
|
||||
assert(did)
|
||||
})
|
||||
})
|
||||
|
||||
describe("#parse()", () => {
|
||||
it("should parse a valid did", () => {
|
||||
|
||||
const id = "1234"
|
||||
const did: DID = DID.parse(`did:op:${id}`)
|
||||
assert(did)
|
||||
|
||||
assert(did.getId() === id, did.getId())
|
||||
})
|
||||
|
||||
it("should throw if prefix does not match", (done) => {
|
||||
|
||||
const id = "1234"
|
||||
try {
|
||||
const did: DID = DID.parse(`did:xxx:${id}`)
|
||||
assert(!did)
|
||||
} catch {
|
||||
done()
|
||||
}
|
||||
})
|
||||
|
||||
it("should throw if id does not match", (done) => {
|
||||
|
||||
const id = "0x1234"
|
||||
try {
|
||||
const did: DID = DID.parse(`did:op:${id}`)
|
||||
assert(!did)
|
||||
} catch {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getDid()", () => {
|
||||
it("should return the full did", () => {
|
||||
|
||||
const did: DID = DID.generate()
|
||||
assert(did)
|
||||
|
||||
assert(did.getDid().startsWith("did:op:"))
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getDid()", () => {
|
||||
it("should return only the id part of the did", () => {
|
||||
|
||||
const id = "1234"
|
||||
const did: DID = DID.parse(`did:op:${id}`)
|
||||
assert(did)
|
||||
|
||||
assert(did.getId() === id)
|
||||
})
|
||||
})
|
||||
})
|
@ -5,6 +5,7 @@ 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 DID from "../../src/ocean/DID"
|
||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||
import Ocean from "../../src/ocean/Ocean"
|
||||
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
||||
@ -23,7 +24,7 @@ let consumerAccount: Account
|
||||
let accessService: Service
|
||||
let metaDataService: Service
|
||||
|
||||
const assetId: string = IdGenerator.generateId()
|
||||
const did: DID = DID.generate()
|
||||
|
||||
describe("ServiceAgreement", () => {
|
||||
|
||||
@ -40,7 +41,7 @@ describe("ServiceAgreement", () => {
|
||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||
new ServiceAgreementTemplate(new Access())
|
||||
|
||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, assetId)
|
||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, did.getId())
|
||||
|
||||
accessService = {
|
||||
type: "Access",
|
||||
@ -58,15 +59,14 @@ describe("ServiceAgreement", () => {
|
||||
describe("#signServiceAgreement()", () => {
|
||||
it("should sign an service agreement", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
|
||||
assert(serviceAgreementSignature)
|
||||
@ -78,43 +78,41 @@ describe("ServiceAgreement", () => {
|
||||
describe("#executeServiceAgreement()", () => {
|
||||
it("should execute an service agreement", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
||||
assert(serviceAgreement)
|
||||
|
||||
const serviceDefinitionId = serviceAgreement.getId()
|
||||
assert(serviceDefinitionId)
|
||||
assert(serviceDefinitionId !== did)
|
||||
assert(serviceDefinitionId !== did.getId())
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getStatus()", () => {
|
||||
it("should get the status of a newly created service agreement", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
assert(serviceAgreementSignature)
|
||||
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
||||
assert(serviceAgreement)
|
||||
|
||||
@ -126,27 +124,26 @@ describe("ServiceAgreement", () => {
|
||||
describe("#payAsset()", () => {
|
||||
it("should lock the payment in that service agreement", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService, metaDataService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService, metaDataService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
assert(serviceAgreementSignature)
|
||||
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
||||
assert(serviceAgreement)
|
||||
|
||||
// get funds
|
||||
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
||||
|
||||
const paid: boolean = await serviceAgreement.payAsset(assetId, metaDataService.metadata.base.price,
|
||||
const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price,
|
||||
consumerAccount)
|
||||
assert(paid)
|
||||
})
|
||||
@ -155,54 +152,54 @@ describe("ServiceAgreement", () => {
|
||||
describe("#grantAccess()", () => {
|
||||
it("should grant access in that service agreement", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
assert(serviceAgreementSignature)
|
||||
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
||||
assert(serviceAgreement)
|
||||
|
||||
// get funds
|
||||
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
||||
|
||||
const paid: boolean = await serviceAgreement.payAsset(assetId, metaDataService.metadata.base.price,
|
||||
const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price,
|
||||
consumerAccount)
|
||||
assert(paid)
|
||||
|
||||
// todo: use document id
|
||||
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId, publisherAccount)
|
||||
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), did.getId(),
|
||||
publisherAccount)
|
||||
assert(accessGranted)
|
||||
})
|
||||
|
||||
it("should fail to grant grant access if there is no payment", async () => {
|
||||
|
||||
const did: string = `did:op:${assetId}`
|
||||
const ddo = new DDO({id: did, service: [accessService]})
|
||||
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
||||
const serviceAgreementId: string = IdGenerator.generateId()
|
||||
|
||||
// @ts-ignore
|
||||
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
||||
const serviceAgreementSignature: string =
|
||||
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, consumerAccount)
|
||||
assert(serviceAgreementSignature)
|
||||
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
||||
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
||||
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
||||
assert(serviceAgreement)
|
||||
|
||||
// todo: use document id
|
||||
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId, publisherAccount)
|
||||
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), did.getId(),
|
||||
publisherAccount)
|
||||
assert(!accessGranted)
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user