[4] Blockchain APIs
1. Private Key
A private key is created by picking a random 256-bit number using a method that is not predictable or repeatable e.g. bitaddress.org asks you to randomly move your mouse around for a few seconds.
This should only be done using a cryptographically secure pseudo-random number generator (CSPRNG). See section 3(6) above.
A private key can be represented in multiple formats:
- Hex - 64 hexadecimal digits
- WIF - Wallet Import Format with the prefix 5
- WIF-compressed with the prefix K or L
Example of a Bitcoin private key in hexadecimal format:
f7d662f28e1e6cbf6ac5a7129f36b2871e72c3f1d31036509ae87962803b53df
Example of a Bitcoin private key in WIF-compressed format:
L5XUUSuoF1SyrZW3kPqyFNWhDS52DsrGh2rQEw6bs4kERcgiJU45
2. Public Key
A Public Key is calculated from the private key using irreversible Elliptic Curve Cryptography.
Public Key = Private Key * Generator point (a constant)
Example of a Bitcoin public key in hexadecimal format:021511f3e82638681e19c2ad9bf8550e82abfcf981449404288d81a84445dbf27a
Calculating the Private Key from the Public Key is computationally infeasible as of today's computing technology.
3. Address
The address is calculated from the public key through a one-way cryptographic hash function.
Example: To calculate a bitcoin address, we compute the SHA256 hash of the Public Key and then compute the RIPEMD160 hash of the result.
Address = RIPEMD160(SHA256(Public Key))
Example of a Bitcoin Address:1AThkzMLY37PYYJMUDydmjzS46xBr9ZMcm
4. Non-deterministic (Random) Wallets
Blockchain Wallets are of two main types:
- Non-deterministic (Random) Wallets
- Deterministic (Seeded) Wallets
Non-deterministic (Random) Wallets are just a collection of randomly generated private keys. The biggest problem with them is that they are difficult to manage, back up, and import.
The basic code to generate a non-deterministic wallet using BlockCypher API services is https://api.blockcypher.com/v1/{BLOCKCHAIN}/main/addrs
You will need to replace {BLOCKCHAIN}
with the blockchain name e.g. btc
for Bitcoin, eth
for Ethereum Mainnet, etc.
Sample code for generating a non-deterministic Bitcoin wallet:
Sample code for generating a non-deterministic Ethereum wallet:
5. Deterministic (Seeded) Wallets
Deterministic (Seeded) Wallets contain private keys that are all derived from a common seed using a one-way hash function.
A seed is a randomly generated number combined with other data e.g. an index number to derive the private keys.
A mnemonic code is a sequence of words that represent a seed e.g.urge pulp usage sister evidence arrest palm math please chief egg abuse
The mnemonic code is used to create and re-create all the keys. That's why you only need to back up these words.
This process is defined in BIP0039:
- Create a random sequence of 128 to 256 bits.
- Create a checksum of the random sequence by taking the first few bits of its SHA256 hash.
- Add the checksum to the end of the random sequence.
- Divide the sequence into sections of 11 bits, using those to index a dictionary of 2048 predefined words.
- Produce 12 to 24 words representing the mnemonic code.
6. Hierarchical Deterministic Wallets
Hierarchical deterministic (HD) wallets contain keys in a tree structure. Parent keys can produce children keys, which in turn can produce grandchildren keys. This can go on infinitely.
Step 1: Generating Mnemonic Code and Extended Public Key
The code for generating a BIP44 HD Wallet for Bitcoin is here. Note: You will need to generate a free Tatum API to use this.
This is a sample response:
SLIP-0044 : Registered coin types for BIP-0044
BIP-0044 defines a logical hierarchy for deterministic wallets. Level 2 of the hierarchy describes a coin type in use. See details here.
Step 2: Generating the address
This code generates the address from the extended public key and derivation path index. Note: You will need to generate a free Tatum API to use this.
This is a sample response.
Step 3: Generating the private key
This code generates a private key for an address from a mnemonic for a given derivation path index. Note: You will need to generate a free Tatum API to use this.
This is a sample response.
7. Chain API
Get the latest general information about a blockchain using https://api.blockcypher.com/v1/{BLOCKCHAIN}/main
You will need to replace {BLOCKCHAIN}
with the blockchain name e.g. btc
for Bitcoin, eth
for Ethereum Mainnet, ltc
for Litecoin etc.
https://api.blockcypher.com/v1/btc/main
https://api.blockcypher.com/v1/eth/main
https://api.blockcypher.com/v1/ltc/main