From 16574ee433c0f0e9875408b261679e0363fdd75e Mon Sep 17 00:00:00 2001 From: jipstavenuiter Date: Wed, 9 Jul 2025 14:38:43 -0400 Subject: [PATCH] feat: implement order validation evaluation method in MarketplaceStrategy - Added evaluateOrderValidationResult method to MarketplaceStrategy for consistent order validation across strategies. - Updated EOACreateOrderStrategy and MultisigCreateOrderStrategy to utilize the new validation method, improving error handling for order validation results. --- src/lib/marketplace/EOACreateOrderStrategy.ts | 25 +++++++++- src/lib/marketplace/MarketplaceStrategy.ts | 8 ++- .../MultisigCreateOrderStrategy.ts | 50 ++++++++++--------- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/lib/marketplace/EOACreateOrderStrategy.ts b/src/lib/marketplace/EOACreateOrderStrategy.ts index a8dbfa9d..34496992 100644 --- a/src/lib/marketplace/EOACreateOrderStrategy.ts +++ b/src/lib/marketplace/EOACreateOrderStrategy.ts @@ -1,5 +1,6 @@ import { HypercertExchangeClient, + OrderValidatorCode, utils, } from "@hypercerts-org/marketplace-sdk"; import { verifyTypedData } from "ethers"; @@ -61,7 +62,8 @@ export default class EOACreateOrderStrategy extends MarketplaceStrategy { id: "temporary", }, ]); - if (!validationResult.valid) { + if (!this.evaluateOrderValidationResult(validationResult)) { + // Check if only error code is TOO_EARLY_TO_EXECUTE_ORDER throw new Errors.InvalidOrder(validationResult); } @@ -117,4 +119,25 @@ export default class EOACreateOrderStrategy extends MarketplaceStrategy { : null, ); } + + evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }) { + if (validationResult.valid) { + return true; + } + + if ( + validationResult.validatorCodes + .filter( + (code) => code !== OrderValidatorCode.TOO_EARLY_TO_EXECUTE_ORDER, + ) + .every((code) => code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID) + ) { + return true; + } + + return false; + } } diff --git a/src/lib/marketplace/MarketplaceStrategy.ts b/src/lib/marketplace/MarketplaceStrategy.ts index 46d56123..d55112fd 100644 --- a/src/lib/marketplace/MarketplaceStrategy.ts +++ b/src/lib/marketplace/MarketplaceStrategy.ts @@ -1,8 +1,7 @@ +import { OrderValidatorCode } from "@hypercerts-org/marketplace-sdk"; import { DataResponse } from "../../types/api.js"; export abstract class MarketplaceStrategy { - constructor() {} - abstract executeCreate(): Promise>; protected returnSuccess( @@ -11,4 +10,9 @@ export abstract class MarketplaceStrategy { ): DataResponse { return { success: true, message, data }; } + + abstract evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }): boolean; } diff --git a/src/lib/marketplace/MultisigCreateOrderStrategy.ts b/src/lib/marketplace/MultisigCreateOrderStrategy.ts index bec153bc..9ebbc4a3 100644 --- a/src/lib/marketplace/MultisigCreateOrderStrategy.ts +++ b/src/lib/marketplace/MultisigCreateOrderStrategy.ts @@ -135,30 +135,11 @@ export default class MultisigCreateOrderStrategy extends MarketplaceStrategy { const [validationResult] = await hec.checkOrdersValidity([orderToValidate]); - if (!validationResult.valid) { - const errorCodes = validationResult.validatorCodes || []; - - // Check if error codes follow the expected pattern. Everything needs to be 0 (valid), - // except for the signature validation error. This is because when this request is - // made, the message is missing one or more signatures. - // The signature will be validated in the command. It's only skipped for now. - // TODO: get the command name when ready - const isValidErrorPattern = errorCodes.every((code, index) => { - if (index === 3) { - return ( - code === - OrderValidatorCode.MISSING_IS_VALID_SIGNATURE_FUNCTION_EIP1271 || - code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID - ); - } - return code === 0; - }); - - // Only proceed if it's the expected signature validation error pattern - if (!isValidErrorPattern) { - throw new Errors.InvalidOrder(validationResult); - } + // Only proceed if it's the expected signature validation error pattern + if (!this.evaluateOrderValidationResult(validationResult)) { + throw new Errors.InvalidOrder(validationResult); } + const tokenIds = orderDetails.itemIds.map( (id) => `${this.request.chainId}-${orderDetails.collection}-${id}`, ); @@ -209,4 +190,27 @@ export default class MultisigCreateOrderStrategy extends MarketplaceStrategy { timestamp: Math.floor(Date.now() / 1000), }); } + + evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }): boolean { + const errorCodes = validationResult.validatorCodes || []; + + // Check if error codes follow the expected pattern. Everything needs to be 0 (valid), + // except for the signature validation error. This is because when this request is + // made, the message is missing one or more signatures. + // The signature will be validated in the command. It's only skipped for now. + // TODO: get the command name when ready + return errorCodes.every((code, index) => { + if (index === 3) { + return ( + code === + OrderValidatorCode.MISSING_IS_VALID_SIGNATURE_FUNCTION_EIP1271 || + code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID + ); + } + return code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID; + }); + } }