Skip to content

Commit d30e3f3

Browse files
Final refactor changes for chainConfigJSON verification
1 parent 919537e commit d30e3f3

File tree

6 files changed

+325
-332
lines changed

6 files changed

+325
-332
lines changed

packages/cactus-plugin-satp-hermes/src/main/typescript/services/utils.ts

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,112 +14,133 @@ export function getEnumValueByKey<T extends object>(
1414
}
1515

1616
export interface chainConfigElement<T> {
17-
elementName: string;
18-
elementType?: T;
19-
elementTypeguard?: (value: unknown) => boolean;
20-
subElementType?: T;
21-
subElementFunctionTypeguard?: (value: unknown) => boolean;
17+
configElement: string;
18+
configElementType?: T;
19+
configElementTypeguard?: (value: unknown, log?: Logger) => boolean;
20+
configSubElementType?: T;
21+
configSubElementFunctionTypeguard?: (value: unknown, log?: Logger) => boolean;
2222
}
2323

2424
export function identifyAndCheckConfigFormat<T>(
25-
obligatoryConfigElements: chainConfigElement<T>[],
25+
configElements: chainConfigElement<T>[],
2626
obj: Record<string, any>,
2727
log: Logger,
2828
fnTag: string,
2929
optionalConfigElements: chainConfigElement<T>[] = [],
3030
): boolean {
31-
if (obligatoryConfigElements.length === 0) {
32-
log.error("No configuration elements provided for validation.");
31+
if (configElements.length === 0) {
32+
log.error("No config elements where provided for validation.");
3333
return false;
3434
}
3535

3636
/* Check if all obligatory elements for the config are present */
37-
for (const element of obligatoryConfigElements) {
38-
if (element.elementName in obj) {
37+
for (const element of configElements) {
38+
if (!(element.configElement in obj)) {
3939
return false;
4040
}
4141
}
4242

43-
for (const element of obligatoryConfigElements) {
43+
/* Check if all obligatory elements are of the correct type */
44+
for (const element of configElements) {
45+
const check1 = checkConfigElementFormat(element, obj, log, fnTag);
46+
if (!check1) {
47+
return false;
48+
}
49+
}
50+
51+
/* Check if all optional elements that are present are of the correct type */
52+
if (optionalConfigElements.length !== 0) {
53+
for (const element of optionalConfigElements) {
54+
const check2 = checkConfigElementFormat(
55+
element,
56+
obj,
57+
log,
58+
fnTag,
59+
element.configElement in obj,
60+
);
61+
if (!check2) {
62+
return false;
63+
}
64+
}
65+
}
66+
67+
return true;
68+
}
69+
70+
export function checkConfigElementFormat<T>(
71+
ccElement: chainConfigElement<T>,
72+
obj: Record<string, any>,
73+
log: Logger,
74+
fnTag: string,
75+
elementInConfig: boolean = true,
76+
): boolean {
77+
if (elementInConfig) {
78+
if (!ccElement.configElementType && !ccElement.configElementTypeguard) {
79+
log.error(
80+
`${fnTag}: ${ccElement.configElement} provided with no corresponding way for typecheck`,
81+
);
82+
return false;
83+
}
4484
if (
45-
element.elementType &&
46-
typeof obj[element.elementName] !== typeof element.elementType
85+
ccElement.configElementType &&
86+
typeof obj[ccElement.configElement] !== typeof ccElement.configElementType
4787
) {
4888
log.error(
49-
`${fnTag}: ${element.elementName} present but not of type ${element.elementType}`,
89+
`${fnTag}: ${ccElement.configElement} present but not of type ${ccElement.configElementType}`,
5090
);
5191
return false;
5292
} else if (
53-
element.elementTypeguard &&
54-
!element.elementTypeguard(obj[element.elementName])
93+
ccElement.configElementTypeguard &&
94+
!ccElement.configElementTypeguard(obj[ccElement.configElement])
5595
) {
5696
log.error(
57-
`${fnTag}: ${element.elementName} present but with invalid format`,
97+
`${fnTag}: ${ccElement.configElement} present but with invalid format`,
5898
);
5999
return false;
60100
} else {
61-
log.error(
62-
`${fnTag}: ${element.elementName} provided with no way for typecheck`,
63-
);
64-
return false;
65-
}
66-
}
67-
68-
if (optionalConfigElements.length !== 0) {
69-
for (const element of optionalConfigElements) {
70-
if (element.elementName in obj) {
71-
if (
72-
element.elementType &&
73-
typeof obj[element.elementName] !== typeof element.elementType
74-
) {
75-
log.error(
76-
`${fnTag}: ${element.elementName} present but not of type ${element.elementType}`,
77-
);
78-
return false;
79-
} else if (
80-
element.elementTypeguard &&
81-
!element.elementTypeguard(obj[element.elementName])
82-
) {
83-
log.error(
84-
`${fnTag}: ${element.elementName} present but with invalid format`,
85-
);
86-
return false;
87-
}
88-
89-
if (
90-
element.elementType === Array &&
91-
!element.subElementType &&
92-
!element.subElementFunctionTypeguard
93-
) {
94-
log.error(
95-
`${fnTag}: ${element.elementName} is an array but no type check is provided for its elements`,
96-
);
97-
return false;
98-
} else if (element.elementType === Array && element.subElementType) {
99-
obj[element.elementName].forEach((subEl: unknown) => {
100-
if (typeof subEl !== typeof element.subElementType) {
101-
log.error(
102-
`${fnTag}: ${element.elementName} is an array but contains invalid type elements`,
103-
);
104-
return false;
105-
}
106-
});
107-
} else if (
108-
element.elementType === Array &&
109-
element.subElementFunctionTypeguard
110-
) {
111-
obj[element.elementName].forEach((subEl: unknown) => {
112-
if (!element.subElementFunctionTypeguard!(subEl)) {
113-
log.error(
114-
`${fnTag}: ${element.elementName} is an array but contains invalid format elements`,
115-
);
116-
return false;
117-
}
118-
});
119-
}
101+
if (
102+
ccElement.configElementType === Array &&
103+
!ccElement.configSubElementType &&
104+
!ccElement.configSubElementFunctionTypeguard
105+
) {
106+
log.error(
107+
`${fnTag}: ${ccElement.configElement} is an array but no type check is provided for its elements`,
108+
);
109+
return false;
110+
} else if (
111+
ccElement.configElementType === Array &&
112+
ccElement.configSubElementType
113+
) {
114+
obj[ccElement.configElement].forEach((subEl: unknown) => {
115+
if (
116+
typeof subEl !== typeof ccElement.configSubElementType ||
117+
subEl === null
118+
) {
119+
log.error(
120+
`${fnTag}: ${ccElement.configElement} is an array but contains invalid type elements`,
121+
);
122+
return false;
123+
}
124+
});
125+
} else if (
126+
ccElement.configElementType === Array &&
127+
ccElement.configSubElementFunctionTypeguard
128+
) {
129+
obj[ccElement.configElement].forEach((subEl: unknown) => {
130+
if (
131+
!ccElement.configSubElementFunctionTypeguard!(subEl) ||
132+
subEl === null
133+
) {
134+
log.error(
135+
`${fnTag}: ${ccElement.configElement} is an array but contains invalid format elements`,
136+
);
137+
return false;
138+
}
139+
});
140+
} else {
141+
return true;
120142
}
121143
}
122144
}
123-
124145
return true;
125146
}

packages/cactus-plugin-satp-hermes/src/main/typescript/services/validation/config-validating-functions/bridges-config-validating-functions/validate-besu-config.ts

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import { KeyPairJSON } from "../validate-key-pair-json";
1515
import { NetworkOptionsJSON } from "../validate-cc-config";
1616
import { isNetworkId } from "../validate-satp-gateway-identity";
1717
import { Logger } from "@hyperledger/cactus-common";
18+
import {
19+
chainConfigElement,
20+
identifyAndCheckConfigFormat,
21+
} from "../../../utils";
1822

1923
export interface BesuConfigJSON extends NetworkOptionsJSON {
2024
signingCredential: Web3SigningCredential;
@@ -111,65 +115,56 @@ export function isBesuConfigJSON(
111115
return false;
112116
}
113117

114-
const objRecord = obj as Record<string, unknown>;
115-
if (
116-
!("networkIdentification" in obj) ||
117-
!("signingCredential" in obj) ||
118-
!("connectorOptions" in obj)
119-
) {
120-
return false;
121-
}
118+
const configDefaultFields: chainConfigElement<unknown>[] = [
119+
{
120+
configElement: "networkIdentification",
121+
configElementTypeguard: isNetworkId,
122+
},
123+
{
124+
configElement: "signingCredential",
125+
configElementTypeguard: isWeb3SigningCredential,
126+
},
127+
{
128+
configElement: "connectorOptions",
129+
configElementTypeguard: isBesuOptionsJSON,
130+
},
131+
];
122132

123-
// Check on obligatory properties
124-
if (!isNetworkId(objRecord.networkIdentification)) {
125-
log.error("isBesuConfigJSON: networkIdentification is invalid");
126-
return false;
127-
}
128-
if (!isWeb3SigningCredential(objRecord.signingCredential)) {
129-
log.error("isBesuConfigJSON: signingCredential is invalid");
130-
return false;
131-
}
132-
if (!isBesuOptionsJSON(objRecord.connectorOptions)) {
133-
log.error("isBesuConfigJSON: connectorOptions is invalid");
134-
return false;
135-
}
133+
const optionalConfigElements: chainConfigElement<unknown>[] = [
134+
{
135+
configElement: "leafId",
136+
configElementType: String,
137+
},
138+
{
139+
configElement: "keyPair",
140+
configElementType: Object,
141+
},
142+
{
143+
configElement: "claimFormats",
144+
configElementType: Array,
145+
configSubElementFunctionTypeguard: isClaimFormat,
146+
},
147+
{
148+
configElement: "wrapperContractName",
149+
configElementType: String,
150+
},
151+
{
152+
configElement: "wrapperContractAddress",
153+
configElementType: String,
154+
},
155+
{
156+
configElement: "gas",
157+
configElementType: Number,
158+
},
159+
];
136160

137-
// Check on optional properties
138-
if ("leafId" in obj && typeof objRecord.leafId !== "string") {
139-
log.error("isBesuConfigJSON: leafId present but not a string");
140-
return false;
141-
}
142-
if ("keyPair" in obj && typeof objRecord.keyPair !== "object") {
143-
log.error("isBesuConfigJSON: keyPair present but not an object");
144-
return false;
145-
}
146-
if (
147-
"claimFormats" in obj &&
148-
Array.isArray(objRecord.claimFormats) &&
149-
objRecord.claimFormats.every(isClaimFormat)
150-
) {
151-
log.error("isBesuConfigJSON: claimFormats present but invalid");
152-
return false;
153-
}
154-
if (
155-
"wrapperContractName" in obj &&
156-
typeof objRecord.wrapperContractName !== "string"
157-
) {
158-
log.error("isBesuConfigJSON: wrapperContractName present but not a string");
159-
return false;
160-
}
161-
if (
162-
"wrapperContractAddress" in obj &&
163-
typeof objRecord.wrapperContractAddress !== "string"
164-
) {
165-
log.error(
166-
"isBesuConfigJSON: wrapperContractAddress present but not a string",
167-
);
168-
return false;
169-
}
170-
if ("gas" in obj && typeof objRecord.gas !== "number") {
171-
log.error("isBesuConfigJSON: gas present but not a number");
172-
return false;
173-
}
174-
return true;
161+
const objRecord = obj as Record<string, unknown>;
162+
163+
return identifyAndCheckConfigFormat(
164+
configDefaultFields,
165+
objRecord,
166+
log,
167+
"isBesuConfigJSON",
168+
optionalConfigElements,
169+
);
175170
}

0 commit comments

Comments
 (0)