Creating a JavaScript GitHub Action for Generating Ethereum Addresses

GitHub Actions allow us to automate software workflows, such as continuous integration and deployment (CI and CD), directly from our GitHub repositories. A GitHub action is a step inside a workflow that performs a specific task, which in our case, will enable generating an Ethereum address inside a workflow, using an arbitrary string as input (usually a public key).
Such action can be useful for a scenario such as a genesis.json
file that is being generated as part as a workflow (that is later copied to a Docker image), which includes pre-population of a compiled smart contract, or an account with pre-allocated balance, both requiring an address as part of their configuration.
Prerequisites
We will be using GitHub’s npm
packages in our action, so Node.js needs to be installed on our workstation, which is available here.
Example Repository
The complete GitHub action example can be downloaded or cloned from the following GitHub repository:
The GitHub action is also published from the above repository to the GitHub marketplace and can be used in other GitHub workflows:
Setup the Project
The initial setup for our action is simple:
- Create a new directory
- Run
npm init
and set the package name to something likegenerate-eth-address
(you can leave the rest with the defaults). - Run
npm install @actions/core
which holds core functionality for GitHub actions. - Run
npm install keccak256
which we will be using to generate a hash for the Ethereum address.
Writing the Action
We will start by writing the action.yml
metadata file required by GitHub, which includes information such as the inputs and outputs of the action, the main entry point, the required Node.js version, the action name, the description and more:
Next, we will write the action’s code inside the index.js
file. The code will take the input passed to the action using core.getInput(‘data’)
, create a has from it using keccak256
, create a hex
string from the result using toString(‘hex’)
, take the rightmost 20 (2 characters per byte) using slice(-40)
(as defined by the Ethereum yellow paper) and lastly, pass the result out using core.setOutput(‘address’, address)
:
Testing the Action
To test the action, we can write a simple GitHub workflow and include it ubnder the .github/workflows
directory, which in turn, is located under the actions root directory. The workflow will check out the repository, install dependencies, run the action and print the result, like the following:
Conclusion
Writing GitHub actions is a straightforward process, both for published actions and actions that are used locally in the same repository. On top of allowing us to encapsulate redundant tasks, they can also help us in other aspects, such as improving security by allowing as to pass secrets as input parameters instead of environment variables that get printed in the execution logs. Overall, they are a great tool for improving our GitHub workflows, therefore it is highly encouraged to write them and even publish them if you feel like giving back to the community.