Wallet import format: Difference between revisions

From Bitcoin Wiki
Jump to navigation Jump to search
(Fixed some formatting issues.)
(scummy WIF converter was just stealing private keys and was written badly anyway)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{sample}}
{{sample}}
A '''wallet import format''' ('''WIF''', also known as a '''wallet export format''') is a way of encoding a private ECDSA key so as to make it easier to copy.
A '''wallet import format''' ('''WIF''', also known as a '''wallet export format''') is a way of encoding a private ECDSA key so as to make it easier to copy.
A testing suite is available for encoding and decoding of WIF at:
http://gobittest.appspot.com/PrivateKey


==Private key to WIF==
==Private key to WIF==
Line 29: Line 25:
3. Drop the last 4 checksum bytes from the byte string.
3. Drop the last 4 checksum bytes from the byte string.
     800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
     800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
4. Drop the first byte (it should be <code>0x80</code>). If the private key corresponded to a compressed public key, also drop the last byte (it should be <code>0x01</code>). If it corresponded to a compressed public key, the WIF string will have started with K or L instead of 5 (or c instead of 9 on testnet). This is the private key.
4. Drop the first byte (it should be <code>0x80</code>, however legacy Electrum<ref name="electrum-300-wif">[https://github.com/spesmilo/electrum/blob/3.0.0/RELEASE-NOTES#L42]</ref><ref name="electrum-310-wif">[https://github.com/spesmilo/electrum/blob/3.1.0/RELEASE-NOTES#L58]</ref> or some SegWit vanity address generators<ref name="segvan-wif">[https://github.com/nym-zone/segvan/blob/388b157c68c3b45f7c3200cc62a2fea6ffcb5555/segvan.c#L88]</ref> may use <code>0x81-0x87</code>). If the private key corresponded to a compressed public key, also drop the last byte (it should be <code>0x01</code>). If it corresponded to a compressed public key, the WIF string will have started with K or L (or M, if it's exported from legacy Electrum<ref name="electrum-300-wif" /><ref name="electrum-310-wif" /> etc<ref name="segvan-wif" />) instead of 5 (or c instead of 9 on testnet). This is the private key.
     0C28FCA386C7A227600B2FE50B7CAE1{{taggant private key}}1EC86D3BF1FBE471BE89827E19D72AA1D
     0C28FCA386C7A227600B2FE50B7CAE1{{taggant private key}}1EC86D3BF1FBE471BE89827E19D72AA1D
==WIF checksum checking==
==WIF checksum checking==
1. Take the wallet import format (WIF) string.
1. Take the wallet import format (WIF) string.
Line 47: Line 44:
     507A5B8D
     507A5B8D
8. If they are, and the byte string from point 2 starts with <code>0x80</code> (<code>0xef</code> for testnet addresses), then there is no error.
8. If they are, and the byte string from point 2 starts with <code>0x80</code> (<code>0xef</code> for testnet addresses), then there is no error.
== References ==
<references />


{{Stub}}
{{Stub}}
{{Bitcoin Core documentation}}
{{Bitcoin Core documentation}}

Latest revision as of 19:54, 11 March 2024

This page contains sample addresses and/or private keys. Do not send bitcoins to or import any sample keys; you will lose your money.

A wallet import format (WIF, also known as a wallet export format) is a way of encoding a private ECDSA key so as to make it easier to copy.

Private key to WIF

1. Take a private key.

   0C28FCA386C7A227600B2FE50B7CAE_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_11EC86D3BF1FBE471BE89827E19D72AA1D

2. Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key.

   800C28FCA386C7A227600B2FE50B7C_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_AE11EC86D3BF1FBE471BE89827E19D72AA1D

3. Perform SHA-256 hash on the extended key.

   8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

4. Perform SHA-256 hash on result of SHA-256 hash.

   507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

5. Take the first 4 bytes of the second SHA-256 hash; this is the checksum.

   507A5B8D

6. Add the 4 checksum bytes from point 5 at the end of the extended key from point 2.

   800C28FCA386C7A227600B2FE50B7CAE11EC8_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_6D3BF1FBE471BE89827E19D72AA1D507A5B8D

7. Convert the result from a byte string into a base58 string using Base58Check encoding. This is the wallet import format (WIF).

   5HueCGU8rMjxEXxiPuD5BDk_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_u4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

WIF to private key

1. Take a wallet import format (WIF) string.

   5HueCGU8rMjxEXxiPuD5BDk_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_u4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

2. Convert it to a byte string using Base58Check encoding.

   800C28FCA386C7A227600B2FE50B7CAE11EC_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_86D3BF1FBE471BE89827E19D72AA1D507A5B8D

3. Drop the last 4 checksum bytes from the byte string.

   800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

4. Drop the first byte (it should be 0x80, however legacy Electrum[1][2] or some SegWit vanity address generators[3] may use 0x81-0x87). If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01). If it corresponded to a compressed public key, the WIF string will have started with K or L (or M, if it's exported from legacy Electrum[1][2] etc[3]) instead of 5 (or c instead of 9 on testnet). This is the private key.

   0C28FCA386C7A227600B2FE50B7CAE1_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_1EC86D3BF1FBE471BE89827E19D72AA1D

WIF checksum checking

1. Take the wallet import format (WIF) string.

   5HueCGU8rMjxEXxiPuD5BD_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_ku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

2. Convert it to a byte string using Base58Check encoding.

   800C28FCA386C7A227600B2FE50B7CAE11E_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_C86D3BF1FBE471BE89827E19D72AA1D507A5B8D

3. Drop the last 4 checksum bytes from the byte string.

   800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

4. Perform SHA-256 hash on the shortened string.

   8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

5. Perform SHA-256 hash on result of SHA-256 hash.

   507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

6. Take the first 4 bytes of the second SHA-256 hash; this is the checksum.

   507A5B8D

7. Make sure it is the same as the last 4 bytes from point 2.

   507A5B8D

8. If they are, and the byte string from point 2 starts with 0x80 (0xef for testnet addresses), then there is no error.

References

  1. 1.0 1.1 [1]
  2. 2.0 2.1 [2]
  3. 3.0 3.1 [3]

This page is a stub. Help by expanding it.