地址和钱包

Bitcoin

Introduction

Ownership of bitcoin is established through digital keys, bitcoin addresses, and digital signatures.

Keys come in pairs consisting of a private (secret) key and a public key. Think of the public key as similar to a bank account number and the private key as similar to the secret PIN. In bitcoin, we use public key cryptography to create a key pair that controls access to bitcoin. The key pair consists of a private key and—derived from it—a unique public key. The public key is used to receive funds, and the private key is used to sign transactions to spend the funds.

post image
  • Digital Keys => Wallet

  • Digital signatures

    The digital signature used to spend funds is also referred to as a witness, a term used in cryptography.

  • Bitcoin address => Pubic Key

Bitcoin Address

Bitcoin addresses are almost always encoded as “Base58Check”, which uses 58 characters (a Base58 number system) and a checksum to help human readability, avoid ambiguity, and protect against errors in address transcription and entry.

post image

Base58Check

Base58 is a set of lowercase and capital letters and numbers without the four (0, O, l, I). and below is the Base58Check encoding process.

post image

Private Key formats

post image

Example

Hex             1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD
WIF             5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn
Hex-compressed  1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD01
WIF-compressed  KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ

Public Key formats

post image

Example

Point:
x = F028892BAD7ED57D2FB57BF33081D5CFCF6F9ED3D3D7F159C2E2FFF579DC341A
y = 07CF33DA18BD734C600B96A72BBC4749D5141C90EC8AC328AE52DDFE2E505BDB
Uncompressed:
K = 04F028892BAD7ED57D2FB57BF33081D5CFCF6F9ED3D3D7F159C2E2FFF579DC341A↵
    07CF33DA18BD734C600B96A72BBC4749D5141C90EC8AC328AE52DDFE2E505BDB
Compressed:
K = 03F028892BAD7ED57D2FB57BF33081D5CFCF6F9ED3D3D7F159C2E2FFF579DC341A

Advanced Keys and Addresses

  • Encrypted Private Keys (BIP-38) BIP-38 proposes a common standard for encrypting private keys with a passphrase and encoding them with Base58Check.

  • Pay-to-Script Hash (P2SH)

    P2SH designate the beneficiary of a bitcoin transaction as the hash of a script, instead of the owner of a public key.

  • Paper Wallets

    Paper wallets are bitcoin private keys printed on paper.

Wallet

  • A wallet is an application that serves as the primary user interface.

  • From a programmer’s perspective, the word “wallet” refers to the data structure used to store and manage a user’s keys.

Wallet Technology Overview

The wallet contains only keys rather than Bitcoin. In a sense, a bitcoin wallet is a keychain.

  • Nondeterministic wallet

    where each key is independently generated from a random number. The keys are not related to each other.

  • D*eterministic wallet W*here all the keys are derived from a single master key, known as the seed. All the keys in this type of wallet are related to each other and can be generated again if one has the original seed. The most commonly used derivation method uses a tree-like structure and is known as a hierarchical deterministic or HD wallet.

    Deterministic wallets are initialized from a seed. To make these easier to use, seeds are encoded as English words, also known as mnemonic code words.

  • HD Wallets (BIP-32/BIP-44)

    HD wallets offer two major advantages over random (nondeterministic) keys. First, the tree structure can be used to express additional organizational meaning; The second advantage is that users can create a sequence of public keys without having access to the corresponding private keys.

  • Seeds and Mnemonic Codes (BIP-39) HD wallets are useful if they are combined with a standardized way of creating seeds from a sequence of English words.

Wallet Technology Details

  • Mnemonic Code Words (BIP-39)

    how to create mnemonic words

post image
post image
  • Creating an HD Wallet from the Seed

    Mnemonic code == > Root Seed == > Master Key + Chain code

    The process of creating the master keys and master chain code for an HD wallet:

post image
  • Extending a parent private key to create a child private key:

post image

Extended keys: The combination of a private key and chain code is used to derive child private keys; a public key and chain code, which can be used to create child public keys (public only);

  • Using an Extended Public Key on a Web Store

BIP-44 HD wallet structure examples:

post image

Ethereum

Keys and Addresses

Ethereum has two different types of accounts: externally owned accounts (EOAs) and contracts. Ownership of ether by EOAs is established through digital private keys, Ethereum addresses, and digital signatures. In public key cryptography–based systems, such as that used by Ethereum, keys come in pairs consisting of a private (secret) key and a public key.

Private Keys

same as Bitcoin.

Public Keys

Same as Bitcoin.

k =f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315
K = k * G and K = (x, y)
where:
   x = 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b
   y = 83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0

Ethereum uses uncompressed public keys; therefore the only prefix is (hex) 04. The serialization concatenates the x and y coordinates of the public key:
   04 + x-coordinate (32 bytes/64 hex) + y-coordinate (32 bytes/64 hex)

Therefore, the public key(K) we calculated earlier is serialized as:
   046e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0

Cryptographic Hash Functions

Ethereum uses the Keccak-256 cryptographic hash function in many places. while Bitcoin uses SHA256 here.

Ethereum Addresses

We use Keccak-256 to calculate the hash of this public key:
Keccak256(K) = 2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9
  • Ethereum Address Formats

Ethereum addresses are hexadecimal numbers, identifiers derived from the last 20 bytes of the Keccak-256 hash of the public key. Unlike Bitcoin addresses, which are encoded in the user interface of all clients to include a built-in checksum to protect against mistyped addresses, Ethereum addresses are presented as raw hexadecimal without any checksum. In reality, this design choice led to a number of problems in the early days of the ecosystem, including the loss of funds due to mistyped addresses and input validation errors.

  • Inter Exchange Client Address Protocol

  • Hex Encoding with Checksum in Capitalization (EIP-55)

    1. address:
        0x001d3F1ef827552Ae1114027BD3ECF1f086bA0E9
    2. hash
        Keccak256("001d3f1ef827552ae1114027bd3ecf1f086ba0e9") =
       5429b5d9460122fb4b11af9cb88b7bb76d8928862e0a57d46dd18dd8e08a6927
    3. Lineup the address and Hash 
        Address: 001d3f1ef827552ae1114027bd3ecf1f086ba0f9
      Hash   : 23a69c1653e4ebbb619b0b2cb8a9bad49892a8b9...
    4. Capitalize each alphabetic address character if the corresponding hex digit of the hash is greater than or equal to 0x8.
        Address: 001d3F1ef827552Ae1114027BD3ECF1f086bA0F9
      Hash   : 23a69c1653e4ebbb619b0b2cb8a9bad49892a8b9...
    

Ethereum Wallet

Wallet Technology Overview

the technology is same as Bitcoin.

  • Nondeterministic (Random) Wallets

  • Deterministic (Seeded) Wallets

  • Hierarchical Deterministic Wallets (BIP-32/BIP-44)

  • Seeds and Mnemonic Codes (BIP-39)

Wallet Best Practices

Same as Bitcoin.