Keys, Addresses, Wallets
Dr.Piyawad Kasabai piyawad.k (at) udru.ac.th
https://piyk.github.io
Outline
● Cryptography behind Bitcoin
● Keys
○ Private keys
○ Public keys
○ Key formats
● Addresses
○ Creating address from keys
● Wallets
○ Wallet types
○ Nondeterministic Wallets
○ Deterministic Wallets
Mastering Bitcoin
Bitcoin Overview
Blockchain
Block Example #0 Block Example #1 Block Example #2
Address in genesis block
https://www.blockchain.com/btc/address/1A1zP1eP5QGefi2DMPTfTL5S Lmv7DivfNa
“Not your keys, not your coins”
Cryptography in Bitcoin
ECDSA (Elliptic Curve Digital Signature Algorithm)
Keys
● Bitcoin account (Wallet) is produced from a public key and a private key.
○ Public key is the account number or address.
○ Private key is used to prevent other people from using bitcoins from your address.
● Private Key is just a randomly generated number.
Keys
● From the private key, elliptic curve multiplication, a one-way
cryptographic function is used to generate a public key.
Private Key ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2 Public Key 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737
Blockchain Demo: Public / Private Keys & Signing
https://andersbrownworth.com/blockchain/public-private-keys/keys
Address
● Address is created from the public key using sha256/RIPEMD-160/base58.
Public key to bitcoin address
Conversion of a public key into a bitcoin address
A = RIPEMD160(SHA256(K))
where K is the public key and A is the resulting bitcoin address.
Base58Check Encoding
● Base58 is a text-based binary-encoding format developed for use in bitcoin to balance between compact representation, readability, and error detection and prevention.
● Base58 is a subset of Base64, using upper- and lowercase letters and numbers, but omitting some characters that are frequently mistaken for one another and can appear identical when displayed in certain fonts.
● Specifically, Base58 is Base64 without the 0 (number zero), O (capital o), l (lower L), I (capital i), and the symbols “``” and "/".
● Bitcoin’s Base58 alphabet
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijk mnopqrstuvwxyz
Address version prefix
Type Version prefix (hex) Base58 result prefix
Bitcoin Address 0x00 1
Pay-to-Script-Hash Address 0x05 3
Bitcoin Testnet Address 0x6F m or n
Private Key WIF* 0x80 5, K, or L
BIP-38 Encrypted Private Key 0x0142 6P
BIP-32 Extended Public Key 0x0488B21E xpub
Key Formats
● Both private and public keys can be represented in a number of different formats.
Private key formats
Example: Same key, different formats
Type Prefix Description
Raw None 32 bytes
Hex None 64 hexadecimal digits
WIF 5 Base58Check encoding: Base58 with version prefix of 0x80 and 4-byte checksum WIF-compressed K or L As above, with added suffix 0x01 before encoding
Format Private key
Hex 1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd WIF 5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn
WIF-compressed KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ
Key Formats
Public key formats: can be presented in different ways, usually compressed or uncompressed
Wallets
● Types of wallets:
○ Nondeterministic (Random) Wallets: Type-0 a collection of randomly generated keys
○ Deterministic (Seeded) Wallets: Type-1 a deterministic sequence of keys derived from a seed
○ HD Wallets: Type-2 a tree of keys generated from a single seed
Types of Wallets
Nondeterministic Wallets Deterministic Wallets Hierarchical Deterministic Wallets
Wallet Technology Details
The wallet starts from a source of entropy, adds a checksum, and then maps the entropy to a word list:
1. Create a random sequence (entropy) of 128 to 256 bits.
2. Create a checksum of the random sequence by taking the first (entropy-length/32) bits of its SHA256 hash.
3. Add the checksum to the end of the random sequence.
4. Split the result into 11-bit length segments.
5. Map each 11-bit value to a word from the predefined dictionary of 2048 words.
6. The mnemonic code is the sequence of words.
Mnemonic codes: entropy and word length
Entropy (bits)
Check sum (bits)
Entropy + checksum
(bits)
Mnemonic length (words)
128 4 132 12
160 5 165 15
192 6 198 18
224 7 231 21
256 8 264 24
● Creating master keys and chain code from a root seed
Creating Hierarchical Deterministic (HD) Wallet from the Seed
● Extending a parent private key to create a child private key
Creating Hierarchical Deterministic (HD) Wallet from the Seed
● Extending a parent public key to create a child public key
Creating Hierarchical Deterministic (HD) Wallet from the Seed
Summary: HD Wallet
A hierarchical deterministic (HD
wallet) provides a useful method
for generating new private keys
and public keys.
Using Bitcoin Wallets
● Hot Wallet vs. Cold Wallet
○ Hot wallet: required to be connected to the Internet
○ Cold wallet: store in offline mode and provide more security
● Software wallets
○ Web: Bitkub, Binance, etc.
○ Desktop and Mobile Apps: Bitcoin core (full node), electrum (light node), Exodus, Mycelium,
samouraiwallet
● Hardware wallets
○ Trezor
○ Ledger
Paper wallet
Demo: electrum
(https://electrum.org)
Example keys in Python
Code:
https://drive.google.com/file/d/1jPjiEJDVlRlG6nKM_4qoYFgwaS5LgPMO/view?usp=sharing
Run:
https://www.programiz.com/python-programming/online-compiler/Work3: การใช้งาน Electrum
https://elearning.chonburi.spu.ac.th