Author

Topic: A little technical information request... (Read 1660 times)

legendary
Activity: 1400
Merit: 1005
June 01, 2011, 02:55:26 PM
#16
Thanks anisoptera.  Guess I'll need to start looking at how data is stored in the wallet.

Thanks for the heads up Maged!  Sounds like that sort of a pull could be very useful!
legendary
Activity: 1204
Merit: 1015
The tricky part is probably getting it into wallet.dat.
As far as that goes, this patch is in the works:
http://forum.bitcoin.org/index.php?topic=8091.0
member
Activity: 308
Merit: 10
If you generated a private key the same way bitcoin does and worked out a way to get that into a wallet.dat, then there's no reason you couldn't do what you say. There's nothing special about the private keys bitcoin generates that makes them addresses, they're standard. The tricky part is probably getting it into wallet.dat.
legendary
Activity: 1400
Merit: 1005
Sorry for the mystery.

Ultimately, I want to create my own vanity bitcoin addresses (i.e., addresses that contain particular letter/number combos).  Gavin has already created a patch for the standard client that can do this, but I'd like to have the code to do it in PHP, and I'd like to know more about how addresses are generated.  I'd also like to know if I can generate such vanity addresses for multiple wallets easily.

I suppose this presents a couple of questions.  Would a server REQUIRE bitcoind to be running in order to find an address for a wallet?  Or is there a way to generate addresses without having the wallet or bitcoind running?

The reason I want to do this in PHP rather than another language is because I am most familiar with PHP.  That way, I can optimize the code and more easily manipulate it to what I want to do beyond these vanity address generations.
sr. member
Activity: 294
Merit: 252
Why don't you tell us what you are attempting to do, and we'll help you find the best way to do it?
legendary
Activity: 1400
Merit: 1005
Then why couldn't I generate a private key, generate a public key based on the private key, and generate an address from the public key?  Or are you saying that it's not possible to generate a private key, then import it into a wallet file?
sr. member
Activity: 294
Merit: 252
Can I manually generate a public/private key that corresponds to a given address

What themos said.

A public key can be derived from a private key, and an address can be derived from a public key, but those operations cannot be reversed.

In order to get a private key from a public one you would have to break ECDSA, and in order to get a public key from an address you'd have to break SHA-256 (or scan the block chain).
administrator
Activity: 5222
Merit: 13032
Can I manually generate a public/private key that corresponds to a given address

No.
member
Activity: 308
Merit: 10
what keeps me from importing the same public/private keys into multiple wallets and causing havoc?

You could accomplish this already by just copying wallet.dat, and the answer is the same there as it is here: whoever spends the coins first wins.
legendary
Activity: 1400
Merit: 1005
Sorry for all the questions.  Smiley

Can I manually generate a public/private key that corresponds to a given address, then import that information into a wallet.dat to make that particular public/private key combo accepted into that particular wallet?  If that is possible, what keeps me from importing the same public/private keys into multiple wallets and causing havoc?
legendary
Activity: 1400
Merit: 1005
Interesting... thank you much!  I will indeed read more in that wiki article.
sr. member
Activity: 294
Merit: 252
Thanks for the answers theymos and BitterTea!

Guess I wasn't asking quite the right questions then... I assumed that each wallet only had one public key!

What I am getting at is, how could I generate a list of addresses that I was sure would go to the same wallet?  Looks like theymos's PHP code is most of the way there too...  Smiley

Also, bittertea, in your equation, is checksum multiplied by the result of "version+ripemd-160(sha-256(public_key))"?

A wallet is merely a container of public-private keypairs. You cannot tell whether two addresses belong to the same wallet without scanning a wallet file for the keys represented by the addresses.

Check out the wiki page I linked, it should be more clear.

Quote
A bitcoin address is in fact the hash of a ECDSA public key, computed this way:

Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111
Key hash = Version concatenated with RIPEMD-160(SHA-256(public key))
Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))
Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)
legendary
Activity: 1400
Merit: 1005
Thanks for the answers theymos and BitterTea!

Guess I wasn't asking quite the right questions then... I assumed that each wallet only had one public key!

What I am getting at is, how could I generate a list of addresses that I was sure would go to the same wallet?  Looks like theymos's PHP code is most of the way there too...  Smiley

Also, bittertea, in your equation, is checksum multiplied by the result of "version+ripemd-160(sha-256(public_key))"?
sr. member
Activity: 294
Merit: 252
First - How can I get the public key of someone else's wallet?  Or better yet, can a public key be derived from an address?

To build an address, a double hash (first SHA-256 then RIPEMD-160) of the public key is appended to the version. Then the first four bytes of the checksum (SHA-256 x 2) of this value are appended. The entire thing is base58 encoded.

When you send someone a transaction, you put the hash of their public key in the scriptsig of the output. When they claim the output, they must have the private key whose public key hashes to that value.

Quote
Second - How can I generate an address based on that public key?  What mathematical calculations, hashes, encryption is being done behind the scenes?

base58encode(version+ripemd-160(sha-256(public_key))+checksum(version+ripemd-160(sha-256(public_key))))

Where checksum is the first four bytes of sha-256(sha-256(data)).

Quote
Third - Would an address still "work" even if it was not generated by the wallet owner's client?  In other words, if I had the public key of someone's wallet, and generated an address for them without them knowing, could I send them bitcoins to that newly generated address and they would receive them?

see this wiki article: https://en.bitcoin.it/wiki/Protocol_specification#Addresses

Yes, as long as they own the private key from which the public key is derived, and it is a valid bitcoin address.
administrator
Activity: 5222
Merit: 13032
First - How can I get the public key of someone else's wallet?  Or better yet, can a public key be derived from an address?

It's listed on Bitcoin Block Explorer if it has ever been seen by the network. The full public key cannot be derived from the address alone, as the address is only a hash of the public key.

Quote
How can I generate an address based on that public key?  What mathematical calculations, hashes, encryption is being done behind the scenes?

Hash it with SHA-256, then hash that with RIPEMD-160. Then add a version and a checksum and convert it to base58.

Input the public key here:
http://blockexplorer.com/q/hashpubkey
And then input that page's output here:
http://blockexplorer.com/q/hashtoaddress

Here's my PHP code to do it (pubKeyToAddress):
http://pastebin.com/vmRQC7ha

Quote
Would an address still "work" even if it was not generated by the wallet owner's client?  In other words, if I had the public key of someone's wallet, and generated an address for them without them knowing, could I send them bitcoins to that newly generated address and they would receive them?

Yes. You'll get the same address. A particular public key always becomes the same address.
legendary
Activity: 1400
Merit: 1005
First - How can I get the public key of someone else's wallet?  Or better yet, can a public key be derived from an address?

Second - How can I generate an address based on that public key?  What mathematical calculations, hashes, encryption is being done behind the scenes?

Third - Would an address still "work" even if it was not generated by the wallet owner's client?  In other words, if I had the public key of someone's wallet, and generated an address for them without them knowing, could I send them bitcoins to that newly generated address and they would receive them?

Thanks much!
Jump to: