Quickstart¶
This page gets you from zero to generating, validating, and signing in a few
minutes. Every snippet uses only libcrypto and the Python standard library.
1. Generate addresses from a private key¶
from libcrypto import Wallet
private_key = "0000000000000000000000000000000000000000000000000000000000000001"
wallet = Wallet(private_key)
print("Bitcoin P2PKH: ", wallet.get_address("bitcoin", "p2pkh"))
print("Bitcoin P2SH-P2WPKH:", wallet.get_address("bitcoin", "p2sh-p2wpkh"))
print("Bitcoin P2WPKH: ", wallet.get_address("bitcoin", "p2wpkh"))
print("Bitcoin Taproot: ", wallet.get_address("bitcoin", "p2tr"))
print("Ethereum: ", wallet.get_address("ethereum"))
print("TRON: ", wallet.get_address("tron"))
Deterministic output for private key 1:
Bitcoin P2PKH: 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
Bitcoin P2SH-P2WPKH: 3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN
Bitcoin P2WPKH: bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
Bitcoin Taproot: bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9
Ethereum: 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf
TRON: TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC
2. Generate a fresh, secure wallet¶
from libcrypto import Wallet
wallet = Wallet.generate()
print("Private key:", wallet.private_key.hex)
print("WIF: ", wallet.private_key.to_wif())
print("Bitcoin: ", wallet.get_address("bitcoin", "p2pkh"))
Keys come from os.urandom via a rejection sampler that stays inside the valid
secp256k1 range.
3. Validate an address before using it¶
from libcrypto import validate_address, get_address_info
print(validate_address("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", "bitcoin")) # True
print(validate_address("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMX")) # False (bad checksum)
info = get_address_info("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
print(info.address_format, info.address_type, info.networks)
# bech32 p2wpkh ('bitcoin',)
4. Sign and verify a message¶
from libcrypto import (
sign_message_ethereum, verify_message_ethereum,
sign_message_bitcoin, verify_message_bitcoin,
)
key = 0x0000000000000000000000000000000000000000000000000000000000000001
eth_sig = sign_message_ethereum(key, "Hello, world!")
print(verify_message_ethereum("0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf",
"Hello, world!", eth_sig)) # True
btc_sig = sign_message_bitcoin(key, "Hello, world!")
print(verify_message_bitcoin("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH",
"Hello, world!", btc_sig)) # True
5. HD wallet from a mnemonic¶
from libcrypto import HDWallet, Wallet, generate_mnemonic
mnemonic = generate_mnemonic(12)
hd = HDWallet.from_mnemonic(mnemonic)
node = hd.derive_from_path("m/44'/60'/0'/0/0")
wallet = Wallet(node.private_key)
print(wallet.get_address("ethereum"))