Bitcoin Forum
May 22, 2024, 10:50:05 AM *
News: Latest Bitcoin Core release: 27.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 132 times)
pbies (OP)
Full Member
***
Offline Offline

Activity: 248
Merit: 126



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
I have 9900K and 1080 Ti, gathering funds for new desktop PC for Bitcoin operations - 12900K and RTX 4090
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1526
Merit: 7401


Farewell, Leo


View Profile
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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
pbies (OP)
Full Member
***
Offline Offline

Activity: 248
Merit: 126



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
I have 9900K and 1080 Ti, gathering funds for new desktop PC for Bitcoin operations - 12900K and RTX 4090
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1526
Merit: 7401


Farewell, Leo


View Profile
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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
NotATether
Legendary
*
Offline Offline

Activity: 1610
Merit: 6753


bitcoincleanup.com / bitmixlist.org


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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
pbies (OP)
Full Member
***
Offline Offline

Activity: 248
Merit: 126



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
I have 9900K and 1080 Ti, gathering funds for new desktop PC for Bitcoin operations - 12900K and RTX 4090
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!