From 8dcdf0a8512e15a1ebe9d275576969f3a2f534b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Thu, 14 Feb 2019 12:37:52 +0100 Subject: [PATCH] moved ID generator --- src/ocean/DID.ts | 4 ++-- src/ocean/IdGenerator.ts | 8 -------- src/ocean/OceanAgreements.ts | 4 ++-- src/squid.ts | 2 -- src/utils/GeneratorHelpers.ts | 9 ++++++++ test/TestIdGenerator.ts | 6 +++--- test/keeper/DIDRegistry.test.ts | 10 ++++----- test/ocean/IdGenerator.test.ts | 26 ----------------------- test/ocean/ServiceAgreement.test.ts | 23 ++++++++++++--------- test/utils/GeneratorHelpers.test.ts | 32 +++++++++++++++++++++++++++++ 10 files changed, 66 insertions(+), 58 deletions(-) delete mode 100644 src/ocean/IdGenerator.ts create mode 100644 src/utils/GeneratorHelpers.ts delete mode 100644 test/ocean/IdGenerator.test.ts create mode 100644 test/utils/GeneratorHelpers.test.ts diff --git a/src/ocean/DID.ts b/src/ocean/DID.ts index 65f6de9..654f41a 100644 --- a/src/ocean/DID.ts +++ b/src/ocean/DID.ts @@ -1,4 +1,4 @@ -import IdGenerator from "./IdGenerator" +import { generateId } from "../utils/GeneratorHelpers" const prefix = "did:op:" @@ -32,7 +32,7 @@ export default class DID { * @return {DID} */ public static generate(): DID { - return new DID(IdGenerator.generateId()) + return new DID(generateId()) } /** diff --git a/src/ocean/IdGenerator.ts b/src/ocean/IdGenerator.ts deleted file mode 100644 index d8d8b8a..0000000 --- a/src/ocean/IdGenerator.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {v4} from "uuid" - -export default class IdGenerator { - public static generateId(): string { - const id = `${v4()}${v4()}` - return id.replace(/-/g, "") - } -} diff --git a/src/ocean/OceanAgreements.ts b/src/ocean/OceanAgreements.ts index 674170c..cad58e7 100644 --- a/src/ocean/OceanAgreements.ts +++ b/src/ocean/OceanAgreements.ts @@ -1,8 +1,8 @@ import AquariusProvider from "../aquarius/AquariusProvider" import BrizoProvider from "../brizo/BrizoProvider" import Account from "./Account" +import { generateId } from "../utils/GeneratorHelpers" import DID from "./DID" -import IdGenerator from "./IdGenerator" import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" export interface AgreementPreparionResult { @@ -48,7 +48,7 @@ export default class OceanAgreements { const d: DID = DID.parse(did as string) const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) - const agreementId: string = IdGenerator.generateId() + const agreementId: string = generateId() const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, consumer) diff --git a/src/squid.ts b/src/squid.ts index 46a316a..bbe05fd 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -1,7 +1,6 @@ import Config from "./models/Config" import Account from "./ocean/Account" import DID from "./ocean/DID" -import IdGenerator from "./ocean/IdGenerator" import Ocean from "./ocean/Ocean" import ServiceAgreement from "./ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreementTemplate from "./ocean/ServiceAgreements/ServiceAgreementTemplate" @@ -26,7 +25,6 @@ export { Config, DID, EventListener, - IdGenerator, Logger, SecretStoreProvider, ServiceAgreement, diff --git a/src/utils/GeneratorHelpers.ts b/src/utils/GeneratorHelpers.ts new file mode 100644 index 0000000..cd92f8c --- /dev/null +++ b/src/utils/GeneratorHelpers.ts @@ -0,0 +1,9 @@ +import {v4} from "uuid" + +export function generateId(length = 64) { + let id = '' + while(id.length < length) { + id += v4().replace(/-/g, "") + } + return id.substr(0, length) +} diff --git a/test/TestIdGenerator.ts b/test/TestIdGenerator.ts index 7742b22..a8ac3a7 100644 --- a/test/TestIdGenerator.ts +++ b/test/TestIdGenerator.ts @@ -1,7 +1,7 @@ -import IdGenerator from "../src/ocean/IdGenerator" +import { generateId } from "../src/utils/GeneratorHelpers" -export default class TestIdGenerator extends IdGenerator { +export default class TestIdGenerator { public static generatePrefixedId() { - return "0x" + this.generateId() + return "0x" + generateId() } } diff --git a/test/keeper/DIDRegistry.test.ts b/test/keeper/DIDRegistry.test.ts index 98cd089..5244cf8 100644 --- a/test/keeper/DIDRegistry.test.ts +++ b/test/keeper/DIDRegistry.test.ts @@ -2,7 +2,7 @@ import {assert} from "chai" import ConfigProvider from "../../src/ConfigProvider" import DIDRegistry from "../../src/keeper/contracts/DIDRegistry" import Account from "../../src/ocean/Account" -import IdGenerator from "../../src/ocean/IdGenerator" +import { generateId } from "../../src/utils/GeneratorHelpers" import Ocean from "../../src/ocean/Ocean" import Logger from "../../src/utils/Logger" import config from "../config" @@ -24,7 +24,7 @@ describe("DIDRegistry", () => { it("should register an attribute in a new did", async () => { const ownerAccount: Account = (await ocean.getAccounts())[0] - const did = IdGenerator.generateId() + const did = generateId() const data = "my nice provider, is nice" const receipt = await didRegistry.registerAttribute(did, `0123456789abcdef`, data, ownerAccount.getId()) assert(receipt.status) @@ -33,7 +33,7 @@ describe("DIDRegistry", () => { it("should register another attribute in the same did", async () => { const ownerAccount: Account = (await ocean.getAccounts())[0] - const did = IdGenerator.generateId() + const did = generateId() { // register the first attribute const data = "my nice provider, is nice" @@ -54,7 +54,7 @@ describe("DIDRegistry", () => { it("should get the owner of a did properly", async () => { const ownerAccount: Account = (await ocean.getAccounts())[0] - const did = IdGenerator.generateId() + const did = generateId() const data = "my nice provider, is nice" await didRegistry.registerAttribute(did, "0123456789abcdef", data, ownerAccount.getId()) @@ -74,7 +74,7 @@ describe("DIDRegistry", () => { it("should the block number of the last update of the did attribute", async () => { const ownerAccount: Account = (await ocean.getAccounts())[0] - const did = IdGenerator.generateId() + const did = generateId() const data = "my nice provider, is nice" await didRegistry.registerAttribute(did, "0123456789abcdef", data, ownerAccount.getId()) diff --git a/test/ocean/IdGenerator.test.ts b/test/ocean/IdGenerator.test.ts deleted file mode 100644 index 2154bee..0000000 --- a/test/ocean/IdGenerator.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import * as assert from "assert" -import IdGenerator from "../../src/ocean/IdGenerator" - -describe("IdGenerator", () => { - - describe("#generateId()", () => { - - it("should generate an id", async () => { - - const id = IdGenerator.generateId() - assert(id) - }) - - it("should generate an id that is 64 chars long", async () => { - - const id: string = IdGenerator.generateId() - assert(id.length === 64, id) - }) - - it("should not contain -", async () => { - - const id: string = IdGenerator.generateId() - assert(id.indexOf("-") === -1) - }) - }) -}) diff --git a/test/ocean/ServiceAgreement.test.ts b/test/ocean/ServiceAgreement.test.ts index a362544..0e3aae6 100644 --- a/test/ocean/ServiceAgreement.test.ts +++ b/test/ocean/ServiceAgreement.test.ts @@ -5,7 +5,7 @@ import { DDO } from "../../src/ddo/DDO" 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 { generateId } from "../../src/utils/GeneratorHelpers" import Ocean from "../../src/ocean/Ocean" import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate" @@ -61,7 +61,7 @@ describe("ServiceAgreement", () => { it("should sign an service agreement", async () => { const ddo = new DDO({id: did.getDid(), service: [accessService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) @@ -80,7 +80,7 @@ describe("ServiceAgreement", () => { it("should execute a service agreement", async () => { const ddo = new DDO({id: did.getDid(), service: [accessService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) @@ -101,7 +101,7 @@ describe("ServiceAgreement", () => { it("should throw on invalid sig", (done) => { const ddo = new DDO({id: did.getDid(), service: [accessService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) @@ -118,7 +118,7 @@ describe("ServiceAgreement", () => { it("should lock the payment in that service agreement", async () => { const ddo = new DDO({id: did.getDid(), service: [accessService, metaDataService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) @@ -146,7 +146,7 @@ describe("ServiceAgreement", () => { it("should grant access in that service agreement", async () => { const ddo = new DDO({id: did.getDid(), service: [accessService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) @@ -161,9 +161,12 @@ describe("ServiceAgreement", () => { assert(serviceAgreement) // get funds - // TODO: remove small delay to prevent failtures - await new Promise((resolve) => setTimeout(resolve, 100)) - await consumerAccount.requestTokens(metaDataService.metadata.base.price) + try { + // Allowing 1 more retry + await consumerAccount.requestTokens(metaDataService.metadata.base.price) + } catch(e) { + await consumerAccount.requestTokens(metaDataService.metadata.base.price) + } const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price, consumerAccount) @@ -177,7 +180,7 @@ describe("ServiceAgreement", () => { it("should fail to grant grant access if there is no payment", async () => { const ddo = new DDO({id: did.getDid(), service: [accessService]}) - const serviceAgreementId: string = IdGenerator.generateId() + const serviceAgreementId: string = generateId() // @ts-ignore WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) diff --git a/test/utils/GeneratorHelpers.test.ts b/test/utils/GeneratorHelpers.test.ts new file mode 100644 index 0000000..71cf934 --- /dev/null +++ b/test/utils/GeneratorHelpers.test.ts @@ -0,0 +1,32 @@ +import { assert } from "chai" +import { generateId } from "../../src/utils/GeneratorHelpers" + +describe("GeneratorHelpers", () => { + + describe("#generateId()", () => { + + it("should generate an ID", async () => { + + const id = generateId() + assert(id) + }) + + it("should generate an ID that is 64 chars long", async () => { + + const id: string = generateId() + assert.equal(id.length, 64) + }) + + it("should not contain -", async () => { + + const id: string = generateId() + assert.match(id, /^[a-f0-9]+$/i) + }) + + it("should generate an ID that is 130 chars long", async () => { + + const id: string = generateId(130) + assert.equal(id.length, 130) + }) + }) +})