Blockchain on Golang Part 1: Wallet Operations

Ajiyba Nesij Korkmaz
3 min readMay 28, 2022

In this article, I will explain how to create a crypto wallet and import an existing wallet.

Before we started the deep dive, if you haven’t read the Blockchain on Golang : Introduction yet, you can find it here.

Create a Wallet

I must mention the Elliptic Curve Digital Signature Algorithm (ECDSA), a cryptographic algorithm widely employed in the field of digital signatures. Its fundamental objective is to verify the integrity of digital documents and data, serving as a cornerstone for identity authentication processes.

ECDSA proves highly effective in ensuring the security, speed, and efficiency of digital signing operations. Consequently, it stands as the preferred choice in numerous cryptocurrency and blockchain-based systems, guaranteeing the security of transactions and bolstering identity verification procedures. Moreover, ECDSA has become a popular and widely-utilized algorithm for generating and validating digital signatures.

I will use the go-ethereum/crypto library for ECDSA, enabling us to generate a private key using this library.

Step 1: Generating Private Key

The GenerateKey() function returns two values. The first value is the generated private key, represented as a *ecdsa.PrivateKey object, and the second value is an error of type error.

generatedPrivateKey, err := crypto.GenerateKey()

Step 2: Find Public Key

You can obtain the Ethereum address (public key) in a human-readable format, which can be shared for receiving transactions or interacting with the Ethereum blockchain.

publicKey := generatedPrivateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA)

Remember, the private key should be kept secure and not shared with others to maintain control over the associated wallet and its assets.

Final Step: CreateWallet Function

This function will generate a new wallet, and provide its corresponding public address as a common.Address object and private key as a *ecdsa.PrivateKey object as the output.

If you want see private key as a hex string you should uncomment these commented lines.

Import a Wallet

You can either create a new wallet or import an existing wallet by entering its private key.

Sample Private Key 4b82b08ab9a64de944fef3d4184e36771dcd31ad8d8c584ce4b353765e692195

Step1: Converting Hex to *ecdsa.PrivateKey

I convert the hexadecimal representation to *ecdsa.PrivateKey because this pointer is utilized in operations such as transfers and contract interactions.

importedPrivateKey, err := crypto.HexToECDSA(privateKey)

Step 2: Find Public Key

You can obtain the Ethereum address (public key) in a human-readable format.

publicKey := generatedPrivateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA)

Final Step: ImportWallet Function

We can now proceed to develop a method for importing a wallet. This function will provide the public address as a common.Address obaject and the private key as a *ecdsa.PrivateKey obaject.

Conclusion

In this article, we have covered how to create a blockchain wallet and import an existing wallet using the ECDSA cryptographic algorithm. We used the go-ethereum/crypto library for ECDSA, which enabled us to generate a private key and find the corresponding public key. Additionally, we developed functions for creating and importing wallets.

Happy coding and exploring the world of blockchain technology!

In the next article, we will cover how to implement the transaction process in a blockchain using Golang. Stay tuned for more!

--

--