| 1 |
Introduction |
| 2 |
============ |
| 3 |
|
| 4 |
.. image:: https://travis-ci.org/prusnak/bip32utils.svg?branch=master |
| 5 |
:target: https://travis-ci.org/prusnak/bip32utils |
| 6 |
|
| 7 |
The bip32utils library is a pure Python implementation of Bitcoin |
| 8 |
hierarchical deterministic wallet ("HD Wallet") ECDSA key generation |
| 9 |
as specified in BIP0032 (Bitcoin Improvement Proposal #0032). |
| 10 |
|
| 11 |
Deterministic ECDSA key generation allows creating a sequence of |
| 12 |
Bitcoin private and public ECDSA keys from an initial seed and a |
| 13 |
hierarchical set of indices. A number of benefits follow: |
| 14 |
|
| 15 |
* An entire wallet can be backed up once by storing the wallet seed or |
| 16 |
master extended private key, and all future addresses in the wallet |
| 17 |
can be restored from it. |
| 18 |
|
| 19 |
* The creation of public and private ECDSA keys may be separated from |
| 20 |
each other. That is, it is possible to create only the public ECDSA |
| 21 |
key half (and receiving address) of an ECDSA key pair, without the |
| 22 |
ability to create the private half. Thus, one can create receiving |
| 23 |
addresses on a public facing system that if compromised would not |
| 24 |
give the attacker the ability to spend bitcoin received at those |
| 25 |
addresses. A separate, offline machine can generate the |
| 26 |
corresponding private ECDSA keys and sign transactions. |
| 27 |
|
| 28 |
* Public and private ECDSA keys may be created in a hierarchy, and |
| 29 |
control over or visibility of portions of the hierarchy may be |
| 30 |
delegated to third parties. This has uses for auditing, for |
| 31 |
separating ECDSA key sequences into different logical groups or |
| 32 |
accounts, and for giving 3rd parties the ability to create spending |
| 33 |
transactions without first getting a receiving address in advance. |
| 34 |
|
| 35 |
BIP0032 is in draft stage, is subject to change, and is documented at: |
| 36 |
|
| 37 |
https://github.com/sipa/bips/blob/bip32update/bip-0032.mediawiki |
| 38 |
|
| 39 |
Python bip32gen Script |
| 40 |
====================== |
| 41 |
|
| 42 |
This library installs the bip32gen script into $PREFIX/bin, which |
| 43 |
wraps a command-line interface around the BIP32Key class functionality |
| 44 |
described in a later section: |
| 45 |
|
| 46 |
Script Parameters |
| 47 |
----------------- |
| 48 |
|
| 49 |
:: |
| 50 |
|
| 51 |
$ bip32gen -h |
| 52 |
usage: bip32gen [-h] [-x] [-X] -i {entropy,xprv,xpub} [-n AMOUNT] |
| 53 |
[-f FROM_FILE] [-F TO_FILE] -o OUTPUT_TYPE [-v] [-d] |
| 54 |
chain [chain ...] |
| 55 |
|
| 56 |
Create hierarchical deterministic wallet addresses |
| 57 |
|
| 58 |
positional arguments: |
| 59 |
chain list of hierarchical key specifiers |
| 60 |
|
| 61 |
optional arguments: |
| 62 |
-h, --help show this help message and exit |
| 63 |
-x, --input-hex input supplied as hex-encoded ascii |
| 64 |
-X, --output-hex output generated (where applicable) as hex-encoded |
| 65 |
ascii |
| 66 |
-i {entropy,xprv,xpub}, --input-type {entropy,xprv,xpub} |
| 67 |
source material to generate key |
| 68 |
-n AMOUNT, --amount AMOUNT |
| 69 |
amount of entropy to to read (bits), None for all of |
| 70 |
input |
| 71 |
-f FROM_FILE, --from-file FROM_FILE |
| 72 |
filespec of input data, '-' for stdin |
| 73 |
-F TO_FILE, --to-file TO_FILE |
| 74 |
filespec of output data, '-' for stdout |
| 75 |
-o OUTPUT_TYPE, --output-type OUTPUT_TYPE |
| 76 |
output types, comma separated, from |
| 77 |
addr|privkey|wif|pubkey|xprv|xpub|chain |
| 78 |
-v, --verbose verbose output, not for machine parsing |
| 79 |
-d, --debug enable debugging output |
| 80 |
-t, --testnet use testnet format |
| 81 |
|
| 82 |
|
| 83 |
The user specifies the type of input data (currently from entropy, a |
| 84 |
serialized extended private key, or serialized extended public key), |
| 85 |
the filespec to get that input data from (or stdin), the set of output |
| 86 |
fields to generate, whether to hex encode those outputs when |
| 87 |
applicable, and a list of key specifier(s). A key specifier will |
| 88 |
either start with 'm' or 'M' when using entropy as an input source; |
| 89 |
otherwise, when importing from a serialized extended key, the key |
| 90 |
specifier(s) start with the first hierarchical child index to create. |
| 91 |
|
| 92 |
For example, to generate a new master wallet key from entropy and |
| 93 |
output the serialized extended private key for that to stdout: |
| 94 |
|
| 95 |
:: |
| 96 |
|
| 97 |
$ bip32gen -i entropy -f /dev/random -n 128 -o xprv -F - m |
| 98 |
xprv9s21ZrQH143K3eqKCaAW9CvAiKR8SHdikQnR8dVs8eBxC9fYtW69k1gLRTG5o2Rn3gtz651yFGzxRFjtfjLQHmh4kT7YF3vZcZgGdfX7ZVS |
| 99 |
|
| 100 |
To generate the BIP0032 test vector #1, using entropy |
| 101 |
supplied as a hex-encoded string on stdin, and output the private |
| 102 |
ECDSA key, wallet import format for that private ECDSA key, public |
| 103 |
ECDSA key, address, and serialized extended private and public keys, |
| 104 |
hex encoding where applicable, and writing to stdout: |
| 105 |
|
| 106 |
:: |
| 107 |
|
| 108 |
$ echo 000102030405060708090A0B0C0D0E0F | \ |
| 109 |
bip32gen -v \ |
| 110 |
-i entropy -f - -x \ |
| 111 |
-o privkey,wif,pubkey,addr,xprv,xpub -F - -X \ |
| 112 |
m \ |
| 113 |
m/0h \ |
| 114 |
m/0h/1 \ |
| 115 |
m/0h/1/2h \ |
| 116 |
m/0h/1/2h/2 \ |
| 117 |
m/0h/1/2h/2/1000000000 |
| 118 |
|
| 119 |
(output not listed) |
| 120 |
|
| 121 |
BIP0032 outlines a hierarchy where individual "accounts" and key series have the following form: |
| 122 |
|
| 123 |
:: |
| 124 |
|
| 125 |
m/ih/0/k - Receiving address series for account 'i', with 'k' as index |
| 126 |
m/ih/1/k - Change address series for spends from account 'i', with 'k' as index |
| 127 |
|
| 128 |
So, to give someone the ability to create receving addresses for |
| 129 |
account 0, (but not the ability to spend from those addresses), one |
| 130 |
would export an _extended public key_ for m/0h/0 (we'll use again the |
| 131 |
entropy from BIP0032 test vector #1 for purpose of explanation, but of |
| 132 |
course this would be unique for each situation): |
| 133 |
|
| 134 |
:: |
| 135 |
|
| 136 |
$ echo 000102030405060708090A0B0C0D0E0F | \ |
| 137 |
bip32gen \ |
| 138 |
-i entropy -f - -x \ |
| 139 |
-o xpub -F - \ |
| 140 |
m/0h/0 |
| 141 |
xpub6ASuArnXKPbfEVRpCesNx4P939HDXENHkksgxsVG1yNp9958A33qYoPiTN9QrJmWFa2jNLdK84bWmyqTSPGtApP8P7nHUYwxHPhqmzUyeFG |
| 142 |
|
| 143 |
Then, to derive public child keys, that person would run the |
| 144 |
key generator using that extended public key as input: |
| 145 |
|
| 146 |
:: |
| 147 |
|
| 148 |
$ echo xpub6ASuArnXKPbfEVRpCesNx4P939HDXENHkksgxsVG1yNp9958A33qYoPiTN9QrJmWFa2jNLdK84bWmyqTSPGtApP8P7nHUYwxHPhqmzUyeFG | \ |
| 149 |
bip32gen \ |
| 150 |
-i xpub -f - \ |
| 151 |
-o addr -F - \ |
| 152 |
0 1 2 3 4 5 6 7 8 9 |
| 153 |
1BvgsfsZQVtkLS69NvGF8rw6NZW2ShJQHr |
| 154 |
1B1TKfsCkW5LQ6R1kSXUx7hLt49m1kwz75 |
| 155 |
1D2LvY1T3yT4xWgoXkXhAbh1fbY39owifJ |
| 156 |
1L71JnrWfB45Z1g2et1zeDAkzrpY8eyJMH |
| 157 |
1ACa2mfirthEwmnHVHcBEAVfTHJkajpjod |
| 158 |
1CR1rZqA8cwbohoj9bNmcrYxx31Zx2jw4c |
| 159 |
1PkyqPChrqwM1dee8KTMpsWWBuXgmGVNoi |
| 160 |
1L3HVcGagSUEb2d24SfHV7Kyu6kUNpDL51 |
| 161 |
17JbSP83rPWmbdcdtiiTNqBE8MgGN8kmUk |
| 162 |
1MWb4Pv4ZCUmbnFgA5D3MtYyhMh4q8KCrd |
| 163 |
|
| 164 |
An offline machine could generate the corresponding private keys to |
| 165 |
spend from those addresses by using an *extended private key* for the |
| 166 |
account: |
| 167 |
|
| 168 |
:: |
| 169 |
|
| 170 |
$ echo 000102030405060708090A0B0C0D0E0F | \ |
| 171 |
bip32gen \ |
| 172 |
-i entropy -f - -x \ |
| 173 |
-o xprv -F - \ |
| 174 |
m/0h/0 |
| 175 |
xprv9wTYmMFdV23N21MM6dLNavSQV7Sj7meSPXx6AV5eTdqqGLjycVjb115Ec5LgRAXscPZgy5G4jQ9csyyZLN3PZLxoM1h3BoPuEJzsgeypdKj |
| 176 |
|
| 177 |
Then to generate the corresponding private keys (here shown in wallet import format): |
| 178 |
|
| 179 |
:: |
| 180 |
|
| 181 |
$ echo xprv9wTYmMFdV23N21MM6dLNavSQV7Sj7meSPXx6AV5eTdqqGLjycVjb115Ec5LgRAXscPZgy5G4jQ9csyyZLN3PZLxoM1h3BoPuEJzsgeypdKj | \ |
| 182 |
bip32gen \ |
| 183 |
-i xprv -f - \ |
| 184 |
-o wif -F - \ |
| 185 |
0 1 2 3 4 5 6 7 8 9 |
| 186 |
L3dzheSvHWc2scJdiikdZmYdFzPcvZMAnT5g62ikVWZdBewoWpL1 |
| 187 |
L22jhG8WTNmuRtqFvzvpnhe32F8FefJFfsLJpSr1CYsRrZCyTwKZ |
| 188 |
KwhVMKLLSMt7DemnUxhSftkdqPjWYmPKt31nmV3BB7TdVaMFzqgP |
| 189 |
KySK2geQrXWYR7f2p9C9Exr4CLngFQnnzQegSaKAfxNupqecSwdU |
| 190 |
KxSKj3b3EdHSyN3C5sSwRL7TMmxpEVkJ7Cw4fQTMUYXsCZRbmBAP |
| 191 |
L5kxG5qokHeTJSwco5JzCEdAy5TN2M2Tj2QvxGpvVuSVP5bJRAUT |
| 192 |
L3oTQGyHQvE3GkRQJkgPs9vY8NRTxdwacHu9Xu9QBPTpgHCSGume |
| 193 |
KxABnXp7SiuWi218c14KkjEMV7SjcfXnvsWaveNVxWZU1Rwi8zNQ |
| 194 |
L1Zku8j3mCiiHxZdo6NDLHv6jcA1JyNufUSHBMiznML38vNr9Agh |
| 195 |
L46bxscw878ytxNHro7ghNXuybUv8aNJAY1UudH1HUxD2pecBhR8 |
| 196 |
|
| 197 |
Python bip32utils Library |
| 198 |
========================= |
| 199 |
|
| 200 |
The BIP32Key Class |
| 201 |
------------------ |
| 202 |
|
| 203 |
The bip32utils python library currently has a single class, BIP32Key, |
| 204 |
which encapsulates a single node in a BIP0032 wallet hierarchy. A |
| 205 |
terminology distinction is made between an ECDSA private and public |
| 206 |
key pair and a full BIP32Key, which internally holds an ECDSA key pair |
| 207 |
and other data. |
| 208 |
|
| 209 |
A BIP32Key may act like a standard Bitcoin keypair, providing the |
| 210 |
means to sign transactions with its internal ECDSA private key or to |
| 211 |
generate a receiving address with its internal ECDSA public key. In |
| 212 |
addition, a BIP32Key can act as the parent node for a set of indexed |
| 213 |
children and thus form a tree of BIP32Key sequences. |
| 214 |
|
| 215 |
A BIP32Key may also be deemed a private or public BIP32Key, depending |
| 216 |
upon whether the secret half of the internal ECDSA key pair is |
| 217 |
present. Private BIP32Keys are able to generate either public or |
| 218 |
private child BIP32Keys, while public BIP32Keys can only generate |
| 219 |
public children. |
| 220 |
|
| 221 |
In other words, a private BIP32Key internally stores an ECDSA private |
| 222 |
key, an ECDSA public key, and some additional pseudorandom bits named |
| 223 |
the *chain code*. Public BIP32Keys are only different in that the |
| 224 |
secret half of the ECDSA key pair does not exist; only the public half |
| 225 |
does. |
| 226 |
|
| 227 |
Creating a BIP32Key |
| 228 |
------------------- |
| 229 |
|
| 230 |
A BIP32Key may come into existence in one of four ways: |
| 231 |
|
| 232 |
* Using the BIP32Key.fromEntropy(entropy, public=False) method, one |
| 233 |
may provide a string of at least 32 bytes (128 bits) to construct a |
| 234 |
new master BIP32Key for an entire tree. From this initial >= 128 |
| 235 |
bits of entropy a new ECDSA private key, ECDSA public key, and |
| 236 |
pseudorandom chain code are derived that preserves the 128 bit |
| 237 |
security parameter as described in BIP0032. This is termed a private |
| 238 |
BIP32Key, and may be used to derive child BIP32Keys that are either |
| 239 |
private or public. |
| 240 |
|
| 241 |
If the public parameter is set to True, then the internal ECDSA |
| 242 |
private key is discarded, the resulting BIP32Key is known as a |
| 243 |
public BIP32Key, and may only be used to generate further public |
| 244 |
BIP32Keys. |
| 245 |
|
| 246 |
* Using the BIP32Key.fromExtendedKey(xkey, public=False) static |
| 247 |
method, one may provide a 78-byte serialized string that is |
| 248 |
formatted as an Extended Private Key, as documented in BIP0032. From |
| 249 |
this, the ECDSA private key, ECDSA public key, and chain code are |
| 250 |
extracted. |
| 251 |
|
| 252 |
If the public parameter is set to True, then the internal ECDSA |
| 253 |
private key is discarded, converting the resulting BIP32Key into a |
| 254 |
public BIP32Key, and may only be used to generate further public |
| 255 |
BIP32Keys. |
| 256 |
|
| 257 |
* Using the BIP32Key.fromExtendedKey(xkey) static method, one may |
| 258 |
provide a 78-byte serialized string that is formatted as an Extended |
| 259 |
Public Key, as documented in BIP0032. From this, the ECDSA public |
| 260 |
key and chain code are extracted, resulting in a public BIP32Key |
| 261 |
that may only be used to generate further public BIP32Keys. |
| 262 |
|
| 263 |
* Finally, using an instance of a BIP32Key resulting from any of the |
| 264 |
three methods above, one may call the member function ChildKey(i) to |
| 265 |
create a child BIP32Key one level lower in the hierarchy, at integer |
| 266 |
index 'i'. If the starting BIP32Key is a private one, then the |
| 267 |
resulting child BIP32Key will also be a private one, using the |
| 268 |
CKDpriv derivation formula in BIP0032. |
| 269 |
|
| 270 |
Likewise, if the starting BIP32Key is a public one (i.e., does not |
| 271 |
contain an internal ECDSA private key half), then the child BIP32Key |
| 272 |
will also be a public one, derived using the CKDpub algorithm in |
| 273 |
BIP0032. |
| 274 |
|
| 275 |
At any time, a private BIP32Key may be turned into a public one by |
| 276 |
calling the instance member function SetPublic(), which discards the |
| 277 |
internal private ECDSA key half and sets an internal flag. |
| 278 |
|
| 279 |
When creating a child BIP32Key from an existing private BIP32Key, one |
| 280 |
may also select from an alternate set of child keys, called *hardened* |
| 281 |
keys, by adding the constant BIP32_HARDEN to the integer index. A |
| 282 |
hardened child BIP32Key avoids a known issue with non-hardened child |
| 283 |
keys where a compromise of one child key may result in a compromise of |
| 284 |
all child keys in the same sequence. |