A private key in the context of Bitcoin is a secret number that allows bitcoins to be spent. Every Bitcoin wallet contains one or more private keys, which are saved in the wallet file. The private keys are mathematically related to all Bitcoin addresses generated for the wallet.
Because the private key is the “ticket” that allows someone to spend bitcoins, it is important that these are kept secret and safe. Private keys can be kept on computer files, but are also often written on paper.
Private keys themselves are almost never handled by the user, instead the user will typically be given a seed phrase that encodes the same information as private keys.
Some wallets allow private keys to be imported without generating any transactions while other wallets or services require that the private key be swept. When a private key is swept, a transaction is broadcast that sends the balance controlled by the private key to a new address in the wallet. Just as with any other transaction, there is risk of swept transactions to be double-spending.
In contrast, bitcoind provides a facility to import a private key without creating a sweep transaction. This is considered very dangerous, and not intended to be used even by power users or experts except in very specific cases. Importing keys could lead to the Bitcoins being stolen at any time, from a wallet which has imported an untrusted or otherwise insecure private key – this can include private keys generated offline and never seen by someone else
An example private key
In Bitcoin, a private key is a 256-bit number, which can be represented one of several ways. Here is a private key in hexadecimal – 256 bits in hexadecimal is 32 bytes, or 64 characters in the range 0-9 or A-F.E9873D79C6D87DC0FB6A5778633389_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_F4453213303DA61F20BD67FC233AA33262
Range of valid ECDSA private keys
Nearly every 256-bit number is a valid ECDSA private key. Specifically, any 256-bit number from 0x1 to 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140 is a valid private key.
The range of valid private keys is governed by the secp256k1 ECDSA standard used by Bitcoin.
Hierarchical Deterministic (HD) Wallet Keys
Main article: Hierarchical deterministic wallet
Wallet software may use a BIP 32 seed to generate many private keys and corresponding public keys from a single secret value. This is called a hierarchical deterministic wallet, or HD wallet for short. The seed value, or master extended key, consists of a 256-bit private key and a 256-bit chain code, for 512 bits in total. The seed value should not be confused with the private keys used directly to sign Bitcoin transactions.
Users are strongly advised to use HD wallets, for safety reasons: An HD wallet only needs to be backed up once typically using a seed phrase; thereafter in the future, that single backup can always deterministically regenerate the same private keys. Therefore, it can safely recover all addresses, and all funds sent to those addresses. Non-HD wallets generate a new randomly-selected private key for each new address; therefore, if the wallet file is lost or damaged, the user will irretrievably lose all funds received to addresses generated after the most recent backup.
Base58 Wallet Import format
Main article: Wallet import format
When importing or sweeping ECDSA private keys, a shorter format known as wallet import format is often used, which offers a few advantages. The wallet import format is shorter, and includes built-in error checking codes so that typos can be automatically detected and/or corrected (which is impossible in hex format) and type bits indicating how it is intended to be used. Wallet import format is the most common way to represent private keys in Bitcoin. For private keys associated with uncompressed public keys, they are 51 characters and always start with the number 5 on mainnet (9 on testnet). Private keys associated with compressed public keys are 52 characters and start with a capital L or K on mainnet (c on testnet). This is the same private key in (mainnet) wallet import format:5Kb8kLf9zgWQnogidDA76Mz_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_PL6TsZZY36hWXMssSzNydYXYB9KF
When a WIF private key is imported, it always corresponds to exactly one Bitcoin address. Any utility which performs the conversion can display the matching Bitcoin address. The mathematical conversion is somewhat complex and best left to a computer, but it’s notable that the WIF guarantees it will always correspond to the same address no matter which program is used to convert it.
The Bitcoin address implemented using the sample above is: 1CC3X2gu58d6wXUW_SAMPLE_ADDRESS_DO_NOT_SEND_MffpuzN9JAfTUWu4Kj
Mini private key format
Main article: Mini private key format
Some applications use the mini private key format. Not every private key or Bitcoin address has a corresponding mini private key – they have to be generated a certain way in order to ensure a mini private key exists for an address. The mini private key is used for applications where space is critical, such as in QR codes and in physical bitcoins. The above example has a mini key, which is:SzavMBLoXU6_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_kDrqtUVmffv
Summary
Any Bitcoins sent to the address 1CC3X2gu58d6wXUW_SAMPLE_ADDRESS_DO_NOT_SEND_MffpuzN9JAfTUWu4Kj can be spent by anybody who knows the private key implementing it in any of the three formats, regardless of when the bitcoins were sent, unless the wallet receiving them has since made use of the coins generated. The private key is only needed to spend the bitcoins, not necessarily to see the value of them.
If a private key controlling unspent bitcoins is compromised or stolen, the value can only be protected if it is immediately spent to a different output which is secure. Because bitcoins can only be spent once, when they are spent using a private key, the private key becomes worthless. It is often possible, but inadvisable and insecure, to use the address implemented by the private key again, in which case the same private key would be reused.
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.
http://gobittest.appspot.com/PrivateKey
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.
How to create a WIF private key.
A WIF private key is a standard private key, but with a few added extras:
- Version Byte prefix – Indicates which network the private key is to be used on.
0x80
= Mainnet0xEF
= Testnet
- Compression Byte suffix (optional) – Indicates if the private key is used to create a compressed public key.
0x01
- Checksum – Useful for detecting errors/typos when you type out your private key.
This is all then converted to Base58, which shortens the entire thing and makes it easier to transcribe…
A WIF private key is just another way of representing your original private key. If you have a WIF private key, you can always convert it back in to its original format.