-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFaucetSystem.cs
More file actions
178 lines (157 loc) · 4.31 KB
/
FaucetSystem.cs
File metadata and controls
178 lines (157 loc) · 4.31 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
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
// CypherNetwork by Matthew Hellyer is licensed under CC BY-NC-ND 4.0.
// To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-nd/4.0
using System.Security;
using Faucet.Cryptography;
using Faucet.Extensions;
using Faucet.Helpers;
using Faucet.Ledger;
using Faucet.Persistence;
using Faucet.Wallet;
using ILogger = Serilog.ILogger;
namespace Faucet;
/// <summary>
///
/// </summary>
public interface IFaucetSystem
{
IServiceScopeFactory ServiceScopeFactory { get; }
IUnitOfWork UnitOfWork();
ICrypto Crypto();
public SecureString PrivateKey { get; init; }
public byte[] PublicKey { get; init; }
IWallet Wallet();
IWalletSession WalletSession();
IBlockchain Blockchain();
int UserOnlineCount { get; set; }
}
/// <summary>
///
/// </summary>
public class FaucetSystem : IFaucetSystem
{
private readonly ILogger _logger;
private IUnitOfWork _unitOfWork;
private IWalletSession _walletSession;
private IBlockchain _blockchain;
private ICrypto _crypto;
public SecureString PrivateKey { get; init; }
public byte[] PublicKey { get; init; }
public IServiceScopeFactory ServiceScopeFactory { get; }
/// <summary>
///
/// </summary>
/// <param name="serviceScopeFactory"></param>
/// <param name="logger"></param>
public FaucetSystem(IServiceScopeFactory serviceScopeFactory, ILogger logger)
{
ServiceScopeFactory = serviceScopeFactory;
_logger = logger;
var keyPair = Crypto().GetOrUpsertKeyName().Result;
PrivateKey = keyPair.PrivateKey.ByteToHex().ToSecureString();
PublicKey = keyPair.PublicKey;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IBlockchain Blockchain()
{
if (_blockchain != null) return _blockchain;
using var scope = ServiceScopeFactory.CreateAsyncScope();
_blockchain = scope.ServiceProvider.GetRequiredService<IBlockchain>();
return _blockchain;
}
/// <summary>
///
/// </summary>
public int UserOnlineCount { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public IUnitOfWork UnitOfWork()
{
return _unitOfWork ??= GetUnitOfWork();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ICrypto Crypto()
{
try
{
if (_crypto != null) return _crypto;
using var scope = ServiceScopeFactory.CreateAsyncScope();
_crypto = scope.ServiceProvider.GetRequiredService<ICrypto>();
return _crypto;
}
catch (Exception ex)
{
_logger.Here().Error("{@Message}",ex.Message);
}
return null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IWallet Wallet()
{
try
{
using var scope = ServiceScopeFactory.CreateAsyncScope();
var wallet = scope.ServiceProvider.GetRequiredService<IWallet>();
return wallet;
}
catch (Exception ex)
{
_logger.Here().Error("{@Message}",ex.Message);
}
return null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IWalletSession WalletSession()
{
return _walletSession ??= GetWalletSession();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private IUnitOfWork GetUnitOfWork()
{
try
{
using var scope = ServiceScopeFactory.CreateScope();
var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
return unitOfWork;
}
catch (Exception ex)
{
_logger.Here().Error("{@Message}",ex.Message);
}
return null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private IWalletSession GetWalletSession()
{
try
{
using var scope = ServiceScopeFactory.CreateAsyncScope();
var walletSession = scope.ServiceProvider.GetRequiredService<IWalletSession>();
return walletSession;
}
catch (Exception ex)
{
_logger.Here().Error("{@Message}",ex.Message);
}
return null;
}
}