Bitcoin Forum
May 04, 2024, 02:17:27 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Recovered some old btc (technical details included)  (Read 114 times)
c_atlas (OP)
Member
**
Offline Offline

Activity: 140
Merit: 56


View Profile
September 08, 2020, 12:54:35 AM
 #1

Not sure if this is the right board for this but I just wanted to share an experience I had since it's bitcoin related and I had a fun time figuring this out. Maybe ITT we can talk about the ways we've strayed from the friendly modern bitcoin and actually played around with the internals a bit.

Tl;dr found some old btc that I forgot about (not in any wallet) and recovered it with Python and Coinbi.n

So, back in 2018 when I was messing around with bitcoin address generation I had sent a small amount of btc to an address that I had generated with a Python script. A couple of days ago I decided to check if the address still had funds and if I still had the private key, turns out the answers were yes and yes.

To give you an idea of what I found, the data consisted of a textfile containing the following:
  • the (hex) private key as a python3 byte string
  • the (hex) public key as a python3 byte string
  • the (hex) public address as a python3 byte string
  • the (hex) bitcoin address (15LZ5SZp8kiegoBWb5zRnVpRy4WvZuQXtd)

After figuring out how to convert byte strings into ordinary UTF-8 python strings, I noticed a couple of things. First, the public key had the prefix 0x04, so it was an uncompressed public key. Second, because I wanted to use coinb.in to send the bitcoin to a segwit address (since I've never had the chance to use segwit before), I needed to convert the hexadecimal private key to WIF in order to sign the transaction.

Converting the private key to WIF was probably the most time consuming part since I wanted to do this in python as well. I ran into a few problems while trying to generate a checksum (steps 3 and 4 of this guide) because I forgot to encode the extended private key (the hex private key with 0x80 in the front).

Here's a dummy example with private key 0000000000000000000000000000000000486920626974636F696E74616C6B21 (aside: put this in a hex to ascii converter),
Code: (python3)
import hashlib

private_key = "0000000000000000000000000000000000486920626974636F696E74616C6B21"

# add the 0x80 prefix for mainnet, do not add a 0x01 suffix since our address is uncompressed.
extended_p_key = "80" + private_key

# the line below is wrong, extended_p_key is a utf-8 string.
first_hash = hashlib.sha256(extended_p_key).hexdigest()

# the line below is correct, bytes.fromhex(extended_p_key) is hex encoded.
first_hash = hashlib.sha256(bytes.fromhex(extended_p_key)).hexdigest()

Once the private key was converted to WIF I simply signed and broadcast the transaction with Coinb.in, which was an exciting moment! However, it wasn't long before I realized that I set the fee far too low. Luckily, I noticed replace-by-fee was enabled, so I quickly put together another transaction (which you can see here) and set a higher fee. This time coinb.in didn't automatically build the transaction for me (I suppose this happened because it couldn't find UTxO due to the previous transaction), so I had to manually enter the details of the input, which I had no trouble doing thanks to this lovely book (link uses Andreas's reference code).

All in all, I'm 0.00074474BTC richer  Grin.

I know this isn't some incredible technical achievement or anything, but it's so refreshing to actually use the technology and see how cool it is first hand, just felt like sharing some excitement.
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!