Hyperledger Firefly Tutorial

Hyperledger Firefly can be a bit difficult to pick up at first. But the benefits outweigh the effort. Here is the tutorial documentation of the things I picked up while using Firefly. 

Introduction


Hyperledger FireFly is an organization’s gateway to Web3, including all the blockchain ecosystems that they participate in.

Hyperledger provides you with an hub to manager all web3 connections. 

In this mode you can:


  • Transfer tokenized value

  • Invoke any other type of smart contract

  • Index data from the blockchain

  • Reliably trigger events in your applications and back-office core systems

  • Manage decentralized data (NFTs etc.)

  • Use a private address book to manage signing identities and relationships


In a multiparty system, Firefly can manage interaction between multiple supernodes. (Light nodes don't exist in the firefly ecosystem). It provides connectors between multiple nodes.


Installation

  1. Docker 

  2. Docker-compose

  3. Openssl

  4. Hyperledger firefly cli


Initialization

Write 

```

sudo ff init [stack_env_type] [extra_parameters]


The configuration I went with is


sudo ff init ethereum -n besu


Since besu is a ethereum based private blockchain network, it supports contracts written in solidity. 


After init 


Specify 

Stack name


Number of members


Important: Number of members is the number of digital signatures(number of wallets) that you will have in the network. If your network needs 100 entities with digital signatures, specify 100 as the number of nodes. 


sudo ff start [stack_name] - gbanker


Starts the ff network. Initial setup takes time as it creates and deploys docker containers. 


Contract Deployment

Now starts the main challenge(If you manage to go through all the dependency issue that is). 

First of all contracts must be compiled to generate a json file having the abi.

To compile contract you can use: solc, hardhat or truffle. Remix contract is not supported by FF’s EVM.

I found versioning problem with the current solc compiler and the EVM currently used by firefly.

So I used hardhat.


npm init


Starts a new node project. Then use

npx hardhat 

To create hardhat modules. Hardhat creates a contracts folder. Place all your contracts in that folder. Next,


npx hardhat compile


Compiles all the contracts and creates the ABI’s in the artifacts folder. Now manually go to each folder named after your contract, for example Demo.sol and you will find the Demo-metadata.json and Demo.json

Demo.json contains your api and you need to deploy this in the firefly network. 


Use


ff deploy ethereum [stack_name] [json_file_name].json


To deploy the contract to the network. You will get the contract address if it is successfully deployed. Use it in the Sandbox to add it to the blockchain. 


If the contract has multiple input in constructor:

ff deploy ethereum [stack_name] [json_file_name].json [constructor1] [constructor2]


From Bear on discord: TO INSTALL SOLC

Hello sorry for late reply, I have used this https://pypi.org/project/solc-select/


You can easy work with older version. For solve my problem I have used 0.8.19


Contract Invoke 

After properly managing the version and adding the contract to the blockchain, you will be provided with the endpoints which can be accessed, either from

Blockchain->Api of Web UI or directly from the SandboxUI


The invoke methods are to be used for payable functions. For view functions, use query methods. 


For each methods,

KEY MUST BE SPECIFIED IF THE CONTRACT SPECIFIES msg.sender. Otherwise the contract execution will not work.


Each nodes has keys. Key is the address of the invoker. Keys can be seen from Nodes->Organizations of WebUI.

Click on your organization and the ethereum_address will be displayed at the bottom. 

Also remember to randomize the idempotency key everytime. 



Creating Identity

So I was wrong when I said that the number of nodes is the number of digital signatures. You can actually create digital signatures on the network and assign them to named individuals(AMAZING). But everytime you need to run


 ff accounts create <stack_name>


It is kind of tedious. 


For the next steps, it is recommended that you have postman or insomnia installed as i couldn’t find the endpoints in swagger.


GET http://localhost:5000/api/v1/status


Returns all the organization of port 5000 and their UUID.


Next the account has to be registered. It is quite simple. 

POST http://localhost:5000/api/v1/identities


Body:

{

    "name": "myCustomIdentity",

    "key": "0xc00109e112e21165c7065da776c75cfbc9cdc5e7", // Signing Key from Step 1

    "parent": "1c0abf75-0f3a-40e4-a8cd-5ff926f80aa8" // Org UUID from Step 2

}



Remember address is your public key AKA signing key.

The new account will be registered under the node of the organization. New version of firefly requires key. 

Here organizations key is provided




To fetch identities:

http://localhost:5000/api/v1/identities?fetchverifiers=true




Can be accessed using swagger through 

http://127.0.0.1:5001/api


Tokens

At first create token pool. 


http://127.0.0.1:5000/api/v1/namespaces/default/tokens/pool


Non fungible tokens follow ERC-721 standard. 




The token pool gives an id



Use it to query further 

http://127.0.0.1:5000/api/v1/namespaces/default/tokens/pools/:id





Now you can mint tokens. 


http://127.0.0.1:5000/api/v1/namespaces/default/tokens/mint


Specify the amount of tokens you want to create and the index.


All this can be done using the Sandbox UI as well. 


The address is the address of the host (Here org 0). He has minted the token and hence he is the owner of the token



Next you can transfer the tokens

http://127.0.0.1:5000/api/v1/namespaces/default/tokens/transfers



The message field is optional.



For some reason you dont need to specify from who it is being transferred, the signing key is the node owner. 

 


Burn tokens

http://127.0.0.1:5000/api/v1/namespaces/default/tokens/burn



More functionalities i didn’t go through

Using metamask wallet to transfer tokens. Approving other wallets to transfer tokens. 



Contract Deployment using solc

Install solc-select


Solc –combined-json abi,bin demo.sol > demo.json

Event Listener


If the contract has events, you can attach event listener to it. First Register the event using

After registering you will get a event id. Keep track of that


Next to add subscriptions make a post request to

http://localhost:5000/api/v1/namespaces/default/subscriptions


This registers the node of port 5000 to the event as a subscriber

Thisis the body

{

  "namespace": "default",

  "name": "simple-storage",

  "transport": "websockets",

  "filter": {

    "events": "blockchain_event_received",

    "blockchainevent": {

      "listener": "1bfa3b0f-3d90-403e-94a4-af978d8c5b14"

    }

  },

  "options": {

    "firstEvent": "oldest"

  }

}





List of listeners can be found in 


After invoking an event it is shown in sandbox UI



Also in Blockchain event

Migrating Firefly Stack

All firefly stacks are created in root/.firefly or username/.firefly folder depending upon what privileges you have used to create the firefly stack. 




Comments