Generate Ethereum Wallet & Key Pairs using PHP — Ethereum Tutorial

Francis Hor
Coinmonks
2 min readOct 17, 2018

--

This article is to provide a guide on how to generate ECDSA private key, then derive to Ethereum wallet address by using PHP 7.0++.

Also Read: Best Hardware Wallets

You can find the working implementation as below:

Prerequisite

The code requires PHP 7.0++ with OpenSSL extension and PHP Composer. You need to use PHP Composer to install the third party packages

Generate Private Key

Ethereum standard is using secp256k1 curve to generate the private key. In my tutorial, I use OpenSSL functions to generate elliptic curve private key in PEM format as like below:

After that I need to convert the private key into ASN1 Sequence, below is the structure of ANS1 Sequence.

The code below is how I retrieve the public key & private key in hex string from the ANS1 Sequence Structure.

Derive Ethereum Wallet Address

The Ethereum Wallet address is derived from the public key. Every EC public key is always started with 0x04. In order to get the correct hash for Ethereum Wallet address, we need to remove the leading 0x04.

We continue with Keccak256 hash of the EC public key. Ethereum Wallet address is 20 bytes length which is 40 characters long, so we only need the last 20 bytes from the hash data.

Now you have $wallter_address which store your new generated Ethereum Wallet address and $wallet_private_key is the private key of your Ethereum Wallet address.

How to run the code

It will output Ethereum Wallet address and its private key like below:

Get Best Software Deals Directly In Your Inbox

--

--

Francis Hor
Coinmonks

m(=-=)m — love to code — curios on new tech