← Return To Home

Publishing and Subscribing to Azure Blockchain Services events using Azure Logic Apps

Blockchain continues to be one of those mysterious technologies that are marred in controversy. There are several different camps with their own perspectives. We have those who feel it is truly a transformational technology due to its trust-building characteristics and its ability to embed transparency in every single transaction. We have some people, like Warren Buffett, in the middle of this spectrum who feels blockchain technology is important, but that cryptocurrency has no value at all. In addition, we have sceptics that claim blockchain is too slow to have any material impact on the industry. For example, credit cards and other traditional transaction processing systems can handle tens of thousands of transactions per second vs 3-7 transactions per second on the Bitcoin blockchain and about 15 transactions per second on the Ethereum blockchain.  Lastly, we have the speculative nature of cryptocurrencies, which have projects that are based-upon blockchain variant implementations.

Regardless of which camp you fall into, I firmly believe that understanding the opportunities that exist with emerging technologies is important. Some technologies fall flat on their face, whereas some technologies will have a pervasive impact on society. I also believe that we need to keep an eye on “where the puck is going”. Just because technology, in its current form, may not be ready for primetime, doesn’t mean that it can’t be, in the not so distant future. I like to relate this situation to a Gary Vaynerchuk quote:

Don’t try to find all the answers. It’s okay to not know everything and still take that leap of faith. That’s how you learned how to ride a bicycle, how to drive a car, and that’s how you learn how to succeed.

While a lot of blockchain skepticism is warranted, I am still optimistic that a future iteration of today’s blockchain will emerge as a useful tool for commerce. Until that day arrives, I am currently monitoring and learning about technology whenever time permits.

Microsoft, Amazon, and IBM are some of the technology majors that have been making significant investments in blockchain technology. In this post, we are going to discuss a popular, open-source blockchain implementation called Ethereum and the offerings that Microsoft is building around it inside of Azure. Since this blog post is primarily focused on integration technologies, we are going to focus on the new Azure Logic Apps (and Microsoft Flow) Ethereum connector that we can use to publish and subscribe to Ethereum blockchain events.

Announcement on Azure Blockchain Service

At the recent Build conference in Seattle, Microsoft announced a new blockchain offering called Azure Blockchain Service.

Here is what Scott Guthrie had to say about the service:

“Blockchain is showing potential across many industries to manage complex workflows and logistics. Last year we announced Azure Blockchain Workbench, which gave developers a simple UI to model blockchain applications on a preconfigured Azure-supported network. Today we are doubling down on our investments to empower blockchain developers by announcing Azure Blockchain Service, which simplifies the formation, management, and governance of consortium blockchain networks, allowing businesses to focus on workflow logic and app development. Azure Blockchain Service deploys a fully managed consortium network and offers built-in governance for common management tasks, such as adding new members, setting permissions and authenticating user applications.”

Now within the announcement, notice that Scott mentioned the term “consortium blockchain networks”. Therefore, some of the content in the introduction paragraph is important. With this implementation, we are not talking about cryptocurrency, but rather generating transactions across business partners within a closed network. There are no mining rewards, no price volatility, just a secure platform for commerce to take place.

Provisioning an Azure Blockchain Service Instance

Before we can start talking about Azure Logic Apps connectivity, we need to provision an Azure Blockchain Service instance on Azure. The steps required to provision an Azure Blockchain Service have been captured in the Azure product documentation.

Something to be mindful of when provisioning your instance is the validator nodes that are required. By default, Standard instances are provisioned which have a significant cost. For the purpose of this post, we can use the Basic version which is around 1/7th of the cost.

Provisioning an Azure Blockchain Service instance

Once your deployment is complete, you will find an Azure Blockchain Service instance within your specified Resource Group.

Azure Blockchain Services events using Azure Logic Apps

An area that you need to become familiar with your Transaction node endpoint configuration which you can access from the Transaction nodes left navigation and then select your node.

transaction nodes

Next, click on Connection strings and note your Access keys. You will need this information later when connecting from MetaMask Azure Logic Apps.

connection strings

Also, note that the section below called Sample Code. We will also need this information when we need to create an account to send transactions from Azure Logic Apps.

Sample Code

At this point, we have our Azure Blockchain Service instance provisioned and the access required to start interacting with our blockchain.

Creating Smart Contract

One of the value propositions of using Ethereum over some other blockchain projects is the ability to create a smart contract. You can think of a smart contract much like an agreement that outlines the terms of a relationship. It may impose different responsibilities on a service requestor or service provider.

The scope of this post is not to go into a deep dive of creating smart contracts as there are many resources already available on the web.

To build a smart contract, we will use a web-based IDE called Remix. To create a new smart contract we will click on the + sign and then provide a name for our smart contract called myPostBox.sol.

new contract

We can now copy and paste the following code which represents our smart contract. Our smart contract is quite simple, we have 3 methods that allow us to post a message, get the message posted – kinda like a typical “echo” demo and lastly determine how many times our smart contract has been called.

pragma solidity ^0.4.22;

contract myPostBox{
    string message;
    int64 count;
    event Posted(string message);
    
    function postMsg(string text) public {
        emit Posted(text);
        count++;
        message = text;
    }
    
    function getMsg() public view returns (string) {
        return message;   
    }
    
    function getCount() public view returns (int64) {
        return count;
    }
}

A couple of things to note:

  • On the first line, we will see the following statement: pragma solidity ^0.4.22; What this means is that this contract requires version 4.22 of the solidity compiler or higher. When it comes to choosing your compiler version, keep this in mind.
  • To address this, I have chosen the compiler version: V0.4.22+commit.4cb486ee.Emscripten.clang which will provide for a successful compilation.

          clean Compile

 

At this point, we have a compiled version of our smart contract, but we haven’t deployed it anywhere. We now want to deploy it to our Azure Blockchain Services instance. We can perform this deployment from Remix, but we need to establish a connection to Azure first. We can do that using a chrome extension called MetaMask. Once we have it installed, we can click on it from the top right-hand corner of our Chrome browser.

meta mask

By default, MetaMask can connect to a variety of Ethereum networks including Main Ethereum Network and test networks. Since we have a private network, we will select Custom RPC.

ethNetworks

Recall earlier in the post when we discussed Azure Blockchain Service Connection strings? This is where we need to use one of our HTTP Access Keys as our New RPC URL. In addition, we need to provide a Network Name.

create Connection

With your Remix tab still active in Chrome, click on the MetaMask icon again and then click on the Connect button.

connect

We can now deploy our smart contract by clicking on the Run tab inside of Remix. For Environment, we need to select Injected Web3 which represents the network we just created in MetaMask. We can now click on the Deploy button to deploy our smart contract.

12-deploy

At the bottom of the Remix IDE, you see a message that looks like the following that confirms your contract has been deployed.

13-deployedcontract

Let’s now go ahead and test our smart contract. We can do so by clicking on our deployed smart contract instance.

14-launchContract

We can then call any of the 3 methods that are exposed. To start with, let’s provide a string text message and then click on postMsg button.

Note: Include quotes around your text

15-postmessage

We will see an updated message in our Remix console:

[block:82387 txIndex:0]from:0xd14…604aeto:myPostBox.postMsg(string) 0x2e8…2afe1value:0 weidata:0xb0f…00000logs:1hash:0xf6b…a2c4b

We can now go ahead and click on getMsg button which should now return this value from our smart contract.

16-getmessage

We can also click on the getCount button to see how many times we have posted a message.

Subscribing to Ethereum Events using Azure Logic Apps

Within our smart contract, a declaration exists for an event called Posted that takes a string. We subsequently call this event within our postMsg.

17-events

 Using Azure Logic Apps we can subscribe to these events. Why would we want to do this? Blockchains are primarily good a tracking transactions in a permission-less, distributed manner. It is not a great tool to integrate with other systems like ERPs, CRMs or even reporting platforms. But, Azure Logic Apps is really good as a connectivity platform that can connect with all of these systems. By using Azure Logic Apps we can subscribe to blockchain events and then integrate with other systems.

Let’s now connect Azure Logic Apps to our Ethereum blockchain instance. To do so we need to perform the following steps:

  1. We need to search for the Ethereum Blockchain connector and then select the When a smart contract event occurs trigger.

    Subscribing to Ethereum Events using Azure Logic Apps
  2. We now need to establish a connection by providing a Connection Name and Ethereum RPC Endpoint. The connection name is naturally arbitrary, but for our Ethereum RPC Endpoint, we need to provide one of our HTTPS Action Keys that we discussed earlier in this post when connecting from MetaMask.

    Note:Since we are only using this connection to read events off of the blockchain, we do not need a Private Key. However, when we submit events to the blockchain later in this post, we will require a private key.

    19-createconnection
  3. With our connection established, we now need to configure some additional details, including Contract ABI, Smart Contract Address and Event Name.

  4. Our Contract ABI can be retrieved from our Remix session. Within Remix, we can click on the Compile tab and then click on the ABI label which will copy this definition for us. You can think of an ABI, kinda like Swagger for an API. It defines the messaging contract between two parties. Once we have our ABI definition, we can paste it into Azure Logic Apps.

    20-ABI
  5. Our Smart Contract Address can also be retrieved from Remix. From the Run tab, we can click on the clipboard beside our deployed smart contract. We can now paste this value into our Logic App trigger.

    21-smartcontract address
  6. With both our ABI and Smart Contract Address populated, we can now select our Event Name which is called Posted.

    22-eventName
  7. Since the Ethereum trigger is a polling trigger, we can change this value as appropriate.

  8. To keep things simple, we will just publish these events to a Request Bin instance to demonstrate that we can take these blockchain events and publish them elsewhere. As you would expect from a Logic Apps trigger, we have access to Dynamic content which we can use to populate our HTTP Body message.

    23-HTTP

    We can now go ahead and test our logic app by adding by calling our postMsg function in our smart contract.

    next Azure Logic Apps polling interval
  9. In our next Azure Logic Apps polling interval, we will discover that it has picked up this event and published it to Request Bin

    next Azure Logic Apps polling interval

Publishing Transactions to Azure Blockchain Service from Azure Logic Apps

The process for publishing is very similar to subscribing with one major difference. We need to have a private key to sign our message before it is added to the blockchain. Private keys are critical in security blockchain applications which you can read more about here.

Perhaps I have missed something, but when provisioning an Azure Blockchain Service instance, nowhere has a private key been provided to me and the credentials that are found within our HTTP Access keys are not private keys.

However, we can use the information found in our endpoint configurations to create an account and subsequently a private key. A walkthrough does exist in the Azure documentation that outlines the process of connecting to our blockchain instance using command line tools. The most significant tool that we need is called Truffle. But, Truffle has some dependencies. I struggled to get all of this working, but after installing:

  • js
  • Python
  • Configured Windows Python PATH variable
  • C++ (by creating a Visual Studio C++ project)
  • Truffle
  • Web3

I was able to get this all working, but getting all of the pre-requisites took some determination and perseverance. Bottom line, if you can’t get Web3 to be installed cleanly, you won’t be able to create an account/private key on your blockchain instance.

  1. With our prerequisites installed from the walkthrough, we need to connect to our blockchain instance using truffle. From a Node.js command prompt (as Administrator) issue the following commands:
    truffle develop
    truffle(develop)> var Web3 = require("Web3");
    truffle(develop)> var provider = new Web3.providers.HttpProvider("<Your HTTP Access Key");
    truffle(develop)> var web3 = new Web3(provider);
    
    connect to our blockchain instance using truffle.
  2. Now create a new account with the following statement: web3.eth.accounts.create(); The result will be a new account is created which includes the address of the account and the private key which is exactly what we need to send transactions from Azure Logic Apps.

    send transactions from Azure Logic Apps
  3. We will now use this information to create a new connection in Azure Logic Apps that includes a private key so that we can sign our transactions. It also needs to include our Ethereum RPC Endpoint which we can once again use one of our HTTP Access Keys that we covered in the MetaMask section.

    create a new connection in Azure Logic Apps
  4. With our connection established, we need to choose our Action which in this case will be Execute smart contract function (incurring gas cost)

    send transaction
  5. Much like our subscribe example, we need to provide our ABI and Smart Contract Address. We also need to provide our Smart Contract Function Name which is postMsg and then provide our Text input.

    ABI and Smart Contract Address for Azure Blockchain Service
  6. Since we are just using a Recurrence trigger, we can just go ahead and Run our logic app.

    Run our Azure logic app.
  7. If we now go back to our logic app that is subscribing to blockchain events, we will discover that it has picked up the event related to the transaction that we just created from Azure Logic Apps.

    logic app that is subscribing to blockchain events

Conclusion

In this blog post, we introduced a new offering from Microsoft called Azure Blockchain Service. Using this service, we can establish a consortium blockchain based upon Ethereum. We also created a simple, smart contract which we deployed to our Azure blockchain instance. We then used the new Ethereum connector to both subscribe to blockchain events and to create new transactions. Azure Logic Apps provides a simple way to connect to your blockchain instance which you can subsequently integrate with many lines of business or SaaS applications. For additional Azure Blockchain Services use cases, please refer to their devkit which is hosted on GitHub.

Author: Kent Weare

Kent Weare is a Microsoft Azure MVP, he has worked on projects for the Canadian Federal Government, a multi-national bank in New York City and integrated Health Care projects throughout Canada. His role as a Senior Enterprise Architect and Integration allows him to get involved in a variety of technology projects for a large Energy company. Since 2004, Kent has been very active in the Integration space having worked with every version of BizTalk from 2004 onwards. He has also spent time using competitor tools including Intersystems, IBM and MuleSoft. He is very passionate about Architecture especially if it involves integrating different systems.