-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTonProofService.cs
207 lines (165 loc) · 7.32 KB
/
TonProofService.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSec.Cryptography;
using TonProof.Extensions;
using TonProof.Types;
using TonLibDotNet;
using TonLibDotNet.Cells;
using TonLibDotNet.Utils;
namespace TonProof;
/// <inheritdoc/>
public class TonProofService : ITonProofService
{
#region Private Fields
private readonly ITonClient tonClient;
private readonly IPublicKeyProvider publicKeyProvider;
private readonly TonProofOptions options;
private readonly ILogger<TonProofService> logger;
private readonly byte[] tonConnectPrefixBytes;
private readonly byte[] tonProofPrefixBytes;
#endregion
#region Constructors
public TonProofService(
ILogger<TonProofService> logger,
ITonClient tonClient,
IPublicKeyProvider publicKeyProvider,
IOptions<TonProofOptions> options)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.tonClient = tonClient ?? throw new ArgumentNullException(nameof(tonClient));
this.publicKeyProvider = publicKeyProvider ?? throw new ArgumentNullException(nameof(publicKeyProvider));
this.options = options.Value;
this.tonConnectPrefixBytes = Encoding.UTF8.GetBytes(this.options.TonConnectPrefix);
this.tonProofPrefixBytes = Encoding.UTF8.GetBytes(this.options.TonProofPrefix);
}
#endregion
#region Public Methods
/// <inheritdoc/>
public async Task<VerifyResult> VerifyAsync(CheckProofRequest request, CancellationToken cancellationToken = default)
{
var requestRaw = new CheckProofRequestRaw(request);
if (!requestRaw.Workchain.HasValue)
{
this.logger.LogDebug(
"Address {Address} is invalid. The address provided lacks the correct format and omits a workchain",
request.Address);
return VerifyResult.InvalidAddress;
}
if (requestRaw.InitState is null)
{
this.logger.LogDebug(
"The InitState {InitState} structure is invalid. This could indicate that the contract is not a well-known wallet",
request.Proof.StateInit);
return VerifyResult.InvalidInitState;
}
var isParsed = this.TryParseWalletPublicKey(requestRaw.InitState.Code, requestRaw.Data, out var walletPublicKey);
var publicKey = isParsed
? walletPublicKey
: await this.publicKeyProvider.GetPublicKeyAsync(requestRaw.Address, cancellationToken);
if (!requestRaw.PublicKey.Equals(publicKey, StringComparison.OrdinalIgnoreCase))
{
this.logger.LogDebug(
"Public key mismatch: provided public key {ProvidedPk} does not match the parsed or retrieved public key {RetrievedPk}",
requestRaw.PublicKey,
publicKey);
return VerifyResult.PublicKeyMismatch;
}
var wantedAddress = await this.tonClient.GetAccountAddress(requestRaw.InitState).ConfigureAwait(false);
if (!AddressUtils.Instance.AddressEquals(wantedAddress.Value, requestRaw.AddressBytes))
{
this.logger.LogDebug(
"Address mismatch: expected address {WantedAddress}, but got {Address}",
wantedAddress.Value,
requestRaw.Address);
return VerifyResult.AddressMismatch;
}
if (!this.options.AllowedDomains.Contains(requestRaw.Proof.Domain.Value))
{
this.logger.LogDebug("Domain not allowed: {Domain} is not in the list of allowed domains",
requestRaw.Proof.Domain.Value);
return VerifyResult.DomainNotAllowed;
}
var dateTime = DateTimeOffset.FromUnixTimeSeconds(requestRaw.Proof.Timestamp).UtcDateTime;
var proofDatetime = dateTime.AddSeconds(this.options.ValidAuthTime);
if (proofDatetime < DateTime.UtcNow)
{
this.logger.LogDebug(
"Proof expired: the proof DateTimes {ProofDateTime} is outside the allowed validity period: {ValidAuthTime} sec.",
proofDatetime,
this.options.ValidAuthTime);
return VerifyResult.ProofExpired;
}
var msg = this.CreateMessage(requestRaw);
var msgHash = SHA256.HashData(msg);
var algorithm = SignatureAlgorithm.Ed25519;
var pKey = PublicKey.Import(
algorithm,
Convert.FromHexString(requestRaw.PublicKey).AsSpan(),
KeyBlobFormat.RawPublicKey);
var result = algorithm.Verify(
pKey,
msgHash,
Convert.FromBase64String(requestRaw.Proof.Signature).AsSpan());
return result ? VerifyResult.Valid : VerifyResult.HashMismatch;
}
#endregion
#region Private Methods
private byte[] CreateMessage(CheckProofRequestRaw request)
{
Span<byte> wc = stackalloc byte[4];
BinaryPrimitives.WriteUInt32BigEndian(wc, request.Workchain!.Value);
Span<byte> ts = stackalloc byte[8];
BinaryPrimitives.WriteUInt64LittleEndian(ts, (ulong)request.Proof.Timestamp);
Span<byte> dl = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(dl, request.Proof.Domain.LengthBytes);
var domainBytes = Encoding.UTF8.GetBytes(request.Proof.Domain.Value);
var payloadBytes = Encoding.UTF8.GetBytes(request.Proof.Payload);
var totalLength = this.tonProofPrefixBytes.Length +
wc.Length + request.AddressBytes.Length +
dl.Length + domainBytes.Length +
ts.Length +
payloadBytes.Length;
// message = utf8_encode("ton-proof-item-v2/") ++
// Address ++
// AppDomain ++
// Timestamp ++
// Payload
var message = new byte[totalLength].AsSpan();
var offset = 0;
message.CopyFrom(this.tonProofPrefixBytes, ref offset);
message.CopyFrom(wc, ref offset);
message.CopyFrom(request.AddressBytes, ref offset);
message.CopyFrom(dl, ref offset);
message.CopyFrom(domainBytes, ref offset);
message.CopyFrom(ts, ref offset);
message.CopyFrom(payloadBytes, ref offset);
var msgHash = SHA256.HashData(message);
// fullMessage = 0xffff ++
// utf8_encode("ton-connect") ++
// sha256(message)
ReadOnlySpan<byte> ff = stackalloc byte[2] { 0xFF, 0xFF };
message = new byte[ff.Length + this.tonConnectPrefixBytes.Length + msgHash.Length].AsSpan();
offset = 0;
message.CopyFrom(ff, ref offset);
message.CopyFrom(this.tonConnectPrefixBytes, ref offset);
message.CopyFrom(msgHash, ref offset);
return message.ToArray();
}
private bool TryParseWalletPublicKey(string code, Cell data, [NotNullWhen(true)] out string publicKey)
{
if (this.options.KnownWallets.TryGetValue(code, out var knownWallet))
{
var wallet = knownWallet.Invoke();
publicKey = wallet.LoadPublicKey(data);
return true;
}
this.logger.LogDebug("Failed to parse wallet publicKey for unknown code: {Code}", code);
publicKey = null;
return false;
}
#endregion
}