Generate Bitcoin Wallet under 15 lines of code

SUSMIT
1 min readOct 1, 2018

To make a bitcoin wallet from scratch we need to perform the following steps

  • Having a private ECDSA key
  • Take the corresponding public key generated with it (33 bytes, 1 byte 0x02 (y-coord is even), and 32 bytes corresponding to X coordinate)
  • Perform SHA-256 hashing on the public key
  • Perform RIPEMD-160 hashing on the result of SHA-256
  • Add version byte in front of RIPEMD-160 hash (0x00 for Main Network)
  • Perform SHA-256 hash on the extended RIPEMD-160 result
  • Perform SHA-256 hash on the result of the previous SHA-256 hash
  • Take the first 4 bytes of the second SHA-256 hash.
  • Add the 4 checksum bytes from stage 7 at the end of extended RIPEMD-160 hash from stage 4.
  • Convert the result from a byte string into a base58 string using Base58Check encoding.
import os
import ecdsa
import hashlib
import base58
private_key = os.urandom(32).encode("hex")print "private : " + private_keysign_key = ecdsa.SigningKey.from_string(private_key.decode("hex") ,curve = ecdsa.SECP256k1)verify_key = sign_key.verifying_keypublic_key = ('\04' + verify_key.to_string()).encode("hex")print "Public key " + public_keyripemd160 = hashlib.new('ripemd160')ripemd160.update(hashlib.sha256(public_key.decode('hex')).digest())checksum = hashlib.sha256(hashlib.sha256('\00' + ripemd160.digest()).digest()).digest()[:4]address = base58.b58encode('\00' + ripemd160.digest() + checksum)print "Address " + address

You can verify the address generated on bitaddress.org .Please do not use this code in productions as there are tons of improvement that need to be done like Extended keys ,seeds, mnemonics ,hd wallet.

--

--