-
Notifications
You must be signed in to change notification settings - Fork 137
/
SecretKeyStoreService.cs
82 lines (69 loc) · 3.35 KB
/
SecretKeyStoreService.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
#pragma warning disable CS1591
using Solnet.KeyStore.Model;
using Solnet.KeyStore.Services;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Solnet.KeyStore
{
/// <summary>
/// Implements a keystore compatible with the web3 secret storage standard.
/// </summary>
public class SecretKeyStoreService
{
private readonly KeyStoreScryptService _keyStoreScryptService;
private readonly KeyStorePbkdf2Service _keyStorePbkdf2Service;
public SecretKeyStoreService()
{
_keyStorePbkdf2Service = new KeyStorePbkdf2Service();
_keyStoreScryptService = new KeyStoreScryptService();
}
public SecretKeyStoreService(KeyStoreScryptService keyStoreScryptService, KeyStorePbkdf2Service keyStorePbkdf2Service)
{
_keyStoreScryptService = keyStoreScryptService;
_keyStorePbkdf2Service = keyStorePbkdf2Service;
}
public static string GetAddressFromKeyStore(string json)
{
if (json == null) throw new ArgumentNullException(nameof(json));
var keyStoreDocument = JsonSerializer.Deserialize<JsonDocument>(json);
if (keyStoreDocument == null) throw new SerializationException("could not process json");
var addrExist = keyStoreDocument.RootElement.TryGetProperty("address", out var address);
if (!addrExist) throw new JsonException("could not get address from json");
return address.GetString();
}
public static string GenerateUtcFileName(string address)
{
if (address == null) throw new ArgumentNullException(nameof(address));
return "utc--" + DateTime.UtcNow.ToString("O").Replace(":", "-") + "--" + address;
}
public byte[] DecryptKeyStoreFromFile(string password, string filePath)
{
if (password == null) throw new ArgumentNullException(nameof(password));
if (filePath == null) throw new ArgumentNullException(nameof(filePath));
using var file = File.OpenText(filePath);
var json = file.ReadToEnd();
return DecryptKeyStoreFromJson(password, json);
}
public byte[] DecryptKeyStoreFromJson(string password, string json)
{
if (password == null) throw new ArgumentNullException(nameof(password));
if (json == null) throw new ArgumentNullException(nameof(json));
var type = KeyStoreKdfChecker.GetKeyStoreKdfType(json);
return type switch
{
KdfType.Pbkdf2 => _keyStorePbkdf2Service.DecryptKeyStoreFromJson(password, json),
KdfType.Scrypt => _keyStoreScryptService.DecryptKeyStoreFromJson(password, json),
_ => throw new Exception("Invalid kdf type")
};
}
public string EncryptAndGenerateDefaultKeyStoreAsJson(string password, byte[] key, string address)
{
if (password == null) throw new ArgumentNullException(nameof(password));
if (address == null) throw new ArgumentNullException(nameof(address));
return _keyStoreScryptService.EncryptAndGenerateKeyStoreAsJson(password, key, address);
}
}
}