I am following the process for bitcoin address creation as outlined in Mastering Bitcoin, however, when I try to reference this address in my bitcoin-cli
, which is set to regtest, it responds with an invalid address error. I am attempting to write some JS to interact with my bitcoin node via the bitcoin-core package, but instead of just using all the built in commands, write some of the code myself to solidify my understanding.
I am using the following npm packages:
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const bs58 = require('bs58')
const SHA256 = require("crypto-js/sha256");
const RIPEMD160 = require("crypto-js/ripemd160");
Here is my address creation function.
function createNewAddress() {
// Get the private key
const privateKey = ec.genKeyPair()
// Get the public point from the private key
const publicPoint = privateKey.getPublic();
// Uncompressed public key
const uncompressedPK = publicPoint.encode('hex');
// SHA256 then RIPEMD160, aka HASH160
const hashedPK = RIPEMD160(SHA256(uncompressedPK).toString()).toString();
// Prepend the version, here we use 6F got the testnet (we are using regtest)
const prependedPK = "6F"+hashedPK;
// Calculate the checksum and take the first 4 btyes
const checksum = SHA256(SHA256(prependedPK).toString()).toString().substring(0,9);
// Append the checksum to the end
const unencodedPK = prependedPK + checksum;
// Base58Encode the unencodedPK
const bytes = Buffer.from(unencodedPK, 'hex');
const bitcoinAddress = bs58.encode(bytes);
return (bitcoinAddress);
}
Here is a simple output: mtURXDaisk6ustpeo7LuiZYje9yV31JhBN