diff --git a/src/examples/BuyAsset.ts b/src/examples/BuyAsset.ts new file mode 100644 index 0000000..643985f --- /dev/null +++ b/src/examples/BuyAsset.ts @@ -0,0 +1,78 @@ +import DDO from "../ddo/DDO" +import MetaData from "../ddo/MetaData" +import MetaDataBase from "../ddo/MetaDataBase" +import Service from "../ddo/Service" +import {Account, Logger, Ocean, ServiceAgreement} from "../squid" + +(async () => { + const ocean: Ocean = await Ocean.getInstance({ + nodeUri: "http://localhost:8545", + aquariusUri: "http://localhost:5000", + brizoUri: "http://localhost:8030", + parityUri: "http://localhost:9545", + secretStoreUri: "http://localhost:12001", + threshold: 0, + password: "unittest", + address: "0xed243adfb84a6626eba46178ccb567481c6e655d", + }) + + const publisher: Account = (await ocean.getAccounts())[0] + const consumer: Account = (await ocean.getAccounts())[1] + + const metaData = new MetaData({ + base: { + name: "Office Humidity", + type: "dataset", + description: "Weather information of UK including temperature and humidity", + size: "3.1gb", + dateCreated: "2012-02-01T10:55:11+00:00", + author: "Met Office", + license: "CC-BY", + copyrightHolder: "Met Office", + encoding: "UTF-8", + compression: "zip", + contentType: "text/csv", + // tslint:disable-next-line + workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68", + contentUrls: [ + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + ], + links: [ + {sample1: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/"}, + {sample2: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/"}, + {fieldsDescription: "http://data.ceda.ac.uk/badc/ukcp09/"}, + ], + inLanguage: "en", + tags: "weather, uk, 2011, temperature, humidity", + price: 10, + } as MetaDataBase, + } as MetaData) + + const ddo: DDO = await ocean.registerAsset(metaData, publisher) + Logger.log("did", ddo.id) + const assetId = ddo.id.replace("did:op:", "") + + const accessService = ddo.findServiceByType("Access") + + const serviceAgreementSignatureResult: any = await ocean.signServiceAgreement(ddo.id, + accessService.serviceDefinitionId, consumer) + Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId) + Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature) + + const service: Service = ddo.findServiceByType("Access") + + const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement( + ddo.id, + service.serviceDefinitionId, + serviceAgreementSignatureResult.serviceAgreementId, + serviceAgreementSignatureResult.serviceAgreementSignature, + consumer, + publisher) + Logger.log("ServiceAgreement Id:", serviceAgreement.getId()) + + await consumer.requestTokens(metaData.base.price) + + const paid = await serviceAgreement.buyAsset(assetId, metaData.base.price, consumer) + Logger.log(`Asset paid: ${paid}`) +})() diff --git a/src/examples/GrantAccess.ts b/src/examples/GrantAccess.ts new file mode 100644 index 0000000..269fa2d --- /dev/null +++ b/src/examples/GrantAccess.ts @@ -0,0 +1,81 @@ +import DDO from "../ddo/DDO" +import MetaData from "../ddo/MetaData" +import MetaDataBase from "../ddo/MetaDataBase" +import Service from "../ddo/Service" +import {Account, Logger, Ocean, ServiceAgreement} from "../squid" + +(async () => { + const ocean: Ocean = await Ocean.getInstance({ + nodeUri: "http://localhost:8545", + aquariusUri: "http://localhost:5000", + brizoUri: "http://localhost:8030", + parityUri: "http://localhost:9545", + secretStoreUri: "http://localhost:12001", + threshold: 0, + password: "unittest", + address: "0xed243adfb84a6626eba46178ccb567481c6e655d", + }) + + const publisher: Account = (await ocean.getAccounts())[0] + const consumer: Account = (await ocean.getAccounts())[1] + + const metaData = new MetaData({ + base: { + name: "Office Humidity", + type: "dataset", + description: "Weather information of UK including temperature and humidity", + size: "3.1gb", + dateCreated: "2012-02-01T10:55:11+00:00", + author: "Met Office", + license: "CC-BY", + copyrightHolder: "Met Office", + encoding: "UTF-8", + compression: "zip", + contentType: "text/csv", + // tslint:disable-next-line + workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68", + contentUrls: [ + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + ], + links: [ + {sample1: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/"}, + {sample2: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/"}, + {fieldsDescription: "http://data.ceda.ac.uk/badc/ukcp09/"}, + ], + inLanguage: "en", + tags: "weather, uk, 2011, temperature, humidity", + price: 10, + } as MetaDataBase, + } as MetaData) + + const ddo: DDO = await ocean.registerAsset(metaData, publisher) + Logger.log("did", ddo.id) + const assetId = ddo.id.replace("did:op:", "") + + const accessService = ddo.findServiceByType("Access") + + const serviceAgreementSignatureResult: any = await ocean.signServiceAgreement(ddo.id, + accessService.serviceDefinitionId, consumer) + Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId) + Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature) + + const service: Service = ddo.findServiceByType("Access") + + const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement( + ddo.id, + service.serviceDefinitionId, + serviceAgreementSignatureResult.serviceAgreementId, + serviceAgreementSignatureResult.serviceAgreementSignature, + consumer, + publisher) + Logger.log("ServiceAgreement Id:", serviceAgreement.getId()) + + await consumer.requestTokens(metaData.base.price) + + const paid = await serviceAgreement.buyAsset(assetId, metaData.base.price, consumer) + Logger.log(`Asset paid: ${paid}`) + + const accessGranted = await serviceAgreement.grantAccess(assetId, assetId) + Logger.log(`Asset access granted: ${accessGranted}`) +})() diff --git a/src/examples/fire_lazers.sh b/src/examples/fire_lazers.sh index f521131..2ace01f 100755 --- a/src/examples/fire_lazers.sh +++ b/src/examples/fire_lazers.sh @@ -7,3 +7,5 @@ npm run run src/examples/RegisterAsset.ts npm run run src/examples/Search.ts npm run run src/examples/SignAgreement.ts npm run run src/examples/ExecuteAgreement.ts +npm run run src/examples/BuyAsset.ts +npm run run src/examples/GrantAccess.ts diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 4784da7..924e882 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -62,12 +62,12 @@ export default class Ocean { const aquarius = AquariusProvider.getAquarius() const brizo = BrizoProvider.getBrizo() - const id: string = IdGenerator.generateId() - const did: string = `did:op:${id}` + const assetId: string = IdGenerator.generateId() + const did: string = `did:op:${assetId}` const serviceDefinitionId: string = IdGenerator.generatePrefixedId() metadata.base.contentUrls = - await SecretStoreProvider.getSecretStore().encryptDocument(id, metadata.base.contentUrls) + await SecretStoreProvider.getSecretStore().encryptDocument(assetId, metadata.base.contentUrls) const template = new Access() const serviceAgreementTemplate = new ServiceAgreementTemplate(template) @@ -98,9 +98,9 @@ export default class Ocean { case "price": return metadata.base.price case "assetId": - return "0x" + id + return "0x" + assetId case "documentKeyId": - return "0x1234" + return "0x" + assetId } return null @@ -182,7 +182,7 @@ export default class Ocean { const storedDdo = await aquarius.storeDDO(ddo) await didRegistry.registerAttribute( - id, + assetId, ValueType.DID, "Metadata", serviceEndpoint, diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index 0f0302b..dba4bc6 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -205,6 +205,7 @@ export default class ServiceAgreement extends OceanBase { super(serviceAgreementId) } + // todo: sync with samer public async buyAsset(assetId: string, price: number, consumer: Account): Promise { const {paymentConditions, token} = await Keeper.getInstance()