Ethereum for Ruby
A straightforward library to build, sign, and broadcast Ethereum transactions. It allows the separation of key and node management. Sign transactions and handle keys anywhere you can run Ruby and broadcast transactions through any local or remote node. Sign messages and recover signatures for authentication.
What you get:
Contents:
1. Installation
Add this line to your application's Gemfile:
gem "eth"
Or install it yourself as:
gem install eth
2. Usage
Check out
and
for full details.
2.1. Ethereum Keys and Addresses (EIP-55)
Generate a random Secp256k1 key-pair.
key = Eth::Key.new
Create an password-encrypted Ethereum key-store.
my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
key_store = Eth::Key::Encrypter.perform my_key, "secret-password-1337"
restored_key = Eth::Key::Decrypter.perform key_store, "secret-password-1337"
Manage Ethereum address objects adhering to EIP-55 checksum format.
address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
address.valid?
address.checksummed
See /spec
or Documentation for more details about key-pairs, encrypting/decrypting key-stores with a secret, and checksummed addresses.
2.2. Ethereum Signatures (EIP-191, EIP-712)
Manage keypairs to sign messages in EIP-191 (personal_sign
) format or typed data in EIP-712 (sign_typed_data
) format.
key = Eth::Key.new priv: "268be6f4a68c40f6862b7ac9aed8f701dc25a95ddb9a44d8b1f520b75f440a9a"
key.public_hex
key.address.to_s
key.personal_sign "Hello World!"
Recover and verify personal signatures respecting EIPs 155, 191, and 712.
address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
signature = "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c19422d"
recovered_key = Eth::Signature.personal_recover "Hello World!", signature, Eth::Chain::GOERLI
Eth::Util.public_key_to_address(recovered_key).to_s
Eth::Signature.verify "Hello World!", signature, address, Eth::Chain::GOERLI
See /spec
or Documentation for signing typed data as per EIP-712.
2.3. Ethereum Chains (EIP-155)
Manage Ethereum chain IDs for EIP-155 replay protection.
chain_id = Eth::Chain::OPTIMISM
v = Eth::Chain.to_v 0, Eth::Chain::OPTIMISM
recovery_id = Eth::Chain.to_recovery_id v, Eth::Chain::OPTIMISM
chain_id = Eth::Chain.to_chain_id v
2.4. Ethereum Transactions (EIP-1559, EIP-2718, EIP-2930)
Create an EIP-1559-conform transaction:
payload = {
chain_id: Eth::Chain::GOERLI,
nonce: 5,
priority_fee: 3 * Eth::Unit::GWEI,
max_gas_fee: 69 * Eth::Unit::GWEI,
gas_limit: 230_420,
to: "0xCaA29806044A08E533963b2e573C1230A2cd9a2d",
value: 0.069423 * Eth::Unit::ETHER,
}
tx = Eth::Tx.new payload
my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
tx.sign my_key
tx.hex
This gem also supports access lists and ABI-encoded data payloads. See /spec
or Documentation for more details about the various supported transaction types (legacy, type-1, type-2), payload parameters, and how to estimate intrinsic gas costs.
2.5. Ethereum ABI Encoder and Decoder
Encode and decode Ethereum application binary interface data (ABI).
Eth::Util.bin_to_hex Eth::Abi.encode(["string", "address"], ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"])
Eth::Abi.decode(["string", "address"], "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9000000000000000000000000000000000000000000000000000000000000000b48656c6c6f2c20426f6221000000000000000000000000000000000000000000")
2.6. Ethereum RLP Encoder and Decoder
Serialize and deserialize Ethereum recursive-length prefix data (RLP).
Eth::Util.bin_to_hex Eth::Rlp.encode ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"]
Eth::Rlp.decode "f78b48656c6c6f2c20426f6221aa307864343936623233643631663838613863373735386663613735363064636661633762336230316639"
Or ;-)
Eth::Rlp.decode "c7c0c1c0c3c0c1c0"
2.7. Ethereum RPC-Client
Create an IPC- or HTTP-RPC-API client to seamlessly query the chain state, e.g., Infura over HTTPS with access token:
infura = Eth::Client.create "https://mainnet.infura.io/v3/#{access_token}"
deposit_contract = Eth::Address.new "0x00000000219ab540356cBB839Cbe05303d7705Fa"
infura.get_balance deposit_contract
Or set up a local development environment with geth --dev
:
cli = Eth::Client.create "/tmp/geth.ipc"
cli.eth_coinbase
tx = cli.transfer_and_wait(Eth::Key.new.address, 1337 * Eth::Unit::ETHER)
cli.eth_get_transaction_by_hash tx
cli.get_balance "0x311c61e5dc6123ad016bb7fd687d283c327bcd5f"
cli.get_nonce cli.eth_coinbase["result"]
Check out Eth::Api
for a list of supported RPC-APIs or consult the Documentation for more details.
2.8. Solidity Compiler Bindings
Link a system-level Solidity compiler (solc
) to your Ruby library and compile contracts.
solc = Eth::Solidity.new
contract = solc.compile "spec/fixtures/contracts/greeter.sol"
The contract["Greeter"]["bin"]
could be directly used to deploy the contract as Eth::Tx
payload. Check out the Documentation for more details.
2.9. Interact with Smart Contract
Create, compile, and deploy smart contracts.
contract = Eth::Contract.from_file(file: 'spec/fixtures/contracts/dummy.sol')
cli = Eth::Client.create "/tmp/geth.ipc"
address = cli.deploy_and_wait(contract)
Transact with or call the deployed contract.
cli.transact_and_wait(contract, "set", 1234)
cli.call(contract, "get")
Or call an existing contract, e.g., the ENS registry:
ens_registry_abi = '[{"inputs":[{"internalType":"contract ENS","name":"_old","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"node","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"label","type":"bytes32"},{"indexed":false,"internalType":"address","name":"owner","type":"address"}],"name":"NewOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"node","type":"bytes32"},{"indexed":false,"internalType":"address","name":"resolver","type":"address"}],"name":"NewResolver","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"node","type":"bytes32"},{"indexed":false,"internalType":"uint64","name":"ttl","type":"uint64"}],"name":"NewTTL","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"node","type":"bytes32"},{"indexed":false,"internalType":"address","name":"owner","type":"address"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"old","outputs":[{"internalType":"contract ENS","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"}],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"}],"name":"recordExists","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"}],"name":"resolver","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"resolver","type":"address"},{"internalType":"uint64","name":"ttl","type":"uint64"}],"name":"setRecord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"address","name":"resolver","type":"address"}],"name":"setResolver","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"bytes32","name":"label","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"}],"name":"setSubnodeOwner","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"bytes32","name":"label","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"resolver","type":"address"},{"internalType":"uint64","name":"ttl","type":"uint64"}],"name":"setSubnodeRecord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"},{"internalType":"uint64","name":"ttl","type":"uint64"}],"name":"setTTL","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"}],"name":"ttl","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"payable":false,"stateMutability":"view","type":"function"}]'
ens_registry_address = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
ens_registry_name = "ENSRegistryWithFallback"
ens_registry = Eth::Contract.from_abi(name: ens_registry_name, address: ens_registry_address, abi: ens_registry_abi)
ens_registry.address
cli.call(ens_registry, "old")
The gem also comes with an EIP-1271 smart-contract authentification interface.
cli.is_valid_signature contract, hash, signature
3. Documentation
The documentation can be found at: https://q9f.github.io/eth.rb
For any specific version, docs can be generated by yard
:
gem install bundler rdoc yard
git checkout v0.5.0
yard doc
The goal is to have 100% API documentation available.
4. Testing
The test suite expects working local HTTP and IPC endpoints with a prefunded developer account, e.g.:
geth --dev --http --ipcpath /tmp/geth.ipc &
To run tests, simply use rspec
. Note, that the Ethereum test fixtures are also required.
git submodule update --init --recursive
bundle install
rspec
The goal is to have 100% specification coverage for all code inside this gem.
5. Contributing
Pull requests are welcome! To contribute, please consider the following:
- Code should be fully documented. Run
yard doc
and make sure it does not yield any warnings or undocumented sets. - Code should be fully covered by tests. Run
rspec
to make sure all tests pass. The CI has an integration that will assis you to identify uncovered lines of code and get coverage up to 100%. - Code should be formatted properly. Try to eliminate the most common issues such as trailing white-spaces or duplicate new-lines. Usage of the
rufo
gem is recommended. - Submit pull requests, questions, or issues to Github: https://github.com/q9f/eth.rb
6. License and Credits
The eth
gem is licensed under the conditions of Apache 2.0. Please see AUTHORS for contributors and copyright notices.
This gem is a complete rewrite of the old eth
gem by Steve Ellis.
It is not only a rewrite of the eth
gem but also a partial merge of the ethereum
gem by Marek Kirejczyk and Yuta Kurotaki.
This gem also includes a revised version of the ABI gem by Jan Xie and Zhang Yaning.
It also contains a condensed version of the RLP gem by Jan Xie and Zhang Yaning.