Skip to content

helenchw/http-proxy-for-log-on-blockchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Basic blockchain-backed HTTP Log Server

Overview

This repository contains an implementation of an HTTP(S) server and a smart contract for audit log. The audit log targets data transfer scenarios under International Data Spaces (IDSA) in specific to the workflow of Eclipse EDC data connectors. The smart contract implementation assumes the blockchain is Ethereum-compatible.

HTTP Server

The HTTP(S) server provides four APIs for logging information on the start and end of (i) contract negotiation and (ii) data transfer. The server API specification can be found in http-server-openapi.yaml.

Smart Contract for Audit Logging

The smart contract tracks the established contracts and their data transfer progress. It emits events to an Ethereum-compatible blockchain to log the operation metadata to the blockchain.

Tools

Tools for reading back (and decoding) the information logged in the blockchain are also provided as a reference for further development.

How to prepare the environment

  • Download and install the prerequisites - Nodejs, following the guide at Nodejs official site: https://nodejs.org/en/download

    • The recommended (and tested) versions are as follows for reference
      • node: v20.12.2
      • npm: 10.5.0
  • Install the required Nodejs packages

    npm install

How to run

The following provides the steps to run the system in an local environment as an example.

To run with a private/public blockchain, specify the correct blockchain JSON-RPC endpoints and wallet (public and private keys) for smart contract deployment and server run.

Compile the smart contract

  1. Go to the 'smart_contracts' folder

    cd smart_contracts
  2. Compile the smart contract using Hardhat compiler

    npx hardhat compile
  3. Go back to the root folder

    cd ..

Deploy the smart contract

In a new terminal,

  1. Go to the smart contract folder

    cd smart_contracts
  2. Run a local Hardhat single-node blockchain

    npx hardhat node

    which gives an output similar to the following on success.

    Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
    
    Accounts
    ========
    
    WARNING: These accounts, and their private keys, are publicly known.
    Any funds sent to them on Mainnet or any other live network WILL BE LOST.
    
    Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
    Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
    
    Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (10000 ETH)
    Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
                                                                                  
    Account #2: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC (10000 ETH)
    Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
    
    Account #3: 0x90F79bf6EB2c4f870365E785982E1f101E93b906 (10000 ETH)
    Private Key: 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
    
    Account #4: 0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65 (10000 ETH)
    Private Key: 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a
    

    The JSON-RPC endpoint is therefore 'http://127.0.0.1:8545/'. We can pick a pair of private and public keys from any account, e.g., for account #0, the public key '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' and the private key '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'.

In the original terminal,

  1. Go to the 'tools' folder

    cd tools
  2. Update the JSON-RPC endpoint (<json-rpc endpoint>), wallet private key (<wallet private key>), and wallet public key (<wallet public key>) in config.js

    module.exports = {
      ethereum: {
        endpoint: '<json-rpc endpoint>',
        private_key: '<wallet private key>',
        public_key: '<wallet public key>'
      },
      contract: {
        abi_file_path: '../smart_contracts/artifacts/contracts/DataTransferAuditLog.sol/DataTransferAuditLog.json'
      }
    };
  3. Run deploy_contract.js to deploy the contract

    node deploy_contract.js

    which gives an output similar to the following on success.

    Deployer account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
    (node:5046) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    Estimated gas: 2231966n
    Contract deployed at address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
    

    And here shows the contract address in the blockchain is '0x5FbDB2315678afecb367f032d93F642f64180aa3'.

  4. Go back to the root folder

    cd ..

Run the HTTP server

In a new terminal,

  1. Go to the 'server/src' folder

    cd server/src
  2. Update the JSON-RPC endpoint (<json-rpc endpoint>), wallet private key (<wallet private key>), wallet public key (<wallet public key>), and deployed contract address (<contract address>) in config.js. The wallet should be different from that used for contract deployment.

    module.exports = {
      server: {
        port: 3000,
        api_key: "myapikey1234",
        ssl: {
          key_file_path: '',
          cert_file_path: '',
        }
      },
      contract: {
        abi_file_path: '../../smart_contracts/artifacts/contracts/DataTransferAuditLog.sol/DataTransferAuditLog.json',
      },
      ethereum: {
        endpoint: '<json-rpc endpoint>',
        private_key: '<wallet private key>',
        public_key: '<wallet public key>',
        contract_address: '<contract address>'
      },
      misc: {
        check_ssl_cert: false,
        async_save_activity: false,
      },
    };
  3. Start the server

    node index.js

Test the APIs

In the original terminal,

  1. Log the start of a contract negotiation

    curl -v http://localhost:3000/contract/negotiation/start -X POST -H 'api_key:myapikey1234' -d@resources/contract_negotiation_start.json
  2. Log the end a contract negotiation

    curl -v http://localhost:3000/contract/negotiation/end -X POST -H 'api_key:myapikey1234' -d@resources/contract_negotiation_end.json
  3. Log the start of a data transfer

    curl -v http://localhost:3000/transfer/start -X POST -H 'api_key:myapikey1234' -d@resources/data_transfer_start.json
  4. Log the end of a data transfer

    curl -v http://localhost:3000/transfer/end -X POST -H 'api_key:myapikey1234' -d@resources/data_transfer_end.json
  5. Go to the tools folder

    cd tools
  6. Display the logs in the blockchain

    node get_logs.js

Configurations

HTTP Server

The HTTP server configuration file is server/src/config.js. Following are the options.

  • server: HTTP server endpoint
    • port: server port
    • api_key: API key for authentication
    • ssl
      • key_file_path: Path to the SSL/TLS private key file
      • cert_file_path: Path to the SSL/TLS certificate file
  • contract: Smart contract
    • abi_file_path: Path to the ABI (application binary interface) specification for the blockchain smart contract
  • ethereum: Blockchain
    • endpoint: Blockchain JSON-RPC endpoint
    • private_key: Blockchain wallet private key
    • public_key: Blockchain wallet public key
    • contract_address: Address of the deployed audit logging smart contract on the blockcahin

For Developers

Source Code

  • server/: Source code for the HTTP server
  • smart_contracts/: Smart contract for audit log, test scripts and configurations for Hardhat
  • tools/: Scripts for contract deployment and log browsing
  • resources/: Example request bodies for each API (for API testing)

Test the smart contract

  1. Go to the smart contract folder

    cd smart_contracts
  2. Test the smart contract via Hardhat

    npx hardhat test