forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cactus-api-client): common verifier-factory
Prototype for common verifier-factory like the one used in cmd-socketio apps. More details in issue hyperledger-cacti#1878 Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
- Loading branch information
Showing
9 changed files
with
332 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/cactus-api-client/src/main/typescript/verifier-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2020-2022 Hyperledger Cactus Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* verifier-factory.ts | ||
*/ | ||
|
||
import { Verifier } from "./verifier"; | ||
|
||
import { | ||
Logger, | ||
LoggerProvider, | ||
LogLevelDesc, | ||
} from "@hyperledger/cactus-common"; | ||
|
||
import { | ||
SocketIOApiClient, | ||
SocketIOApiClientOptions, | ||
} from "./socketio-api-client"; | ||
|
||
import { | ||
BesuApiClient, | ||
BesuApiClientOptions, | ||
} from "@hyperledger/cactus-plugin-ledger-connector-besu"; | ||
import { ISocketApiClient } from "@hyperledger/cactus-core-api"; | ||
|
||
// All ApiClients supported by the factory must be added here! | ||
type ClientApiConfig = { | ||
"legacy-socketio": { | ||
in: SocketIOApiClientOptions; | ||
out: SocketIOApiClient; | ||
}; | ||
besu: { | ||
in: BesuApiClientOptions; | ||
out: BesuApiClient; | ||
}; | ||
}; | ||
|
||
export function getValidatorApiClient<K extends keyof ClientApiConfig>( | ||
validatorType: K, | ||
options: ClientApiConfig[K]["in"], | ||
): ClientApiConfig[K]["out"] { | ||
switch (validatorType) { | ||
case "legacy-socketio": | ||
return new SocketIOApiClient(options as SocketIOApiClientOptions); | ||
case "besu": | ||
return new BesuApiClient(options as BesuApiClientOptions); | ||
default: | ||
// Will not compile if any ClientApiConfig keys was not handled by this switch | ||
const _exhaustiveCheck: never = validatorType; | ||
return _exhaustiveCheck; | ||
} | ||
} | ||
|
||
// Verifier Factory class | ||
export type VerifierFactoryConfig = { | ||
logLevel: LogLevelDesc; | ||
validators: (Record<PropertyKey, unknown> & { | ||
validatorID: string; | ||
clientType: keyof ClientApiConfig; | ||
})[]; | ||
}; | ||
|
||
export class VerifierFactory { | ||
private verifierMap = new Map<string, Verifier<ISocketApiClient<unknown>>>(); | ||
private readonly log: Logger; | ||
|
||
readonly className: string; | ||
|
||
constructor(private readonly verifierConfig: VerifierFactoryConfig) { | ||
this.className = this.constructor.name; | ||
if ( | ||
!verifierConfig.validators || | ||
verifierConfig.validators.some((v) => !v.validatorID) || | ||
verifierConfig.validators.some((v) => !v.clientType) | ||
) { | ||
throw new Error( | ||
"Invalid VerifierFactory configuration; all validators needs to define at least 'validatorID' and 'clientType'", | ||
); | ||
} | ||
|
||
const level: LogLevelDesc = verifierConfig.logLevel || "info"; | ||
const label = this.className; | ||
this.log = LoggerProvider.getOrCreate({ level, label }); | ||
} | ||
|
||
getVerifier<K extends keyof ClientApiConfig>( | ||
validatorId: string, | ||
type?: K, | ||
): Verifier<ClientApiConfig[K]["out"]> { | ||
// Read validator config | ||
const validatorConfig = this.verifierConfig.validators.find( | ||
(v) => v.validatorID === validatorId, | ||
); | ||
if (!validatorConfig) { | ||
throw new Error( | ||
`VerifierFactory - Missing validator (type = ${type}) with ID ${validatorId}`, | ||
); | ||
} | ||
|
||
// Assert ClientApi types | ||
if (type && type !== validatorConfig.clientType) { | ||
throw new Error( | ||
`VerifierFactory - Validator ${validatorId} type mismatch; requested=${type}, config=${validatorConfig.clientType}`, | ||
); | ||
} | ||
|
||
// Return / create verifier | ||
if (this.verifierMap.has(validatorId)) { | ||
return this.verifierMap.get(validatorId) as Verifier< | ||
ClientApiConfig[K]["out"] | ||
>; | ||
} else { | ||
const clientApi = getValidatorApiClient( | ||
validatorConfig.clientType, | ||
(validatorConfig as unknown) as ClientApiConfig[K]["in"], | ||
); | ||
|
||
const verifier = new Verifier(clientApi); | ||
this.verifierMap.set(validatorId, verifier); | ||
return verifier; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.