Skip to content

Commit 7739b79

Browse files
committed
v0.18.1: TryNormalizeName for RootDnsRecipes, documentation file
1 parent 2fac981 commit 7739b79

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

TonLibDotNet/Recipes/RootDnsRecipes_Read.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Globalization;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Globalization;
23
using System.Numerics;
4+
using System.Xml.Linq;
35
using TonLibDotNet.Types.Dns;
46
using TonLibDotNet.Types.Smc;
57
using TonLibDotNet.Types.Tvm;
@@ -10,37 +12,60 @@ namespace TonLibDotNet.Recipes
1012
public partial class RootDnsRecipes
1113
{
1214
/// <summary>
13-
/// Computes 'index' value for NFT with specified name.
15+
/// Tries to normalize domain name to form used by Collection.
1416
/// </summary>
15-
/// <param name="domainName">Domain name to resolve, with or without '.ton' suffix.</param>
16-
/// <returns><b>byte[8]</b> with required value (<i>cell_hash(domain)</i>, according to <see href="https://github.com/ton-blockchain/dns-contract/blob/main/func/nft-collection.fc#L133">contract source</see>).</returns>
17+
/// <param name="name">Domain name to normalize.</param>
18+
/// <param name="normalizedName">Normalized domain name, if possible.</param>
19+
/// <returns>Returns <b>true</b> when name had been successfully normalized, and <b>false</b> otherwise.</returns>
1720
/// <remarks>
18-
/// <para>Only second-level .ton domains are allowed, with or without '.ton' suffix <br/>
21+
/// <para>Only second-level .ton domains are "valid", with or without '.ton' suffix <br/>
1922
/// <i>(e.g. 'alice.ton.', 'alice.ton' and 'alice' are allowed, but 'alice.t.me' or 'thisis.alice.ton' is not)</i>.</para>
2023
/// </remarks>
2124
/// <exception cref="ArgumentNullException">Requested <paramref name="domainName"/> null or empty.</exception>
22-
/// <exception cref="ArgumentOutOfRangeException">Requested <paramref name="domainName"/> is not second-level one, or not from '.t.me' namespace, or too long.</exception>
23-
public byte[] GetNftIndex(string domainName)
25+
public bool TryNormalizeName(string domainName, [NotNullWhen(true)] out string normalizedName)
2426
{
2527
if (string.IsNullOrEmpty(domainName))
2628
{
2729
throw new ArgumentNullException(nameof(domainName));
2830
}
2931

30-
var parts = domainName.ToLowerInvariant().Split('.', StringSplitOptions.RemoveEmptyEntries);
32+
normalizedName = string.Empty;
33+
34+
var parts = domainName.Trim().ToLowerInvariant().Split('.', StringSplitOptions.RemoveEmptyEntries);
3135

3236
if (parts.Length > 2)
3337
{
34-
throw new ArgumentOutOfRangeException(nameof(domainName), "Only second-level domains (e.g. 'alice.ton') are supported.");
38+
return false;
3539
}
3640

3741
if (parts.Length == 2 && !string.Equals(parts[1], "ton", StringComparison.InvariantCulture))
3842
{
39-
throw new ArgumentOutOfRangeException(nameof(domainName), "Only '.ton' domains (e.g. 'alice.ton') are supported.");
43+
return false;
44+
}
45+
46+
normalizedName = parts[0];
47+
return true;
48+
}
49+
50+
/// <summary>
51+
/// Computes 'index' value for NFT with specified name.
52+
/// </summary>
53+
/// <param name="domainName">Domain name to resolve, with or without '.ton' suffix.</param>
54+
/// <returns><b>byte[8]</b> with required value (<i>cell_hash(domain)</i>, according to <see href="https://github.com/ton-blockchain/dns-contract/blob/main/func/nft-collection.fc#L133">contract source</see>).</returns>
55+
/// <remarks>
56+
/// <para>Check overriden <see cref="TryNormalizeName">TryNormalizeName</see> docs for 'valid' vs 'invalid' names description.</para>
57+
/// </remarks>
58+
/// <exception cref="ArgumentNullException">Requested <paramref name="domainName"/> null or empty.</exception>
59+
/// <exception cref="ArgumentOutOfRangeException">Requested <paramref name="domainName"/> is too long.</exception>
60+
public byte[] GetNftIndex(string domainName)
61+
{
62+
if (!TryNormalizeName(domainName, out var normalizedName))
63+
{
64+
throw new ArgumentOutOfRangeException(nameof(domainName), "Invalid domain name.");
4065
}
4166

4267
// UTF-8 encoded string up to 126 bytes, https://github.com/ton-blockchain/TEPs/blob/master/text/0081-dns-standard.md#domain-names
43-
var bytes = System.Text.Encoding.UTF8.GetBytes(parts[0]);
68+
var bytes = System.Text.Encoding.UTF8.GetBytes(normalizedName);
4469
if (bytes.Length > 126)
4570
{
4671
throw new ArgumentOutOfRangeException(nameof(domainName), "Value is too long. No more than 126 chars are allowed.");
@@ -296,11 +321,8 @@ public async Task<DnsEntries> GetEntries(ITonClient tonClient, string nftAddress
296321
/// <remarks>
297322
/// <para>DNS Item contract must be deployed and active (to execute get-method).</para>
298323
/// <para>⚠ Will work only for <see href="https://github.com/ton-blockchain/dns-contract/blob/main/func/nft-item.fc">DNS Item smartcontract</see>. May fail if future version will change stored data layout.</para>
299-
/// <para>Only second-level .ton domains are allowed, with or without '.ton' suffix <br/>
300-
/// <i>(e.g. 'alice.ton.', 'alice.ton' and 'alice' are allowed, but 'alice.t.me' or 'thisis.alice.ton' is not)</i>.</para>
324+
/// <para>Check overriden <see cref="TryNormalizeName">TryNormalizeName</see> docs for 'valid' vs 'invalid' names description.</para>
301325
/// </remarks>
302-
/// <exception cref="ArgumentOutOfRangeException">Requested <paramref name="domainName"/> is not second-level one, or not from '.ton' namespace.</exception>
303-
/// <exception cref="ArgumentNullException">Requested <paramref name="domainName"/> null or white-space only.</exception>
304326
public async Task<DomainInfo> GetAllInfoByName(ITonClient tonClient, string domainName)
305327
{
306328
var address = await GetNftAddress(tonClient, domainName).ConfigureAwait(false);

TonLibDotNet/TonLibDotNet.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>ton, toncoin, the-open-network, tonlib, tonlibjson</PackageTags>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
19-
<Version>0.18.0</Version>
19+
<Version>0.18.1</Version>
2020
<PackageReleaseNotes>`TonRecipes.Telemint` replaced with two (`TonRecipes.TelegramUsernames` and `TonRecipes.TelegramNumbers`) to work with both .t.me domains and +888 anonymous numbers.</PackageReleaseNotes>
2121
<Description>TonLib (tonlibjson) wrapper for accessing Telegram Open Network lite servers (nodes) via ADNL protocol.</Description>
22+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
2223
</PropertyGroup>
2324

2425
<ItemGroup>

0 commit comments

Comments
 (0)