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
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ export async function launchGateway(): Promise<void> {
logger.debug("SATP Gateway instanceId is valid.");

logger.debug("Validating SATP Gateway Identity...");
const gid = validateSatpGatewayIdentity({
configValue: config.gid,
});
const gid = validateSatpGatewayIdentity(
{
configValue: config.gid,
},
logger,
);
logger.debug("Valid SATP Gateway Identity");

logger.debug("Validating SATP Counter Party Gateways...");
const counterPartyGateways = validateSatpCounterPartyGateways({
configValue: config.counterPartyGateways,
});
const counterPartyGateways = validateSatpCounterPartyGateways(
{
configValue: config.counterPartyGateways,
},
logger,
);
logger.debug("Valid SATP Counter Party Gateways");

logger.debug("Validating SATP Log Level...");
Expand Down Expand Up @@ -103,9 +109,12 @@ export async function launchGateway(): Promise<void> {
);

logger.debug("Validating SATP KeyPair...");
const keyPair = validateSatpKeyPairJSON({
configValue: config.keyPair,
});
const keyPair = validateSatpKeyPairJSON(
{
configValue: config.keyPair,
},
logger,
);
logger.debug("SATP KeyPair is valid.");

logger.debug("Validating Cross Chain Config...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger } from "@hyperledger/cactus-common";

export function getEnumKeyByValue<T extends object>(
enumObj: T,
value: number,
Expand All @@ -10,3 +12,125 @@ export function getEnumValueByKey<T extends object>(
): number | undefined {
return enumObj[key as keyof T] as unknown as number;
}

export interface chainConfigElement<T> {
configElement: string;
configElementType?: T | string;
configElementTypeguard?:
| ((value: unknown, log: Logger) => boolean)
| ((value: unknown) => boolean);
configSubElementType?: T;
configSubElementFunctionTypeguard?:
| ((value: unknown, log: Logger) => boolean)
| ((value: unknown) => boolean);
}

export function identifyAndCheckConfigFormat<T>(
configElements: chainConfigElement<T>[],
obj: Record<string, any>,
log: Logger,
fnTag: string,
optionalConfigElements: chainConfigElement<T>[] = [],
): boolean {
if (configElements.length === 0) {
log.error("No config elements where provided for validation.");
return false;
}

/* Check if all obligatory elements for the config are present */
for (const element of configElements) {
if (!(element.configElement in obj)) {
return false;
}
}

/* Check if all obligatory elements are of the correct type */
for (const element of configElements) {
const check1 = checkConfigElementFormat(element, obj, log, fnTag);
if (!check1) {
return false;
}
}

/* Check if all optional elements that are present are of the correct type */
if (optionalConfigElements.length !== 0) {
for (const element of optionalConfigElements) {
const check2 = checkConfigElementFormat(
element,
obj,
log,
fnTag,
element.configElement in obj,
);
if (!check2) {
return false;
}
}
}

return true;
}

export function checkConfigElementFormat<T>(
ccElement: chainConfigElement<T>,
obj: Record<string, any>,
log: Logger,
fnTag: string,
elementInConfig: boolean = true,
): boolean {
if (elementInConfig) {
if (!ccElement.configElementType && !ccElement.configElementTypeguard) {
log.error(
`${fnTag}: ${ccElement.configElement} provided with no corresponding way for typecheck`,
);
return false;
}

if (
ccElement.configElementType &&
typeof obj[ccElement.configElement] !== ccElement.configElementType
) {
log.error(
`${fnTag}: ${ccElement.configElement} present but not of type ${ccElement.configElementType}`,
);
return false;
} else if (
ccElement.configElementTypeguard &&
!ccElement.configElementTypeguard(obj[ccElement.configElement], log)
) {
log.error(
`${fnTag}: ${ccElement.configElement} present but with invalid format`,
);
return false;
} else {
if (ccElement.configSubElementType) {
obj[ccElement.configElement].forEach((subEl: unknown) => {
if (
typeof subEl !== ccElement.configSubElementType ||
subEl === null
) {
log.error(
`${fnTag}: ${ccElement.configElement} is an array but contains invalid type elements`,
);
return false;
}
});
} else if (ccElement.configSubElementFunctionTypeguard) {
obj[ccElement.configElement].forEach((subEl: unknown) => {
if (
!ccElement.configSubElementFunctionTypeguard!(subEl, log) ||
subEl === null
) {
log.error(
`${fnTag}: ${ccElement.configElement} is an array but contains invalid format elements`,
);
return false;
}
});
} else {
return true;
}
}
}
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import {
import { isClaimFormat } from "./validate-bungee-options";
import type { ClaimFormat } from "../../../../generated/proto/cacti/satp/v02/common/message_pb";
import { KeyPairJSON } from "../validate-key-pair-json";
import { NetworkOptionsJSON } from "../validate-cc-config";
import { NetworkId, NetworkOptionsJSON } from "../validate-cc-config";
import { isNetworkId } from "../validate-satp-gateway-identity";
import { Logger } from "@hyperledger/cactus-common";
import {
chainConfigElement,
identifyAndCheckConfigFormat,
} from "../../../utils";
import { LedgerType } from "@hyperledger/cactus-core-api";

export interface BesuConfigJSON extends NetworkOptionsJSON {
signingCredential: Web3SigningCredential;
Expand Down Expand Up @@ -89,8 +95,12 @@ function isWeb3SigningCredentialNone(
}

// Type guard for Web3SigningCredential
function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
function isWeb3SigningCredential(
obj: unknown,
log: Logger,
): obj is Web3SigningCredential {
if (!obj || typeof obj !== "object") {
log.error("isWeb3SigningCredential: obj is not an object or is null");
return false;
}
return (
Expand All @@ -100,31 +110,73 @@ function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
);
}

export function isBesuNetworkId(obj: NetworkId) {
return (
(obj.ledgerType as LedgerType) === LedgerType.Besu1X ||
(obj.ledgerType as LedgerType) === LedgerType.Besu2X
);
}

// Type guard for BesuConfigJSON
export function isBesuConfigJSON(obj: unknown): obj is BesuConfigJSON {
export function isBesuConfigJSON(
obj: unknown,
log: Logger,
): obj is BesuConfigJSON {
if (typeof obj !== "object" || obj === null) {
log.error("isBesuConfigJSON: obj is not an object or is null");
return false;
}

const configDefaultFields: chainConfigElement<unknown>[] = [
{
configElement: "networkIdentification",
configElementTypeguard: isNetworkId,
},
{
configElement: "signingCredential",
configElementTypeguard: isWeb3SigningCredential,
},
{
configElement: "connectorOptions",
configElementTypeguard: isBesuOptionsJSON,
},
];

const optionalConfigElements: chainConfigElement<unknown>[] = [
{
configElement: "leafId",
configElementType: "string",
},
{
configElement: "keyPair",
configElementType: "object",
},
{
configElement: "claimFormats",
configElementTypeguard: Array.isArray,
configSubElementFunctionTypeguard: isClaimFormat,
},
{
configElement: "wrapperContractName",
configElementType: "string",
},
{
configElement: "wrapperContractAddress",
configElementType: "string",
},
{
configElement: "gas",
configElementType: "number",
},
];

const objRecord = obj as Record<string, unknown>;
return (
"networkIdentification" in obj &&
isNetworkId(objRecord.networkIdentification) &&
"signingCredential" in obj &&
isWeb3SigningCredential(objRecord.signingCredential) &&
"connectorOptions" in obj &&
isBesuOptionsJSON(objRecord.connectorOptions) &&
(!("leafId" in obj) || typeof objRecord.leafId === "string") &&
(!("keyPair" in obj) || typeof objRecord.keyPair === "object") &&
("claimFormats" in objRecord
? Array.isArray(objRecord.claimFormats) &&
objRecord.claimFormats.every(isClaimFormat)
: true) &&
("wrapperContractName" in objRecord
? typeof objRecord.wrapperContractName === "string"
: true) &&
("wrapperContractAddress" in objRecord
? typeof objRecord.wrapperContractAddress === "string"
: true) &&
("gas" in objRecord ? typeof objRecord.gas === "number" : true)

return identifyAndCheckConfigFormat(
configDefaultFields,
objRecord,
log,
"isBesuConfigJSON",
optionalConfigElements,
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from "@hyperledger/cactus-common/";
import { IPluginLedgerConnectorBesuOptions } from "@hyperledger/cactus-plugin-ledger-connector-besu";

export interface BesuOptionsJSON {
Expand All @@ -7,8 +8,12 @@ export interface BesuOptionsJSON {
}

// Type guard for BesuOptionsJSON
export function isBesuOptionsJSON(obj: unknown): obj is BesuOptionsJSON {
export function isBesuOptionsJSON(
obj: unknown,
log: Logger,
): obj is BesuOptionsJSON {
if (typeof obj !== "object" || obj === null) {
log.error("isBesuOptionsJSON: obj is not an object or is null");
return false;
}
const objRecord = obj as Record<string, unknown>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Logger } from "@hyperledger/cactus-common/";
import { ClaimFormat } from "../../../../generated/proto/cacti/satp/v02/common/message_pb";

// Type guard for ClaimFormat
export function isClaimFormat(obj: unknown): obj is ClaimFormat {
export function isClaimFormat(obj: unknown, log: Logger): obj is ClaimFormat {
if (typeof obj !== "number") {
log.error("isClaimFormat: obj is not a number");
return false;
}
return Object.values(ClaimFormat).includes(obj);
Expand Down
Loading
Loading