Bitcoin Forum
December 11, 2024, 06:17:49 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need info: generate private key in Bitcoin Core 0.21.0  (Read 151 times)
pbies (OP)
Full Member
***
Offline Offline

Activity: 298
Merit: 133



View Profile
February 06, 2021, 10:53:53 AM
 #1

Hi guys,

can anyone share where in source code of Bitcoin Core 0.21.0 is the function that generates new wallet and/or each single private key in that wallet?

I want to make some research about this part of code but can't find the proper file/place...

Thanks!

BTC: bc1qmrexlspd24kevspp42uvjg7sjwm8xcf9w86h5k
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1736
Merit: 8448


Fiatheist


View Profile WWW
February 06, 2021, 11:12:13 AM
 #2

can anyone share where in source code of Bitcoin Core 0.21.0 is the function that generates new wallet and/or each single private key in that wallet?
Do you mean how it generates the address? Well, once you enter "getnewaddress" on the console it calls this UniValue object on bitcoin-cli.cpp

(line: 728)
Code:
/**
 * Call RPC getnewaddress.
 * @returns getnewaddress response as a UniValue object.
 */
static UniValue GetNewAddress()
{
    Optional<std::string> wallet_name{};
    if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
    DefaultRequestHandler rh;
    return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name);
}

As for how it performs ECDSA to a private key, I don't know, but it must be somehow connected with the above.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
pbies (OP)
Full Member
***
Offline Offline

Activity: 298
Merit: 133



View Profile
February 06, 2021, 11:51:04 AM
 #3

Seems like later this code calls GetNewDestination.

But also seems like this is getting more public addresses deriving from the already existing wallet/private key, than producing whole new private key/wallet.

BTC: bc1qmrexlspd24kevspp42uvjg7sjwm8xcf9w86h5k
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1736
Merit: 8448


Fiatheist


View Profile WWW
February 06, 2021, 12:00:30 PM
 #4

But also seems like this is getting more public addresses deriving from the already existing wallet/private key, than producing whole new private key/wallet.
I don't remember this exactly, but once you create a new wallet it has already generated some addresses. When you enter "getnewaddress" it just picks an address from the already generated ones. It doesn't generate you a new address.

Here's a thread to read: About getnewaddress RPC API.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
NotATether
Legendary
*
Offline Offline

Activity: 1806
Merit: 7475


Top Crypto Casino


View Profile WWW
February 06, 2021, 12:40:17 PM
Merited by ABCbits (1)
 #5

The private key class is located in src/key.h and is called CPrivKey. It contains the functions that make a signature and extract the public key (the result of which is usually cached as it takes a long time to run). And in walletdb.cpp is the function that writes the public and (encrypted) private keys to the wallet.dat file, which is just a Berkeley database. It also has a big function that reads al of the database keys from the wallet.dat, including the encrypted private keys.

And to answer your question, here is where the actual wallet structure is created inside the program. As you can see, it sets its password to blank first, calls the unlock function for it, and then generates the initial private keys within SetupGeneration(), sets a "seed" (not a mnemonic phrase) for the purposes of deriving keys from the newly created HD wallet, and creates an empty keypool for it with a no keys inside.

███████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████

███████████████████████
.
BC.GAME
▄▄▀▀▀▀▀▀▀▄▄
▄▀▀░▄██▀░▀██▄░▀▀▄
▄▀░▐▀▄░▀░░▀░░▀░▄▀▌░▀▄
▄▀▄█▐░▀▄▀▀▀▀▀▄▀░▌█▄▀▄
▄▀░▀░░█░▄███████▄░█░░▀░▀▄
█░█░▀░█████████████░▀░█░█
█░██░▀█▀▀█▄▄█▀▀█▀░██░█
█░█▀██░█▀▀██▀▀█░██▀█░█
▀▄▀██░░░▀▀▄▌▐▄▀▀░░░██▀▄▀
▀▄▀██░░▄░▀▄█▄▀░▄░░██▀▄▀
▀▄░▀█░▄▄▄░▀░▄▄▄░█▀░▄▀
▀▄▄▀▀███▄███▀▀▄▄▀
██████▄▄▄▄▄▄▄██████
.
..CASINO....SPORTS....RACING..


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
pbies (OP)
Full Member
***
Offline Offline

Activity: 298
Merit: 133



View Profile
February 06, 2021, 03:03:55 PM
 #6

Thanks guys once again!

Key.cpp was the file I was looking for.

BTC: bc1qmrexlspd24kevspp42uvjg7sjwm8xcf9w86h5k
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!