Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: pbies on February 06, 2021, 10:53:53 AM



Title: Need info: generate private key in Bitcoin Core 0.21.0
Post by: pbies on February 06, 2021, 10:53:53 AM
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!


Title: Re: Need info: generate private key in Bitcoin Core 0.21.0
Post by: BlackHatCoiner on February 06, 2021, 11:12:13 AM
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.


Title: Re: Need info: generate private key in Bitcoin Core 0.21.0
Post by: pbies on February 06, 2021, 11:51:04 AM
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.


Title: Re: Need info: generate private key in Bitcoin Core 0.21.0
Post by: BlackHatCoiner on February 06, 2021, 12:00:30 PM
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 (https://bitcointalk.org/index.php?topic=5315016.0).


Title: Re: Need info: generate private key in Bitcoin Core 0.21.0
Post by: NotATether on February 06, 2021, 12:40:17 PM
The private key class is located in src/key.h (https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/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 (https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/wallet/walletdb.cpp#L111-L135) 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 (https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/wallet/walletdb.cpp#L407-L427).

And to answer your question, here (https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/wallet/wallet.cpp#L246-L325) 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() (https://github.com/bitcoin/bitcoin/blob/afdfd3c8c1ce96adae11809e3989de381137fee9/src/wallet/scriptpubkeyman.cpp#L409-L425), 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.


Title: Re: Need info: generate private key in Bitcoin Core 0.21.0
Post by: pbies on February 06, 2021, 03:03:55 PM
Thanks guys once again!

Key.cpp was the file I was looking for.