44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
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);
|