Skip to content

Commit

Permalink
feat(wip): eth scripts and fixes done
Browse files Browse the repository at this point in the history
  • Loading branch information
outSH committed Jul 19, 2024
1 parent ad23aec commit 7d9e414
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@
"type": "string"
},
"gasConfig": {
"$ref": "#/components/schemas/GasTransactionConfig",
"nullable": false
"$ref": "#/components/schemas/GasTransactionConfig"
}
}
},
Expand Down Expand Up @@ -365,8 +364,7 @@
"additionalProperties": false,
"properties": {
"contractJSON": {
"$ref": "#/components/schemas/ContractJSON",
"nullable": false
"$ref": "#/components/schemas/ContractJSON"
}
}
},
Expand All @@ -376,8 +374,7 @@
"additionalProperties": false,
"properties": {
"contractJSON": {
"$ref": "#/components/schemas/ContractJSON",
"nullable": false
"$ref": "#/components/schemas/ContractJSON"
},
"contractAddress": {
"type": "string",
Expand Down Expand Up @@ -412,12 +409,10 @@
"additionalProperties": false,
"properties": {
"web3SigningCredential": {
"$ref": "#/components/schemas/Web3SigningCredential",
"nullable": false
"$ref": "#/components/schemas/Web3SigningCredential"
},
"transactionConfig": {
"$ref": "#/components/schemas/EthereumTransactionConfig",
"nullable": false
"$ref": "#/components/schemas/EthereumTransactionConfig"
},
"timeoutMs": {
"type": "number",
Expand All @@ -443,18 +438,15 @@
"additionalProperties": false,
"properties": {
"web3SigningCredential": {
"$ref": "#/components/schemas/Web3SigningCredential",
"nullable": false
"$ref": "#/components/schemas/Web3SigningCredential"
},
"contract": {
"oneOf": [
{
"$ref": "#/components/schemas/ContractJsonDefinition",
"description": "Send contract ABI directly in the request."
"$ref": "#/components/schemas/ContractJsonDefinition"
},
{
"$ref": "#/components/schemas/ContractKeychainDefinition",
"description": "Read contract definition from the keychain plugin."
"$ref": "#/components/schemas/ContractKeychainDefinition"
}
],
"nullable": false
Expand All @@ -466,8 +458,7 @@
"items": {}
},
"gasConfig": {
"$ref": "#/components/schemas/GasTransactionConfig",
"nullable": false
"$ref": "#/components/schemas/GasTransactionConfig"
},
"value": {
"type": "string",
Expand All @@ -484,15 +475,12 @@
"contract": {
"oneOf": [
{
"$ref": "#/components/schemas/DeployedContractJsonDefinition",
"description": "Send contract ABI and address directly in the request."
"$ref": "#/components/schemas/DeployedContractJsonDefinition"
},
{
"$ref": "#/components/schemas/ContractKeychainDefinition",
"description": "Read contract definition from the keychain plugin."
"$ref": "#/components/schemas/ContractKeychainDefinition"
}
],
"nullable": false
]
},
"methodName": {
"description": "The name of the contract method to invoke.",
Expand All @@ -508,17 +496,13 @@
"items": {}
},
"invocationType": {
"$ref": "#/components/schemas/EthContractInvocationType",
"nullable": false,
"description": "Indicates wether it is a CALL or a SEND type of invocation where only SEND ends up creating an actual transaction on the ledger."
"$ref": "#/components/schemas/EthContractInvocationType"
},
"web3SigningCredential": {
"$ref": "#/components/schemas/Web3SigningCredential",
"nullable": false
"$ref": "#/components/schemas/Web3SigningCredential"
},
"gasConfig": {
"$ref": "#/components/schemas/GasTransactionConfig",
"nullable": false
"$ref": "#/components/schemas/GasTransactionConfig"
},
"value": {
"type": "string"
Expand Down Expand Up @@ -599,7 +583,6 @@
"type": "string"
},
"invocationType": {
"description": "Contract invocation method to be performed (send, call, etc...)",
"$ref": "#/components/schemas/EthContractInvocationWeb3Method"
},
"invocationParams": {
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-plugin-persistence-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"web3-validator": "2.0.2"
},
"devDependencies": {
"@hyperledger/cactus-cmd-api-server": "2.0.0-alpha.2",
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0-alpha.2",
"@hyperledger/cactus-test-geth-ledger": "2.0.0-alpha.2",
"@hyperledger/cactus-test-tooling": "2.0.0-alpha.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Common setup code for the persistence plugin with detailed comments on each step.
* Requires environment variable `SUPABASE_CONNECTION_STRING` to be set before running the script that includes this!
*/

import process, { exit } from "process";
import { v4 as uuidV4 } from "uuid";
import {
LoggerProvider,
Logger,
LogLevelDesc,
} from "@hyperledger/cactus-common";
import { PluginRegistry } from "@hyperledger/cactus-core";
import { Configuration } from "@hyperledger/cactus-core-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import {
EthereumApiClient,
PluginLedgerConnectorEthereum,
} from "@hyperledger/cactus-plugin-ledger-connector-ethereum";
import {
ApiServer,
AuthorizationProtocol,
ConfigService,
} from "@hyperledger/cactus-cmd-api-server";

import { PluginPersistenceEthereum } from "../../../main/typescript/plugin-persistence-ethereum";

//////////////////////////////////
// Constants
//////////////////////////////////

const SUPABASE_CONNECTION_STRING = process.env.SUPABASE_CONNECTION_STRING ?? "";
if (!SUPABASE_CONNECTION_STRING) {
console.error(
"Please set SUPABASE_CONNECTION_STRING environment variable before running this script",
);
exit(1);
}

const testLogLevel: LogLevelDesc = "info";
const sutLogLevel: LogLevelDesc = "info";

// Logger setup
const log: Logger = LoggerProvider.getOrCreate({
label: "common-setup-methods",
level: testLogLevel,
});

/**
* Common ApiServer instance, can be empty if setup was not called yet!
*/
let apiServer: ApiServer;

//////////////////////////////////
// Methods
//////////////////////////////////

/**
* Setup Cacti ApiServer instance containing Ethereum Connector plugin (for accessing the ethereum ledger)
* and Ethereum Persistence plugin (for storing data read from ledger to the database).
*
* @param port Port under which an ApiServer will be started. Can't be 0.
* @param rpcApiHttpHost Ledger RPC HTTP URL
* @param rpcApiWsHost Ledger RPC WS URL
*/
export async function setupApiServer(
port: number,
rpcApiHttpHost: string,
rpcApiWsHost: string,
) {
// PluginLedgerConnectorEthereum requires a keychain plugin to operate correctly, ensuring secure data storage.
// For testing and debugging purposes, we use PluginKeychainMemory, which stores all secrets in memory (remember: this is not secure!).
const keychainPlugin = new PluginKeychainMemory({
instanceId: uuidV4(),
keychainId: uuidV4(),
backend: new Map([]),
logLevel: testLogLevel,
});

// We create ethereum connector instance. It will connect to the ledger through RPC endpoints rpcApiHttpHost and rpcApiWsHost.
const connector = new PluginLedgerConnectorEthereum({
instanceId: uuidV4(),
rpcApiHttpHost,
rpcApiWsHost,
logLevel: sutLogLevel,
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
});
await connector.onPluginInit();

// We need an `EthereumApiClient` to access `PluginLedgerConnectorEthereum` methods from our `PluginPersistenceEthereum`.
const apiConfig = new Configuration({ basePath: `http://127.0.0.1:${port}` });
const apiClient = new EthereumApiClient(apiConfig);

// We create persistence plugin, it will read data from ethereum ledger through `apiClient` we've just created,
// and push it to PostgreSQL database accessed by it's SUPABASE_CONNECTION_STRING (read from the environment variable)
const persistence = new PluginPersistenceEthereum({
apiClient,
logLevel: sutLogLevel,
instanceId: uuidV4(),
connectionString: SUPABASE_CONNECTION_STRING,
});
// Plugin initialization will check connection to the database and setup schema if needed.
await persistence.onPluginInit();

// The API Server is a common "container" service that manages our plugins (connector and persistence).
// We use a sample configuration with most security measures disabled for simplicity.
log.info("Create ApiServer...");
const configService = new ConfigService();
const cactusApiServerOptions = await configService.newExampleConfig();
cactusApiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
cactusApiServerOptions.configFile = "";
cactusApiServerOptions.apiCorsDomainCsv = "*";
cactusApiServerOptions.apiTlsEnabled = false;
cactusApiServerOptions.apiPort = port;
const config = await configService.newExampleConfigConvict(
cactusApiServerOptions,
);

apiServer = new ApiServer({
config: config.getProperties(),
pluginRegistry: new PluginRegistry({ plugins: [connector, persistence] }),
});

const apiServerStartOut = await apiServer.start();
log.debug(`apiServerStartOut:`, apiServerStartOut);
// Our setup is operational now!

return persistence;
}

/**
* Cleanup all the resources allocated by our Api Server.
* Remember to call it before exiting!
*/
export async function cleanupApiServer() {
log.info("cleanupApiServer called.");

if (apiServer) {
log.info("Shutdown the server...");
await apiServer.shutdown();
}
}
Loading

0 comments on commit 7d9e414

Please sign in to comment.