Here, we will use that key to get the public address and then the Ethereum wallet address of that private key.

Creating a bitcoin wallet address from a private key is a bit complicated. Here, the process will be very simple. We need to apply a hash function to get the public key and another to get the address.

so let’s get started.

public key
This part is almost similar to what we discussed in the Bitcoin article, so if you read this, you can skip it (unless you need a refresher).

The first thing we need to do is apply ECDSA, or Elliptic Curve Digital Signature Algorithm, to our private key. An elliptic curve is a curve that is defined by the equation y² = x³ + ax + b with a and b chosen. There is a whole family of such curves that are widely known and used. Bitcoin uses the secp256k1 curve. If you want to know more about Elliptic Curve Cryptography, I will refer you to this article.

Ethereum uses the same elliptic curve, secp256k1, so the process of obtaining the public key is the same in both cryptocurrencies.

By applying ECDSA to the private key, we get a 64-byte integer, which is two 32-byte integers representing the x and y of a point on the elliptic curve, concatenated together.

Note: As you can see from the code above, I used a method from the ecdsa module and I decoded the private key using the codec. This is more relevant to Python and less to the algorithm itself, but I’ll explain what we’re doing here to clear up potential confusion.

In Python, there are at least two classes that can hold private and public keys: “str” ​​and “bytes”. The first is a string and the second is a byte array. Cryptographic methods in Python work with the “bytes” class, taking it as input and returning it as a result.

Now, there’s a small catch: a string of, say, 4f3c byte array is not equal to 4f3c . Rather, it is equivalent to a byte array with two elements, O<. And that’s what the codecs.decode method does: it converts a string into a byte array. It will be the same for all cryptographic manipulations that we will do in this article.

wallet address
After getting the public key, we can calculate the address. Now, unlike Bitcoin, Ethereum has identical addresses on both the main and all test networks. Users specify the network they want to use later in the process when transacting and signing up.

To create an address from a public key, we just need to apply Keccak-256 to the key and then take the last 20 bytes of the result. And all. No other hash functions, no base58 or any other conversions. The only thing you need is to add ‘0x’ to the beginning of the address.

Now, as you may recall, bitcoin creates a checksum by hashing the public key and taking the first 4 bytes of the result. This is true for all bitcoin addresses, so you can’t get a valid address without adding the checksum bytes.

In Ethereum, that’s not how things work. Initially, there was no checksum mechanism to verify the integrity of the key. However, in 2016, Vitalik Buterin introduced a checksum mechanism, which has since been adopted by wallets and exchanges.

Adding a checksum to an Ethereum wallet address makes it case-sensitive.

First, you need to obtain the Keccak-256 hash of the address. Note that this address must be passed to the hash function without the 0x part.

Second, you iterate over the starting address characters. If the ith byte of the hash is greater than or equal to 8, you convert the character of the ith address to uppercase, otherwise you drop it to lowercase.

Finally, you append 0x back to the beginning of the resulting string. If you ignore case the checksum address is the same as the starting address. But capital letters let anyone check that the address is actually valid. You can find the algorithm of checksum verification on the page linked here.

If you want to play with the code, I have published it in the GitHub repository.

I am taking a course on cryptocurrencies here at Freecodecamp News. The first part is a detailed description of the blockchain.

I also post random thoughts about crypto on Twitter, so you might want to check it out.

Leave a Reply

Your email address will not be published. Required fields are marked *