From 33256dbe9c53334611e8607d40e975bf8184a5d2 Mon Sep 17 00:00:00 2001 From: Edgar Goetzendorff Date: Thu, 5 Dec 2019 05:45:29 +0100 Subject: [PATCH] fix(core-api): businesses/bridgechains search by name (#3313) --- .../core-api/src/handlers/blocks/schema.ts | 24 +++----- .../src/handlers/bridgechains/schema.ts | 15 ++--- .../src/handlers/businesses/schema.ts | 15 ++--- .../core-api/src/handlers/delegates/schema.ts | 57 +++++-------------- .../core-api/src/handlers/locks/schema.ts | 18 ++---- .../src/handlers/shared/schemas/address.ts | 5 ++ .../handlers/shared/schemas/generic-name.ts | 6 ++ .../src/handlers/shared/schemas/index.ts | 5 +- .../src/handlers/shared/schemas/public-key.ts | 5 ++ .../src/handlers/transactions/schema.ts | 22 ++----- .../core-api/src/handlers/votes/schema.ts | 14 ++--- .../core-api/src/handlers/wallets/schema.ts | 46 ++++----------- packages/crypto/src/validation/schemas.ts | 2 +- 13 files changed, 78 insertions(+), 156 deletions(-) create mode 100644 packages/core-api/src/handlers/shared/schemas/address.ts create mode 100644 packages/core-api/src/handlers/shared/schemas/generic-name.ts create mode 100644 packages/core-api/src/handlers/shared/schemas/public-key.ts diff --git a/packages/core-api/src/handlers/blocks/schema.ts b/packages/core-api/src/handlers/blocks/schema.ts index e232f3b217..0a2dc1d6a7 100644 --- a/packages/core-api/src/handlers/blocks/schema.ts +++ b/packages/core-api/src/handlers/blocks/schema.ts @@ -1,5 +1,5 @@ import Joi from "@hapi/joi"; -import { blockId, orderBy, pagination } from "../shared/schemas"; +import { address, blockId, orderBy, pagination, publicKey } from "../shared/schemas"; export const index: object = { query: { @@ -33,9 +33,7 @@ export const index: object = { .integer() .positive(), payloadHash: Joi.string().hex(), - generatorPublicKey: Joi.string() - .hex() - .length(66), + generatorPublicKey: publicKey, blockSignature: Joi.string().hex(), transform: Joi.bool().default(true), }, @@ -73,7 +71,7 @@ export const transactions: object = { orderBy, id: Joi.string() .hex() - .length(66), + .length(64), blockId, type: Joi.number() .integer() @@ -81,15 +79,9 @@ export const transactions: object = { version: Joi.number() .integer() .min(0), - senderPublicKey: Joi.string() - .hex() - .length(66), - senderId: Joi.string() - .alphanum() - .length(34), - recipientId: Joi.string() - .alphanum() - .length(34), + senderPublicKey: publicKey, + senderId: address, + recipientId: address, timestamp: Joi.number() .integer() .min(0), @@ -119,9 +111,7 @@ export const search: object = { .min(0), previousBlock: blockId, payloadHash: Joi.string().hex(), - generatorPublicKey: Joi.string() - .hex() - .length(66), + generatorPublicKey: publicKey, blockSignature: Joi.string().hex(), timestamp: Joi.object().keys({ from: Joi.number() diff --git a/packages/core-api/src/handlers/bridgechains/schema.ts b/packages/core-api/src/handlers/bridgechains/schema.ts index a8613ff801..fcacf0af45 100644 --- a/packages/core-api/src/handlers/bridgechains/schema.ts +++ b/packages/core-api/src/handlers/bridgechains/schema.ts @@ -1,14 +1,12 @@ import Joi from "@hapi/joi"; -import { orderBy, pagination } from "../shared/schemas"; +import { address, genericName, orderBy, pagination, publicKey } from "../shared/schemas"; export const index: object = { query: { ...pagination, ...{ orderBy, - publicKey: Joi.string() - .hex() - .length(66), + publicKey, isResigned: Joi.bool(), }, }, @@ -30,16 +28,13 @@ export const search: object = { }, }, payload: { + address, + publicKey, bridgechainRepository: Joi.string().max(80), - publicKey: Joi.string() - .hex() - .length(66), genesisHash: Joi.string() .hex() .length(64), - name: Joi.string() - .regex(/^[a-zA-Z0-9_-]+$/) - .max(40), + name: genericName, seedNodes: Joi.array() .unique() .min(1) diff --git a/packages/core-api/src/handlers/businesses/schema.ts b/packages/core-api/src/handlers/businesses/schema.ts index f2811b6f07..2cfd6a9bf5 100644 --- a/packages/core-api/src/handlers/businesses/schema.ts +++ b/packages/core-api/src/handlers/businesses/schema.ts @@ -1,14 +1,12 @@ import Joi from "@hapi/joi"; -import { orderBy, pagination, walletId } from "../shared/schemas"; +import { address, genericName, orderBy, pagination, publicKey, walletId } from "../shared/schemas"; export const index: object = { query: { ...pagination, ...{ orderBy, - publicKey: Joi.string() - .hex() - .length(66), + publicKey, isResigned: Joi.bool(), }, }, @@ -41,12 +39,9 @@ export const search: object = { }, }, payload: { - publicKey: Joi.string() - .hex() - .length(66), - name: Joi.string() - .regex(/^[a-zA-Z0-9_-]+$/) - .max(40), + address, + publicKey, + name: genericName, website: Joi.string().max(80), vat: Joi.string() .alphanum() diff --git a/packages/core-api/src/handlers/delegates/schema.ts b/packages/core-api/src/handlers/delegates/schema.ts index 6386f926bd..bf94c1fa5f 100644 --- a/packages/core-api/src/handlers/delegates/schema.ts +++ b/packages/core-api/src/handlers/delegates/schema.ts @@ -1,14 +1,9 @@ import { app } from "@arkecosystem/core-container"; import Joi from "@hapi/joi"; -import { blockId, orderBy, pagination } from "../shared/schemas"; +import { address, blockId, orderBy, pagination, publicKey, walletId } from "../shared/schemas"; const config = app.getConfig(); -const schemaIdentifier = Joi.string() - .regex(/^[a-zA-Z0-9!@$&_.]+$/) - .min(1) - .max(66); - const schemaUsername = Joi.string() .regex(/^[a-z0-9!@$&_.]+$/) .min(1) @@ -40,18 +35,10 @@ export const index: object = { ...{ orderBy, type: Joi.string().valid("resigned", "never-forged"), - address: Joi.string() - .alphanum() - .length(34), - publicKey: Joi.string() - .hex() - .length(66), - secondPublicKey: Joi.string() - .hex() - .length(66), - vote: Joi.string() - .hex() - .length(66), + address, + publicKey, + secondPublicKey: publicKey, + vote: publicKey, username: schemaUsername, balance: Joi.number() .integer() @@ -68,7 +55,7 @@ export const index: object = { export const show: object = { params: { - id: schemaIdentifier, + id: walletId, }, }; @@ -80,12 +67,8 @@ export const search: object = { }, }, payload: { - address: Joi.string() - .alphanum() - .length(34), - publicKey: Joi.string() - .hex() - .length(66), + address, + publicKey, username: schemaUsername, usernames: Joi.array() .unique() @@ -103,7 +86,7 @@ export const search: object = { export const blocks: object = { params: { - id: schemaIdentifier, + id: walletId, }, query: { ...pagination, @@ -136,9 +119,7 @@ export const blocks: object = { .integer() .min(0), payloadHash: Joi.string().hex(), - generatorPublicKey: Joi.string() - .hex() - .length(66), + generatorPublicKey: publicKey, blockSignature: Joi.string().hex(), transform: Joi.bool().default(true), }, @@ -147,24 +128,16 @@ export const blocks: object = { export const voters: object = { params: { - id: schemaIdentifier, + id: walletId, }, query: { ...pagination, ...{ orderBy, - address: Joi.string() - .alphanum() - .length(34), - publicKey: Joi.string() - .hex() - .length(66), - secondPublicKey: Joi.string() - .hex() - .length(66), - vote: Joi.string() - .hex() - .length(66), + address, + publicKey, + secondPublicKey: publicKey, + vote: publicKey, username: schemaUsername, balance: Joi.number() .integer() diff --git a/packages/core-api/src/handlers/locks/schema.ts b/packages/core-api/src/handlers/locks/schema.ts index b9d44b56c9..b998937d9f 100644 --- a/packages/core-api/src/handlers/locks/schema.ts +++ b/packages/core-api/src/handlers/locks/schema.ts @@ -1,18 +1,14 @@ import { Enums } from "@arkecosystem/crypto"; import Joi from "@hapi/joi"; -import { orderBy, pagination } from "../shared/schemas"; +import { address, orderBy, pagination, publicKey } from "../shared/schemas"; export const index: object = { query: { ...pagination, ...{ orderBy, - recipientId: Joi.string() - .alphanum() - .length(34), - senderPublicKey: Joi.string() - .hex() - .length(66), + recipientId: address, + senderPublicKey: publicKey, lockId: Joi.string() .hex() .length(64), @@ -47,12 +43,8 @@ export const search: object = { }, }, payload: { - recipientId: Joi.string() - .alphanum() - .length(34), - senderPublicKey: Joi.string() - .hex() - .length(66), + recipientId: address, + senderPublicKey: publicKey, lockId: Joi.string() .hex() .length(64), diff --git a/packages/core-api/src/handlers/shared/schemas/address.ts b/packages/core-api/src/handlers/shared/schemas/address.ts new file mode 100644 index 0000000000..14777984c2 --- /dev/null +++ b/packages/core-api/src/handlers/shared/schemas/address.ts @@ -0,0 +1,5 @@ +import Joi from "@hapi/joi"; + +export const address = Joi.string() + .alphanum() + .length(34); diff --git a/packages/core-api/src/handlers/shared/schemas/generic-name.ts b/packages/core-api/src/handlers/shared/schemas/generic-name.ts new file mode 100644 index 0000000000..1af382b0e2 --- /dev/null +++ b/packages/core-api/src/handlers/shared/schemas/generic-name.ts @@ -0,0 +1,6 @@ +import Joi from "@hapi/joi"; + +export const genericName = Joi.string() + .regex(/^[a-zA-Z0-9]+(( - |[ ._-])[a-zA-Z0-9]+)*[.]?$/) + .min(1) + .max(40); diff --git a/packages/core-api/src/handlers/shared/schemas/index.ts b/packages/core-api/src/handlers/shared/schemas/index.ts index 27c2bc9b83..aacce47ed2 100644 --- a/packages/core-api/src/handlers/shared/schemas/index.ts +++ b/packages/core-api/src/handlers/shared/schemas/index.ts @@ -1,6 +1,9 @@ +import { address } from "./address"; import { blockId } from "./block-id"; +import { genericName } from "./generic-name"; import { orderBy } from "./order-by"; import { pagination } from "./pagination"; +import { publicKey } from "./public-key"; import { walletId } from "./wallet-id"; -export { blockId, orderBy, pagination, walletId }; +export { address, blockId, genericName, orderBy, pagination, publicKey, walletId }; diff --git a/packages/core-api/src/handlers/shared/schemas/public-key.ts b/packages/core-api/src/handlers/shared/schemas/public-key.ts new file mode 100644 index 0000000000..9851b4fe10 --- /dev/null +++ b/packages/core-api/src/handlers/shared/schemas/public-key.ts @@ -0,0 +1,5 @@ +import Joi from "@hapi/joi"; + +export const publicKey = Joi.string() + .hex() + .length(66); diff --git a/packages/core-api/src/handlers/transactions/schema.ts b/packages/core-api/src/handlers/transactions/schema.ts index 181a020ab2..f58f61eeed 100644 --- a/packages/core-api/src/handlers/transactions/schema.ts +++ b/packages/core-api/src/handlers/transactions/schema.ts @@ -1,10 +1,6 @@ import { app } from "@arkecosystem/core-container"; import Joi from "@hapi/joi"; -import { blockId, orderBy, pagination } from "../shared/schemas"; - -const address: object = Joi.string() - .alphanum() - .length(34); +import { address, blockId, orderBy, pagination, publicKey } from "../shared/schemas"; export const index: object = { query: { @@ -24,15 +20,9 @@ export const index: object = { version: Joi.number() .integer() .positive(), - senderPublicKey: Joi.string() - .hex() - .length(66), - senderId: Joi.string() - .alphanum() - .length(34), - recipientId: Joi.string() - .alphanum() - .length(34), + senderPublicKey: publicKey, + senderId: address, + recipientId: address, timestamp: Joi.number() .integer() .min(0), @@ -114,9 +104,7 @@ export const search: object = { version: Joi.number() .integer() .positive(), - senderPublicKey: Joi.string() - .hex() - .length(66), + senderPublicKey: publicKey, senderId: address, recipientId: address, addresses: Joi.array() diff --git a/packages/core-api/src/handlers/votes/schema.ts b/packages/core-api/src/handlers/votes/schema.ts index 3ba69059e8..46dd7d5b7c 100644 --- a/packages/core-api/src/handlers/votes/schema.ts +++ b/packages/core-api/src/handlers/votes/schema.ts @@ -1,5 +1,5 @@ import Joi from "@hapi/joi"; -import { blockId, orderBy, pagination } from "../shared/schemas"; +import { address, blockId, orderBy, pagination, publicKey } from "../shared/schemas"; export const index: object = { query: { @@ -13,15 +13,9 @@ export const index: object = { version: Joi.number() .integer() .positive(), - senderPublicKey: Joi.string() - .hex() - .length(66), - senderId: Joi.string() - .alphanum() - .length(34), - recipientId: Joi.string() - .alphanum() - .length(34), + senderPublicKey: publicKey, + senderId: address, + recipientId: address, timestamp: Joi.number() .integer() .min(0), diff --git a/packages/core-api/src/handlers/wallets/schema.ts b/packages/core-api/src/handlers/wallets/schema.ts index e9355436db..e42520bc44 100644 --- a/packages/core-api/src/handlers/wallets/schema.ts +++ b/packages/core-api/src/handlers/wallets/schema.ts @@ -1,27 +1,15 @@ import Joi from "@hapi/joi"; -import { blockId, orderBy, pagination, walletId } from "../shared/schemas"; - -const address: object = Joi.string() - .alphanum() - .length(34); +import { address, blockId, orderBy, pagination, publicKey, walletId } from "../shared/schemas"; export const index: object = { query: { ...pagination, ...{ orderBy, - address: Joi.string() - .alphanum() - .length(34), - publicKey: Joi.string() - .hex() - .length(66), - secondPublicKey: Joi.string() - .hex() - .length(66), - vote: Joi.string() - .hex() - .length(66), + address, + publicKey, + secondPublicKey: publicKey, + vote: publicKey, username: Joi.string(), balance: Joi.number().integer(), voteBalance: Joi.number() @@ -100,9 +88,7 @@ export const transactionsSent: object = { version: Joi.number() .integer() .positive(), - recipientId: Joi.string() - .alphanum() - .length(34), + recipientId: address, timestamp: Joi.number() .integer() .min(0), @@ -142,12 +128,8 @@ export const transactionsReceived: object = { version: Joi.number() .integer() .positive(), - senderPublicKey: Joi.string() - .hex() - .length(66), - senderId: Joi.string() - .alphanum() - .length(34), + senderPublicKey: publicKey, + senderId: address, timestamp: Joi.number() .integer() .min(0), @@ -205,15 +187,9 @@ export const search: object = { .min(1) .max(50) .items(address), - publicKey: Joi.string() - .hex() - .length(66), - secondPublicKey: Joi.string() - .hex() - .length(66), - vote: Joi.string() - .hex() - .length(66), + publicKey, + secondPublicKey: publicKey, + vote: publicKey, username: Joi.string(), producedBlocks: Joi.number() .integer() diff --git a/packages/crypto/src/validation/schemas.ts b/packages/crypto/src/validation/schemas.ts index 1c7a67604c..064bf0a723 100644 --- a/packages/crypto/src/validation/schemas.ts +++ b/packages/crypto/src/validation/schemas.ts @@ -54,7 +54,7 @@ export const schemas = { genericName: { $id: "genericName", allOf: [ - { type: "string", pattern: "^[a-zA-Z0-9]+([ ._-][a-zA-Z0-9]+)*[.]?$" }, + { type: "string", pattern: "^[a-zA-Z0-9]+(( - |[ ._-])[a-zA-Z0-9]+)*[.]?$" }, { minLength: 1, maxLength: 40 }, ], },