Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: ThePiachu on January 10, 2012, 01:38:45 AM



Title: What is the "wallet export format"?
Post by: ThePiachu on January 10, 2012, 01:38:45 AM
It was mentioned here:
https://en.bitcoin.it/wiki/Mini_private_key_format
but it doesn't seem to be explained on the wiki. Is it just a base58 encoded private key, or is there more to it than that?


Title: Re: What is the "wallet export format"?
Post by: bitfreak! on January 10, 2012, 05:56:18 AM
You mean "wallet import format" right?

Quote
Is it just a base58 encoded private key, or is there more to it than that?
To turn a normal private key into a WIF key you need to perform Base58Check encoding.

I'm pretty sure steps 4 to 9 on this page (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) describe the process.
EDIT: btw, at step 4 you start with the private key and attach 0x80 to the front instead of 0x00.

Quote
9 - Convert the result from a byte string into a base58 string using Base58Check encoding.

I think this step is incorrect. The process completed between steps 4 and 9 is Base58Check encoding from what I understand. At step 9 you convert the result from a byte string into a base58 string using a specialized base58 character set: Base58 symbol chart (https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart). On that same page you'll also see another step by step guide for Creating a Base58Check string (https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string).

To turn a public key into a bitcoin address it's the exact same process, except you need to start at step 1 instead of step 4. So you perform Base58Check encoding on the RIPEMD-160 hash.


Title: Re: What is the "wallet export format"?
Post by: bitfreak! on January 10, 2012, 06:14:32 AM
Ok I see what you were referring to now:

Quote
This sample key in wallet export format is 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ, and the corresponding Bitcoin address is 1GAehh7TsJAHuUAeKZcXf5CnwuGuGgyX2S.

I think that's actually meant to say "wallet import format".

Also have a look at this: Base 58 Wallet Import format (https://en.bitcoin.it/wiki/Private_key#Base_58_Wallet_Import_format)


Title: Re: What is the "wallet export format"?
Post by: mcorlett on January 10, 2012, 06:24:56 AM
The export/import wording can be used interchangeably.


Title: Re: What is the "wallet export format"?
Post by: ThePiachu on January 10, 2012, 08:24:19 AM
You mean "wallet import format" right?

Quote
Is it just a base58 encoded private key, or is there more to it than that?
To turn a normal private key into a WIF key you need to perform Base58Check encoding.

I'm pretty sure steps 4 to 9 on this page (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) describe the process.
EDIT: btw, at step 4 you start with the private key and attach 0x80 to the front instead of 0x00.
That link is about creating a Bitcoin address from a key pair, not from the "wallet import/export format" which is said to "include built-in error checking codes" (from the "Private key" wiki entry).
Quote
9 - Convert the result from a byte string into a base58 string using Base58Check encoding.

I think this step is incorrect. The process completed between steps 4 and 9 is Base58Check encoding from what I understand.
The steps are correct for creating a Bitcoin address from a key pair, wrote that step-by-step guide myself, originally basing it off a public key used by Satoshi.
To turn a public key into a bitcoin address it's the exact same process, except you need to start at step 1 instead of step 4. So you perform Base58Check encoding on the RIPEMD-160 hash.
Didn't ask about turning a public key into a Bitcoin address, but turning the "wallet import/export format" into a private key. The rest I understand.

Also have a look at this: Base 58 Wallet Import format (https://en.bitcoin.it/wiki/Private_key#Base_58_Wallet_Import_format)
That Wiki page is a bit vague on the details as to how one should convert the WIF into a private key.


Title: Re: What is the "wallet export format"?
Post by: bitfreak! on January 10, 2012, 08:27:25 AM
Quote
That link is about creating a Bitcoin address from a key pair, not from the "wallet import/export format" which is said to "include built-in error checking codes" (from the "Private key" wiki entry).
Steps 4 to 9 describe how to do Base58Check encoding (but remember what I said about step 9). A WIF (wallet import format) key is simply a Base58Check encoded private key.

EDIT: so let me make this clearer:

1) Attach '80' to start of private key.
2) Perform SHA-256 hash on the whole string.
3) Perform SHA-256 hash on the result of the previous SHA-256 hash.
4) Take the first 4 bytes of the last SHA-256 hash. This is the checksum.
5) Add the 4 checksum bytes from point 4 to the end of the string from point 1.
6) Convert the result from a byte string into a base58 string.

EDIT: If you look here (https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string) you'll see the process (Base58Check encoding) described in a few steps:

Quote
A Base58Check string is created from a version/application byte and payload as follows.

1.  Take the version/application byte and payload bytes, and concatenate them together (bytewise).
2.  Take the first four bytes of SHA256(SHA256(results of step 1))
3.  Concatenate the results of step 1 and the results of step 2 together (bytewise).
4.  Treating the results of step 3 - a series of bytes - as a single big-endian bignumber, convert to base-58 using normal mathematical steps (bignumber division) and the base-58 alphabet described below. The result should be normalized to not have any leading base-58 zeroes (character '1').


Title: Re: What is the "wallet export format"?
Post by: ThePiachu on January 10, 2012, 10:24:45 AM
Okay, now I get it. You were just explaining things the other way around (private key -> import format), whereas I was trying to understand how to convert things back.

So in the end, to convert things from WIF to a private key, I would:
1) Convert the WIF into a hex array as any base58 encoded string
2) Drop the first byte and last 4 bytes

And I should also perform some checks to make sure the string wasn't corrupted by calculating the checksum.


Title: Re: What is the "wallet export format"?
Post by: bitfreak! on January 10, 2012, 10:44:01 AM
So in the end, to convert things from WIF to a private key, I would:
1) Convert the WIF into a hex array as any base58 encoded string
2) Drop the first byte and last 4 bytes

And I should also perform some checks to make sure the string wasn't corrupted by calculating the checksum.
Yeah I think that would be pretty much how to do it. Make sure to use the Base58 character set used by Bitcoin when doing the Base58 conversions.


Title: Re: What is the "wallet export format"?
Post by: ThePiachu on January 10, 2012, 01:25:59 PM
Oh well, now it's all nice and explained:
https://en.bitcoin.it/wiki/Wallet_import_format
And I've also written a test suite:
http://gobittest.appspot.com/PrivateKey