Skip to content

Commit 628e7a5

Browse files
fix(satp): fix isFabricConfigJSON always printing error
Signed-off-by: Tomás Silva <[email protected]>
1 parent c449cfb commit 628e7a5

File tree

15 files changed

+586
-254
lines changed

15 files changed

+586
-254
lines changed

packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway-cli.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ export async function launchGateway(): Promise<void> {
5555
logger.debug("SATP Gateway instanceId is valid.");
5656

5757
logger.debug("Validating SATP Gateway Identity...");
58-
const gid = validateSatpGatewayIdentity({
59-
configValue: config.gid,
60-
});
58+
const gid = validateSatpGatewayIdentity(
59+
{
60+
configValue: config.gid,
61+
},
62+
logger,
63+
);
6164
logger.debug("Valid SATP Gateway Identity");
6265

6366
logger.debug("Validating SATP Counter Party Gateways...");
64-
const counterPartyGateways = validateSatpCounterPartyGateways({
65-
configValue: config.counterPartyGateways,
66-
});
67+
const counterPartyGateways = validateSatpCounterPartyGateways(
68+
{
69+
configValue: config.counterPartyGateways,
70+
},
71+
logger,
72+
);
6773
logger.debug("Valid SATP Counter Party Gateways");
6874

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

105111
logger.debug("Validating SATP KeyPair...");
106-
const keyPair = validateSatpKeyPairJSON({
107-
configValue: config.keyPair,
108-
});
112+
const keyPair = validateSatpKeyPairJSON(
113+
{
114+
configValue: config.keyPair,
115+
},
116+
logger,
117+
);
109118
logger.debug("SATP KeyPair is valid.");
110119

111120
logger.debug("Validating Cross Chain Config...");

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

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Logger } from "@hyperledger/cactus-common";
2+
13
export function getEnumKeyByValue<T extends object>(
24
enumObj: T,
35
value: number,
@@ -10,3 +12,125 @@ export function getEnumValueByKey<T extends object>(
1012
): number | undefined {
1113
return enumObj[key as keyof T] as unknown as number;
1214
}
15+
16+
export interface chainConfigElement<T> {
17+
configElement: string;
18+
configElementType?: T | string;
19+
configElementTypeguard?:
20+
| ((value: unknown, log: Logger) => boolean)
21+
| ((value: unknown) => boolean);
22+
configSubElementType?: T;
23+
configSubElementFunctionTypeguard?:
24+
| ((value: unknown, log: Logger) => boolean)
25+
| ((value: unknown) => boolean);
26+
}
27+
28+
export function identifyAndCheckConfigFormat<T>(
29+
configElements: chainConfigElement<T>[],
30+
obj: Record<string, any>,
31+
log: Logger,
32+
fnTag: string,
33+
optionalConfigElements: chainConfigElement<T>[] = [],
34+
): boolean {
35+
if (configElements.length === 0) {
36+
log.error("No config elements where provided for validation.");
37+
return false;
38+
}
39+
40+
/* Check if all obligatory elements for the config are present */
41+
for (const element of configElements) {
42+
if (!(element.configElement in obj)) {
43+
return false;
44+
}
45+
}
46+
47+
/* Check if all obligatory elements are of the correct type */
48+
for (const element of configElements) {
49+
const check1 = checkConfigElementFormat(element, obj, log, fnTag);
50+
if (!check1) {
51+
return false;
52+
}
53+
}
54+
55+
/* Check if all optional elements that are present are of the correct type */
56+
if (optionalConfigElements.length !== 0) {
57+
for (const element of optionalConfigElements) {
58+
const check2 = checkConfigElementFormat(
59+
element,
60+
obj,
61+
log,
62+
fnTag,
63+
element.configElement in obj,
64+
);
65+
if (!check2) {
66+
return false;
67+
}
68+
}
69+
}
70+
71+
return true;
72+
}
73+
74+
export function checkConfigElementFormat<T>(
75+
ccElement: chainConfigElement<T>,
76+
obj: Record<string, any>,
77+
log: Logger,
78+
fnTag: string,
79+
elementInConfig: boolean = true,
80+
): boolean {
81+
if (elementInConfig) {
82+
if (!ccElement.configElementType && !ccElement.configElementTypeguard) {
83+
log.error(
84+
`${fnTag}: ${ccElement.configElement} provided with no corresponding way for typecheck`,
85+
);
86+
return false;
87+
}
88+
89+
if (
90+
ccElement.configElementType &&
91+
typeof obj[ccElement.configElement] !== ccElement.configElementType
92+
) {
93+
log.error(
94+
`${fnTag}: ${ccElement.configElement} present but not of type ${ccElement.configElementType}`,
95+
);
96+
return false;
97+
} else if (
98+
ccElement.configElementTypeguard &&
99+
!ccElement.configElementTypeguard(obj[ccElement.configElement], log)
100+
) {
101+
log.error(
102+
`${fnTag}: ${ccElement.configElement} present but with invalid format`,
103+
);
104+
return false;
105+
} else {
106+
if (ccElement.configSubElementType) {
107+
obj[ccElement.configElement].forEach((subEl: unknown) => {
108+
if (
109+
typeof subEl !== typeof ccElement.configSubElementType ||
110+
subEl === null
111+
) {
112+
log.error(
113+
`${fnTag}: ${ccElement.configElement} is an array but contains invalid type elements`,
114+
);
115+
return false;
116+
}
117+
});
118+
} else if (ccElement.configSubElementFunctionTypeguard) {
119+
obj[ccElement.configElement].forEach((subEl: unknown) => {
120+
if (
121+
!ccElement.configSubElementFunctionTypeguard!(subEl, log) ||
122+
subEl === null
123+
) {
124+
log.error(
125+
`${fnTag}: ${ccElement.configElement} is an array but contains invalid format elements`,
126+
);
127+
return false;
128+
}
129+
});
130+
} else {
131+
return true;
132+
}
133+
}
134+
}
135+
return true;
136+
}

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

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import type { ClaimFormat } from "../../../../generated/proto/cacti/satp/v02/com
1414
import { KeyPairJSON } from "../validate-key-pair-json";
1515
import { NetworkOptionsJSON } from "../validate-cc-config";
1616
import { isNetworkId } from "../validate-satp-gateway-identity";
17+
import { Logger } from "@hyperledger/cactus-common";
18+
import {
19+
chainConfigElement,
20+
identifyAndCheckConfigFormat,
21+
} from "../../../utils";
1722

1823
export interface BesuConfigJSON extends NetworkOptionsJSON {
1924
signingCredential: Web3SigningCredential;
@@ -89,8 +94,12 @@ function isWeb3SigningCredentialNone(
8994
}
9095

9196
// Type guard for Web3SigningCredential
92-
function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
97+
function isWeb3SigningCredential(
98+
obj: unknown,
99+
log: Logger,
100+
): obj is Web3SigningCredential {
93101
if (!obj || typeof obj !== "object") {
102+
log.error("isWeb3SigningCredential: obj is not an object or is null");
94103
return false;
95104
}
96105
return (
@@ -101,30 +110,65 @@ function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
101110
}
102111

103112
// Type guard for BesuConfigJSON
104-
export function isBesuConfigJSON(obj: unknown): obj is BesuConfigJSON {
113+
export function isBesuConfigJSON(
114+
obj: unknown,
115+
log: Logger,
116+
): obj is BesuConfigJSON {
105117
if (typeof obj !== "object" || obj === null) {
118+
log.error("isBesuConfigJSON: obj is not an object or is null");
106119
return false;
107120
}
121+
122+
const configDefaultFields: chainConfigElement<unknown>[] = [
123+
{
124+
configElement: "networkIdentification",
125+
configElementTypeguard: isNetworkId,
126+
},
127+
{
128+
configElement: "signingCredential",
129+
configElementTypeguard: isWeb3SigningCredential,
130+
},
131+
{
132+
configElement: "connectorOptions",
133+
configElementTypeguard: isBesuOptionsJSON,
134+
},
135+
];
136+
137+
const optionalConfigElements: chainConfigElement<unknown>[] = [
138+
{
139+
configElement: "leafId",
140+
configElementType: "string",
141+
},
142+
{
143+
configElement: "keyPair",
144+
configElementType: "object",
145+
},
146+
{
147+
configElement: "claimFormats",
148+
configElementTypeguard: Array.isArray,
149+
configSubElementFunctionTypeguard: isClaimFormat,
150+
},
151+
{
152+
configElement: "wrapperContractName",
153+
configElementType: "string",
154+
},
155+
{
156+
configElement: "wrapperContractAddress",
157+
configElementType: "string",
158+
},
159+
{
160+
configElement: "gas",
161+
configElementType: "number",
162+
},
163+
];
164+
108165
const objRecord = obj as Record<string, unknown>;
109-
return (
110-
"networkIdentification" in obj &&
111-
isNetworkId(objRecord.networkIdentification) &&
112-
"signingCredential" in obj &&
113-
isWeb3SigningCredential(objRecord.signingCredential) &&
114-
"connectorOptions" in obj &&
115-
isBesuOptionsJSON(objRecord.connectorOptions) &&
116-
(!("leafId" in obj) || typeof objRecord.leafId === "string") &&
117-
(!("keyPair" in obj) || typeof objRecord.keyPair === "object") &&
118-
("claimFormats" in objRecord
119-
? Array.isArray(objRecord.claimFormats) &&
120-
objRecord.claimFormats.every(isClaimFormat)
121-
: true) &&
122-
("wrapperContractName" in objRecord
123-
? typeof objRecord.wrapperContractName === "string"
124-
: true) &&
125-
("wrapperContractAddress" in objRecord
126-
? typeof objRecord.wrapperContractAddress === "string"
127-
: true) &&
128-
("gas" in objRecord ? typeof objRecord.gas === "number" : true)
166+
167+
return identifyAndCheckConfigFormat(
168+
configDefaultFields,
169+
objRecord,
170+
log,
171+
"isBesuConfigJSON",
172+
optionalConfigElements,
129173
);
130174
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Logger } from "@hyperledger/cactus-common/";
12
import { IPluginLedgerConnectorBesuOptions } from "@hyperledger/cactus-plugin-ledger-connector-besu";
23

34
export interface BesuOptionsJSON {
@@ -7,8 +8,12 @@ export interface BesuOptionsJSON {
78
}
89

910
// Type guard for BesuOptionsJSON
10-
export function isBesuOptionsJSON(obj: unknown): obj is BesuOptionsJSON {
11+
export function isBesuOptionsJSON(
12+
obj: unknown,
13+
log: Logger,
14+
): obj is BesuOptionsJSON {
1115
if (typeof obj !== "object" || obj === null) {
16+
log.error("isBesuOptionsJSON: obj is not an object or is null");
1217
return false;
1318
}
1419
const objRecord = obj as Record<string, unknown>;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { Logger } from "@hyperledger/cactus-common/";
12
import { ClaimFormat } from "../../../../generated/proto/cacti/satp/v02/common/message_pb";
23

34
// Type guard for ClaimFormat
4-
export function isClaimFormat(obj: unknown): obj is ClaimFormat {
5+
export function isClaimFormat(obj: unknown, log: Logger): obj is ClaimFormat {
56
if (typeof obj !== "number") {
7+
log.error("isClaimFormat: obj is not a number");
68
return false;
79
}
810
return Object.values(ClaimFormat).includes(obj);

0 commit comments

Comments
 (0)