mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added buy asset and grant access samples, prepare for consumption
This commit is contained in:
parent
47f245bbed
commit
8d018633a9
78
src/examples/BuyAsset.ts
Normal file
78
src/examples/BuyAsset.ts
Normal file
@ -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}`)
|
||||||
|
})()
|
81
src/examples/GrantAccess.ts
Normal file
81
src/examples/GrantAccess.ts
Normal file
@ -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}`)
|
||||||
|
})()
|
@ -7,3 +7,5 @@ npm run run src/examples/RegisterAsset.ts
|
|||||||
npm run run src/examples/Search.ts
|
npm run run src/examples/Search.ts
|
||||||
npm run run src/examples/SignAgreement.ts
|
npm run run src/examples/SignAgreement.ts
|
||||||
npm run run src/examples/ExecuteAgreement.ts
|
npm run run src/examples/ExecuteAgreement.ts
|
||||||
|
npm run run src/examples/BuyAsset.ts
|
||||||
|
npm run run src/examples/GrantAccess.ts
|
||||||
|
@ -62,12 +62,12 @@ export default class Ocean {
|
|||||||
const aquarius = AquariusProvider.getAquarius()
|
const aquarius = AquariusProvider.getAquarius()
|
||||||
const brizo = BrizoProvider.getBrizo()
|
const brizo = BrizoProvider.getBrizo()
|
||||||
|
|
||||||
const id: string = IdGenerator.generateId()
|
const assetId: string = IdGenerator.generateId()
|
||||||
const did: string = `did:op:${id}`
|
const did: string = `did:op:${assetId}`
|
||||||
const serviceDefinitionId: string = IdGenerator.generatePrefixedId()
|
const serviceDefinitionId: string = IdGenerator.generatePrefixedId()
|
||||||
|
|
||||||
metadata.base.contentUrls =
|
metadata.base.contentUrls =
|
||||||
await SecretStoreProvider.getSecretStore().encryptDocument(id, metadata.base.contentUrls)
|
await SecretStoreProvider.getSecretStore().encryptDocument(assetId, metadata.base.contentUrls)
|
||||||
|
|
||||||
const template = new Access()
|
const template = new Access()
|
||||||
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
||||||
@ -98,9 +98,9 @@ export default class Ocean {
|
|||||||
case "price":
|
case "price":
|
||||||
return metadata.base.price
|
return metadata.base.price
|
||||||
case "assetId":
|
case "assetId":
|
||||||
return "0x" + id
|
return "0x" + assetId
|
||||||
case "documentKeyId":
|
case "documentKeyId":
|
||||||
return "0x1234"
|
return "0x" + assetId
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
@ -182,7 +182,7 @@ export default class Ocean {
|
|||||||
const storedDdo = await aquarius.storeDDO(ddo)
|
const storedDdo = await aquarius.storeDDO(ddo)
|
||||||
|
|
||||||
await didRegistry.registerAttribute(
|
await didRegistry.registerAttribute(
|
||||||
id,
|
assetId,
|
||||||
ValueType.DID,
|
ValueType.DID,
|
||||||
"Metadata",
|
"Metadata",
|
||||||
serviceEndpoint,
|
serviceEndpoint,
|
||||||
|
@ -205,6 +205,7 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
super(serviceAgreementId)
|
super(serviceAgreementId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: sync with samer
|
||||||
public async buyAsset(assetId: string, price: number, consumer: Account): Promise<boolean> {
|
public async buyAsset(assetId: string, price: number, consumer: Account): Promise<boolean> {
|
||||||
const {paymentConditions, token} = await Keeper.getInstance()
|
const {paymentConditions, token} = await Keeper.getInstance()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user