Skip to content

Commit 8c6f499

Browse files
Added logger to all verification functions used at config checks
1 parent 4bc7243 commit 8c6f499

File tree

15 files changed

+276
-139
lines changed

15 files changed

+276
-139
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
@@ -53,15 +53,21 @@ export async function launchGateway(): Promise<void> {
5353
logger.debug("SATP Gateway instanceId is valid.");
5454

5555
logger.debug("Validating SATP Gateway Identity...");
56-
const gid = validateSatpGatewayIdentity({
57-
configValue: config.gid,
58-
});
56+
const gid = validateSatpGatewayIdentity(
57+
{
58+
configValue: config.gid,
59+
},
60+
logger,
61+
);
5962
logger.debug("Valid SATP Gateway Identity");
6063

6164
logger.debug("Validating SATP Counter Party Gateways...");
62-
const counterPartyGateways = validateSatpCounterPartyGateways({
63-
configValue: config.counterPartyGateways,
64-
});
65+
const counterPartyGateways = validateSatpCounterPartyGateways(
66+
{
67+
configValue: config.counterPartyGateways,
68+
},
69+
logger,
70+
);
6571
logger.debug("Valid SATP Counter Party Gateways");
6672

6773
logger.debug("Validating SATP Log Level...");
@@ -101,9 +107,12 @@ export async function launchGateway(): Promise<void> {
101107
);
102108

103109
logger.debug("Validating SATP KeyPair...");
104-
const keyPair = validateSatpKeyPairJSON({
105-
configValue: config.keyPair,
106-
});
110+
const keyPair = validateSatpKeyPairJSON(
111+
{
112+
configValue: config.keyPair,
113+
},
114+
logger,
115+
);
107116
logger.debug("SATP KeyPair is valid.");
108117

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export function getEnumValueByKey<T extends object>(
1616
export interface chainConfigElement<T> {
1717
configElement: string;
1818
configElementType?: T;
19-
configElementTypeguard?: (value: unknown, log?: Logger) => boolean;
19+
configElementTypeguard?: (value: unknown, log: Logger) => boolean;
2020
configSubElementType?: T;
21-
configSubElementFunctionTypeguard?: (value: unknown, log?: Logger) => boolean;
21+
configSubElementFunctionTypeguard?: (value: unknown, log: Logger) => boolean;
2222
}
2323

2424
export function identifyAndCheckConfigFormat<T>(
@@ -91,7 +91,7 @@ export function checkConfigElementFormat<T>(
9191
return false;
9292
} else if (
9393
ccElement.configElementTypeguard &&
94-
!ccElement.configElementTypeguard(obj[ccElement.configElement])
94+
!ccElement.configElementTypeguard(obj[ccElement.configElement], log)
9595
) {
9696
log.error(
9797
`${fnTag}: ${ccElement.configElement} present but with invalid format`,
@@ -128,7 +128,7 @@ export function checkConfigElementFormat<T>(
128128
) {
129129
obj[ccElement.configElement].forEach((subEl: unknown) => {
130130
if (
131-
!ccElement.configSubElementFunctionTypeguard!(subEl) ||
131+
!ccElement.configSubElementFunctionTypeguard!(subEl, log) ||
132132
subEl === null
133133
) {
134134
log.error(

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ function isWeb3SigningCredentialNone(
9494
}
9595

9696
// Type guard for Web3SigningCredential
97-
function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
97+
function isWeb3SigningCredential(
98+
obj: unknown,
99+
log: Logger,
100+
): obj is Web3SigningCredential {
98101
if (!obj || typeof obj !== "object") {
102+
log.error("isWeb3SigningCredential: obj is not an object or is null");
99103
return false;
100104
}
101105
return (

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);

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ function isWeb3SigningCredentialNone(
113113
}
114114

115115
// Type guard for Web3SigningCredential
116-
function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
116+
function isWeb3SigningCredential(
117+
obj: unknown,
118+
log: Logger,
119+
): obj is Web3SigningCredential {
117120
if (!obj || typeof obj !== "object") {
121+
log.error("isWeb3SigningCredential: obj is not an object or is null");
118122
return false;
119123
}
120124
return (
@@ -125,8 +129,9 @@ function isWeb3SigningCredential(obj: unknown): obj is Web3SigningCredential {
125129
);
126130
}
127131

128-
function isGasConfig(obj: unknown): obj is GasTransactionConfig {
132+
function isGasConfig(obj: unknown, log: Logger): obj is GasTransactionConfig {
129133
if (!obj || typeof obj !== "object") {
134+
log.error("isGasConfig: obj null or not obj");
130135
throw new TypeError(
131136
"isGasConfig: obj null or not obj" + JSON.stringify(obj),
132137
);
@@ -186,10 +191,6 @@ export function isEthereumConfigJSON(
186191
configElement: "gasConfig",
187192
configElementTypeguard: isGasConfig,
188193
},
189-
{
190-
configElement: "gasConfig",
191-
configElementTypeguard: isGasConfig,
192-
},
193194
{
194195
configElement: "leafId",
195196
configElementType: String,

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

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

34
export interface EthereumOptionsJSON {
@@ -8,8 +9,10 @@ export interface EthereumOptionsJSON {
89

910
export function isEthereumOptionsJSON(
1011
obj: unknown,
12+
log: Logger,
1113
): obj is EthereumOptionsJSON {
1214
if (typeof obj !== "object" || obj === null) {
15+
log.error("isEthereumOptionsJSON: obj is not an object or is null");
1316
return false;
1417
}
1518
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-fabric-config.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,46 +126,46 @@ function isFabricSigningCredential(
126126
);
127127
}
128128

129-
function isUserIdentity(obj: unknown, log?: Logger): obj is X509Identity {
129+
function isUserIdentity(obj: unknown, log: Logger): obj is X509Identity {
130130
const objRecord = obj as Record<string, unknown>;
131131
if (typeof obj !== "object" || obj === null) {
132-
log!.error("isUserIdentity: obj is not an object or is null");
132+
log.error("isUserIdentity: obj is not an object or is null");
133133
return false;
134134
}
135135
if (!("type" in obj)) {
136-
log!.error("isUserIdentity: 'type' property missing");
136+
log.error("isUserIdentity: 'type' property missing");
137137
return false;
138138
}
139139
if (typeof objRecord.type !== "string") {
140-
log!.error("isUserIdentity: 'type' property is not a string");
140+
log.error("isUserIdentity: 'type' property is not a string");
141141
return false;
142142
}
143143
if (!("credentials" in obj)) {
144-
log!.error("isUserIdentity: 'credentials' property missing");
144+
log.error("isUserIdentity: 'credentials' property missing");
145145
return false;
146146
}
147147
if (
148148
typeof objRecord.credentials !== "object" ||
149149
objRecord.credentials === null
150150
) {
151-
log!.error("isUserIdentity: 'credentials' is not an object or is null");
151+
log.error("isUserIdentity: 'credentials' is not an object or is null");
152152
return false;
153153
}
154154
const credentials = objRecord.credentials as Record<string, unknown>;
155155
if (!("certificate" in credentials)) {
156-
log!.error("isUserIdentity: 'certificate' property missing in credentials");
156+
log.error("isUserIdentity: 'certificate' property missing in credentials");
157157
return false;
158158
}
159159
if (typeof credentials.certificate !== "string") {
160-
log!.error("isUserIdentity: 'certificate' property is not a string");
160+
log.error("isUserIdentity: 'certificate' property is not a string");
161161
return false;
162162
}
163163
if (!("privateKey" in credentials)) {
164-
log!.error("isUserIdentity: 'privateKey' property missing in credentials");
164+
log.error("isUserIdentity: 'privateKey' property missing in credentials");
165165
return false;
166166
}
167167
if (typeof credentials.privateKey !== "string") {
168-
log!.error("isUserIdentity: 'privateKey' property is not a string");
168+
log.error("isUserIdentity: 'privateKey' property is not a string");
169169
return false;
170170
}
171171
return true;

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,18 @@ function isSignPayloadCallback(
254254
// Type guard for FabricOptionsJSON
255255
export function isFabricOptionsJSON(
256256
obj: unknown,
257-
log?: Logger,
257+
log: Logger,
258258
): obj is FabricOptionsJSON {
259259
if (typeof obj !== "object" || obj === null) {
260-
log!.error("FabricOptionsJSON is not an object:", obj);
260+
log.error("FabricOptionsJSON is not an object:", obj);
261261
return false;
262262
}
263263
const objRecord = obj as Record<string, unknown>;
264264
if (
265265
"dockerNetworkName" in obj &&
266266
typeof objRecord.dockerNetworkName !== "string"
267267
) {
268-
log!.error(
268+
log.error(
269269
"FabricOptionsJSON invalid 'dockerNetworkName':",
270270
objRecord.dockerNetworkName,
271271
);
@@ -275,54 +275,54 @@ export function isFabricOptionsJSON(
275275
"discoveryOptions" in obj &&
276276
!isGatewayDiscoveryOptions(objRecord.discoveryOptions)
277277
) {
278-
log!.error(
278+
log.error(
279279
"FabricOptionsJSON invalid 'discoveryOptions':",
280280
objRecord.discoveryOptions,
281281
);
282282
return false;
283283
}
284284
if (
285285
"eventHandlerOptions" in obj &&
286-
!isGatewayEventHandlerOptions(objRecord.eventHandlerOptions, log!)
286+
!isGatewayEventHandlerOptions(objRecord.eventHandlerOptions, log)
287287
) {
288-
log!.error(
288+
log.error(
289289
"FabricOptionsJSON invalid 'eventHandlerOptions':",
290290
objRecord.eventHandlerOptions,
291291
);
292292
return false;
293293
}
294294
if (
295295
"supportedIdentity" in obj &&
296-
!isFabricSigningCredentialType(objRecord.supportedIdentity, log!)
296+
!isFabricSigningCredentialType(objRecord.supportedIdentity, log)
297297
) {
298-
log!.error(
298+
log.error(
299299
"FabricOptionsJSON invalid 'supportedIdentity':",
300300
objRecord.supportedIdentity,
301301
);
302302
return false;
303303
}
304-
if ("vaultConfig" in obj && !isIVaultConfig(objRecord.vaultConfig, log!)) {
305-
log!.error(
304+
if ("vaultConfig" in obj && !isIVaultConfig(objRecord.vaultConfig, log)) {
305+
log.error(
306306
"FabricOptionsJSON invalid 'vaultConfig':",
307307
objRecord.vaultConfig,
308308
);
309309
return false;
310310
}
311311
if (
312312
"webSocketConfig" in obj &&
313-
!isIWebSocketConfig(objRecord.webSocketConfig, log!)
313+
!isIWebSocketConfig(objRecord.webSocketConfig, log)
314314
) {
315-
log!.error(
315+
log.error(
316316
"FabricOptionsJSON invalid 'webSocketConfig':",
317317
objRecord.webSocketConfig,
318318
);
319319
return false;
320320
}
321321
if (
322322
"signCallback" in obj &&
323-
!isSignPayloadCallback(objRecord.signCallback, log!)
323+
!isSignPayloadCallback(objRecord.signCallback, log)
324324
) {
325-
log!.error(
325+
log.error(
326326
"FabricOptionsJSON invalid 'signCallback':",
327327
objRecord.signCallback,
328328
);

packages/cactus-plugin-satp-hermes/src/main/typescript/services/validation/config-validating-functions/validate-key-pair-json.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1+
import { Logger } from "@hyperledger/cactus-common";
2+
13
export interface KeyPairJSON {
24
privateKey: string;
35
publicKey: string;
46
}
57

68
// Type guard for the keyPairJSON
7-
export function isKeyPairJSON(obj: unknown): obj is KeyPairJSON {
8-
return (
9-
typeof obj === "object" &&
10-
obj !== null &&
11-
"privateKey" in obj &&
12-
typeof (obj as Record<string, unknown>).privateKey === "string" &&
13-
"publicKey" in obj &&
14-
typeof (obj as Record<string, unknown>).publicKey === "string"
15-
);
9+
export function isKeyPairJSON(obj: unknown, log: Logger): obj is KeyPairJSON {
10+
if (typeof obj !== "object" || obj === null) {
11+
log.error("isKeyPairJSON: obj is not an object or is null");
12+
return false;
13+
} else if (!("privateKey" in obj)) {
14+
log.error("isKeyPairJSON: 'privateKey' property missing");
15+
return false;
16+
} else if (typeof (obj as Record<string, unknown>).privateKey !== "string") {
17+
log.error("isKeyPairJSON: 'privateKey' property is not a string");
18+
return false;
19+
} else if (!("publicKey" in obj)) {
20+
log.error("isKeyPairJSON: 'publicKey' property missing");
21+
return false;
22+
} else if (typeof (obj as Record<string, unknown>).publicKey !== "string") {
23+
log.error("isKeyPairJSON: 'publicKey' property is not a string");
24+
return false;
25+
}
26+
return true;
1627
}
1728

18-
export function validateSatpKeyPairJSON(opts: {
19-
readonly configValue: unknown;
20-
}): KeyPairJSON | undefined {
29+
export function validateSatpKeyPairJSON(
30+
opts: {
31+
readonly configValue: unknown;
32+
},
33+
log: Logger,
34+
): KeyPairJSON | undefined {
2135
if (!opts || !opts.configValue) {
2236
return;
2337
}
2438

25-
if (!isKeyPairJSON(opts.configValue)) {
39+
if (!isKeyPairJSON(opts.configValue, log)) {
2640
throw new TypeError(
2741
`Invalid config.keyPair: ${JSON.stringify(opts.configValue)}. Expected a keyPair object with 'publicKey' and 'privateKey' string fields.`,
2842
);

0 commit comments

Comments
 (0)