0

Basically I'm trying to create a system in which each user has it's own address to deposit on the site, however I'm having issues with monitoring addresses as they appear to be different results within CLI & Node.

Here's the code used to import the private key based on derived user ID:

const { exec } = require('child_process');

const hdNode = bitcoinjs.bip32.fromBase58(config.BIP32_DERIVED_PRIVATE, bitcoinjs.networks.bitcoin);

exec('bitcoin-cli importprivkey "' + hdNode.derive(user.id).toWIF() + '" "' + email + '" false');

Okay, now for the user side of things, the derived key is displayed using the following:

var derivedPubKey = config.BIP32_DERIVED;
if (!derivedPubKey)
    throw new Error('Must set env var BIP32_DERIVED_KEY');

const hdNode = bitcoinjs.bip32.fromBase58(derivedPubKey, bitcoinjs.networks.bitcoin);

exports.deriveAddress = function(index) {
    return bitcoinjs.payments.p2pkh({
        pubkey: hdNode.derive(index).publicKey,
        network: bitcoinjs.networks.bitcoin
    }).address;
};

However both results appear to be completely different? if I type bitcoin-cli listreceivedbyaddress 1 true into my SSH I get the following result: enter image description here

But then for this same account, on the user interface I get the following: enter image description here

Curtis
  • 101

1 Answers1

1

bitcoin-cli is generating a P2WPKH address (bc1...), but then on the user side you are generating a P2PKH address (1....).

You may also need to check to ensure the same address derivation path is being used (Bitcoin-core uses hardened derivation).

For more info on address encodings see:

What is P2PK, P2PKH, P2SH, P2WPKH - ELI5

What is the size of different Bitcoin transaction types?

chytrik
  • 18,170
  • 3
  • 19
  • 48