< Summary

Information
Class: EF.Blockchain.Domain.Wallet
Assembly: EF.Blockchain.Domain
File(s): C:\dev\@web3\web3-001-ef-blockchain\backend\EF.Blockchain\src\EF.Blockchain.Domain\Wallet.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_PrivateKey()100%11100%
get_PublicKey()100%11100%
.ctor(...)100%44100%

File(s)

C:\dev\@web3\web3-001-ef-blockchain\backend\EF.Blockchain\src\EF.Blockchain.Domain\Wallet.cs

#LineLine coverage
 1using NBitcoin;
 2
 3namespace EF.Blockchain.Domain;
 4
 5/// <summary>
 6/// Represents a wallet with a public and private key used to sign and verify blockchain transactions.
 7/// </summary>
 8public class Wallet
 9{
 10    /// <summary>
 11    /// The private key in hexadecimal format. Used to sign transactions.
 12    /// </summary>
 140013    public string PrivateKey { get; private set; }
 14
 15    /// <summary>
 16    /// The public key in hexadecimal format. Used to verify ownership and generate the wallet address.
 17    /// </summary>
 200818    public string PublicKey { get; private set; }
 19
 20    /// <summary>
 21    /// Initializes a new wallet.
 22    /// Can use a raw private key (64 hex chars), a WIF string, or generate a new key if none is provided.
 23    /// </summary>
 24    /// <param name="wifOrPrivateKey">
 25    /// Optional string representing a raw private key (64 hex characters) or a WIF-encoded private key.
 26    /// If null, a new random key is generated.
 27    /// </param>
 116028    public Wallet(string? wifOrPrivateKey = null)
 116029    {
 30        Key key;
 31
 116032        if (!string.IsNullOrEmpty(wifOrPrivateKey))
 12833        {
 12834            if (wifOrPrivateKey.Length == 64)
 12035            {
 36                // From raw private key hex
 12037                var bytes = Convert.FromHexString(wifOrPrivateKey);
 12038                key = new Key(bytes);
 12039            }
 40            else
 841            {
 42                // From WIF
 843                key = Key.Parse(wifOrPrivateKey, Network.Main);
 844            }
 12845        }
 46        else
 103247        {
 48            // Random new key
 103249            key = new Key();
 103250        }
 51
 116052        PrivateKey = key.ToHex();
 116053        PublicKey = key.PubKey.ToHex();
 116054    }
 55}