Generate Cryptocurrency Private Keys And Public Addresses With Golang

Over the past month or so I’ve demonstrated how to generate address information for a variety of cryptocurrency Altcoins using technologies like Node.js, Vue.js, and Angular. The thing about my previous tutorials are that they all used the JavaScript stack in some sense. What if we wanted to adventure into other technologies like Golang?

The process for generating key and address information for Bitcoin and popular Altcoins is pretty much the same. In reality, the difference is defined by the private key and public key prefix information, typically associated to a network.

We’re going to see how to generate and import private keys for a variety of cryptocurrency coins as well as their addresses using the Go programming language.

Getting the Go Project Dependencies

Rather than reinventing the wheel and developing our own cryptocurrency related algorithms, we’re going to leverage a very popular set of packages.

Assuming that Go is installed and the $GOPATH is configured, execute the following:

go get github.com/btcsuite/btcutil
go get github.com/btcsuite/btcd

We’ll be using the btcutil and btcd packages which are technically designed for Bitcoin, but we’re going to change some things to get most other Altcoins supported.

With the appropriate packages installed, we can proceed to figuring out the cryptocurrency network information.

Obtaining and Calculating Prefix Information for Private Keys and Public Keys

When it comes to all cryptocurrency coins, there are a diverse set of key prefixes. These prefixes are simply a byte that alters how the final key looks.

Take Bitcoin for example:

xpubkey: 0x00
xprivatekey: 0x80

Before we figure out what exactly the above values mean, you’ll probably be wondering how exactly I came up with them. The honest answer is that I dug around official coin source code repositories for every coin I was interested in.

If you look at the popular Bitcore repository, you’ll notice the following in the networks.js file:

The above block was taken from the Reddcore project’s networks.js file. As you can see the information is a bit different. We weren’t lucky enough to have the byte information drawn out for us.

Using the Bitcoin Wiki, we can learn about how prefixes are actually calculated. With that knowledge, we can find a decimal to hexadecimal calculator such as BinaryHex Converter, to take the addressVersion and privKeyVersion above and convert it into something we can use.

The Reddcoin values that we want are as follows:

xpubkey: 0x3d
xprivatekey: 0xbd

Depending on what you want to accomplish with this tutorial, do some digging around in official cryptocurrency coin repositories. With the prefix information, we’ll be able to accomplish quite a bit.

Developing the Application Logic for Generating Altcoin Keys

With the dependencies installed and the prefix information for our keys known, we can start developing an application. Create a main.go file somewhere in a new project start out with the following code:

For now I’ve purposefully left the methods empty. We want to highlight that we’ve created a data structure called Network to hold our prefix information. We’re also defining our networks in a map so that we can easily use them.

Before we can use the network information to generate keys, we need to set it. Take the GetNetworkParams function for example:

The above code will generate a new private WIF key using the network parameters of the passed network. If we wanted to turn this around, we could accept a WIF key and validate it for a particular network:

In the above code, we accept a WIF string. If the WIF string is malformed we’ll return an error. In this case, malformed does not mean incorrect for a particular network. After we confirm the WIF key is not malformed, we can validate that it is correct for a particular network and return it.

Since we’ve probably created or imported a WIF key by now, we should probably create a public address. After all, you don’t want to share your WIF key.

The above code will take a WIF key and use it along with the network information to create a public address. The WIF key and the public address are really all you need when it comes to creating a cryptocurrency wallet for Bitcoin or similar Altcoins.

See how we made use of the map in the above code? We’re saying that we want to create a Bitcoin WIF key as well as a public address for Bitcoin and print out both. Had we wanted to use a different cryptocurrency, we would have defined it differently in our map variable.

Conclusion

You just saw how to generate WIF keys for Bitcoin and other cryptocurrency Altcoins. By determining the network information for the desired coin, you can generate, import, and validate quite a variety of coins.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.