This repository contains a C# application for managing cryptocurrency wallets using the HDWallet library. The application provides various functionalities for creating wallets, generating derived keys, performing transactions, and exploring blockchain data through APIs and WebSockets.
The HDWallet
class serves as the entry point for the application. It provides a command-line interface for interacting with the wallet functionalities. Below, you'll find a detailed explanation of each method and how to use them.
- Description: Initializes the HDWallet service provider.
- Usage: Call this method before using any other wallet-related functionality.
- Description: Retrieves the wallet service for performing wallet-related operations.
- Usage: Call this method to get an instance of the wallet service.
The HDWallet
is a .NET library that provides functionality for creating and managing hierarchical deterministic wallets (HD wallets) for various cryptocurrencies. This library allows you to generate mnemonic phrases, create wallets with or without passwords, derive child keys, create transactions, recover wallets, and interact with cryptocurrency APIs. This documentation provides a detailed explanation of the HDWallet
library and its methods.
First, import the necessary namespaces:
using DSW.HDWallet.Application.Provider;
Then, initialize the HDWalletServiceProvider
:
HDWalletServiceProvider.Initialize();
Now, you are ready to use the HDWallet
library.
The CreateWallet
method allows you to generate a new wallet without a password. It returns a wallet object containing the seed and mnemonic.
WordCount wordCount = WordCount.Twelve;
var createdWallet = walletAppService.CreateWallet(wordCount);
// Access wallet properties
string seedHex = createdWallet.SeedHex;
string mnemonic = createdWallet.Mnemonic;
The CreateWalletWithPassword
method creates a new wallet with a password. It returns a wallet object containing the seed and mnemonic.
WordCount wordCount = WordCount.Twelve;
string password = "your_password";
var walletWithPassword = walletAppService.CreateWalletWithPassword(wordCount, password);
// Access wallet properties
string seedHex = walletWithPassword.SeedHex;
string mnemonic = walletWithPassword.Mnemonic;
You can derive child keys from a mnemonic and password using the CreateDerivedKey
method. Provide the mnemonic, password, coin type, and the index of the derived key.
string mnemonic = "your_mnemonic_phrase";
string password = "your_password";
CoinType coinType = CoinType.Bitcoin;
int index = 0;
var derivedKey = walletAppService.CreateDerivedKey(coinType, mnemonic, index, password);
// Access derived key properties
string address = derivedKey.Address;
string keyPath = derivedKey.Path;
PubKey pubKey = derivedKey.PubKey;
This method combines wallet creation with deriving child keys. It creates a wallet with a password, generates a public key, and then derives child keys from it.
WordCount wordCount = WordCount.Twelve;
string password = "your_password";
var newWalletWithPassword = walletAppService.CreateWalletWithPassword(wordCount, password);
// Generate a public key
CoinType selectedCoin = CoinType.Bitcoin;
var generatePubKey = walletAppService.GeneratePubkey(selectedCoin, newWalletWithPassword.SeedHex, true);
// Access generated public key properties
string publicKey = generatePubKey.PubKey;
string coinType = generatePubKey.CoinType;
string path = generatePubKey.Path;
// Derive child keys
int numberOfKeysToDerive = 5;
for (int i = 0; i < numberOfKeysToDerive; i++)
{
var deriveKey = walletAppService.GenerateDerivePubKey(generatePubKey.PubKey, selectedCoin, i, true);
// Access derived key properties
string derivedAddress = deriveKey.Address;
string keyPath = generatePubKey.Path + "/" + deriveKey.Path;
}
You can use the TransactionAsync
method to create a cryptocurrency transaction. Provide the coin name, recipient's address, and transaction value.
string coinName = "Bitcoin";
string recipientAddress = "recipient_address";
ulong transactionValue = 100000; // Value in the smallest denomination of the cryptocurrency
var transactionResponse = walletAppService.TransactionAsync(coinName, recipientAddress, transactionValue).Result;
// Access transaction details
foreach (var transaction in transactionResponse)
{
string voutIndex = transaction.Vout;
string txId = transaction.Txid;
string value = transaction.Value.ToFormattedString(); // Formatted value of the transaction
}
The RecoverWallet
method allows you to recover a wallet using a mnemonic phrase and password.
string mnemonicWords = "your_mnemonic_phrase";
string passwordRecover = "your_password";
string recoveredSeedHex = walletAppService.RecoverWallet(mnemonicWords, passwordRecover);
// Access the recovered seed hex
Generate random secret words using the CreateWallet
method with a WordCount
of your choice.
WordCount wordCount = WordCount.TwentyFour;
var secretWordsWallet = walletAppService.CreateWallet(wordCount);
// Get random mnemonic words
string[] randomWords = secretWordsWallet.GetRandomMnemonic(3);
These methods interact with cryptocurrency APIs for various purposes.
Retrieve information about a cryptocurrency address using the GetAddressAsync
method.
string coinName = "Bitcoin";
string addressCoin = "address_to_query";
var resultAPI = walletAppService.GetAddressAsync(coinName, addressCoin).Result;
// Access address information
string address = resultAPI.Address;
string balance = resultAPI.Balance;
string totalReceived = resultAPI.TotalReceived;
string totalSent = resultAPI.TotalSent;
string txs = resultAPI.Txs;
// Access a list of transaction IDs
List<string> txIds = resultAPI.Txids;
Retrieve information about a cryptocurrency transaction using the GetTransactionAsync
method.
string coinName = "Bitcoin";
string txId = "transaction_id_to_query";
var resultTransaction = walletAppService.GetTransactionAsync(coinName, txId).Result;
// Access transaction details
string blockHash = resultTransaction.BlockHash;
string blockHeight = resultTransaction.BlockHeight;
string blockTime = resultTransaction.BlockTime;
string confirmations = resultTransaction.Confirmations;
string size = resultTransaction.Size;
string txid = resultTransaction.Txid;
string value = resultTransaction.Value;
string valueIn = resultTransaction.ValueIn;
string version = resultTransaction.Version;
string hex = resultTransaction.Hex;
Retrieve specific details about a cryptocurrency transaction using the GetTransactionSpecificAsync
method.
string coinName = "Bitcoin";
string txId = "transaction_id_to_query";
var resultTransactionSpecific = walletAppService.GetTransactionSpecificAsync(coinName, txId).Result;
// Access specific transaction details
string blockHash = resultTransactionSpecific.Blockhash;
string blockTime = resultTransactionSpecific.Blocktime;
string confirmations = resultTransactionSpecific.Confirmations;
string expiryHeight = resultTransactionSpecific.Expiryheight;
string time = resultTransactionSpecific.Time;
string txid = resultTransactionSpecific.Txid;
string valueBalance = resultTransaction
Specific.ValueBalance;
string version = resultTransactionSpecific.Version;
string hex = resultTransactionSpecific.Hex;
Retrieve the block hash of a specific block using the GetBlockHash
method.
string coinName = "Bitcoin";
string blockHeight = "block_height_to_query";
var resultBlockHash = walletAppService.GetBlockHash(coinName, blockHeight).Result;
// Access the block hash
string blockHash = resultBlockHash.BlockHash;
Retrieve extended public key (xPub) information for a specific cryptocurrency using the GetXpub
method.
string coinName = "Bitcoin";
string xPub = "your_xpub_to_query";
var xPubResult = walletAppService.GetXpub(coinName, xPub).Result;
// Access xPub information (not yet implemented in the code sample)
Retrieve unspent transaction outputs (UTXOs) for a specific address using the GetUtxo
method.
string coinName = "Bitcoin";
string utxoAddress = "address_to_query";
bool utxoConfirmed = false; // Set to true for confirmed UTXOs, false for unconfirmed
var utxoResult = walletAppService.GetUtxo(coinName, utxoAddress, utxoConfirmed).Result;
// Access UTXO details
foreach (var item in utxoResult)
{
string height = item.Height;
string txid = item.Txid;
string value = item.Value;
string vout = item.Vout;
string confirmations = item.Confirmations;
}
Retrieve transaction details via WebSocket for a specific transaction using the GetWSTransactionAsync
method.
string coinName = "Bitcoin";
string txId = "transaction_id_to_query";
var wss = walletAppService.GetWSTransactionAsync(coinName, txId).Result;
// Access WebSocket transaction details
string blockHash = wss.data.blockHash;
string blockTime = wss.data.blockTime;
string blockHeight = wss.data.blockHeight;
string txid = wss.data.txid;
string valueIn = wss.data.valueIn;
string value = wss.data.value;
string size = wss.data.size;
string confirmations = wss.data.confirmations;
string hex = wss.data.hex;
Subscribe to new transactions via WebSocket using the SubscribeNewTransaction
method.
string coinName = "Bitcoin";
var subscription = walletAppService.SubscribeNewTransaction(coinName).Result;
// This method does not return transaction details in this code sample
To use the HDWallet application, follow these steps:
-
Initialize the HDWallet service provider by calling
HDWalletServiceProvider.Initialize()
. -
Retrieve the wallet service by calling
HDWalletServiceProvider.GetWalletService()
. -
Select a menu option by entering the corresponding option number.
-
Follow the prompts to perform the desired wallet operation.
-
To exit the application, select option 0.
Feel free to explore the various wallet functionalities provided by the HDWallet application and interact with cryptocurrency wallets and blockchain data.
This documentation provides an overview of the HDWallet
library and its methods for creating and managing hierarchical deterministic wallets and interacting with cryptocurrency APIs. You can use these methods to perform various cryptocurrency-related tasks in your .NET applications.