-
Notifications
You must be signed in to change notification settings - Fork 48
/
ActivationKey.cs
63 lines (51 loc) · 1.85 KB
/
ActivationKey.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
using System;
using Libplanet.Common;
using Libplanet.Crypto;
using Nekoyume.Action;
using Nekoyume.Model.State;
namespace Nekoyume.Model
{
public struct ActivationKey
{
public const string DeriveKey = "activated";
public PrivateKey PrivateKey { get; }
public Address PendingAddress { get; }
private ActivationKey(PrivateKey privateKey, Address pendingAddress)
{
PrivateKey = privateKey;
PendingAddress = pendingAddress;
}
public static (ActivationKey, PendingActivationState) Create(
PrivateKey privateKey,
byte[] nonce
)
{
if (privateKey is null)
{
throw new ArgumentNullException(nameof(privateKey));
}
if (nonce is null)
{
throw new ArgumentNullException(nameof(nonce));
}
var pendingActivation = new PendingActivationState(nonce, privateKey.PublicKey);
var activationKey = new ActivationKey(privateKey, pendingActivation.address);
return (activationKey, pendingActivation);
}
public static ActivationKey Decode(string hexWithSlash)
{
if (string.IsNullOrEmpty(hexWithSlash) || !hexWithSlash.Contains("/"))
{
throw new ArgumentException($"{nameof(hexWithSlash)} seems invalid. [{hexWithSlash}]");
}
string[] parts = hexWithSlash.Split('/');
var privateKey = new PrivateKey(ByteUtil.ParseHex(parts[0]));
var pendingAddress = new Address(ByteUtil.ParseHex(parts[1]));
return new ActivationKey(privateKey, pendingAddress);
}
public string Encode()
{
return $"{ByteUtil.Hex(PrivateKey.ByteArray)}/{ByteUtil.Hex(PendingAddress.ByteArray)}";
}
}
}