Zum Hauptinhalt springen

How to Generate a New Ethereum Address in Ruby

Aktualisiert am
26. November 2025

4 Minuten Lesezeit

Übersicht

With high usage in web applications and straightforward syntax, Ruby is used by a vast number of people. This guide will cover creating an Ethereum address in Ruby using ruby-eth gem/package.

Voraussetzungen

  • Ruby installed in your system(Ruby 2.x +)
  • Ruby environment manager (RBEnv)
  • Ein Texteditor

Was ist eine Ethereum-Adresse?

Wenn Sie sich bei einer beliebigen Plattform im Internet anmelden, müssen Sie sich mithilfe einer Kombination aus Anmeldedaten authentifizieren. Stellen Sie sich eine Ethereum-Adresse als Ihren Benutzernamen und den dazugehörigen privaten Schlüssel als Passwort vor. Während Ihre Ethereum-Adresse öffentlich ist und weitergegeben werden kann, muss der private Schlüssel stets geheim gehalten werden. Mit dieser Kombination können Sie mit der Ethereum-Blockchain interagieren. Eine Ethereum-Adresse ist Ihre Identität in der Blockchain und sieht beispielsweise so aus: „0x6E0d01A76C3Cf4288372a29124A26D4353EE51BE“. Eine gültige Ethereum-Adresse ist erforderlich für:

  • Ethereum-Währung empfangen/senden
  • Transaktionen signieren/senden
  • Verbindung zu dezentralen Anwendungen herstellen

So wird eine Ethereum-Adresse generiert:

  • A random private key of 64 (hex) characters (256 bits / 32 bytes) is generated first. 
    For example:
0xf4a2b939592564feb35ab10a8e04f6f2fe0943579fb3c9c33505298978b74893
  • A 128 (hex) character (64 bytes) public key is then derived from the generated private key using Elliptic Curve Digital Signature Algorithm (ECDSA). 
    For example:
0x04345f1a86ebf24a6dbeff80f6a2a574d46efaa3ad3988de94aa68b695f09db9ddca37439f99548da0a1fe4acf4721a945a599a5d789c18a06b20349e803fdbbe
  • The Keccak-256 hash function is then applied to (128 characters / 64 bytes) the public key to obtain a 64 character (32 bytes) hash string. The last 40 characters / 20 bytes of this string prefixed with 0x become the final Ethereum address. 
    For example:
0xd5e099c71b797516c10ed0f0d895f429c2781142

Hinweis: „0x“ in der Codierung bedeutet, dass die Zahl bzw. die Zeichenfolge im Hexadezimalformat geschrieben ist.

What is Ruby?

Ruby is an open-source interpreted high-level language. Ruby was made with the blend of Perl, Smalltalk, Eiffel, Ada, and Lisp, Yukihiro “Matz” Matsumoto’s favorite languages. Ruby is entirely object-oriented; even the most basic data types like integers have methods and instance variables in them. This provides method chaining, where many lines of code can be combined into one. Ruby on Rails, a ruby framework, has helped the language gain popularity for web programming, with the main appeal is that programmers don’t have to spend a lot of time creating files.

To accomplish our goal of creating an ethereum address, we’ll use a ruby gem called ruby-eth.

What is ruby-eth?

Ruby-eth is a library that helps in signing transactions and generating keys, ruby-eth allows us to build and broadcast ethereum transactions using any node. The gem allows for the separation of keys from nodes. Sign transactions and handle keys anywhere you can run ruby, broadcast transactions through any node endpoint you’d like.

Installation der Abhängigkeiten

Before installing the gems, let's make sure that ruby is installed. Open a terminal and run:

$ ruby -v

For Linux/UNIX: you can use your distro’s package management system by following the guide here or using third-party tools like rbenv or RVM.

For macOS: you can use third-party tools like rbenv or RVM.

For Windows: you can use RubyInstaller.

We’ll use rbenv to manage the ruby version for our project. First, to check if rbenv is installed or not, type:

$ rbenv -v

It must output the current version of the rbenv installed on your system; if it doesn’t, download it using the information here. Then go to your terminal and type the following to activate a specific ruby version in that directory.

$ rbenv install 2.6.5
$ rbenv local 2.6.5

Once we ensure ruby is installed and at the correct version (2.6.5 in this case), let's move forward and install the required gems. You can install it from the command line using RubyGems, the package manager for ruby:

$ gem install eth

If you're using macOS, you may encounter a permission-related problem while installing gem because the version of Ruby that ships with macOS is usually for Apple's own use; we suggest using rbenv and RVM (Ruby Version Manager) to manage a separate Ruby version, which will be installed into a sandbox in your home directory, that you can make changes to without worrying about messing up the system Ruby. 

Generating an Ethereum address.

Go to your text editor and make a new ruby file address.rb and copy-paste the following code in it.

require "eth"
key = Eth::Key.new
key.public_hex
puts "SAVE BUT DO NOT SHARE THIS (Private Key): 0x#{key.private_hex}"
puts "Address: #{key.address}"

Erläuterung des obigen Codes

Line 1: Importing eth gem/package.

Line 2: Instantiating the Eth object, making a new key, and storing it in the key variable.

Line 3: Generating a hexadecimal public key from key using public_hex method.

Line 4: Generating a hexadecimal private key from key using private_key method and appending 0x prefix to it; then printing it with a warning.

Line 5: Generating the address and printing it with a string “Address:”

Please save the file and run it using.

$ ruby address.rb

If everything goes fine and the code gets executed successfully, it should look like this.

Fazit

Congratulations, you now have your very own Ethereum address, which you can use to sign/send transactions and interact with the Ethereum network. Check out the guide on How to connect to Ethereum using Ruby (ethereum.rb).

Abonniere unseren Newsletter, um weitere Artikel und Anleitungen zu Ethereum zu erhalten. Wenn du Feedback hast, kannst du dich gerne über Twitter an uns wenden. Du kannst dich jederzeit auf unserem Discord-Community-Server mit uns unterhalten – dort findest du einige der coolsten Entwickler, die du je treffen wirst :)

Diesen Leitfaden teilen