Skip to content

Commit

Permalink
feat(cactus-plugin-ledger-connector-quorum):additional type checking
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Purohit <[email protected]>
  • Loading branch information
shivam-Purohit committed Nov 7, 2023
1 parent 84af271 commit b2c7d0d
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PluginLedgerConnectorQuorum } from "../plugin-ledger-connector-quorum";
import OAS from "../../json/openapi.json";
import sanitizeHtml from "sanitize-html";
import { InvokeRawWeb3EthContractV1Response } from "../generated/openapi/typescript-axios";
import type { FunctionFragment } from "ethers";

export interface IInvokeRawWeb3EthContractEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -86,9 +87,45 @@ export class InvokeRawWeb3EthContractEndpoint implements IWebServiceEndpoint {
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`;
this.log.debug(reqTag);

const userInput = req.body;
const abi = userInput.abi;

if (!Array.isArray(abi) || abi.length == 0) {
throw new Error("Invalid or missing ABI in the request");
}

const methodName = userInput.contractMethod;
const method = abi.find(
(item: FunctionFragment) => item.name === methodName,
);

//getting methodparams from request
const contractMethodArgs = userInput.contractMethodArgs || [];

if (!method) {
throw new Error("Method not found in the provided ABI");
}

//getting methodparams from abi
const functionInputs = method.inputs || [];

for (const input of functionInputs) {
if (
!Object.prototype.hasOwnProperty.call(contractMethodArgs, input.name)
) {
throw new Error(`Missing input parameter: ${input.name}`);
}

// Check if the input type matches the expected type
const isValidType = typeof userInput[input.name] === input.type;
if (!isValidType) {
throw new Error(`Invalid data type for parameter ${input.name}`);
}
}

try {
const methodResponse =
await this.options.connector.invokeRawWeb3EthContract(req.body);
await this.options.connector.invokeRawWeb3EthContract(userInput);
const response: InvokeRawWeb3EthContractV1Response = {
status: 200,
data: methodResponse,
Expand Down

0 comments on commit b2c7d0d

Please sign in to comment.