Skip to content

Commit

Permalink
feat(satp-hermes): gateway runner and docker for SATP
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Mateus <brumat315@gmail.com>
  • Loading branch information
brunoffmateus committed Sep 3, 2024
1 parent a7fa53e commit 9bd1f19
Show file tree
Hide file tree
Showing 14 changed files with 983 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@
SATP_PRIVATE_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
SATP_PUBLIC_KEY=fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210


# SATP Gateway Configuration
SATP_LOG_LEVEL=INFO

# Environment and API
SATP_NODE_ENV=development
SATP_ENABLE_OPEN_API=true

# Gateway Identity
SATP_GATEWAY_ID=gateway1
SATP_GATEWAY_NAME=ExampleGateway
SATP_GATEWAY_VERSION=v02,v02,v02
SATP_SUPPORTED_DLTS=FabricSATPGateway,BesuSATPGateway

# Detailed Version Configuration
SATP_GATEWAY_VERSION_CORE=v02
SATP_GATEWAY_VERSION_ARCHITECTURE=v02
SATP_GATEWAY_VERSION_CRASH=v02

# Proof and Ports
SATP_PROOF_ID=proof123
SATP_PROOF_ID=mockProofID1
SATP_GATEWAY_SERVER_PORT=3010
SATP_GATEWAY_CLIENT_PORT=3011
SATP_GATEWAY_GRPC_PORT=3012
SATP_GATEWAY_GRPC_PORT=4010

# Gateway Address
SATP_GATEWAY_ADDRESS=http://localhost:3010
SATP_GATEWAY_ADDRESS=http://localhost

# Counter Party Gateways (JSON array)
SATP_COUNTER_PARTY_GATEWAYS=[{"id":"gateway2","name":"OtherGateway","address":"http://other-gateway:3010"}]
SATP_COUNTER_PARTY_GATEWAYS=[{"id":"gateway2","version":"v02,v02,v02","supportedDLTs":"FabricSATPGateway,BesuSATPGateway"}] #"http://other-gateway:3010"}]

# Validation Options (JSON object)
SATP_VALIDATION_OPTIONS={"skipMissingProperties":true}

# Privacy Policies (JSON array)
SATP_PRIVACY_POLICIES=[{"policy":"GDPR","policyHash":"hash123"}]
SATP_PRIVACY_POLICIES=[{"policy":"singleTransaction","policyHash":"hash123"}]

# Merge Policies (JSON array)
SATP_MERGE_POLICIES=[{"policy":"LatestWins","policyHash":"hash456"}]
SATP_MERGE_POLICIES=[{"policy":"NONE","policyHash":"hash456"}]
71 changes: 71 additions & 0 deletions packages/cactus-plugin-satp-hermes/Dockerfile.gateway
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
FROM ubuntu:22.04

USER root
RUN groupadd -r docker
RUN usermod -aG docker root

RUN apt-get update
RUN apt-get -y install --no-install-recommends supervisor
RUN apt-get -y install --no-install-recommends openssl
RUN apt-get -y install --no-install-recommends jq
RUN apt-get -y install --no-install-recommends curl
RUN apt-get -y install --no-install-recommends file
RUN apt-get -y install --no-install-recommends ca-certificates
RUN apt-get -y install --no-install-recommends tzdata
RUN apt-get -y install --no-install-recommends git
RUN apt-get -y install --no-install-recommends apt-utils
RUN apt-get -y install --no-install-recommends net-tools
RUN apt-get -y install --no-install-recommends default-jdk

RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

ARG APP=/usr/src/app/cactus/
COPY . ${APP}

RUN mkdir -p ${APP}
RUN mkdir -p "${APP}/log/"
RUN mkdir -p /app/keys

WORKDIR ${APP}

SHELL ["/bin/bash", "--login", "-i", "-c"]
# Installing Node Version Manager (nvm)
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

RUN source ~/.bashrc && \
nvm install 18.18.2 && \
npm install -g yarn ts-node

SHELL ["/bin/bash", "--login", "-c"]

COPY ./supervisord.conf /etc/supervisord.conf
COPY ./run-satp-gateway.sh /
COPY ./tsconfig.json /usr/src/tsconfig.json
COPY ./tsconfig.json /tsconfig.json

RUN chmod +x /run-satp-gateway.sh

EXPOSE 3010 3011 4010

EXPOSE 9001

ENV SATP_PRIVATE_KEY="default_private_key"
ENV SATP_PUBLIC_KEY="default_public_key"

ENV SATP_LOG_LEVEL=INFO
ENV SATP_NODE_ENV=development
ENV SATP_ENABLE_OPEN_API=true

ENV SATP_GATEWAY_VERSION_CORE=v02
ENV SATP_GATEWAY_VERSION_ARCHITECTURE=v02
ENV SATP_GATEWAY_VERSION_CRASH=v02
ENV SATP_GATEWAY_SERVER_PORT=3010
ENV SATP_GATEWAY_CLIENT_PORT=3011
ENV SATP_GATEWAY_GRPC_PORT=4010
ENV SATP_VALIDATION_OPTIONS={"skipMissingProperties":true}
ENV SATP_PRIVACY_POLICIES=[{"policy":"singleTransaction","policyHash":"hash123"}]
ENV SATP_MERGE_POLICIES=[{"policy":"NONE","policyHash":"hash456"}]

ENTRYPOINT ["/usr/bin/supervisord"]
CMD ["--configuration", "/etc/supervisord.conf", "--nodaemon"]
54 changes: 54 additions & 0 deletions packages/cactus-plugin-satp-hermes/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: '3.8'

services:
### To build new image version:
### uncomment and run in the terminal "docker compose build"
# satp-hermes-gateway-base:
# image: ghcr.io/brunoffmateus/cactus-plugin-satp-hermes-gateway:new-version
# build:
# context: ./
# dockerfile: Dockerfile.gateway

satp-hermes-gateway1:
image: ghcr.io/brunoffmateus/cactus-plugin-satp-hermes-gateway:2024-09-02-3117
ports:
- 13010:3010/tcp # SERVER_PORT
- 13011:3011/tcp # CLIENT_PORT
environment:
- SATP_GATEWAY_ID=gateway1
- SATP_GATEWAY_NAME=ExampleGateway1
- SATP_SUPPORTED_DLTS=FabricSATPGateway,BesuSATPGateway
- SATP_GATEWAY_ADDRESS=http://localhost
- SATP_PROOF_ID=mockProofID1
- SATP_COUNTER_PARTY_GATEWAYS=[{"id":"gateway2","version":"v02,v02,v02","supportedDLTs":"FabricSATPGateway,BesuSATPGateway"}]
volumes:
- gateway1_keys:/app/keys
deploy:
resources:
limits:
memory: 4G
cpus: '0.9'

# satp-hermes-gateway2:
# image: ghcr.io/brunoffmateus/cactus-plugin-satp-hermes-gateway:2024-09-02-3117
# ports:
# - 23020:3010/tcp # SERVER_PORT
# - 23021:3011/tcp # CLIENT_PORT
# environment:
# - SATP_GATEWAY_ID=gateway2
# - SATP_GATEWAY_NAME=ExampleGateway2
# - SATP_SUPPORTED_DLTS=FabricSATPGateway,BesuSATPGateway
# - SATP_GATEWAY_ADDRESS=http://localhost
# - SATP_PROOF_ID=mockProofID2
# - SATP_COUNTER_PARTY_GATEWAYS=[{"id":"gateway1","version":"v02,v02,v02","supportedDLTs":"FabricSATPGateway,BesuSATPGateway"}]
# volumes:
# - gateway2_keys:/app/keys
# deploy:
# resources:
# limits:
# memory: 2G
# cpus: '0.5'

volumes:
gateway1_keys:
# gateway2_keys:
48 changes: 48 additions & 0 deletions packages/cactus-plugin-satp-hermes/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.PHONY: setup-env run-gateway clean

setup-env:
@echo "Setting up environment for SATP-Hermes..."

# Check if Node.js is installed, if not, install it
@if ! command -v node >/dev/null 2>&1; then \
echo "Installing Node.js 18.18.2..."; \
sudo curl -fsSL https://nodejs.org/dist/v18.18.2/node-v18.18.2-linux-x64.tar.xz | sudo tar -xJ -C /usr/local --strip-components=1; \
fi

@if ! command -v yarn >/dev/null 2>&1; then \
echo "Installing Yarn..."; \
sudo npm install -g yarn; \
fi

# Install dependencies and compile the plugin
yarn install
yarn tsc

@echo "Initializing databases..."
yarn db:init

@echo "Environment setup complete."

run-gateway:
# @echo "Building SATP Gateway Docker image..."
# docker compose build

@echo "Running SATP Gateway Docker containers..."
docker compose up

# @echo "Pushing SATP Gateway Docker image..."
# docker push ${DOCKER_IMAGE}

clean:
@echo "Cleaning up SATP-Hermes environment..."

@echo "Rolling back database migrations..."
yarn db:rollback

@echo "Removing SQLite database files..."
find src/knex -name "*.sqlite3" -type f -delete

@echo "Stopping and removing Docker containers..."
docker compose down

@echo "Cleanup complete."
11 changes: 9 additions & 2 deletions packages/cactus-plugin-satp-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
"codegen:openapi": "npm run generate-sdk",
"codegen:proto": "npm run generate-proto",
"codegen:abi": "yarn forge:all && abi-types-generator './src/solidity/generated/satp-wrapper.sol/SATPWrapperContract.json' --output='./src/main/typescript/generated'",
"db:init": "run-s db:init:local db:init:remote",
"db:init:local": "knex migrate:latest --knexfile src/knex/knexfile.ts",
"db:init:remote": "knex migrate:latest --knexfile src/knex/knexfile-remote.ts",
"db:rollback": "run-s db:rollback:local db:rollback:remote",
"db:rollback:local": "knex migrate:rollback --knexfile src/knex/knexfile.ts",
"db:rollback:remote": "knex migrate:rollback --knexfile src/knex/knexfile-remote.ts",
"generate-proto": "cd src/main/proto && buf generate --template buf.gen.yaml --config buf.yaml --verbose",
"generate-sdk": "run-p 'generate-sdk:*'",
"generate-sdk:typescript-axios-bol": "yarn bundle-openapi-yaml && yarn bundle-openapi-json && openapi-generator-cli generate -i ./src/main/yml/bol/openapi-blo-bundled.yml -g typescript-axios -o ./src/main/typescript/generated/gateway-client/typescript-axios/ --reserved-words-mappings protected=protected --enable-post-process-file",
Expand Down Expand Up @@ -110,8 +116,8 @@
"crypto-js": "4.2.0",
"dotenv": "16.4.5",
"ethers": "6.13.1",
"express": "4.17.2",
"fabric-network": "2.2.19",
"express": "4.19.2",
"fabric-network": "2.2.20",
"fs-extra": "11.2.0",
"google-protobuf": "3.21.2",
"hardhat": "2.22.5",
Expand Down Expand Up @@ -155,6 +161,7 @@
"make-dir-cli": "3.1.0",
"protobufjs": "7.2.5",
"swagger-cli": "4.0.4",
"ts-node": "10.9.1",
"typescript": "5.5.2"
},
"engines": {
Expand Down
36 changes: 36 additions & 0 deletions packages/cactus-plugin-satp-hermes/run-satp-gateway.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

generate_keys() {
# Check if we need to generate new keys
if [ "$SATP_PRIVATE_KEY" = "default_private_key" ] || [ "$SATP_PUBLIC_KEY" = "default_public_key" ]; then
echo "Generating new key pair..."
# Using OpenSSL to generate a 256-bit private key and derive public key
PRIVATE_KEY=$(openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//')
PUBLIC_KEY=$(openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout | grep pub -A 5 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^04//')

export SATP_PRIVATE_KEY=$PRIVATE_KEY
export SATP_PUBLIC_KEY=$PUBLIC_KEY
else
echo "Using existing key pair from environment variables"
fi

echo "SATP_PRIVATE_KEY: $SATP_PRIVATE_KEY"
echo "SATP_PUBLIC_KEY: $SATP_PUBLIC_KEY"
}

run_gateway() {
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 18.18.2

echo "Starting SATP Hermes Gateway CLI..."
node --max-old-space-size=2048 -r ts-node/register /usr/src/app/cactus/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway-cli.ts
echo "SATP Hermes Gateway CLI execution completed."
}

main() {
generate_keys
run_gateway
}

main
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,45 @@

import { LoggerProvider, LogLevelDesc } from "@hyperledger/cactus-common";
import { SATPGateway, SATPGatewayConfig } from "./plugin-satp-hermes-gateway";
import { SupportedChain, DraftVersions, CurrentDrafts } from "./core/types";
import { SupportedChain } from "./core/types";
import { SATP_VERSION } from "./core/constants";
import { v4 as uuidv4 } from "uuid";
import dotenv from "dotenv";
import path from "path";

export async function launchGateway(env?: NodeJS.ProcessEnv): Promise<void> {
dotenv.config();
dotenv.config({ path: path.resolve(__dirname, "../../../.env.example") });

const logLevel: LogLevelDesc =
(env?.SATP_LOG_LEVEL as LogLevelDesc) || "INFO";
(env?.SATP_LOG_LEVEL?.toUpperCase() as LogLevelDesc) || "INFO";
const logger = LoggerProvider.getOrCreate({
level: logLevel,
label: "SATP-Gateway",
});

// Parse the version string into DraftVersions object
const parseVersion = (versionString: string): DraftVersions => {
const [Core, Architecture, Crash] = versionString.split(",");
return {
[CurrentDrafts.Core]: Core,
[CurrentDrafts.Architecture]: Architecture,
[CurrentDrafts.Crash]: Crash,
};
const parseSupportedDLTs = (): SupportedChain[] => {
return env
?.SATP_SUPPORTED_DLTS!.split(",")
.filter((dlt) =>
Object.values(SupportedChain).includes(dlt as SupportedChain),
) as SupportedChain[];
};
const gatewayConfig: SATPGatewayConfig = {
gid: {
id: env?.SATP_GATEWAY_ID || "",
id: env?.SATP_GATEWAY_ID || uuidv4(),
name: env?.SATP_GATEWAY_NAME,
version: env?.SATP_GATEWAY_VERSION
? [parseVersion(env.SATP_GATEWAY_VERSION)]
: [],
supportedDLTs:
env?.SATP_SUPPORTED_DLTS?.split(",").map(
(dlt) => dlt as SupportedChain,
) || [],
version: [
{
Core: env?.SATP_GATEWAY_VERSION_CORE || SATP_VERSION,
Architecture: env?.SATP_GATEWAY_VERSION_ARCHITECTURE || SATP_VERSION,
Crash: env?.SATP_GATEWAY_VERSION_CRASH || SATP_VERSION,
},
],
supportedDLTs: env?.SATP_SUPPORTED_DLTS ? parseSupportedDLTs() : [],
proofID: env?.SATP_PROOF_ID,
gatewayServerPort: parseInt(env?.SATP_GATEWAY_SERVER_PORT || "0", 10),
gatewayClientPort: parseInt(env?.SATP_GATEWAY_CLIENT_PORT || "0", 10),
gatewayGrpcPort: parseInt(env?.SATP_GATEWAY_GRPC_PORT || "0", 10),
address: env?.SATP_GATEWAY_ADDRESS as
| `http://${string}`
| `https://${string}`
Expand Down
Loading

0 comments on commit 9bd1f19

Please sign in to comment.