Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zilveer opened this issue Dec 15, 2022 · 1 comment
Open

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

zilveer opened this issue Dec 15, 2022 · 1 comment

Comments

@zilveer
Copy link

zilveer commented Dec 15, 2022

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
Copy link
Author

zilveer commented Dec 15, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant