Author

Topic: Need info: generate private key in Bitcoin Core 0.21.0 (Read 151 times)

full member
Activity: 297
Merit: 133
Thanks guys once again!

Key.cpp was the file I was looking for.
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
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.
legendary
Activity: 1512
Merit: 7340
Farewell, Leo
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.
full member
Activity: 297
Merit: 133
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.
legendary
Activity: 1512
Merit: 7340
Farewell, Leo
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 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.
full member
Activity: 297
Merit: 133
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!
Jump to: