2026-04-24 14:06:19 +08:00

44 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using NBitcoin;
const string defaultWif = "Kwa6hgpEfzwcsvcTCetNjfkPtcCgVt9suVo4zjtBp7At17GAQZUi";
var wif = args.FirstOrDefault() ?? defaultWif;
var report = Create(wif);
Console.WriteLine($"WIF私钥{wif}");
Console.WriteLine($"其网络是 {report.NetworkName}");
Console.WriteLine($"对应的public key 是 {report.PublicKey}");
Console.WriteLine($"对应的public key hash 是 {report.PublicKeyHash}");
Console.WriteLine($"对应的地址是 {report.Address}");
static WifDetails Create(string wif)
{
foreach (var candidate in new[]
{
new NetworkCandidate(Network.Main, "Main"),
new NetworkCandidate(Network.TestNet, "TestNet")
})
{
try
{
var secret = new BitcoinSecret(wif, candidate.Network);
var pubKey = secret.PrivateKey.PubKey;
return new WifDetails(
candidate.NetworkName,
pubKey.ToHex(),
pubKey.Hash.ToString(),
pubKey.GetAddress(ScriptPubKeyType.Legacy, candidate.Network).ToString());
}
catch (FormatException)
{
}
}
throw new FormatException($"无法识别 WIF 私钥对应的网络: {wif}");
}
sealed record NetworkCandidate(Network Network, string NetworkName);
sealed record WifDetails(string NetworkName, string PublicKey, string PublicKeyHash, string Address);