UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
coin_address.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_COINS_COIN_ADDRESS_HPP
2#define SECP256K1_COINS_COIN_ADDRESS_HPP
3#pragma once
4
5// ============================================================================
6// Coin Address -- Unified address generation for all secp256k1 coins
7// ============================================================================
8// Thin wrappers over existing address.hpp functions, parameterized by
9// CoinParams. Automatically selects the correct encoding, version bytes,
10// and hash algorithm for each coin.
11//
12// Usage:
13// #include "secp256k1/coins/coin_address.hpp"
14// using namespace secp256k1::coins;
15//
16// auto addr = coin_address(pubkey, Bitcoin); // P2WPKH bc1q...
17// auto addr = coin_address(pubkey, Litecoin); // ltc1q...
18// auto addr = coin_address(pubkey, Dogecoin); // D...
19// auto addr = coin_address(pubkey, Ethereum); // 0x... (EIP-55)
20// auto wif = coin_wif_encode(privkey, Bitcoin); // 5... or K/L...
21// auto p2pk = coin_address_p2pkh(pubkey, Dogecoin); // explicit P2PKH
22// ============================================================================
23
24#include <string>
25#include <cstdint>
27#include "secp256k1/point.hpp"
28#include "secp256k1/scalar.hpp"
29#include "secp256k1/address.hpp"
30#include "secp256k1/context.hpp"
31
33
34// -- Default Address (best format for each coin) ------------------------------
35
36// Generate the default/preferred address format for a coin.
37// - Bitcoin/Litecoin/DigiByte: Bech32 (P2WPKH)
38// - Dogecoin/Dash: Base58Check (P2PKH)
39// - Ethereum/BSC/Polygon: EIP-55 hex address
40std::string coin_address(const fast::Point& pubkey,
41 const CoinParams& coin,
42 bool testnet = false);
43
44// -- Explicit Address Types ---------------------------------------------------
45
46// P2PKH address with coin-specific version byte
47std::string coin_address_p2pkh(const fast::Point& pubkey,
48 const CoinParams& coin,
49 bool testnet = false);
50
51// P2WPKH (SegWit v0) address with coin-specific Bech32 HRP
52// Returns empty string if coin doesn't support SegWit
53std::string coin_address_p2wpkh(const fast::Point& pubkey,
54 const CoinParams& coin,
55 bool testnet = false);
56
57// P2TR (Taproot) address with coin-specific Bech32m HRP
58// Returns empty string if coin doesn't support Taproot
59std::string coin_address_p2tr(const fast::Point& internal_key,
60 const CoinParams& coin,
61 bool testnet = false);
62
63// P2SH-P2WPKH (nested/wrapped SegWit) address with coin-specific version byte
64// Returns empty string if coin doesn't support SegWit
65std::string coin_address_p2sh_p2wpkh(const fast::Point& pubkey,
66 const CoinParams& coin,
67 bool testnet = false);
68
69// P2SH address from a 20-byte script hash with coin-specific version byte
70std::string coin_address_p2sh(const std::array<std::uint8_t, 20>& script_hash,
71 const CoinParams& coin,
72 bool testnet = false);
73
74// CashAddr address (Bitcoin Cash) with coin-specific prefix
75// Returns empty string if coin doesn't use CASHADDR encoding
76std::string coin_address_cashaddr(const fast::Point& pubkey,
77 const CoinParams& coin,
78 bool testnet = false);
79
80// -- WIF (Wallet Import Format) -----------------------------------------------
81
82// Encode private key as WIF with coin-specific prefix
83std::string coin_wif_encode(const fast::Scalar& private_key,
84 const CoinParams& coin,
85 bool compressed = true,
86 bool testnet = false);
87
88// -- Full Key Generation ------------------------------------------------------
89
90// Result of full key generation
94 std::string address; // Default format for coin
95 std::string wif; // WIF-encoded private key (empty for EVM coins)
96};
97
98// Generate address from private key in one call (uses default format)
99// ctx: optional custom generator context (nullptr = standard secp256k1)
101 const CoinParams& coin,
102 bool testnet = false,
103 const CurveContext* ctx = nullptr);
104
105} // namespace secp256k1::coins
106
107#endif // SECP256K1_COINS_COIN_ADDRESS_HPP
std::string coin_wif_encode(const fast::Scalar &private_key, const CoinParams &coin, bool compressed=true, bool testnet=false)
std::string coin_address(const fast::Point &pubkey, const CoinParams &coin, bool testnet=false)
std::string coin_address_p2pkh(const fast::Point &pubkey, const CoinParams &coin, bool testnet=false)
std::string coin_address_p2sh_p2wpkh(const fast::Point &pubkey, const CoinParams &coin, bool testnet=false)
std::string coin_address_p2tr(const fast::Point &internal_key, const CoinParams &coin, bool testnet=false)
std::string coin_address_p2wpkh(const fast::Point &pubkey, const CoinParams &coin, bool testnet=false)
std::string coin_address_p2sh(const std::array< std::uint8_t, 20 > &script_hash, const CoinParams &coin, bool testnet=false)
std::string coin_address_cashaddr(const fast::Point &pubkey, const CoinParams &coin, bool testnet=false)
CoinKeyPair coin_derive(const fast::Scalar &private_key, const CoinParams &coin, bool testnet=false, const CurveContext *ctx=nullptr)