Documentation
¶
Overview ¶
Package wallet provides Bitcoin wallet functionality including address generation and encoding
Package wallet implements Bitcoin HD (Hierarchical Deterministic) wallet functionality according to BIP32, BIP44, and BIP49 specifications.
wallet/wallet.go
Package wallet implements secure storage functionality for HD wallets.
Index ¶
- func Base58Decode(input string) ([]byte, error)
- func Base58Encode(input []byte) string
- func GenerateEncryptionKey() ([]byte, error)
- func IsBitcoinAddress(address string) (bool, string)
- type Address
- type BTCHDWallet
- func (w *BTCHDWallet) Currency() string
- func (w *BTCHDWallet) DeriveNextAddress() (string, error)
- func (w *BTCHDWallet) GetAddress() (string, error)
- func (w *BTCHDWallet) GetAddressBalance(address string) (float64, error)
- func (w *BTCHDWallet) GetTransactionConfirmations(txID string) (int, error)
- func (w *BTCHDWallet) RecoverNextIndex() error
- func (w *BTCHDWallet) SaveToFile(config StorageConfig) error
- type HDWallet
- type MoneroConfig
- type MoneroHDWallet
- func (w *MoneroHDWallet) Currency() string
- func (w *MoneroHDWallet) DeriveNextAddress() (string, error)
- func (w *MoneroHDWallet) GetAddress() (string, error)
- func (w *MoneroHDWallet) GetAddressBalance(address string) (float64, error)
- func (w *MoneroHDWallet) GetTransactionConfirmations(txID string) (int, error)
- type StorageConfig
- type WalletType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base58Decode ¶
Base58Decode converts a base58-encoded string back into bytes.
Parameters:
- input: Base58-encoded string to decode
Returns:
- []byte: Decoded bytes
- error: Invalid character error if input contains characters outside base58 alphabet
Error cases:
- Returns error if input contains invalid base58 characters
- Never returns error for empty input (returns empty byte slice)
Features:
- Preserves leading zeros (encoded as '1' characters)
- Validates all input characters
- Handles arbitrary-length inputs via big.Int
Example:
decoded, err := Base58Decode("12f9b")
// decoded = []byte{0, 60, 23, 110}
Related: Base58Encode for reverse operation
func Base58Encode ¶
Base58Encode converts a byte slice into a base58-encoded string using Bitcoin's alphabet.
Parameters:
- input: Raw bytes to encode
Returns:
- string: Base58-encoded representation of the input
Features:
- Preserves leading zeros in the input
- Uses Bitcoin's specific base58 alphabet
- Handles arbitrary-length inputs via big.Int
Example:
bytes := []byte{0, 60, 23, 110}
encoded := Base58Encode(bytes) // "12f9b"
Related: Base58Decode for reverse operation
func GenerateEncryptionKey ¶
GenerateEncryptionKey creates a cryptographically secure 32-byte key suitable for AES-256 encryption.
Returns:
- []byte: 32-byte random encryption key
- error: If secure random number generation fails
Security:
- Uses crypto/rand for secure random number generation
- Generates sufficient entropy for AES-256
Usage:
key, err := GenerateEncryptionKey()
config := StorageConfig{
DataDir: "/path/to/storage",
EncryptionKey: key,
}
func IsBitcoinAddress ¶
IsBitcoinAddress checks if a string is a valid Bitcoin address and returns whether it's a mainnet or testnet address, or "invalid" if the address is not valid.
Types ¶
type Address ¶
type Address string
Address represents a Bitcoin address and wraps a `string` to implement the btcutil.Address interface. It provides methods to encode the address, retrieve the raw bytes of the address, and check if the address is valid for a specific Bitcoin network (mainnet or testnet).
func (Address) EncodeAddress ¶
EncodeAddress returns the string encoding of the payment address associated with the Address value. See the comment on String for how this method differs from String.
func (Address) IsForNet ¶
IsForNet returns whether or not the address is associated with the passed bitcoin network.
func (Address) ScriptAddress ¶
ScriptAddress returns the raw bytes of the address to be used when inserting the address into a txout's script.
func (Address) String ¶
String returns the string encoding of the transaction output destination. Please note that String differs subtly from EncodeAddress: String will return the value as a string without any conversion, while EncodeAddress may convert destination types (for example, converting pubkeys to P2PKH addresses) before encoding as a payment address string.
type BTCHDWallet ¶
type BTCHDWallet struct {
// contains filtered or unexported fields
}
BTCHDWallet represents a hierarchical deterministic Bitcoin wallet implementing BIP32 and BIP44 standards.
func LoadFromFile ¶
func LoadFromFile(config StorageConfig) (*BTCHDWallet, error)
LoadFromFile loads and decrypts a wallet from a file.
Parameters:
- config: StorageConfig containing storage location and encryption key
Returns:
- *HDWallet: Decrypted wallet instance
- error: If decryption fails, file is corrupt, or file operations fail
Security:
- Validates data integrity using AES-GCM authentication
- Verifies minimum data length requirements
- Returns errors for any decryption failures
Related: SaveToFile
func NewBTCHDWallet ¶
func NewBTCHDWallet(seed []byte, testnet bool) (*BTCHDWallet, error)
NewHDWallet creates a new HD wallet from a seed.
Parameters:
- seed: Random seed bytes (must be 16-64 bytes)
- testnet: Boolean flag for testnet/mainnet network selection
Returns:
- *HDWallet: Initialized wallet instance
- error: If seed length is invalid
Security:
- Seed must be generated with sufficient entropy
- Seed should be backed up securely
Related: DeriveNextAddress, GetAddress
func (*BTCHDWallet) Currency ¶
func (w *BTCHDWallet) Currency() string
Currency implements HDWallet interface
func (*BTCHDWallet) DeriveNextAddress ¶
func (w *BTCHDWallet) DeriveNextAddress() (string, error)
DeriveNextAddress derives the next Bitcoin address using BIP44 path m/44'/0'/0'/0/index
Returns:
- string: Base58Check encoded Bitcoin address
- error: If key derivation or address generation fails
Path components:
- 44' : BIP44 purpose
- 0' : Bitcoin coin type
- 0' : Account 0
- 0 : External chain
- i : Address index
Related: GetAddress, pubKeyToAddress
func (*BTCHDWallet) GetAddress ¶
func (w *BTCHDWallet) GetAddress() (string, error)
GetAddress returns the next available Bitcoin address.
Returns:
- string: Base58Check encoded Bitcoin address
- error: If address derivation fails
Notes:
- Increments internal address counter
- Thread-safe for single wallet instance
Related: DeriveNextAddress
func (*BTCHDWallet) GetAddressBalance ¶
func (w *BTCHDWallet) GetAddressBalance(address string) (float64, error)
GetAddressBalance implements paywall.CryptoClient Returns the balance for a specific Bitcoin address.
Parameters:
- address: Bitcoin address to check
Returns:
- float64: Current balance in BTC
- error: If address is invalid or query fails
Related: GetTransactionConfirmations
func (*BTCHDWallet) GetTransactionConfirmations ¶
func (w *BTCHDWallet) GetTransactionConfirmations(txID string) (int, error)
GetTransactionConfirmations implements paywall.CryptoClient. Returns the number of confirmations for a specific transaction.
Parameters:
- txID: Bitcoin transaction ID
Returns:
- int: Number of confirmations
- error: If transaction is not found or query fails
Related: GetAddressBalance
func (*BTCHDWallet) RecoverNextIndex ¶
func (w *BTCHDWallet) RecoverNextIndex() error
func (*BTCHDWallet) SaveToFile ¶
func (w *BTCHDWallet) SaveToFile(config StorageConfig) error
SaveToFile encrypts and saves the wallet to a file.
Parameters:
- config: StorageConfig containing storage location and encryption key
Returns:
- error: If encryption fails or file operations fail
Security:
- Uses AES-256-GCM for encryption
- Generates random nonce for each save
- Sets restrictive file permissions (0600)
Related: LoadFromFile
type HDWallet ¶
type HDWallet interface {
DeriveNextAddress() (string, error)
GetAddress() (string, error)
Currency() string
GetAddressBalance(address string) (float64, error)
GetTransactionConfirmations(txID string) (int, error)
}
HDWallet defines the interface for cryptocurrency wallets
type MoneroConfig ¶
MoneroConfig holds Monero wallet RPC connection details
type MoneroHDWallet ¶
type MoneroHDWallet struct {
// contains filtered or unexported fields
}
MoneroHDWallet implements the HDWallet interface for Monero using RPC
func NewMoneroWallet ¶
func NewMoneroWallet(config MoneroConfig) (*MoneroHDWallet, error)
NewMoneroWallet creates a new Monero wallet instance
func (*MoneroHDWallet) Currency ¶
func (w *MoneroHDWallet) Currency() string
Currency implements HDWallet interface
func (*MoneroHDWallet) DeriveNextAddress ¶
func (w *MoneroHDWallet) DeriveNextAddress() (string, error)
DeriveNextAddress implements HDWallet interface by creating a new subaddress
func (*MoneroHDWallet) GetAddress ¶
func (w *MoneroHDWallet) GetAddress() (string, error)
GetAddress implements HDWallet interface by deriving next address
func (*MoneroHDWallet) GetAddressBalance ¶
func (w *MoneroHDWallet) GetAddressBalance(address string) (float64, error)
GetAddressBalance implements paywall.CryptoClient by getting balance for specific address
func (*MoneroHDWallet) GetTransactionConfirmations ¶
func (w *MoneroHDWallet) GetTransactionConfirmations(txID string) (int, error)
GetTransactionConfirmations implements paywall.CryptoClient.
type StorageConfig ¶
StorageConfig defines configuration parameters for wallet storage operations.
Fields:
- DataDir: Directory path where wallet files will be stored
- EncryptionKey: 32-byte key used for AES-256 encryption
Security:
- DataDir should have appropriate filesystem permissions
- EncryptionKey must be securely generated and stored
type WalletType ¶
type WalletType string
WalletType identifies the cryptocurrency wallet implementation
const ( Bitcoin WalletType = "BTC" Monero WalletType = "XMR" )