Skip to content

Commit b5891e5

Browse files
Add a comparing service
1 parent 9c7ae20 commit b5891e5

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using Autarkysoft.Bitcoin.Blockchain.Scripts;
7+
using Autarkysoft.Bitcoin.Encoders;
8+
9+
namespace FinderOuter.Services
10+
{
11+
public class AddressService
12+
{
13+
private readonly Address addrMan = new Address();
14+
15+
public bool CheckAndGetHash(string address, out byte[] hash)
16+
{
17+
hash = null;
18+
if (string.IsNullOrWhiteSpace(address))
19+
{
20+
return false;
21+
}
22+
23+
return (address[0]) switch
24+
{
25+
'1' => addrMan.VerifyType(address, PubkeyScriptType.P2PKH, out hash),
26+
'3' => addrMan.VerifyType(address, PubkeyScriptType.P2SH, out hash),
27+
'b' => addrMan.VerifyType(address, PubkeyScriptType.P2WPKH, out hash),
28+
_ => false,
29+
};
30+
}
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
namespace FinderOuter.Services
7+
{
8+
public interface ICompareService
9+
{
10+
bool Compare(byte[] key);
11+
}
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using Autarkysoft.Bitcoin.Cryptography.Asymmetric.EllipticCurve;
7+
using FinderOuter.Backend.Cryptography.Hashing;
8+
using System;
9+
using System.Numerics;
10+
11+
namespace FinderOuter.Services
12+
{
13+
public class PrivateKeyToAddressComparer : ICompareService
14+
{
15+
private readonly SecP256k1 curve = new SecP256k1();
16+
private readonly EllipticCurveCalculator calc = new EllipticCurveCalculator();
17+
private byte[] hash;
18+
private Ripemd160Sha256 hash160 = new Ripemd160Sha256();
19+
20+
21+
public bool TrySetHash(string address)
22+
{
23+
AddressService serv = new AddressService();
24+
return serv.CheckAndGetHash(address, out hash);
25+
}
26+
27+
28+
public bool Compare(byte[] key)
29+
{
30+
BigInteger kVal = new BigInteger(key, true, true);
31+
if (kVal >= curve.N)
32+
{
33+
return false;
34+
}
35+
36+
EllipticCurvePoint point = calc.MultiplyByG(kVal);
37+
38+
byte[] xBytes = point.X.ToByteArray(true, true);
39+
byte[] toHash = new byte[65];
40+
toHash[0] = point.Y.IsEven ? (byte)2 : (byte)3;
41+
Buffer.BlockCopy(xBytes, 0, toHash, 33 - xBytes.Length, xBytes.Length);
42+
43+
ReadOnlySpan<byte> compHash = hash160.Compress33(toHash);
44+
if (compHash.SequenceEqual(hash))
45+
{
46+
return true;
47+
}
48+
49+
byte[] yBytes = point.Y.ToByteArray(true, true);
50+
toHash[0] = 4;
51+
Buffer.BlockCopy(yBytes, 0, toHash, 65 - yBytes.Length, yBytes.Length);
52+
53+
ReadOnlySpan<byte> uncompHash = hash160.Compress65(toHash);
54+
if (uncompHash.SequenceEqual(hash))
55+
{
56+
return true;
57+
}
58+
59+
return false;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)