-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddressEncoding.cs
43 lines (38 loc) · 1.48 KB
/
AddressEncoding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using HashLib;
using System.IO;
using Blake2Sharp;
namespace WavesCS.Core
{
public static class AddressEncoding
{
private static readonly IHash Keccak256 = HashFactory.Crypto.SHA3.CreateKeccak256();
private static byte[] Hash(byte[] message, int offset, int length, IHash algorithm)
{
algorithm.Initialize();
algorithm.TransformBytes(message, offset, length);
return algorithm.TransformFinal().GetBytes();
}
public static byte[] FastHash(byte[] message, int offset, int length)
{
var blakeConfig = new Blake2BConfig { OutputSizeInBits = 256 };
return Blake2B.ComputeHash(message, offset, length, blakeConfig);
}
public static byte[] SecureHash(byte[] message, int offset, int length)
{
var blake2B = FastHash(message, offset, length);
return Hash(blake2B, 0, blake2B.Length, Keccak256);
}
public static string GetAddressFromPublicKey(byte[] publicKey, char chainId)
{
var stream = new MemoryStream(26);
var hash = SecureHash(publicKey, 0, publicKey.Length);
var writer = new BinaryWriter(stream);
writer.Write((byte)1);
writer.Write((byte)chainId);
writer.Write(hash, 0, 20);
var checksum = SecureHash(stream.ToArray(), 0, 22);
writer.Write(checksum, 0, 4);
return Base58.Encode(stream.ToArray());
}
}
}