Bitcoin Forum
December 09, 2025, 06:08:25 PM *
News: Latest Bitcoin Core release: 30.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Private key and filling change in bitcoin core v0.1  (Read 164 times)
Tfs (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 63


View Profile
March 05, 2025, 08:41:46 AM
Last edit: March 05, 2025, 12:59:55 PM by Tfs
Merited by ABCbits (1)
 #1

Hello,

In bitcoin core v0.1 in the file main.cpp starting at line 2514 there is a function CreateTransaction. In it there is a part as follows:

// Fill vin
                foreach(CWalletTx* pcoin, setCoins)
                    for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
                        if (pcoin->vout[nOut].IsMine())
                            wtxNew.vin.push_back(CTxIn(pcoin->GetHash(), nOut));

I believe this is where a new adress is created for the change, is that correct? If so, how is it created exactly? I mean I am scanning the code to find where exactly is computed the private key of the change adress.

Edit: and in the function ProcessMessage, there is on line 1984 the following

// Keep giving the same key to the same ip until they use it
        if (!mapReuseKey.count(pfrom->addr.ip))
            mapReuseKey[pfrom->addr.ip] = GenerateNewKey();

It is the only place that calls the function GenerateNewKey that uses MakeNewKey from key.h so ot mist somehow be called when creating change...
pooya87
Legendary
*
Offline Offline

Activity: 4018
Merit: 12083



View Profile
March 05, 2025, 02:22:31 PM
Merited by ABCbits (4), DaveF (3), vapourminer (1), nc50lc (1), mcdouglasx (1)
 #2

// Fill vin
                foreach(CWalletTx* pcoin, setCoins)
                    for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
                        if (pcoin->vout[nOut].IsMine())
                            wtxNew.vin.push_back(CTxIn(pcoin->GetHash(), nOut));

I believe this is where a new adress is created for the change, is that correct?
I don't think so. "vin" is the list of inputs not outputs and here inside the loop it is just setting the input.

Quote
If so, how is it created exactly? I mean I am scanning the code to find where exactly is computed the private key of the change adress.
It looks like the change is being sent to the public key of one of the inputs a couple of lines above what you posted L2545. The code checks if the "coin" belongs to the wallet then extracts its pubkey script to be used for change.
Code:
// Fill vout[1] back to self with any change
if (nValueIn > nValue)
{
    // Use the same key as one of the coins
    vector<unsigned char> vchPubKey;
    CTransaction& txFirst = *(*setCoins.begin());
    foreach(const CTxOut& txout, txFirst.vout)
        if (txout.IsMine())
            if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
                break;
    if (vchPubKey.empty())
        return false;

    // Fill vout[1] to ourself
    CScript scriptPubKey;
    scriptPubKey << vchPubKey << OP_CHECKSIG;
    wtxNew.vout.push_back(CTxOut(nValueIn - nValue, scriptPubKey));
}

Tfs (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 63


View Profile
March 05, 2025, 02:58:19 PM
 #3

Thank you. I saw that also but I was puzzled since I thought the change was sent to a new adress, not an existing one.
DaveF
Legendary
*
Offline Offline

Activity: 4046
Merit: 6956



View Profile WWW
March 05, 2025, 03:58:21 PM
Last edit: March 05, 2025, 05:14:34 PM by DaveF
Merited by pooya87 (4), vapourminer (2), ABCbits (2)
 #4

Thank you. I saw that also but I was puzzled since I thought the change was sent to a new adress, not an existing one.


It does now, it did not then.

Part of the issue of looking at really old code be it BTC or any other program is things that are done a certain way now and may have been done that way for over a decade may have started out working in a different way.
So, when you go look at something with the 2025 mindset a program that was written in 2015 may behave in a totally different way. And then you spend hours trying to figure out how something worked, when in reality the feature / specification did not even exist back then. Never mind work in the way it does today.

Been there, done that.

-Dave

This space for rent.
Tfs (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 63


View Profile
March 05, 2025, 06:07:07 PM
 #5

Thank you Dave.

Does anybody know in which release it changed and where is the corresponding source code?
DaveF
Legendary
*
Offline Offline

Activity: 4046
Merit: 6956



View Profile WWW
March 05, 2025, 07:02:50 PM
 #6

Thank you Dave.

Does anybody know in which release it changed and where is the corresponding source code?

Sometime in 2010.
Take a look at the 0.3.xx release notes not sure when.

You might be able to google the exact one.

At a customer so I'm limited as to what I can check from here.

-Dave

This space for rent.
Tfs (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 63


View Profile
March 05, 2025, 10:46:09 PM
Merited by vapourminer (1)
 #7

Thank ypu very much Dave, it lead me to 0.3 on sourceforge which already had the new key thing https://sourceforge.net/p/bitcoin/code/HEAD/tree/tags/0.3.0/main.cpp

nc50lc
Legendary
*
Offline Offline

Activity: 2982
Merit: 8060


Self-proclaimed Genius


View Profile
March 06, 2025, 08:50:58 AM
Merited by vapourminer (2), DaveF (2), ABCbits (1)
 #8

it lead me to 0.3 on sourceforge which already had the new key thing https://sourceforge.net/p/bitcoin/code/HEAD/tree/tags/0.3.0/main.cpp
If you want the actual commit, it's: Commit 99cef99 (look for the highlighted lines)
That was from Bitcoin v0.1.6 ALPHA, a lot earlier than version 0.3, just a two months after official release.

I don't know how accurate the commit's timestamp in GitHub but that commit is dated Oct 21, 2009.

Tfs (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 63


View Profile
March 06, 2025, 12:59:21 PM
 #9

Thank you nc50lc, we're close now: indeed on sourceforge the v0.2 has it (dated 2009-12-14) and the v0.1.5 hasn't (dated 2009-08-30), so if those dates are correct then it indeed must have been implemented around september or october 2009.
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!