Generating an address from a private key in Java?

3

I'd like to write a function in Java that takes a private key and generates the address to use in a QR code. I'd like to make this , but I'm not very good with the hashing algorithms.

I've checked with https://www.bitaddress.org and what is generated on there is not the same as what I'm getting in my program.

Can anyone help me with this? Google has failed me

user1009013

Posted 2013-12-30T20:56:21.907

Reputation: 163

Answers

1

Don't reinvent the wheel, use an existing library. bitcoinj for instance provides the method getPubKeyHash of the class ECKey which does what you are looking for.

Edit: getPubKeyHash does not return the human readable address directly, but can be used to create an instance of the class Address. This (as a subclass of VersionedChecksummedBytes) has in turn the method toString which presents the hash as bitcoin address.

jnnk

Posted 2013-12-30T20:56:21.907

Reputation: 1 860

But isn't that only the hash, and not the address? And I've tried using that library, it gives me errors, like java.lang.ClassNotFoundException: org.slf4j.LoggerFactory, which I'll have to find myself I guess :(user1009013 2013-12-30T21:19:27.330

Sorry about that, you're right. See my edit.

I haven't used bitcoinj yet, so I'm afraid I can't help you with the installation. – jnnk 2013-12-30T22:22:06.927

Everything should be included in the two jar files. Make sure you have bitcoinj.jar and bitcoinj-tools.jar in your classpath. The error you received sounds like bitcoinj-tools.jar is missing.ScripterRon 2013-12-31T01:49:08.947

1Yup you're right. And if anyone wants to see how I did this: Address address = new Address(NetworkParameters.prodNet(), Utils.sha256hash160(new ECKey(privkey, null).getPubKey())); Then toString() gives the readable address.user1009013 2013-12-31T11:11:34.143