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

0
32


I’m following the method for bitcoin handle creation as outlined in Mastering Bitcoin, nonetheless, when I attempt to reference this handle in my bitcoin-cli, which is ready to regtest, it responds with an invalid handle 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 inbuilt instructions, write among 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 handle creation operate.

operate 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 bought 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 an easy output:
mtURXDaisk6ustpeo7LuiZYje9yV31JhBN

LEAVE A REPLY

Please enter your comment!
Please enter your name here