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

moved ID generator

This commit is contained in:
Pedro Gutiérrez 2019-02-14 12:37:52 +01:00 committed by Pedro Gutiérrez
parent 3fd8f995f6
commit 8dcdf0a851
10 changed files with 66 additions and 58 deletions

View File

@ -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())
}
/**

View File

@ -1,8 +0,0 @@
import {v4} from "uuid"
export default class IdGenerator {
public static generateId(): string {
const id = `${v4()}${v4()}`
return id.replace(/-/g, "")
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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)
}

View File

@ -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()
}
}

View File

@ -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())

View File

@ -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)
})
})
})

View File

@ -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))

View File

@ -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)
})
})
})