Sunday, December 22, 2024

javascript – Generate Bitcoin Tackle JS Step-By-Step “Invalid Tackle” Error

I’m following the method for bitcoin tackle creation as outlined in Mastering Bitcoin, nonetheless, when I attempt to reference this tackle in my bitcoin-cli, which is about to regtest, it responds with an invalid tackle error. I’m making an attempt to write down some JS to work together with my bitcoin node by way of the bitcoin-core bundle, however as a substitute of simply utilizing all of the in-built instructions, write a few of the code myself to solidify my understanding.

I’m utilizing the next 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");

Right here is my tackle creation perform.

perform createNewAddress() {
  // Get the personal key
  const privateKey = ec.genKeyPair()
  // Get the general public level from the personal 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 model, right here we use 6F acquired the testnet (we're utilizing regtest)
  const prependedPK = "6F"+hashedPK;
  // Calculate the checksum and take the primary 4 btyes
  const checksum = SHA256(SHA256(prependedPK).toString()).toString().substring(0,9);
  // Append the checksum to the top
  const unencodedPK = prependedPK + checksum;
  // Base58Encode the unencodedPK
  const bytes = Buffer.from(unencodedPK, 'hex');
  const bitcoinAddress = bs58.encode(bytes);
  return (bitcoinAddress);
}

Right here is a straightforward output:
mtURXDaisk6ustpeo7LuiZYje9yV31JhBN

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles