-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaiWallet.cs
More file actions
89 lines (76 loc) · 2.68 KB
/
Copy pathRaiWallet.cs
File metadata and controls
89 lines (76 loc) · 2.68 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Linq;
using Blake2Core;
using Chaos.NaCl;
namespace NetRaiBlocksAddress
{
public class RaiWallet
{
readonly string _base32Alphabet = "13456789abcdefghijkmnopqrstuwxyz";
public readonly byte[] Seed;
public RaiWallet()
{
Seed = GenerateSeed();
}
public RaiWallet(byte[] seed)
{
Seed = seed;
}
public string PublicAddress(int index = 0)
{
var addressSeed = GenerateRaiAddressSeed(index);
var pk = Ed25519.PublicKeyFromSeed(addressSeed);
var pk32 = Base32withPadding.Encode(pk, _base32Alphabet, 260);
var blake2bConfig = new Blake2BConfig
{
OutputSizeInBytes = 5
};
var hasher = Blake2B.Create(blake2bConfig);
hasher.Update(pk);
var checksum = hasher.Finish().Reverse().ToArray();
var checksum32 = Base32withPadding.Encode(checksum, _base32Alphabet, 40);
return $"xrb_{pk32}{checksum32}";
}
public byte[] SecretKey(int index = 0)
{
var addressSeed = GenerateRaiAddressSeed(index);
return Ed25519.ExpandedPrivateKeyFromSeed(addressSeed);
}
public bool ValidateAddress(string address)
{
if (address.Length != 64 && !address.StartsWith("xrb_")) return false;
var pk32 = address.Substring(4, 52);
var pk = Base32withPadding.Decode(pk32, _base32Alphabet, 4);
var checksum = address.Substring(56);
var checksumDecoded = Base32withPadding.Decode(checksum, _base32Alphabet, 0);
checksumDecoded = checksumDecoded.Reverse().ToArray();
var blake2bConfig = new Blake2BConfig
{
OutputSizeInBytes = 5
};
var hasher = Blake2B.Create(blake2bConfig);
hasher.Update(pk);
var pkChecksum = hasher.Finish();
return pkChecksum.SequenceEqual(checksumDecoded);
}
byte[] GenerateRaiAddressSeed(int index)
{
var blake2bConfig = new Blake2BConfig
{
OutputSizeInBytes = 32
};
var hasher = Blake2B.Create(blake2bConfig);
hasher.Update(Seed);
var indexBytes = BitConverter.GetBytes(index).Reverse().ToArray();
hasher.Update(indexBytes);
return hasher.Finish();
}
byte[] GenerateSeed()
{
byte[] array = new byte[32];
Random random = new Random();
random.NextBytes(array);
return array;
}
}
}