Friday, November 29, 2024
HomeBitcoinbitcoin cli - PT2R and P2WSH Addresses from Public key hex

bitcoin cli – PT2R and P2WSH Addresses from Public key hex


I am making an attempt to type addresses PT2R and P2WSH from Public key hex. Receiving this Public key hex from API like array of objects:

const information = [
  {
    staker_pk_hex: "511eff6fdfec5a0d592c49ee1120d96756d16797263dea5973f6b53e5e1e2a11",
    active_tvl: 5000000,
    total_tvl: 5000000,
    active_delegations: 1,
    total_delegations: 1
  },
  {
    staker_pk_hex: "21657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4",
    active_tvl: 5000000,
    total_tvl: 5000000,
    active_delegations: 1,
    total_delegations: 1
  }
];

I do know precisely that these accs have transactions, however once I making an attempt to create addresses from pb_key, they’re mistaken. As a result of they haven’t any txs.(checking with mempool)
Code:

const bitcoin = require('bitcoinjs-lib');
const nodeCrypto = require('crypto'); 
const ecc = require('tiny-secp256k1');

// Initialize ECC library for Taproot
bitcoin.initEccLib(ecc);

// Step 1: Outline the compressed public key (33 bytes)
const publicKey = Buffer.from('21657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4', 'hex');

// Step 2: Assemble the script: OP_PUSH33  OP_CHECKSIG
// OP_PUSH33 is represented as 0x21 (hex for 33), OP_CHECKSIG is 0xAC
const script = Buffer.concat([
  Buffer.from([0x21]), // OP_PUSH33 (33 bytes)
  publicKey,           // The compressed public key
  Buffer.from([0xAC])  // OP_CHECKSIG
]);

console.log("Script (hex):", script.toString('hex'));

const witnessProgram = nodeCrypto.createHash('sha256').replace(script).digest(); 
console.log("Witness Program (hex):", witnessProgram.toString('hex'));

const { deal with: p2wshAddress } = bitcoin.funds.p2wsh({
  redeem: { output: script },
  community: bitcoin.networks.bitcoin, 
});

console.log("P2WSH Tackle:", p2wshAddress);

const taprootInternalPubkey = publicKey; 
const { deal with: p2trAddress } = bitcoin.funds.p2tr({
  internalPubkey: taprootInternalPubkey,
  community: bitcoin.networks.bitcoin, 
});

console.log("P2TR Tackle:", p2trAddress);

Am I doing this proper? or is there a mistake within the code?

Results of this script:

Script (hex): 2121657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4ac
Witness Program (hex): 224bb54f40d38c5f0c24d13687396cd5b9cf08f7b9dd0d782834641daf2ad6fb

P2WSH Tackle: bc1qyf9m2n6q6wx97rpy6ymgwwtv6kuu7z8hh8ws67pgx3jpmte26masn0eg0p

P2TR Tackle: bc1px44ha7elzraz9av4her96me0lslts8zvd3vgv9zc740ss8aa0c3shkkqqs

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments