Skip to content

🎫 ERC-20 Token implementing methods for Hashed Time-Lock transactions on Hyperledger Fabric

Notifications You must be signed in to change notification settings

rubcv/ERC20-token-HLF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ERC-20 Token implementing methods for Hashed Time-Lock transactions on Hyperledger Fabric

Table of contents

Description

This project has been developed using Hyperledger Fabric private blockchain. The blockchain has only one organization and the network topology can be seen in the image below. In addition, a REST API developed in Nest.js is available that allows connecting to the blockchain through the Fabric SDK, registering and enrolling a user through the Fabric CA, and making invocations and queries to the blockchain.

Hyperledger Firefly fabconnect can be connected to the blockchain using the configuration file that is available in the project.

Along with the blockchain, an ERC20-token Smart Contract is deployed which implements Hash TimeLock (HTLC) functions, those functions allow transfers to be blocked until the requirements are met, thus achieving atomic swaps.

Network diagram

Pre-requisites

  1. Hyperledger Fabric v2.4
  2. Hyperledger Fabric CA v1.5
  3. Docker v20.10
  4. Docker-Compose v2.14.0
  5. Golang v1.19.4
  6. NodeJS / TypeScript v16.13.0
  7. npm v9.2.0

Note: This has been developed using macOS, other OS have not been tested.

Hyperledger Fabric network

The Hyperledger Fabric network consists of:

  • One orderer
    • orderer.example.com
  • One organization
    • MSPID: Org1MSP
    • Two peers:
      • peer0.org1.example.com
      • peer1.org1.example.com
    • One CouchDB state database deployment for each peer
    • Fabric CA
      • ca.org1.example.com
  • Fabric CLI

Network deployment

Deploy the network

  1. Install Hyperledger Fabric latest docker images and binaries.

    ./install-fabric.sh

    this will generate a fabric-samples folder inside the project directory.

    Note: The fabric-samples folder is not needed, as we will only be using the downloaded Docker images to deploy the network

  2. Generate the network artifacts.

    cd network-files
    ./generate.sh

    Network deployment

  3. Deploy the network.

    ./start.sh

    Network deployment

  4. Teardown the network

    ./teardown.sh

    Network deployment

Manually test the chaincode

The ERC-20 token chaincode can be tested making use of the CLI Docker container. In order to do so, get a shell inside the container:

docker exec -it cli bash

Once inside, some transactions can be submitted using the peer chaincode command. Note that this chaincode needs to be initialized, therefore the Initialize function must be executed first.

  1. Initialize

    peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_TLS_CA -C default -n erc20token --peerAddresses $CORE_PEER_ADDRESS --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE -c '{"function":"Initialize","args":["mytoken",  "mysymbol", "2"]}' --waitForEvent

    Init chaincode

  2. Mint some tokens

    peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_TLS_CA -C default -n erc20token --peerAddresses $CORE_PEER_ADDRESS --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE -c '{"function":"Mint","args":["5000"]}' --waitForEvent

    Mint tokens

  3. Query the account balance

    peer chaincode query -C default -n erc20token -c '{"function":"ClientAccountBalance","Args":[]}'
    

    Query balance

Web Application

A Web Application is made available using Nest.js

cd webapp/

Installation

npm install

Running the app

npm start

Web Application

Using the Web Application

Fabric CA user creation

Whenever the web application is started, the user for the application is registered and enrolled on the Fabric CA making use of the Fabric SDK. The configuration is specified in webapp/src/blockchain/fabric.ts. That being the user ID, the channel and chainode names to connect to and the path in which the wallet of the user will be generated.

Web Application

Transactions

Transactions can be submitted via the web application. The Nest.js webapp supports both invokes and queries to the blockchain.

The application listens on localhost:3000 and has two endpoints:

As mentioned before, this chaincode needs to be initialized in order to use it. Therefore, the Initialize function can be called via POSTMAN. Web Application

Furthermore, an Invoke can be made via POSTMAN to Mint some tokens Web Application

  • http://localhost:3000/query

    body:

    {
        "function": "FunctionName",
        "args": ["list", "of", "arguments"]
    }

    Similarly, a Query can be made via POSTMAN to get the account balance Web Application

Firefly fabconnect

Firefly fabconnect is a reliable REST and websocket API to interact with a Fabric network and stream events.

See: https://github.com/hyperledger/firefly-fabconnect

Firefly fabconnect can be connected to the network and used to interact with it.

Steps:

  1. Pull the firefly-fabconnect repository

    git pull https://github.com/hyperledger/firefly-fabconnect.git
  2. Go into the folder and run make

    cd firefly-fabconnect
    make
  3. Firefly fabconnect needs 2 configuration files: firefily-fabconnect-config.json with all the Firefly configuration, and connection-profile.json with the Hyperledger Fabric blockchain network configuration. firefly-fabconnect-config.json can be found inside the firefly-fabconnect/ folder of this repository. connection-profile.json is the one used by the Nest.js WebAPP (Fabric SDK) to connect to the blockchain.

    Launch the connector:

    ./fabconnect -f ${PWD}/firefly-fabconnect/firefly-fabconnect-config.json

    Additionally, here is the firefly-fabconnect-config.json file's content:

    {
      "maxInFlight": 10,
      "maxTXWaitTime": 60,
      "sendConcurrency": 25,
      "receipts": {
        "maxDocs": 1000,
        "queryLimit": 100,
        "retryInitialDelay": 5,
        "retryTimeout": 30,
        "leveldb": {
          "path": "./receipts"
        }
      },
      "events": {
        "webhooksAllowPrivateIPs": true,
        "leveldb": {
          "path": "./events"
        }
      },
      "http": {
        "port": 3000
      },
      "rpc": {
        "useGatewayClient": true,
        "configPath": "../webapp/src/blockchain/utils/connection-profile.json"
      }
    }

About

🎫 ERC-20 Token implementing methods for Hashed Time-Lock transactions on Hyperledger Fabric

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages