-
Notifications
You must be signed in to change notification settings - Fork 18
/
Account.cs
178 lines (159 loc) · 5.07 KB
/
Account.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Chaos.NaCl;
using Newtonsoft.Json;
using Schnorrkel;
using Substrate.NetApi.Model.Types.Base;
using System;
using System.Security.Cryptography.X509Certificates;
namespace Substrate.NetApi.Model.Types
{
/// <summary>
/// Represents a key type.
/// </summary>
public enum KeyType
{
/// <summary>
/// Ed25519
/// </summary>
Ed25519,
/// <summary>
/// Sr25519
/// </summary>
Sr25519
}
/// <summary>
/// Interface for an account.
/// </summary>
public interface IAccount
{
/// <summary>
/// Sign the specified message.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
byte[] Sign(byte[] message);
/// <summary>
/// Verifies a signature from this account.
/// </summary>
/// <param name="signature"></param>
/// <param name="publicKey"></param>
/// <param name="message"></param>
/// <returns></returns>
bool Verify(byte[] signature, byte[] publicKey, byte[] message);
}
/// <summary>
/// Represents an account.
/// </summary>
public class Account : AccountId, IAccount
{
/// <summary>
/// Key Type
/// </summary>
public KeyType KeyType { get; private set; }
/// <summary>
/// Key Type Byte
/// </summary>
[JsonIgnore]
public byte KeyTypeByte
{
get
{
switch (KeyType)
{
case KeyType.Ed25519:
return 0;
case KeyType.Sr25519:
return 1;
default:
throw new NotSupportedException($"Unknown key type found '{KeyType}'.");
}
}
}
/// <summary>
/// Private Key
/// </summary>
[JsonIgnore]
public byte[] PrivateKey { get; private set; }
/// <summary>
/// Creates the specified key type with private key.
/// </summary>
/// <param name="keyType"></param>
/// <param name="privateKey"></param>
/// <param name="publicKey"></param>
public void Create(KeyType keyType, byte[] privateKey, byte[] publicKey)
{
KeyType = keyType;
PrivateKey = privateKey;
base.Create(publicKey);
}
/// <summary>
/// Creates the specified key type, without private key.
/// </summary>
/// <param name="keyType"></param>
/// <param name="publicKey"></param>
public void Create(KeyType keyType, byte[] publicKey)
{
Create(keyType, null, publicKey);
}
/// <summary>
/// Builds the specified key type.
/// </summary>
/// <param name="keyType"></param>
/// <param name="privateKey"></param>
/// <param name="publicKey"></param>
/// <returns></returns>
public static Account Build(KeyType keyType, byte[] privateKey, byte[] publicKey)
{
var account = new Account();
account.Create(keyType, privateKey, publicKey);
return account;
}
/// <summary>
/// Signs the specified message.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public byte[] Sign(byte[] message)
{
switch (KeyType)
{
case KeyType.Ed25519:
return Ed25519.Sign(message, PrivateKey);
case KeyType.Sr25519:
return Sr25519v091.SignSimple(Bytes, PrivateKey, message);
default:
throw new NotSupportedException($"Unknown key type found '{KeyType}'.");
}
}
/// <summary>
/// Verifies a signature from this account.
/// </summary>
/// <param name="signature"></param>
/// <param name="message"></param>
/// <returns></returns>
public bool Verify(byte[] signature, byte[] message)
{
return Verify(signature, Bytes, message);
}
/// <summary>
/// Verifies the specified signature.
/// </summary>
/// <param name="signature"></param>
/// <param name="publicKey"></param>
/// <param name="message"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public bool Verify(byte[] signature, byte[] publicKey, byte[] message)
{
switch (KeyType)
{
case KeyType.Ed25519:
return Ed25519.Verify(signature, message, publicKey);
case KeyType.Sr25519:
return Sr25519v091.Verify(signature, publicKey, message);
default:
throw new NotSupportedException($"Unknown key type found '{KeyType}'.");
}
}
}
}