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

changed some methods

This commit is contained in:
Pedro Gutiérrez 2019-01-30 15:35:31 +01:00 committed by Pedro Gutiérrez
parent dd2b672adb
commit 5694ae29b5
7 changed files with 28 additions and 64 deletions

6
package-lock.json generated
View File

@ -130,6 +130,12 @@
"integrity": "sha512-Yo2/9MJC1vlWXGIaU+35naYHWWYGbtzTu2t5dW4Ro1JuIRkzxfM3TSNzPUAgsCrUBUxsCsu9pd/RjvPuE45SGw==",
"requires": {
"node-fetch": "^2.3.0"
},
"dependencies": {
"node-fetch": {
"version": "2.3.0",
"bundled": true
}
}
},
"@types/chai": {

View File

@ -39,12 +39,7 @@ export default class ServiceExecutionAgreement extends ContractBase {
return this.call("getTemplateOwner", [templateId])
}
public async getAgreementStatus(serviceDefinitionId: string) {
return this.call("getAgreementStatus", [serviceDefinitionId])
}
public async executeAgreement(
public async initializeAgreement(
serviceAgreementTemplateId: string,
serviceAgreementSignatureHash: string,
consumerAddress: string,
@ -54,8 +49,8 @@ export default class ServiceExecutionAgreement extends ContractBase {
did: DID,
publisherAddress: string,
): Promise<Receipt> {
console.log({serviceAgreementTemplateId, serviceAgreementSignatureHash, consumerAddress, valueHashes, timeoutValues, serviceAgreementId, did, publisherAddress})
return this.send("executeAgreement", publisherAddress, [
return this.send("initializeAgreement", publisherAddress, [
serviceAgreementTemplateId, serviceAgreementSignatureHash, consumerAddress, valueHashes,
timeoutValues, "0x" + serviceAgreementId, "0x" + did.getId(),
])

View File

@ -11,10 +11,10 @@ export default class AccessConditions extends ContractBase {
// todo add check permissions proxy
public async grantAccess(serviceAgreementId: any, assetId: any, documentKeyId: any, publisherAddress: string)
public async grantAccess(serviceAgreementId: any, documentKeyId: any, publisherAddress: string)
: Promise<Receipt> {
return this.send("grantAccess", publisherAddress, [
serviceAgreementId, "0x" + assetId, "0x" + documentKeyId,
serviceAgreementId, "0x" + documentKeyId,
])
}
}

View File

@ -108,11 +108,11 @@ export default class OceanAssets {
// the id of the service agreement template
templateId: serviceAgreementTemplate.getId(),
serviceAgreementContract: {
contractName: "ServiceAgreement",
contractName: "ServiceExecutionAgreement",
fulfillmentOperator: template.fulfillmentOperator,
events: [
{
name: "ExecuteAgreement",
name: "AgreementInitialized",
actorType: "consumer",
handler: {
moduleName: "payment",

View File

@ -62,11 +62,13 @@ export default class ServiceAgreement extends OceanBase {
return serviceAgreement
}
private static async createSAHashSignature(service: Service,
private static async createSAHashSignature(
service: Service,
serviceAgreementId: string,
valueHashes: string[],
timeoutValues: number[],
consumer: Account): Promise<string> {
consumer: Account,
): Promise<string> {
if (!service.templateId) {
throw new Error("TemplateId not found in ddo.")
@ -113,23 +115,20 @@ export default class ServiceAgreement extends OceanBase {
const {serviceAgreement} = await Keeper.getInstance()
console.log(1)
const service: Service = ddo.findServiceById(serviceDefinitionId)
if (!service.templateId) {
throw new Error(`TemplateId not found in service "${service.type}" ddo.`)
}
console.log(2)
const templateActive = await serviceAgreement.getTemplateStatus(service.templateId)
if (!templateActive) {
throw new Error(`Template with id ${service.templateId} is not active.`)
}
console.log(3)
const executeAgreementReceipt = await serviceAgreement
.executeAgreement(
.initializeAgreement(
service.templateId,
serviceAgreementHashSignature,
consumerAddress,
@ -140,13 +139,11 @@ export default class ServiceAgreement extends OceanBase {
publisher.getId(),
)
console.log(4)
if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) {
if (!executeAgreementReceipt.status) {
throw new Error("executing service agreement failed.")
}
return new ServiceAgreement(
executeAgreementReceipt.events.ExecuteAgreement.returnValues.serviceAgreementId,
executeAgreementReceipt.events.AgreementInitialized.returnValues.agreementId,
)
}
@ -237,17 +234,12 @@ export default class ServiceAgreement extends OceanBase {
return lockPaymentReceipt.status
}
public async grantAccess(assetId: string, documentId: string, publisher: Account): Promise<boolean> {
public async grantAccess(documentId: string, publisher: Account): Promise<boolean> {
const {accessConditions} = await Keeper.getInstance()
const grantAccessReceipt =
await accessConditions.grantAccess(this.getId(), assetId, documentId, publisher.getId())
await accessConditions.grantAccess(this.getId(), documentId, publisher.getId())
return !!grantAccessReceipt.events.AccessGranted
}
public async getStatus() {
const {serviceAgreement} = await Keeper.getInstance()
return serviceAgreement.getAgreementStatus(this.getId())
}
}

View File

@ -43,10 +43,6 @@ export default class Access extends TemplateBase {
methodName: "grantAccess",
timeout: 0,
parameters: [
{
name: "assetId",
type: "bytes32",
} as Parameter,
{
name: "documentKeyId",
type: "bytes32",

View File

@ -76,7 +76,7 @@ describe("ServiceAgreement", () => {
})
describe("#executeServiceAgreement()", () => {
it("should execute an service agreement", async () => {
it("should execute a service agreement", async () => {
const ddo = new DDO({id: did.getDid(), service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
@ -113,29 +113,6 @@ describe("ServiceAgreement", () => {
})
})
describe("#getStatus()", () => {
it("should get the status of a newly created service agreement", async () => {
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(ddo, accessService.serviceDefinitionId,
serviceAgreementId, consumerAccount)
assert(serviceAgreementSignature)
const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement)
const status = await serviceAgreement.getStatus()
assert(status === false)
})
})
describe("#payAsset()", () => {
it("should lock the payment in that service agreement", async () => {
@ -190,8 +167,7 @@ describe("ServiceAgreement", () => {
assert(paid)
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), did.getId(),
publisherAccount)
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), publisherAccount)
assert(accessGranted)
})
@ -213,8 +189,7 @@ describe("ServiceAgreement", () => {
assert(serviceAgreement)
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), did.getId(),
publisherAccount)
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), publisherAccount)
assert(!accessGranted)
})
})