Bitcoin Forum
June 22, 2024, 08:06:09 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Please point me to the code that generates new bitcoin addresses.  (Read 2109 times)
hello_good_sir (OP)
Hero Member
*****
Offline Offline

Activity: 1008
Merit: 531



View Profile
May 07, 2013, 08:56:54 PM
 #1

I would like to look at the code that creates a random number, then makes a private key from that, then makes the address from that.

I looked here: https://github.com/bitcoin/bitcoin/tree/master/src

I checked in a few likely files and didn't see it.  I realized that I might not recognize it when I see it.  So please point me to the file name and function name.

My goal is to make an offline paper wallet generator where the user generates the random number by hand (by shuffling a deck of cards multiple times).  This way I will not have to trust the random number generator.  I don't want to trust anything except myself.  I will make the source code for my program available.

bukaj
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
May 07, 2013, 09:03:10 PM
 #2

Here you can find some code in JS.
DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4672



View Profile
May 07, 2013, 09:26:29 PM
 #3

Is this what you're looking for?

https://github.com/bitcoin/bitcoin/blob/master/src/wallet.cpp#L30
bitcool
Legendary
*
Offline Offline

Activity: 1441
Merit: 1000

Live and enjoy experiments


View Profile
May 07, 2013, 09:38:24 PM
 #4

https://github.com/bitcoin/bitcoin/blob/master/src/util.cpp#L147
hello_good_sir (OP)
Hero Member
*****
Offline Offline

Activity: 1008
Merit: 531



View Profile
May 07, 2013, 10:07:37 PM
 #5


That's a bit of a start.  I traced it back a bit, and I'm thinking that this EC_KEY_generate_key might be the function that I am looking for.  Is there an easy way to search across all of the files?  ctrl-f in my browser just searches within the current page.  I could download the whole source but I'd really rather not.

DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4672



View Profile
May 07, 2013, 10:18:08 PM
 #6


That's a bit of a start.  I traced it back a bit, and I'm thinking that this EC_KEY_generate_key might be the function that I am looking for.  Is there an easy way to search across all of the files?  ctrl-f in my browser just searches within the current page.  I could download the whole source but I'd really rather not.

Ok, so:

CPubKey CWallet::GenerateNewKey() in wallet.dat

randomizes the seed with RandAddSeedPerfmon();

then it references key.MakeNewKey(fCompressed);


void CKey::MakeNewKey(bool fCompressed) in key.cpp

references EC_KEY_generate_key(pkey) which is a part of the openssl/ecdsa package as described here:
http://openssl.sourcearchive.com/documentation/1.0.0e-2/crypto_2ec_2ec_8h_a690c3227c776f6b53a004e22d03055da.html#a690c3227c776f6b53a004e22d03055da
DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4672



View Profile
May 07, 2013, 10:24:05 PM
 #7

It sounds like for some reason you don't want to trust the well reviewed, tested, and industry accepted openssl to generate the keypair.  If that's true, then you probably don't want EC_KEY_generate_key, since that generates the pair (both private and public keys).

If for some reason, you'd rather trust your own ability to generate a random private key with enough entropy, then you might want to look at the section of code that imports private keys and calculates the public keys and/or bitcoin address.  This might be a good place to start:

https://github.com/bitcoin/bitcoin/blob/master/src/rpcdump.cpp#L35
hello_good_sir (OP)
Hero Member
*****
Offline Offline

Activity: 1008
Merit: 531



View Profile
May 07, 2013, 11:49:50 PM
 #8

Thank you for taking the time to help me with this.

My goal is to have a short program that I can give 256 random bits to (which I am confident that I can generate by hand) and it will produce a bitcoin address and its private key.  I know how to program but I do not know much about cryptography.  I could try to write the key generation process but I would rather use proven code.  I was thinking about making the entire key pair by hand but then I realized that the hashing algorithm is far too computationally intensive to do by hand (as it should be)

Edit: thanks for the link.  I think that I'm going to have to read a lot more code than I thought I would.  I was hoping for a big ball of mud function that I could just change a little.

calian
Sr. Member
****
Offline Offline

Activity: 354
Merit: 250



View Profile
May 08, 2013, 12:02:19 AM
 #9

Thank you for taking the time to help me with this.

My goal is to have a short program that I can give 256 random bits to (which I am confident that I can generate by hand) and it will produce a bitcoin address and its private key.  I know how to program but I do not know much about cryptography.  I could try to write the key generation process but I would rather use proven code.  I was thinking about making the entire key pair by hand but then I realized that the hashing algorithm is far too computationally intensive to do by hand (as it should be)

Edit: thanks for the link.  I think that I'm going to have to read a lot more code than I thought I would.  I was hoping for a big ball of mud function that I could just change a little.

If you're just going to do this a few times use your random bits as a passphrase with one of the web-based address generators saved in offline mode.

http://brainwallet.org/
https://www.bitaddress.org/bitaddress.org-v2.4-SHA1-1d5951f6a04dd5a287ac925da4e626870ee58d60.html
DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4672



View Profile
May 08, 2013, 01:36:54 AM
 #10

Thank you for taking the time to help me with this.

My goal is to have a short program that I can give 256 random bits to (which I am confident that I can generate by hand) and it will produce a bitcoin address and its private key.  I know how to program but I do not know much about cryptography.  I could try to write the key generation process but I would rather use proven code.  I was thinking about making the entire key pair by hand but then I realized that the hashing algorithm is far too computationally intensive to do by hand (as it should be)

Edit: thanks for the link.  I think that I'm going to have to read a lot more code than I thought I would.  I was hoping for a big ball of mud function that I could just change a little.

According to this discussion I just found:
http://openssl.6102.n7.nabble.com/Question-on-EC-KEY-generate-key-td27218.html

It sounds like you can use openssl and call:

Code:
EC_KEY_set_private_key
http://openssl.sourcearchive.com/documentation/1.0.0e-2/crypto_2ec_2ec_8h_ac71ebd801dd40f9b9c42b61aff685f3d.html#ac71ebd801dd40f9b9c42b61aff685f3d


Followed by:
Code:
EC_POINT_mul
http://openssl.sourcearchive.com/documentation/1.0.0e-2/crypto_2ec_2ec_8h_a5e5532068774ce6da8ca68ac627176a8.html#a5e5532068774ce6da8ca68ac627176a8


Once you have the public key, you can use the information here to generate an address:
https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!