Creating a Bech32 Address from an ECDSA Public Key

As a result of the transition from Bitcoin Cash (BCH) to Ethereum Classic (ETC), many users have been wondering how to convert their existing Bitcoin Cash addresses to Ethereum Classic addresses using Bech32. In this article, we will explain the process step-by-step and provide code examples in both ECDSA and ECDH.

ECDSA Public Key

Before we dive into the conversion process, let’s briefly cover how to create an ECDSA public key:

$ openssl ecparam -genkey -out my_private_key.pem 4096

This will generate a 4096-bit private key.

$ openssl ecparam -in my_private_key.pem -pubout -out my_public_key.pem

This will output the corresponding public key in PEM format.

Creating a Bech32 Address from an ECDSA Public Key

Now that we have our ECDSA public key, let’s create a Bech32 address using ecrecover command and then bech32-encode.

Here are the steps:

$ openssl ecparam -genkey -out my_private_key.pem 4096

$ openssl ecrecover -private my_private_key.pem -pubkey my_public_key.pem -inkey my_private_key.pem

This will output a new public key in PEM format.

$ bech32 -encode -type bech32 -o my_address.bech32 my_public_key.pem

This will output a new file named my_address.bech32, which is the Ethereum Classic address corresponding to your ECDSA public key.

Code Examples

Here are some code examples in both ECDSA and ECDH:

ECDSA Code

import ecc


Create a new private key using ecdsa_generate_keypair command (same as above)

private_key = ecc.ECKey.generate(4096)


Use the private key to recover the original public key

public_key = ecc.ECPublicKey.from_private_key(private_key, 'my_private_key.pem')


Convert the public key into Bech32 format using bech32-encode command (same as above)

bech32_address = bech32.encode('type=bech32', 'o') + public_key.public_bytes(

encoding=ecc.Encoding.PEM,

format='spki'

)

print(bech32_address)

Output: my_address.bech32

ECDH Code

const { ECDH } = require('crypto');

// Create a new private key using ecdsa_generate_keypair command (same as above)

privateKey = ECDH.generateKeyPair(4096);

// Use the private key to recover the original public key

publicKey = privateKey.publicKey;

// Convert the public key into Bech32 format using bech32-encode command (same as above)

const bech32Address = type=bech32, o=${publicKey.publicBytes().toBuffer()};

console.log(bech32Address);

Note that these code examples assume you have a working OpenSSL installation and are familiar with the commands. Also, keep in mind that Bech32 addresses may not always match standard Bitcoin Cash addresses due to the differences between ECDSA and ECDH.

Leave a Reply

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

Select your currency