This blog post contains quick tips for using the Bech32 format private key with avalanche js.
When you use “DefaultLocalGenesisPrivateKey” variable it returns your private key as a cb58 format string. In my case I export my private key from Avalanche Cli which is an awesome tool for deploying subnet, It returns a Bech32 format private key.
The solution is just to use a buffer and import your private key in this way.
privKey = Buffer.from(privKey);
xKeychain.importKey(privKey);
Here is full code:
const { Avalanche, Buffer } = require("avalanche"); async function main() { const ip = "api.avax-test.network/"; const avalanche = new Avalanche(ip, null, "https"); const xchain = avalanche.XChain(); const xKeychain = xchain.keyChain(); let privKey = "Your Private Key"; privKey = Buffer.from(privKey); xKeychain.importKey(privKey); const xAddresses = xchain.keyChain().getAddresses(); const xAddressStrings = xchain.keyChain().getAddressStrings(); console.log(xAddressStrings); } main();
What is the Bech32
Bech32 is a standardized format for representing Bitcoin addresses that was introduced in 2017. It is designed to be more user-friendly and more efficient than other address formats, with a number of benefits including:
- Case-insensitive: Bech32 addresses are case-insensitive, which means that “bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty” and “BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KG3G4TY” are considered to be the same address. This is a feature that is not present in other address formats such as base58, which can cause confusion for users.
- Compact size: Bech32 addresses are shorter than other address formats, which makes them easier to read and write.
- Human-readable: Bech32 addresses are designed to be more easily read and understood by humans, with a clear separation between the address prefix and the data payload.
- Error-resistant: Bech32 addresses include a built-in error-checking mechanism that allows users to detect and correct typos or other mistakes.
Bech32 addresses are used primarily on the Bitcoin network, but they are also used on some other cryptocurrency networks such as Litecoin. They are becoming increasingly popular due to their improved usability and error-resistant properties.
What is base58 format?
base58, which is a way of encoding data (including addresses) as alphanumeric strings. Base58 is used in a number of contexts, including Bitcoin addresses, but it is not a standardized format like Bech32.
In base58 encoding, data is represented using a set of 58 characters: the digits 0-9, the uppercase letters A-Z, and the lowercase letters a-z (excluding O, I, and l to avoid confusion with the numerals 0 and 1). The data is first converted to a base-256 representation, and then each group of eight bits (a byte) is split into two four-bit groups. The numerical value of each group is used to look up the corresponding character in the base58 alphabet, and the resulting string is the base58-encoded representation of the data.
Base58 has the advantage of being more compact than other encoding schemes, but it is less error-resistant than Bech32. It is also not case-sensitive, which can cause confusion for users.