OutCast3k / coinbin

Javascript Bitcoin Wallet. Supports Multisig, Stealth, HD, SegWit, Bech32, Time Locked Addresses, RBF and more!
https://coinb.in/
MIT License
907 stars 622 forks source link

use a HEX private key and generate all its addresses? #259

Open zilveer opened 1 year ago

zilveer commented 1 year ago

Hi, I wonder if someone has actually written a function to generate addresses (and private/pubkeys) from a given HEX key?

for example I am using the verify page on my local and I write in the HEX key 0000000000000000000000000000000000000000000000000000000000000001

this gives me:

address: 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
pubkey: 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
private key: 0000000000000000000000000000000000000000000000000000000000000001

But I would like to generate all the bitcoin addresses, compressed, uncompressed, bech32 and segwit.

Any guidance here?

regards Zilveer

zilveer commented 1 year ago

Hi again, so I managed to convert given HEX key to all bitcoin address formats supported by Coinbin.

But it seems that we should not pass over the the uncompressed public key to to coinjs.segwitAddress(pubKeyU.pubkey) and coinjs.bech32Address(pubKeyU.pubkey), am I correct?

We can only pass over the compressed public keys for generating bech32 and segwit addresses. Can someone please correct me here?

Anyway here is the given code for generating all bitcoin address format supported by Coinbin:

        //compressed
        var h= '0000000000000000000000000000000000000000000000000000000000000001';
        var r = Crypto.util.hexToBytes(h);

        r.push(0x01);

        r.unshift(coinjs.priv);
        var hash = Crypto.SHA256(Crypto.SHA256(r, {asBytes: true}), {asBytes: true});
        var checksum = hash.slice(0, 4);

        var privKeyWifC = coinjs.base58encode(r.concat(checksum));
        var addressC = coinjs.wif2address(privKeyWifC);
        var pubKeyC = coinjs.wif2pubkey(privKeyWifC);
        var swbech32C = coinjs.bech32Address(pubKeyC.pubkey);  //<-- works good by passing over the compressed public key!
        var swC = coinjs.segwitAddress(pubKeyC.pubkey);  //<-- works good by passing over the compressed public key
        //<<--compressed

        //uncompressed
        var r = Crypto.util.hexToBytes(h);

        r.unshift(coinjs.priv);
        var hash = Crypto.SHA256(Crypto.SHA256(r, {asBytes: true}), {asBytes: true});
        var checksum = hash.slice(0, 4);

        var privKeyWifU = coinjs.base58encode(r.concat(checksum));
        var addressU = coinjs.wif2address(privKeyWifU);
        var pubKeyU = coinjs.wif2pubkey(privKeyWifU);
        //var swbech32U = coinjs.bech32Address(pubKeyU.pubkey);    //<--we should not pass over uncompressed public keys for bech32
        //var swU = coinjs.segwitAddress(pubKeyU.pubkey);  //<-- error we should not pass over uncompressed public keys for segwit

//compressed
        console.log('privKeyWifC: ', privKeyWifC);
        console.log('addressC: ', addressC);
        console.log('pubKeyC: ', pubKeyC);
        console.log('swbech32C: ', swbech32C);
        console.log('swC: ', swC);
//uncompressed
        console.log('privKeyWifU: ', privKeyWifU);
        console.log('addressU: ', addressU);
        console.log('pubKeyU: ', pubKeyU);

        //console.log('swbech32U: ', swbech32U);
        //console.log('swU: ', swU);

regards Zilveer