Interact with Flow using Ruby by Daniel Podaru
In this article, we'll explore how to interact with Flow using the Ruby programming language. The article can also be a good example on how you can leverage any programming language that doesn't have a Flow SDK support yet to interact with Flow, expanding drastically the range of choices at your disposal.
The code for this article is available on Github Here
Use Terminal on MacOS or Command Prompt on Windows to run commands.
Before starting, check if you have Ruby installed. In the terminal, run the following command:
If you don't have Ruby installed, you can install it from https://www.ruby-lang.org.
Make a new folder for your project:
We'll interact with the Flow node using gRPC and Protocol Buffers.
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML or JSON but smaller, faster, and simpler.
gRPC is an RPC framework based on Protocol Buffers and HTTP2.
Install the grpc, grpc-tools, and json gem:
Clone the Flow repository from GitHub.
In the terminal, run the following commands:
Generate the Ruby code from the Flow .proto files.
In the terminal, run the following commands:
Copy the newly generated flow folder from protobuf/ruby/ to flow-ruby .In the terminal, run the following command:
List the flow-ruby folder contents to make sure the flow folder was copied.In the terminal, run the following command:
Fire up your favorite IDE and create a new file called flow.rb .
Add the following code to the flow.rb file:
The Flow class is a wrapper of the code that was generated from the protocol definitions.
The Flow Emulator is a lightweight tool that emulates the behaviour of the real Flow network.
We'll use the emulator to test the Ruby client. Follow these steps to install the emulator.
Start the Flow emulator.In a separate terminal window, run the following command:
Start the Ruby REPL and invoke the flow.rb file.
In the terminal, run the following command:
In the terminal window where you started the REPL, run the following commands:
We pinged the Flow emulator and it responded successfully.
To test with an existing Flow address, look in the terminal window where the Flow emulator was started and use the address of the service account.
Run the following command:
The response displays info about the Flow account with the address: 0xf8d6e0586b0a20c7.
The data is displayed as bytes, we can decode it into a hex string using unpack('H*').first.
We can see that the account has a 0 balance, and that currently one public key is authorized to access it.
A script is a read-only Cadence snippet that is used to query the computational state of the blockchain.
Run the following command:
script is a simple Cadence script that returns 1.
The script was executed by the execute_script function and the result is available in the result variable.
There are more methods that the Flow Access node and the Flow Execution node provide.
Explore the method definitions in the flow/access, flow/execution and flow/entities folders.
A transaction is a Cadence snippet that is executed to update the computational state of the blockchain. A transaction can update the storage of one or more signing accounts.
The code to execute a transaction is already generated in the flow/execution folder but the code needed to cryptographically sign the payload of a transaction can't be generated from the .proto definitions and must be implemented. In a secure environment, the signing would be handled by a Hardware Security Module (HSM), which is able to securely store private keys. If you are interested in signing transactions from Ruby, you will need to write this part yourself.
First, you'll need to install the rlp and openssl gems. Next, you'll need to take a look at the signature implementation in the Go SDK to understand how signatures work before porting the code to Ruby.
If you are developing in a programming language for which a Flow SDK is not available yet, you can still interact with Flow using that language. Protocol buffers are language-neutral and platform-neutral so you can generate code from the protocol definitions for any programming language.
The Ruby code we wrote in this article could be the start of a Flow SDK for Ruby. We encourage you to join the Flow community on Discord, where you can join forces with other developers to build Flow SDKs in your favorite programming language.
Subscribe to stay tuned for latest updates.