Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lib/marketplace/EOACreateOrderStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
HypercertExchangeClient,
OrderValidatorCode,
utils,
} from "@hypercerts-org/marketplace-sdk";
import { verifyTypedData } from "ethers";
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions src/lib/marketplace/MarketplaceStrategy.ts
Original file line number Diff line number Diff line change
@@ -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<DataResponse<unknown>>;

protected returnSuccess(
Expand All @@ -11,4 +10,9 @@ export abstract class MarketplaceStrategy {
): DataResponse<unknown> {
return { success: true, message, data };
}

abstract evaluateOrderValidationResult(validationResult: {
valid: boolean;
validatorCodes: OrderValidatorCode[];
}): boolean;
}
50 changes: 27 additions & 23 deletions src/lib/marketplace/MultisigCreateOrderStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down Expand Up @@ -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;
});
}
}