UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
wallet.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_COINS_WALLET_HPP
2#define SECP256K1_COINS_WALLET_HPP
3#pragma once
4
5// ============================================================================
6// Unified Wallet API -- One interface for all secp256k1 chains
7// ============================================================================
8//
9// Usage:
10//
11// using namespace secp256k1::coins;
12//
13// // Create wallet for any chain
14// auto btc = wallet::create_random(Bitcoin);
15// auto eth = wallet::create_random(Ethereum);
16// auto trx = wallet::create_random(Tron);
17//
18// // Same API regardless of chain
19// auto addr = wallet::get_address(Bitcoin, btc);
20// auto sig = wallet::sign_message(Bitcoin, btc, msg, msg_len);
21// bool ok = wallet::verify_message(Bitcoin, btc.pub, msg, msg_len, sig);
22//
23// Core design:
24// - No chain-specific knowledge leaks to the caller
25// - CoinParams descriptor drives all chain-specific behavior
26// - Thin facade over existing battle-tested implementations
27// - Zero heap allocation in hot paths
28//
29// ============================================================================
30
31#include <array>
32#include <cstdint>
33#include <cstddef>
34#include <string>
35#include "secp256k1/scalar.hpp"
36#include "secp256k1/point.hpp"
37#include "secp256k1/ecdsa.hpp"
40
42
43// -- Key Types ----------------------------------------------------------------
44
45struct WalletKey {
46 fast::Scalar priv; // 32-byte private scalar
47 fast::Point pub; // Compressed or uncompressed public key
48};
49
50// -- Signature Result (chain-agnostic) ----------------------------------------
51
53 std::array<std::uint8_t, 32> r;
54 std::array<std::uint8_t, 32> s;
55 int recid; // Recovery ID (0-3)
56 std::uint64_t v; // EIP-155 v value (EVM) or 27+recid (Bitcoin)
57
58 // Convenience: 65-byte compact form [r:32][s:32][v:1]
59 std::array<std::uint8_t, 65> to_rsv() const;
60};
61
62// -- Key Management -----------------------------------------------------------
63
64// Create a wallet key from raw 32-byte private key
65// Returns (key, success). Fails if privkey is zero or >= curve order.
66std::pair<WalletKey, bool> from_private_key(const std::uint8_t* priv32);
67
68// Generate address string for a given coin
69std::string get_address(const CoinParams& coin, const WalletKey& key,
70 bool testnet = false);
71
72// Generate P2PKH (legacy) address for a coin
73std::string get_address_p2pkh(const CoinParams& coin, const WalletKey& key,
74 bool testnet = false);
75
76// Generate P2WPKH (native SegWit) address for a coin
77// Returns empty string if coin doesn't support SegWit
78std::string get_address_p2wpkh(const CoinParams& coin, const WalletKey& key,
79 bool testnet = false);
80
81// Generate P2SH-P2WPKH (nested SegWit, "3...") address for a coin
82// Returns empty string if coin doesn't support SegWit
83std::string get_address_p2sh_p2wpkh(const CoinParams& coin, const WalletKey& key,
84 bool testnet = false);
85
86// Generate P2TR (Taproot) address for a coin
87// Returns empty string if coin doesn't support Taproot
88std::string get_address_p2tr(const CoinParams& coin, const WalletKey& key,
89 bool testnet = false);
90
91// Generate CashAddr address for a coin (Bitcoin Cash)
92// Returns empty string if coin doesn't use CASHADDR encoding
93std::string get_address_cashaddr(const CoinParams& coin, const WalletKey& key,
94 bool testnet = false);
95
96// Export private key in chain-appropriate format:
97// Bitcoin-family: WIF (Base58Check)
98// EVM-family: 0x-prefixed hex
99// Tron: raw hex (no 0x)
100std::string export_private_key(const CoinParams& coin, const WalletKey& key,
101 bool testnet = false);
102
103// Export public key as hex string (compressed for Bitcoin, uncompressed for EVM)
104std::string export_public_key_hex(const CoinParams& coin, const WalletKey& key);
105
106// -- Signing ------------------------------------------------------------------
107
108// Sign a message using chain-appropriate format:
109// Bitcoin-family: "\x18Bitcoin Signed Message:\n" + varint(len) + msg -> dSHA256
110// EVM-family: "\x19Ethereum Signed Message:\n" + decimal(len) + msg -> Keccak
111// Generic: raw SHA-256(msg) signing
113 const std::uint8_t* msg, std::size_t msg_len);
114
115// Sign a raw 32-byte hash (no message prefix, no hashing)
117 const std::uint8_t* hash32);
118
119// -- Verification -------------------------------------------------------------
120
121// Verify a signed message against a public key
122bool verify_message(const CoinParams& coin, const fast::Point& pubkey,
123 const std::uint8_t* msg, std::size_t msg_len,
124 const MessageSignature& sig);
125
126// -- Recovery -----------------------------------------------------------------
127
128// Recover public key from signed message + signature
129std::pair<fast::Point, bool>
131 const std::uint8_t* msg, std::size_t msg_len,
132 const MessageSignature& sig);
133
134// Recover address string from signed message + signature
135std::pair<std::string, bool>
137 const std::uint8_t* msg, std::size_t msg_len,
138 const MessageSignature& sig);
139
140} // namespace secp256k1::coins::wallet
141
142#endif // SECP256K1_COINS_WALLET_HPP
std::string get_address(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::string get_address_p2wpkh(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::string get_address_p2tr(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::pair< fast::Point, bool > recover_signer(const CoinParams &coin, const std::uint8_t *msg, std::size_t msg_len, const MessageSignature &sig)
std::string get_address_cashaddr(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::pair< WalletKey, bool > from_private_key(const std::uint8_t *priv32)
MessageSignature sign_hash(const CoinParams &coin, const WalletKey &key, const std::uint8_t *hash32)
std::string export_public_key_hex(const CoinParams &coin, const WalletKey &key)
std::string export_private_key(const CoinParams &coin, const WalletKey &key, bool testnet=false)
MessageSignature sign_message(const CoinParams &coin, const WalletKey &key, const std::uint8_t *msg, std::size_t msg_len)
bool verify_message(const CoinParams &coin, const fast::Point &pubkey, const std::uint8_t *msg, std::size_t msg_len, const MessageSignature &sig)
std::string get_address_p2pkh(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::string get_address_p2sh_p2wpkh(const CoinParams &coin, const WalletKey &key, bool testnet=false)
std::pair< std::string, bool > recover_address(const CoinParams &coin, const std::uint8_t *msg, std::size_t msg_len, const MessageSignature &sig)
std::array< std::uint8_t, 32 > r
Definition wallet.hpp:53
std::array< std::uint8_t, 65 > to_rsv() const
std::array< std::uint8_t, 32 > s
Definition wallet.hpp:54