|
| 1 | +/** |
| 2 | + * Common setup code for the persistence plugin with detailed comments on each step. |
| 3 | + * Requires environment variable `SUPABASE_CONNECTION_STRING` to be set before running the script that includes this! |
| 4 | + * If not provided, a localhost instance of supabase will be assumed. |
| 5 | + */ |
| 6 | + |
| 7 | +import process from "process"; |
| 8 | +import { v4 as uuidV4 } from "uuid"; |
| 9 | +import { PluginRegistry } from "@hyperledger/cactus-core"; |
| 10 | +import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; |
| 11 | +import { |
| 12 | + LogLevelDesc, |
| 13 | + LoggerProvider, |
| 14 | + Logger, |
| 15 | +} from "@hyperledger/cactus-common"; |
| 16 | +import { Configuration } from "@hyperledger/cactus-core-api"; |
| 17 | +import { |
| 18 | + ApiServer, |
| 19 | + AuthorizationProtocol, |
| 20 | + ConfigService, |
| 21 | +} from "@hyperledger/cactus-cmd-api-server"; |
| 22 | +import { DiscoveryOptions, X509Identity } from "fabric-network"; |
| 23 | +import { |
| 24 | + DefaultEventHandlerStrategy, |
| 25 | + FabricApiClient, |
| 26 | + PluginLedgerConnectorFabric, |
| 27 | +} from "@hyperledger/cactus-plugin-ledger-connector-fabric"; |
| 28 | + |
| 29 | +import { PluginPersistenceFabric } from "../../../main/typescript"; |
| 30 | + |
| 31 | +////////////////////////////////// |
| 32 | +// Constants |
| 33 | +////////////////////////////////// |
| 34 | + |
| 35 | +const SUPABASE_CONNECTION_STRING = |
| 36 | + process.env.SUPABASE_CONNECTION_STRING ?? |
| 37 | + "postgresql://postgres:[email protected]:5432/postgres"; |
| 38 | + |
| 39 | +const testLogLevel: LogLevelDesc = "info"; |
| 40 | +const sutLogLevel: LogLevelDesc = "info"; |
| 41 | + |
| 42 | +// Logger setup |
| 43 | +const log: Logger = LoggerProvider.getOrCreate({ |
| 44 | + label: "common-setup-methods", |
| 45 | + level: testLogLevel, |
| 46 | +}); |
| 47 | + |
| 48 | +/** |
| 49 | + * Common ApiServer instance, can be empty if setup was not called yet! |
| 50 | + */ |
| 51 | +let apiServer: ApiServer; |
| 52 | + |
| 53 | +////////////////////////////////// |
| 54 | +// Methods |
| 55 | +////////////////////////////////// |
| 56 | + |
| 57 | +/** |
| 58 | + * Setup Cacti ApiServer instance containing Fabric Connector plugin (for accessing the fabric ledger) |
| 59 | + * and Fabric Persistence plugin (for storing data read from ledger to the database). |
| 60 | + * |
| 61 | + * @param port Port under which an ApiServer will be started. Can't be 0. |
| 62 | + * @param channelName Channel that we want to connect to. |
| 63 | + * @param connectionProfile Fabric connection profile (JSON object, not a string!) |
| 64 | + * @param userIdentity Signing identity to use to connect to the channel (object, not a string!) |
| 65 | + * |
| 66 | + * @returns `{ persistence, apiClient, signingCredential }` |
| 67 | + */ |
| 68 | +export async function setupApiServer( |
| 69 | + port: number, |
| 70 | + channelName: string, |
| 71 | + connectionProfile: any, |
| 72 | + userIdentity: X509Identity, |
| 73 | +) { |
| 74 | + // PluginLedgerConnectorFabric requires a keychain plugin to operate correctly, ensuring secure data storage. |
| 75 | + // We will store our userIdentity in it. |
| 76 | + // For testing and debugging purposes, we use PluginKeychainMemory, which stores all secrets in memory (remember: this is not secure!). |
| 77 | + const keychainId = uuidV4(); |
| 78 | + const keychainEntryKey = "monitorUser"; |
| 79 | + const keychainPlugin = new PluginKeychainMemory({ |
| 80 | + instanceId: uuidV4(), |
| 81 | + keychainId, |
| 82 | + backend: new Map([[keychainEntryKey, JSON.stringify(userIdentity)]]), |
| 83 | + logLevel: testLogLevel, |
| 84 | + }); |
| 85 | + const signingCredential = { |
| 86 | + keychainId, |
| 87 | + keychainRef: keychainEntryKey, |
| 88 | + }; |
| 89 | + |
| 90 | + // We create fabric connector instance with some default settings assumed. |
| 91 | + const discoveryOptions: DiscoveryOptions = { |
| 92 | + enabled: true, |
| 93 | + asLocalhost: true, |
| 94 | + }; |
| 95 | + const connector = new PluginLedgerConnectorFabric({ |
| 96 | + instanceId: uuidV4(), |
| 97 | + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), |
| 98 | + sshConfig: {}, |
| 99 | + cliContainerEnv: {}, |
| 100 | + peerBinary: "/fabric-samples/bin/peer", |
| 101 | + logLevel: sutLogLevel, |
| 102 | + connectionProfile, |
| 103 | + discoveryOptions, |
| 104 | + eventHandlerOptions: { |
| 105 | + strategy: DefaultEventHandlerStrategy.NetworkScopeAnyfortx, |
| 106 | + commitTimeout: 300, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + // Remember to initialize a plugin |
| 111 | + await connector.onPluginInit(); |
| 112 | + |
| 113 | + // We need an `FabricApiClient` to access `PluginLedgerConnectorFabric` methods from our `PluginPersistenceFabric`. |
| 114 | + const apiConfig = new Configuration({ basePath: `http://127.0.0.1:${port}` }); |
| 115 | + const apiClient = new FabricApiClient(apiConfig); |
| 116 | + |
| 117 | + // We create persistence plugin, it will read data from fabric ledger through `apiClient` we've just created, |
| 118 | + // and push it to PostgreSQL database accessed by it's SUPABASE_CONNECTION_STRING (read from the environment variable). |
| 119 | + const persistence = new PluginPersistenceFabric({ |
| 120 | + channelName, |
| 121 | + gatewayOptions: { |
| 122 | + identity: signingCredential.keychainRef, |
| 123 | + wallet: { |
| 124 | + keychain: signingCredential, |
| 125 | + }, |
| 126 | + }, |
| 127 | + apiClient, |
| 128 | + logLevel: sutLogLevel, |
| 129 | + instanceId: uuidV4(), |
| 130 | + connectionString: SUPABASE_CONNECTION_STRING, |
| 131 | + }); |
| 132 | + // Plugin initialization will check connection to the database and setup schema if needed. |
| 133 | + await persistence.onPluginInit(); |
| 134 | + |
| 135 | + // The API Server is a common "container" service that manages our plugins (connector and persistence). |
| 136 | + // We use a sample configuration with most security measures disabled for simplicity. |
| 137 | + log.info("Create ApiServer..."); |
| 138 | + const configService = new ConfigService(); |
| 139 | + const cactusApiServerOptions = await configService.newExampleConfig(); |
| 140 | + cactusApiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; |
| 141 | + cactusApiServerOptions.configFile = ""; |
| 142 | + cactusApiServerOptions.apiCorsDomainCsv = "*"; |
| 143 | + cactusApiServerOptions.apiTlsEnabled = false; |
| 144 | + cactusApiServerOptions.apiPort = port; |
| 145 | + const config = await configService.newExampleConfigConvict( |
| 146 | + cactusApiServerOptions, |
| 147 | + ); |
| 148 | + |
| 149 | + apiServer = new ApiServer({ |
| 150 | + config: config.getProperties(), |
| 151 | + pluginRegistry: new PluginRegistry({ plugins: [connector, persistence] }), |
| 152 | + }); |
| 153 | + |
| 154 | + const apiServerStartOut = await apiServer.start(); |
| 155 | + log.debug(`apiServerStartOut:`, apiServerStartOut); |
| 156 | + // Our setup is operational now! |
| 157 | + |
| 158 | + return { persistence, apiClient, signingCredential }; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Cleanup all the resources allocated by our Api Server. |
| 163 | + * Remember to call it before exiting! |
| 164 | + */ |
| 165 | +export async function cleanupApiServer() { |
| 166 | + log.info("cleanupApiServer called."); |
| 167 | + |
| 168 | + if (apiServer) { |
| 169 | + await apiServer.shutdown(); |
| 170 | + } |
| 171 | +} |
0 commit comments