优化结构
This commit is contained in:
parent
cbc0cd988c
commit
213589020a
23
.tmp/ReflectionProbe/Program.cs
Normal file
23
.tmp/ReflectionProbe/Program.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using NBitcoin;
|
||||
|
||||
DumpType("Key", typeof(Key));
|
||||
Console.WriteLine("---");
|
||||
DumpType("PubKey", typeof(PubKey));
|
||||
|
||||
static void DumpType(string label, Type type)
|
||||
{
|
||||
Console.WriteLine(label);
|
||||
foreach (var method in type.GetMethods()
|
||||
.Where(m => m.Name.Contains("Shared", StringComparison.Ordinal)
|
||||
|| m.Name.Contains("Address", StringComparison.Ordinal)
|
||||
|| m.Name.Contains("Wif", StringComparison.Ordinal)
|
||||
|| m.Name.Contains("Hash", StringComparison.Ordinal)
|
||||
|| m.Name.Contains("Bytes", StringComparison.Ordinal)
|
||||
|| m.Name.Contains("Hex", StringComparison.Ordinal))
|
||||
.OrderBy(m => m.Name)
|
||||
.ThenBy(m => m.ToString(), StringComparer.Ordinal))
|
||||
{
|
||||
Console.WriteLine(method);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
71
bsv-blockchain.sln
Normal file
71
bsv-blockchain.sln
Normal file
@ -0,0 +1,71 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A83F02BA-518A-4675-956F-B0CC2E3DC541}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bsv.BlockStats", "src\Bsv.BlockStats\Bsv.BlockStats.csproj", "{F40A60EB-646F-4182-ABBF-D0350B89E1D8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bsv.WifTool", "src\Bsv.WifTool\Bsv.WifTool.csproj", "{527B4760-7362-49A1-96A1-216A1695B49C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bsv.MessageTool", "src\Bsv.MessageTool\Bsv.MessageTool.csproj", "{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {FF435FC6-AECA-4149-AD60-06092EB59AB8}
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|x64.Build.0 = Release|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8}.Release|x86.Build.0 = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|x64.Build.0 = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C}.Release|x86.Build.0 = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|x64.Build.0 = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{F40A60EB-646F-4182-ABBF-D0350B89E1D8} = {A83F02BA-518A-4675-956F-B0CC2E3DC541}
|
||||
{527B4760-7362-49A1-96A1-216A1695B49C} = {A83F02BA-518A-4675-956F-B0CC2E3DC541}
|
||||
{DB2B26EB-9FD9-4B48-87CE-9AFEBB2CB683} = {A83F02BA-518A-4675-956F-B0CC2E3DC541}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@ -1,7 +1,12 @@
|
||||
using BitcoinBlockchain.Data;
|
||||
using BitcoinBlockchain.Parser;
|
||||
|
||||
var blockchainFilePath = @"C:\Users\feie9454\source\repos\BSVTest\BSVTest\blk00000.dat";
|
||||
var blockchainFilePath = args.FirstOrDefault();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(blockchainFilePath))
|
||||
{
|
||||
blockchainFilePath = Path.Combine(Directory.GetCurrentDirectory(), "blk00000.dat");
|
||||
}
|
||||
|
||||
if (!File.Exists(blockchainFilePath))
|
||||
{
|
||||
14
src/Bsv.MessageTool/Bsv.MessageTool.csproj
Normal file
14
src/Bsv.MessageTool/Bsv.MessageTool.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BsvBitcoinBlockChain" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
157
src/Bsv.MessageTool/Program.cs
Normal file
157
src/Bsv.MessageTool/Program.cs
Normal file
@ -0,0 +1,157 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
using NBitcoin;
|
||||
|
||||
const string cipherTextBase64 = "QklFMQMdos3oztNAUqW++4+GjfblOT2MSNZEd18K++IqbAckRCq7/RjlzUJbgonrHbZabwdDSyTkrpUM2X8K0aip90/7K0fNvbILk7kgfiQBxMErAwnJzcJXLtKI/Yb3M4Kpj3BSWLhFpYkpx8lMmBsg8lHHnD+JIkf8johOj60UzL0CxeFS5NvohFRmzdesox2Ge8CoNn8A+szcsrENmxvDWGsQx0C/D1ya6GobKYZpWnG+KA0yui7KpfQCM6jP7JvnaLc=";
|
||||
const string recipientWif = "cRLzVXAinDb2Fvn3hTn5R3q57wbigm5SvFe4WbiDmzrMqGYwPfD3";
|
||||
const string candidateAPublicKey = "029b9415580dc6fb0bc298923470ce2f2289cfefb6a06e14f1620e877c164a65b7";
|
||||
const string candidateCPrivateKey = "cUsnFska4Ksz8eqCA31JBVYCPPe453jQ8FXHfACaL23sZcBarXKS";
|
||||
const string candidateDPublicKey = "03f25e285ae095e927ef3477c05d67ce8d4d31e3a151459c9a76f124248a3738b5";
|
||||
|
||||
var recipientSecret = ResolveSecret(recipientWif);
|
||||
var envelope = Bie1Envelope.Parse(cipherTextBase64);
|
||||
var plainText = envelope.Decrypt(recipientSecret.PrivateKey);
|
||||
var sender = InferSender(envelope.SenderPublicKey, plainText, recipientSecret.PrivateKey.PubKey);
|
||||
|
||||
Console.WriteLine($"密文解密后是 {plainText}");
|
||||
Console.WriteLine($"内嵌发送方公钥是 {envelope.SenderPublicKey.ToHex()}");
|
||||
Console.WriteLine($"这条消息来自 {sender}");
|
||||
|
||||
static string InferSender(PubKey embeddedSenderPublicKey, string plainText, PubKey recipientPublicKey)
|
||||
{
|
||||
var candidates = new Dictionary<string, PubKey>(StringComparer.Ordinal)
|
||||
{
|
||||
["A"] = new PubKey(Convert.FromHexString(candidateAPublicKey)),
|
||||
["B"] = recipientPublicKey,
|
||||
["C"] = ResolveSecret(candidateCPrivateKey).PrivateKey.PubKey,
|
||||
["D"] = new PubKey(Convert.FromHexString(candidateDPublicKey))
|
||||
};
|
||||
|
||||
var signedMessage = ParseSignedMessage(plainText);
|
||||
if (signedMessage is not null)
|
||||
{
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (candidate.Value.VerifyMessage(signedMessage.Message, signedMessage.Signature))
|
||||
{
|
||||
return candidate.Key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (candidate.Value.ToHex().Equals(embeddedSenderPublicKey.ToHex(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return candidate.Key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var candidate in candidates.Keys)
|
||||
{
|
||||
if (plainText.Contains($"来自{candidate}", StringComparison.Ordinal)
|
||||
|| plainText.Contains($"我是{candidate}", StringComparison.Ordinal)
|
||||
|| plainText.Contains($"from {candidate}", StringComparison.OrdinalIgnoreCase)
|
||||
|| plainText.Contains($"sender:{candidate}", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return "无法仅凭当前信息自动判定";
|
||||
}
|
||||
|
||||
static SignedMessage? ParseSignedMessage(string plainText)
|
||||
{
|
||||
var match = Regex.Match(plainText, "^Msg: \"(?<message>.*)\", signature: \"(?<signature>.*)\"$", RegexOptions.CultureInvariant);
|
||||
if (!match.Success)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new SignedMessage(match.Groups["message"].Value, match.Groups["signature"].Value);
|
||||
}
|
||||
|
||||
static BitcoinSecret ResolveSecret(string wif)
|
||||
{
|
||||
foreach (var network in new[] { Network.Main, Network.TestNet })
|
||||
{
|
||||
try
|
||||
{
|
||||
return new BitcoinSecret(wif, network);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
throw new FormatException($"无法识别 WIF 私钥对应的网络: {wif}");
|
||||
}
|
||||
|
||||
sealed class Bie1Envelope
|
||||
{
|
||||
private Bie1Envelope(byte[] rawBytes, PubKey senderPublicKey, byte[] cipherText, byte[] mac)
|
||||
{
|
||||
RawBytes = rawBytes;
|
||||
SenderPublicKey = senderPublicKey;
|
||||
CipherText = cipherText;
|
||||
Mac = mac;
|
||||
}
|
||||
|
||||
public byte[] RawBytes { get; }
|
||||
|
||||
public PubKey SenderPublicKey { get; }
|
||||
|
||||
public byte[] CipherText { get; }
|
||||
|
||||
public byte[] Mac { get; }
|
||||
|
||||
public static Bie1Envelope Parse(string base64)
|
||||
{
|
||||
var rawBytes = Convert.FromBase64String(base64);
|
||||
if (rawBytes.Length <= 69)
|
||||
{
|
||||
throw new FormatException("BIE1 密文长度无效。");
|
||||
}
|
||||
|
||||
var magic = Encoding.ASCII.GetString(rawBytes, 0, 4);
|
||||
if (!string.Equals(magic, "BIE1", StringComparison.Ordinal))
|
||||
{
|
||||
throw new FormatException("密文不是 BIE1 格式。");
|
||||
}
|
||||
|
||||
var senderPublicKeyBytes = rawBytes[4..37];
|
||||
var cipherText = rawBytes[37..^32];
|
||||
var mac = rawBytes[^32..];
|
||||
|
||||
return new Bie1Envelope(rawBytes, new PubKey(senderPublicKeyBytes), cipherText, mac);
|
||||
}
|
||||
|
||||
public string Decrypt(Key recipientPrivateKey)
|
||||
{
|
||||
var sharedPublicKey = SenderPublicKey.GetSharedPubkey(recipientPrivateKey);
|
||||
var derived = SHA512.HashData(sharedPublicKey.ToBytes());
|
||||
var iv = derived[..16];
|
||||
var encryptionKey = derived[16..32];
|
||||
var macKey = derived[32..64];
|
||||
var expectedMac = HMACSHA256.HashData(macKey, RawBytes[..^32]);
|
||||
|
||||
if (!CryptographicOperations.FixedTimeEquals(expectedMac, Mac))
|
||||
{
|
||||
throw new CryptographicException("BIE1 HMAC 校验失败。");
|
||||
}
|
||||
|
||||
using var aes = Aes.Create();
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Key = encryptionKey;
|
||||
aes.IV = iv;
|
||||
|
||||
using var decryptor = aes.CreateDecryptor();
|
||||
var plainBytes = decryptor.TransformFinalBlock(CipherText, 0, CipherText.Length);
|
||||
return Encoding.UTF8.GetString(plainBytes);
|
||||
}
|
||||
}
|
||||
|
||||
sealed record SignedMessage(string Message, string Signature);
|
||||
14
src/Bsv.WifTool/Bsv.WifTool.csproj
Normal file
14
src/Bsv.WifTool/Bsv.WifTool.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BsvBitcoinBlockChain" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
43
src/Bsv.WifTool/Program.cs
Normal file
43
src/Bsv.WifTool/Program.cs
Normal file
@ -0,0 +1,43 @@
|
||||
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);
|
||||
Loading…
x
Reference in New Issue
Block a user