get bitcoin cash address from public key, using bitcoin-abc

401 views Asked by At

hey i am using https://github.com/Bitcoin-ABC/bitcoin-abc and trying to get bch adress form ecdsa public key.

I have this compressed public key:

     publickKey = "02c0fe3501b514b1b2136b4d923de0907314a7c92499fd29a0cb7cf9f731711a19"

This is its bitcoin address: "39n8cpkeHhJDzhTVkboagHMbr9WYSLv2Yk"

And according to https://cashaddr.bitcoincash.org/ this is its bitcoin-cash address: "bitcoincash:ppvt3t9zkp3flgvgunp0wpp6l9t9tc85kvhtg5zkvr"

i tried to get the bch address like this:

    const auto netParams = bCreateChainParams(bCBaseChainParams::MAIN);
    CTxDestination dst = CKeyID(uint160S(publickKey));
    std::string encoded = EncodeCashAddr(dst, *netParams);

and i get different bitcoincash address (and not the one above)

i also tried:

    std::vector<uint8_t> hashVector = std::vector<uint8_t>(uncompressKey(publickKey));
    bCashAddrContent keyContent{PUBKEY_TYPE, hashVector};
    string encoded = EncodeCashAddr("bitcoincash", keyContent);

when uncompressKey function uncompressing the ecdsa key and removes the headres from it.

i also tried with SCRIPT_TYPE instead of PUBKEY_TYPE, yet i dont get the right address....

i also cant find any example code

any idea?

1

There are 1 answers

0
Ofir On

i succeeded to get to the Compressed Bitcoin Cash Address:

CPubKey cPubKey = CPubKey(hash.begin(), hash.end()); 
auto dst = GetDestinationForKey(cPubKey, OutputType::LEGACY); 
std::string encoded = EncodeCashAddr(dst, *netParams);

when "hash" is std::vector of the compressed public key (66 characters) and "encoded" is Bitcoin Cash Address Compressed

if someone knows how to get the regular address (uncompressed) please let me know