id0-rsa.pubのメモ【Hello Bitcoin】

以下問題文。

Do you know how bitcoin addresses are constructed?

This a number. A big number specifically. If it were a bitcoin secret key, what would its (uncompressed) address be?

94176137926187438630526725483965175646602324181311814940191841477114099191175

secret keyが渡されているので、それからaddressを求めれば良さそう。

だが、そもそもBitcoinのsecret keyが何なのか少し調べてみる。 en.bitcoin.it

Private Keyにもいくつが種類があるようだ。正直よくわからないが、とりあえずBitcoinのPrivate KeyがWalletに紐づいているっぽい。

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.

いかにもそれっぽい記事を見つけた。private keyからbitcoin wallet addressを導出できるらしい。 medium.freecodecamp.org

上記の記事によると以下の手順を踏むと最終的にアドレスが導出できるらしい。

  1. Elliptic Curve Cryptography
  2. Public key
  3. Compressed public key
  4. Encrypting the public key
  5. Adding the network byte
  6. Checksum
  7. Getting the address

思ったより手順が多い...
そういう訳で先人の知恵を借りることにする。どうやらpybitcoinというPythonのモジュールで同様のことをサクッとできるようだ。 github.com

以下、solverです。

$ python2
Python 2.7.15 (default, Sep 18 2018, 20:16:18)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pybitcoin
>>> sk = 94176137926187438630526725483965175646602324181311814940191841477114099191175
>>>
>>> pybitcoin.BitcoinPrivateKey(sk).public_key().address()
'18GZRs5nx8sVhF1xVAaEjKrYJga4hMbYc2'

参考