1

I'm facing an issue regarding generation of Bitcoin addresses.

I've public key:

String xpub"xpub661MyMwAqRbcGJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

I want to generate multiple addresses from xpub.

I'm using bitcoinJ library.

ECKey key=ECKey.fromPublicOnly(Base58.decode(xpub));

It throws exception of:

Incorrect length for uncompressed encoding

I've read many articles, I found that Ripemd160(SHA256(string));.
After hashing, I encode in Base58, but couldn't achieve multiple addresses.

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Muhammad Saad
  • 713
  • 1
  • 9
  • 31

1 Answers1

3

I'm not sure if my answer is the right one because I don't know how the BitcoinJ library works but just for info:

The bitcoin public key is kind of different from RSA public keys, you should add 04 at the beginning of the string.

In your case the key value should be equals to:

04xpub661MyMwAqRbcGJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

So the public key generated will have the following:

1 byte 0x04 + 65 bytes: 32 bytes corresponding to X coordinate and 32 bytes corresponding to Y coordinate

Check:

https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses https://en.bitcoin.it/w/images/en/9/9b/PubKeyToAddr.png

To test/validate your address you can use this site:

http://gobittest.appspot.com/Address

Good luck

Elie Daher
  • 271
  • 1
  • 4
  • 13
Bilal EL CHAMI
  • 414
  • 1
  • 3
  • 14
  • 3
    Yes, I agree with this answer – Elie Daher Mar 26 '18 at 10:40
  • I followed this but without adding 04 at the start. try again with your. hope it will generate valid address but still confused about multiple addresses – Muhammad Saad Mar 26 '18 at 10:44
  • `MessageDigest digest = MessageDigest.getInstance("SHA-256"); String hash=Base58.encode(digest.digest(xpub.getBytes(StandardCharsets.UTF_8)));` trying this accroding to picture – Muhammad Saad Mar 26 '18 at 10:45
  • 1
    What about the `Ripemd160`? As I see you've just hashed it with SHA256 then with Base58. @MuhammadSaad – Bilal EL CHAMI Mar 26 '18 at 10:49
  • `MessageDigest digest = MessageDigest.getInstance("SHA-256"); String ripe=Base58.encode(ripemd160.getHash(digest.digest(xpub.getBytes(StandardCharsets.UTF_8)))); System.out.println("RIPE"+ ripe);` but its not returning valid address – Muhammad Saad Mar 26 '18 at 10:52
  • Humm, that's really sad! =( I don't know if you complete the process like mentioned in the image. After the ripemd160 hashing try: - Adding network bytes to the result (`result1`) - Double hashing `result1` using SHA256 (`result2`) - Taking first four bytes of `result2` and add it to `result1` (`result3`) - Finally encode using Base58 result3 And good luck ^^ – Bilal EL CHAMI Mar 26 '18 at 11:04