<p>I'm trying to form addresses PT2R and P2WSH from <strong>Public key hex</strong>. Receiving this Public key hex from API like array of objects:</p>
<pre><code>const data = [
  {
    staker_pk_hex: &quot;511eff6fdfec5a0d592c49ee1120d96756d16797263dea5973f6b53e5e1e2a11&quot;,
    active_tvl: 5000000,
    total_tvl: 5000000,
    active_delegations: 1,
    total_delegations: 1
  },
  {
    staker_pk_hex: &quot;21657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4&quot;,
    active_tvl: 5000000,
    total_tvl: 5000000,
    active_delegations: 1,
    total_delegations: 1
  }
];
</code></pre>
<p>I know exactly that these accs have transactions, but when I trying to create addresses from pb_key, they are wrong. Because they don't have any txs.(checking with <em>mempool</em>)
Code:</p>
<pre><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: Define the compressed public key (33 bytes)
const publicKey = Buffer.from('21657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4', 'hex');

// Step 2: Construct the script: OP_PUSH33 &lt;publicKey&gt; 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(&quot;Script (hex):&quot;, script.toString('hex'));

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

const { address: p2wshAddress } = bitcoin.payments.p2wsh({
  redeem: { output: script },
  network: bitcoin.networks.bitcoin, 
});

console.log(&quot;P2WSH Address:&quot;, p2wshAddress);

const taprootInternalPubkey = publicKey; 
const { address: p2trAddress } = bitcoin.payments.p2tr({
  internalPubkey: taprootInternalPubkey,
  network: bitcoin.networks.bitcoin, 
});

console.log(&quot;P2TR Address:&quot;, p2trAddress);
</code></pre>
<p>Am I doing this right? or is there a mistake in the code?</p>
<p>Result of this script:</p>
<blockquote>
<p>Script (hex): 2121657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4ac
Witness Program (hex): 224bb54f40d38c5f0c24d13687396cd5b9cf08f7b9dd0d782834641daf2ad6fb</p>
</blockquote>
<blockquote>
<p>P2WSH Address: bc1qyf9m2n6q6wx97rpy6ymgwwtv6kuu7z8hh8ws67pgx3jpmte26masn0eg0p</p>
</blockquote>
<blockquote>
<p>P2TR Address: bc1px44ha7elzraz9av4her96me0lslts8zvd3vgv9zc740ss8aa0c3shkkqqs</p>
</blockquote>

CC BY-SA 4.0
2024-08-25T18:44:55.987Z
Hi dill, can you link to the documentation of the API/protocol you're interacting with? That might help us figure out how exactly the outputs are constructed, it might be more complicated than single key P2WSH and key path P2TR.
Vojtěch Strnad
CC BY-SA 4.0
2024-08-25T19:11:10.663Z
https://staking-api.babylonlabs.io/swagger/index.html#/

https://docs.babylonlabs.io/docs/user-guides/phase1-mainnet

hello, thank you. @VojtěchStrnad 

/v1/stats/staker - endpoint
dill
<public-key><bitcoin-cli><p2tr><p2wsh>
PT2R and P2WSH Addresses from Public key hex
dill
# PT2R and P2WSH Addresses from Public key hex

(Asked by: username_0 on 2024-08-25)

I'm trying to form addresses PT2R and P2WSH from Public key hex. Receiving this Public key hex from API like array of objects:
const data = [
  {
    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 know exactly that these accs have transactions, but when I trying to create addresses from pb_key, they are wrong. Because they don't have 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: Define the compressed public key (33 bytes)
const publicKey = Buffer.from('21657f042073b8c4d72ecfcc8826e49a21d4ab3ed7e0b5c45d70fe2e28b726d4', 'hex');

// Step 2: Construct 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').update(script).digest(); 
console.log("Witness Program (hex):", witnessProgram.toString('hex'));

const { address: p2wshAddress } = bitcoin.payments.p2wsh({
  redeem: { output: script },
  network: bitcoin.networks.bitcoin, 
});

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

const taprootInternalPubkey = publicKey; 
const { address: p2trAddress } = bitcoin.payments.p2tr({
  internalPubkey: taprootInternalPubkey,
  network: bitcoin.networks.bitcoin, 
});

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

Am I doing this right? or is there a mistake in the code?
Result of this script:

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


P2WSH Address: bc1qyf9m2n6q6wx97rpy6ymgwwtv6kuu7z8hh8ws67pgx3jpmte26masn0eg0p


P2TR Address: bc1px44ha7elzraz9av4her96me0lslts8zvd3vgv9zc740ss8aa0c3shkkqqs

**Tags:** public-key, bitcoin-cli, p2tr, p2wsh

> **Comments:**
> **username_1**: Hi username_0, can you link to the documentation of the API/protocol you're interacting with? That might help us figure out how exactly the outputs are constructed, it might be more complicated than single key P2WSH and key path P2TR.  
> **username_0**: https://staking-api.babylonlabs.io/swagger/index.html#/  https://docs.babylonlabs.io/docs/user-guides/phase1-mainnet  hello, thank you. @VojtěchStrnad   /v1/stats/staker - endpoint  

---

## 0 answers

