2.0.0 • Published 9 months ago

@hyperledger/cactus-verifier-client v2.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months ago

@hyperledger/cactus-verifier-client

Summary

This package provides Verifier and VerifierFactory components that can be used to communicate with compatible Cactus ledger connectors (validators) through single, unified interface.

Supported ledger connectors

validatorTypecactus ledger connector plugin
BESU_1XBESU_2Xcactus-plugin-ledger-connector-besu
QUORUM_2Xcactus-plugin-ledger-connector-quorum
ETH_1Xcactus-plugin-ledger-connector-ethereum
CORDA_4Xcactus-plugin-ledger-connector-corda
IROHA_2Xcactus-plugin-ledger-connector-iroha2
FABRIC_2Xcactus-plugin-ledger-connector-fabric
SAWTOOTH_1Xcactus-plugin-ledger-connector-sawtooth
legacy-socketiocactus-plugin-ledger-connector-go-ethereum-socketio

VerifierFactory

  • Used to create single verifier per ledger based on pre-defined configuration.
  • See verifier-factory.test.ts for unit tests.
  • In order to use VerifierFactory or getValidatorApiClient you must manually install the connector package that provides given ledger ApiClient!
    • Example: if your project uses ethereum and corda validators, you must install cactus-plugin-ledger-connector-ethereum and cactus-plugin-ledger-connector-corda. See table above for validator to package mapping.

Usage

import {
  VerifierFactory,
  VerifierFactoryConfig,
} from "@hyperledger/cactus-verifier-client";

// Create VerifierFactory configuration that should describe all validators we want to connect to.
// This can be read from a file or typed manually, the config is a superset of cactus-cmd-socketio-server ledger plugin config.
const ledgerPluginInfo: VerifierFactoryConfig = [
    {
        validatorID: "some_legacy_connector",    // required
        validatorType: "legacy-socketio",        // required - see table above for supported validator types
        validatorURL: "https://localhost:9999",  // legacy-socketio specific config
        validatorKeyPath: "./keysUr7d10R.crt",   // legacy-socketio specific config
        ledgerInfo: {                            // optional
            ledgerAbstract: "My legacy Ledger",
        },
        apiInfo: [],                             // optional
    },
    {
        validatorID: "besu_openapi_connector", // required
        validatorType: "BESU_2X",              // required - see table above for supported validator types
        basePath: "https://localhost:9999",    // besu specific config
    },
];

// Instantiate single VerifierFactory from given config in your Bussiness Logic Plugin.
const verifierFactory = new VerifierFactory(ledgerPluginInfo);

// Get ApiClient to validator with ID "myBesuValidatorId"
// Second argument will determine type of returned Verifier (BesuApiClient in this case)
const myBesu: Verifier<BesuApiClient> = await sut.getVerifier("myBesuValidatorId", "BESU_1X"))

// Second argument can be ignored for backward-compatibility
// It will return Verifier<(union of all supported ApiClients)>
const client: Verifier<any> = sut.getVerifier(validatorId);

Verifier

Construction

// Use VerifierFactory to get an instance of ledger connector Verifier
const myBesu: Verifier<BesuApiClient> = sut.getVerifier("myBesuValidatorId", "BESU_1X"))

// Or create it manually from ledger ApiClient
const besuApiClientOptions = new BesuApiClientOptions({
    basePath: "https://localhost:9999",
});
const apiClient = new BesuApiClient(besuApiClientOptions);
const myBesu: Verifier<BesuApiClient> = new Verifier("besu_openapi_connector", apiClient, "info");

Interface

export interface IVerifier {
  // Immediately sends request to the validator, doesn't report any error or responses.
  sendAsyncRequest(
    contract: Record<string, unknown>,
    method: Record<string, unknown>,
    args: Record<string, unknown>,
  ): Promise<void>;

  // Sends request to be executed on the ledger, watches and reports any error and the response from a ledger.
  sendSyncRequest(
    contract: Record<string, unknown>,
    method: Record<string, unknown>,
    args: Record<string, unknown>,
  ): Promise<any>;

  // Start monitoring for new events / blocks from underlying ledger.
  startMonitor(
    id: string,
    options: Record<string, unknown>,
    eventListener: IVerifierEventListener,
  ): void;

  // Stops the monitor for specified app, removes it's subscription from internal storage.
  stopMonitor(id?: string): void;
}