Skip to content

Commit 4bc7243

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

File tree

6 files changed

+376
-171
lines changed

6 files changed

+376
-171
lines changed

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

Lines changed: 134 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,135 @@ 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;
19+
configElementTypeguard?: (value: unknown, log?: Logger) => boolean;
20+
configSubElementType?: T;
21+
configSubElementFunctionTypeguard?: (value: unknown, log?: Logger) => boolean;
22+
}
23+
24+
export function identifyAndCheckConfigFormat<T>(
25+
configElements: chainConfigElement<T>[],
26+
obj: Record<string, any>,
27+
log: Logger,
28+
fnTag: string,
29+
optionalConfigElements: chainConfigElement<T>[] = [],
30+
): boolean {
31+
if (configElements.length === 0) {
32+
log.error("No config elements where provided for validation.");
33+
return false;
34+
}
35+
36+
/* Check if all obligatory elements for the config are present */
37+
for (const element of configElements) {
38+
if (!(element.configElement in obj)) {
39+
return false;
40+
}
41+
}
42+
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+
}
84+
if (
85+
ccElement.configElementType &&
86+
typeof obj[ccElement.configElement] !== typeof ccElement.configElementType
87+
) {
88+
log.error(
89+
`${fnTag}: ${ccElement.configElement} present but not of type ${ccElement.configElementType}`,
90+
);
91+
return false;
92+
} else if (
93+
ccElement.configElementTypeguard &&
94+
!ccElement.configElementTypeguard(obj[ccElement.configElement])
95+
) {
96+
log.error(
97+
`${fnTag}: ${ccElement.configElement} present but with invalid format`,
98+
);
99+
return false;
100+
} else {
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;
142+
}
143+
}
144+
}
145+
return true;
146+
}

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

Lines changed: 61 additions & 21 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;
@@ -101,30 +106,65 @@ function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
101106
}
102107

103108
// Type guard for BesuConfigJSON
104-
export function isBesuConfigJSON(obj: unknown): obj is BesuConfigJSON {
109+
export function isBesuConfigJSON(
110+
obj: unknown,
111+
log: Logger,
112+
): obj is BesuConfigJSON {
105113
if (typeof obj !== "object" || obj === null) {
114+
log.error("isBesuConfigJSON: obj is not an object or is null");
106115
return false;
107116
}
117+
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+
];
132+
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+
];
160+
108161
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)
162+
163+
return identifyAndCheckConfigFormat(
164+
configDefaultFields,
165+
objRecord,
166+
log,
167+
"isBesuConfigJSON",
168+
optionalConfigElements,
129169
);
130170
}

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

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

2025
export interface EthereumConfigJSON extends NetworkOptionsJSON {
2126
signingCredential: Web3SigningCredential;
@@ -144,27 +149,69 @@ function isGasConfig(obj: unknown): obj is GasTransactionConfig {
144149
}
145150

146151
// Type guard for EthereumConfigJSON
147-
export function isEthereumConfigJSON(obj: unknown): obj is EthereumConfigJSON {
152+
export function isEthereumConfigJSON(
153+
obj: unknown,
154+
log: Logger,
155+
): obj is EthereumConfigJSON {
148156
if (typeof obj !== "object" || obj === null) {
157+
log.error("isEthereumConfigJSON: obj is not an object or is null");
149158
return false;
150159
}
160+
161+
const configDefaultFields: chainConfigElement<unknown>[] = [
162+
{
163+
configElement: "networkIdentification",
164+
configElementTypeguard: isNetworkId,
165+
},
166+
{
167+
configElement: "signingCredential",
168+
configElementTypeguard: isWeb3SigningCredential,
169+
},
170+
{
171+
configElement: "connectorOptions",
172+
configElementTypeguard: isEthereumOptionsJSON,
173+
},
174+
];
175+
176+
const configOptionalFields: chainConfigElement<unknown>[] = [
177+
{
178+
configElement: "wrapperContractName",
179+
configElementType: String,
180+
},
181+
{
182+
configElement: "wrapperContractAddress",
183+
configElementType: String,
184+
},
185+
{
186+
configElement: "gasConfig",
187+
configElementTypeguard: isGasConfig,
188+
},
189+
{
190+
configElement: "gasConfig",
191+
configElementTypeguard: isGasConfig,
192+
},
193+
{
194+
configElement: "leafId",
195+
configElementType: String,
196+
},
197+
{
198+
configElement: "keyPair",
199+
configElementType: Object,
200+
},
201+
{
202+
configElement: "claimFormats",
203+
configElementType: Array,
204+
configSubElementFunctionTypeguard: isClaimFormat,
205+
},
206+
];
207+
151208
const objRecord = obj as Record<string, unknown>;
152-
return (
153-
"networkIdentification" in obj &&
154-
isNetworkId(objRecord.networkIdentification) &&
155-
"signingCredential" in obj &&
156-
isWeb3SigningCredential(objRecord.signingCredential) &&
157-
"connectorOptions" in obj &&
158-
isEthereumOptionsJSON(objRecord.connectorOptions) &&
159-
(!("wrapperContractName" in obj) ||
160-
typeof objRecord.wrapperContractName === "string") &&
161-
(!("wrapperContractAddress" in obj) ||
162-
typeof objRecord.wrapperContractAddress === "string") &&
163-
(!("gasConfig" in obj) || isGasConfig(objRecord.gasConfig)) &&
164-
(!("leafId" in obj) || typeof objRecord.leafId === "string") &&
165-
(!("keyPair" in obj) || typeof objRecord.keyPair === "object") &&
166-
(!("claimFormats" in obj) ||
167-
(Array.isArray(objRecord.claimFormats) &&
168-
objRecord.claimFormats.every((format) => isClaimFormat(format))))
209+
210+
return identifyAndCheckConfigFormat(
211+
configDefaultFields,
212+
objRecord,
213+
log,
214+
"isEthereumConfigJSON",
215+
configOptionalFields,
169216
);
170217
}

0 commit comments

Comments
 (0)