1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-22 16:18:11 +00:00

added code example in text and the output of running the code

This commit is contained in:
Andreas M. Antonopoulos 2014-06-02 15:10:42 -04:00
parent 4ab26920b9
commit 774a778a45

View File

@ -380,6 +380,42 @@ Here's the same key, encoded in WIF and WIF-compressed formats
"Compressed private keys" is a misnomer! They are not compressed, rather the WIF-compressed format signifies that they should only be used to derive compressed public keys and their corresponding bitcoin addresses. Ironically, a "WIF-compressed" encoded private key is one byte longer because it has the added 01 suffix to distinguish it from an "uncompressed" one.
====
==== Implementing Keys and Addresses in Python
The most comprehensive bitcoin library in Python is "pybitcointools"by Vitalik Buterin (https://github.com/vbuterin/pybitcointools). In the following code example, we use the pybitcointools library (imported as "bitcoin") to generate and display keys and addresses in various formats:
.Key and Address generation and formatting with the pybitcointools library
[source,python]
----
include::code/key-to-address-ecc-example.py[]
----
Here's the output from running this code:
----
$ python key-to-address-ecc-example.py
Private Key (hex) is: 3c8a71bb695971173ccae60a45318344be0cf9417cce96f6567bd75f63a1907b
Private Key (decimal) is: 27383380759877199262221438557833276618414788020082369083037022892126887841915
Private Key (WIF) is: 5JGx1pWAQajcVG6wTRxh8tgABamvFYTDwzQptbhvmExbCzb8ttL
Private Key Compressed (hex) is: 3c8a71bb695971173ccae60a45318344be0cf9417cce96f6567bd75f63a1907b01
Private Key (WIF-Compressed) is: KyFPpcfiX5u5djfDMfAp9q5qXi5G3Bpk9iQANkBeJGcMyt8hRq3b
Public Key (x,y) coordinates is: (33709093068486263861880850179979243620450326373098856144029226806527777418227L, 87754931750760896623087037281983555752553586136725155039294247309811192676148L)
Public Key (hex) is: 044a86aba96feef152a5b874c6837c0f7ed5682ae187a4d6e3054e840b713dabf3c20387fe07e0acc79ffc6471df76bd8cfeb417814ffaf5aa13a351b12fd3bf34
Compressed Public Key (hex) is: 024a86aba96feef152a5b874c6837c0f7ed5682ae187a4d6e3054e840b713dabf3
Bitcoin Address (b58check) is: 1JXa2Ed859qjc34NZpuWBw4DbsuDM29iVU
Compressed Bitcoin Address (b58check) is: 1B6bnePrivate Key (hex) is: 8fef4b5db53784bf1ba9eb71ef29fed17e0dd303eaaec86a30758ddc3926b379
Private Key (decimal) is: 65103533953224540524547925689829615797042404424439863593942250239002409350009
Private Key (WIF) is: 5JugCdctbqsPLTjMpWKiwD4zUWMBmcPSmk3qV7ChZGvGb28Xpsf
Private Key Compressed (hex) is: 8fef4b5db53784bf1ba9eb71ef29fed17e0dd303eaaec86a30758ddc3926b37901
Private Key (WIF-Compressed) is: L23W2LdQknhPatyxtgRe6GGcgQY6XuC4kV1X14Ne1TG7nwmHYYsG
Public Key (x,y) coordinates is: (67578901098964128544627522542591015499444294518340016249145862167441690490539L, 28310898985008778347793460074000413092507596924295253907953807553014541258705L)
Public Key (hex) is: 0495684d742137171b53e82e1f7c80f9990b6882dee4ffa7d4b7b8d6676c2276ab3e9766a677835fc0841d68d0b51c5c28358240758e6bcf852cbcb884822b0fd1
Compressed Public Key (hex) is: 0395684d742137171b53e82e1f7c80f9990b6882dee4ffa7d4b7b8d6676c2276ab
Bitcoin Address (b58check) is: 1EfvZqAJM9AUcAwFHKQodK7rKzcbVRqztw
Compressed Bitcoin Address (b58check) is: 14oxVkXBUsxZSWqjR3i9Q4ru9MPVNpH3LE
----
=== Wallets
Wallets are containers for private keys, usually implemented as structured files or simple databases.