Generate Public Key From Wif

Posted By admin On 14.12.20

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?

  • Oct 31, 2015  Generate the public key and p2pkh address from a WIF encoded private key /bitcointool -command pubfrompriv -p KwmAqzEiP7nJbQi6ofQywSEad4j5b9BXDJvyypQDDLSvrV6wACG8 pubkey: 023d86ca58e2519cce1729b4d36dfe5a053ad5f4ae6f7ef9360bee4e657f7e41c9 p2pkh address: 1N5ZkjyabcZLLHMweJrSkn3qedsPGzAx9m.
  • Nov 04, 2018 Tiny Bitcoin Library written in C. Contribute to libbtc/libbtc development by creating an account on GitHub. Generate the public key and p2pkh address from a WIF.
  • First, create the key pair: sn -k keypair.snk Next, extract the public key from the key pair and copy it to a separate file: sn -p keypair.snk public.snk Once you create the key pair, you must put the file where the strong name signing tools can find it.
  • 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. You just saw how to generate WIF keys for Bitcoin and other cryptocurrency Altcoins.

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.

Three very small observations: In your function generateprivatekeywif, you never use hashed, you re-compute it for the return value. In a nice self-contained module like this one, you should definitely add docstrings to your functions. How to generate bitcoin P2SH WIF key and address pair from sha256 private key? Create a private key of my choosing, then generate public key and address.

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:

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. Avg antivirus license key generator.

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.

Generate Public Key From Wif

Take Bitcoin for example:

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.

Generate Public Key From Wif To Windows 10

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

So yes, the Bitcore repository is a Node.js project, but it is only an example. You can easily find this information in other repositories that are not Node.js.

Now let’s look at an Altcoin. Let’s take a look at Reddcoin (RDD):

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:

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.

Public Key Example

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:

We can then use the GetNetworkParams function within any function that creates or imports keys. For example, if we wanted to create keys, we would use the following:

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.

To test our code, we can do the following:

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.

Facebook

Each of the btcutil and btcd packages do quite a bit when it comes to the blockchain, far beyond the examples used in this tutorial. If you need to create transactions, take a look at my tutorial titled, Create and Sign Bitcoin Transactions with Golang. If you’d like to create a fully functional hardware wallet with all the bells and whistles, check out my tutorial titled, Create a Bitcoin Hardware Wallet with Golang and a Raspberry Pi Zero.

A video version of this article can be seen below.

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.

Please enable JavaScript to view the comments powered by Disqus.

Disclaimer

This project was written in May 2013 for educational purposes.

Modern cryptocurrency wallets should use hierarchical deterministic (HD) keys instead.

Introduction

btckeygenie is a standalone Bitcoin keypair/address generator written in Go.btckeygenie generates an ECDSA secp256k1 keypair, dumps the public key incompressed and uncompressed Bitcoin address, hexadecimal, and base64 formats,and dumps the private key in Wallet Import Format (WIF), Wallet Import FormatCompressed (WIFC), hexadecimal, and base64 formats.

btckeygenie includes a lightweight Go package called btckey to easily generatekeypairs, and convert them between compressed and uncompressed varieties ofBitcoin Address, Wallet Import Format, and raw bytes.

See documentation on btckey here: https://godoc.org/github.com/vsergeev/btckeygenie/btckey

Donations are welcome at 15PKyTs3jJ3Nyf3i6R7D9tfGCY1ZbtqWdv :-)

Usage

Generating a new keypair

Importing an existing WIF/WIFC

Help/Usage

Generate Public Key From Wife And Kids

Installation

To fetch, build, and install btckeygenie to $GOPATH/bin:

License

What Is Public Key

btckeygenie is MIT licensed. See the included LICENSE file for more details.