Skip to content

Commit 3eeca4b

Browse files
committed
UNTESTED and missing PublicKeyExtensions implementation
1 parent f3b39f5 commit 3eeca4b

13 files changed

+284
-16
lines changed

InterlockLedger.Rest.Client/Abstractions/RestAbstractNode.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ protected internal async Task<string> CallApiPlainDocAsync(string url, string me
157157
return (ulong.Parse(resp.Headers["x-app-id"].WithDefault("0")), ulong.Parse(resp.Headers["x-payload-type-id"].WithDefault("0")), createdAt, resp.GetResponseStream());
158158
}
159159

160-
protected internal async Task<TR?> PostAsync<TR>(string url, object? body)
161-
=> Deserialize<TR>(await GetStringResponseAsync(PreparePostRequest(url, body, accept: "application/json")).ConfigureAwait(false));
160+
protected internal async Task<TR?> PostAsync<TR>(string url, object? body, string[]? extraHeaders = null)
161+
=> Deserialize<TR>(await GetStringResponseAsync(PreparePostRequest(url, body, accept: "application/json", extraHeaders)).ConfigureAwait(false));
162162

163163
protected internal async Task<TR?> PostRawAsync<TR>(string url, byte[] body, string contentType)
164164
=> Deserialize<TR>(await GetStringResponseAsync(PreparePostRawRequest(url, body, accept: "application/json", contentType)).ConfigureAwait(false));
@@ -251,9 +251,12 @@ private HttpWebRequest PreparePostRawRequest(string url, byte[] body, string acc
251251
return request;
252252
}
253253

254-
private HttpWebRequest PreparePostRequest(string url, object? body, string accept) {
254+
private HttpWebRequest PreparePostRequest(string url, object? body, string accept, string[]? extraHeaders) {
255255
var request = PrepareRequest(url, "POST", accept);
256256
request.ContentType = "application/json; charset=utf-8";
257+
if (extraHeaders != null)
258+
foreach (var extraHeader in extraHeaders)
259+
request.Headers.Add(extraHeader);
257260
using (var stream = request.GetRequestStream()) {
258261
var json = JsonSerializer.Serialize(body, Globals.JsonSettings);
259262
using var writer = new StreamWriter(stream, _utf8WithoutBOM);

InterlockLedger.Rest.Client/ConnectionString.cs

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
using System.ComponentModel;
3434
using System.Diagnostics.CodeAnalysis;
3535
using System.Globalization;
36-
using System.Net;
37-
using System.Xml.Linq;
3836

3937
namespace InterlockLedger.Rest.Client;
4038

InterlockLedger.Rest.Client/InterlockLedger.Rest.Client.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>net8.0</TargetFrameworks>
55
<LangVersion>preview</LangVersion>
6-
<Version>15.3.0</Version>
6+
<Version>16.0.0</Version>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Copyright>Copyright (c) 2018-2024 InterlockLedger Network</Copyright>
99
<Description>
@@ -14,7 +14,7 @@
1414
<RepositoryType>git</RepositoryType>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<PackageTags>REST Client InterlockLedger</PackageTags>
17-
<PackageReleaseNotes>DRYed ConnectionString - also fixed it to require 'https' scheme</PackageReleaseNotes>
17+
<PackageReleaseNotes>Add methods to JsonStore, to actually store some data</PackageReleaseNotes>
1818
<PackageIcon>il2.png</PackageIcon>
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
2020
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace InterlockLedger.Rest.Client;
34+
35+
public class JsonConverterFor<T> : JsonConverter<T> where T : class, IParsable<T>
36+
{
37+
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
38+
T.TryParse(reader.GetString(), null, out var recordReference) ? recordReference : null;
39+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
40+
writer.WriteStringValue(value?.ToString());
41+
}

InterlockLedger.Rest.Client/Models/RecordModelAsJson.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace InterlockLedger.Rest.Client;
3838
public class RecordModelAsJson : RecordModelBase
3939
{
4040
/// <summary>
41-
/// The payload's bytes
41+
/// The payload's json representation
4242
/// </summary>
4343
public object? Payload { get; set; }
4444
public override string ToString() => JsonSerializer.Serialize(this, Globals.JsonSettings);

InterlockLedger.Rest.Client/Models/RecordModelBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public abstract class RecordModelBase
6565
/// <summary>
6666
/// Record universal reference [Network]:[ChainId]@[Serial]
6767
/// </summary>
68-
public required string Reference { get; set; }
68+
public required UniversalRecordReference Reference { get; set; }
6969

7070
/// <summary>
7171
/// Record serial number.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
using System.Diagnostics.CodeAnalysis;
34+
35+
namespace InterlockLedger.Rest.Client;
36+
37+
/// <summary>
38+
/// Network-local reference to a record
39+
/// </summary>
40+
/// <param name="chainId">Id of the chain containing the record</param>
41+
/// <param name="serial">Serial numbr of the record in the chain (zero-based)</param>
42+
[JsonConverter(typeof(JsonConverterFor<RecordReference>))]
43+
public class RecordReference(string chainId, ulong serial) : IParsable<RecordReference>
44+
{
45+
public static RecordReference Parse(string s, IFormatProvider? provider) => TryParse(s, provider, out var result) ? result : throw new FormatException($"Invalid string '{s}' to parse as a RecordReference");
46+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out RecordReference result) {
47+
if (!string.IsNullOrWhiteSpace(s)) {
48+
var parts = s.Split(_separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
49+
if (parts.Length == 2 && ulong.TryParse(parts[1], provider, out ulong serial)) {
50+
result = new RecordReference(parts[0], serial);
51+
return true;
52+
}
53+
}
54+
result = null;
55+
return false;
56+
}
57+
public override string ToString() => $"{ChainId}{_separator}{Serial:0}";
58+
private const char _separator = '@';
59+
60+
public string ChainId { get; } = chainId;
61+
public ulong Serial { get; } = serial;
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
using System.Diagnostics.CodeAnalysis;
34+
35+
namespace InterlockLedger.Rest.Client;
36+
37+
/// <summary>
38+
/// Universal reference to a record
39+
/// </summary>
40+
/// <param name="network">Network name where the chain is located</param>
41+
/// <param name="chainId"><inheritdoc/></param>
42+
/// <param name="serial"><inheritdoc/></param>
43+
[JsonConverter(typeof(JsonConverterFor<UniversalRecordReference>))]
44+
public class UniversalRecordReference(string network, string chainId, ulong serial) : RecordReference(chainId, serial), IParsable<UniversalRecordReference>
45+
{
46+
public string Network { get; } = network;
47+
48+
private UniversalRecordReference(string network, RecordReference recordReference) : this(network, recordReference.ChainId, recordReference.Serial) { }
49+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out UniversalRecordReference result) {
50+
if (!string.IsNullOrWhiteSpace(s)) {
51+
var parts = s.Split(_separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
52+
if (parts.Length == 2 && RecordReference.TryParse(parts[1], provider, out var recordReference)) {
53+
result = new UniversalRecordReference(parts[0], recordReference);
54+
return true;
55+
}
56+
}
57+
result = null;
58+
return false;
59+
}
60+
static UniversalRecordReference IParsable<UniversalRecordReference>.Parse(string s, IFormatProvider? provider) =>
61+
TryParse(s, provider, out var result) ? result : throw new FormatException($"Invalid string '{s}' to parse as an UniversalRecordReference");
62+
63+
private const char _separator = ':';
64+
65+
}

InterlockLedger.Rest.Client/V14_2_2/Implementations/JsonStoreImplementation.cs

+17
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ public JsonStoreImplementation(RestChainV14_2_2 parent) {
4545

4646
public Task<PageOfAllowedReadersRecordModel?> RetrieveAllowedReadersAsync(string chain, string? contextId = null, bool lastToFirst = false, int page = 0, int pageSize = 10)
4747
=> _node.GetAsync<PageOfAllowedReadersRecordModel>($"/jsonDocuments@{_id}/allow?lastToFirst={lastToFirst}&page={page}&pageSize={pageSize}&contextId={contextId}");
48+
public Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument)
49+
=> _node.PostAsync<JsonDocumentModel>($"/jsonDocuments@{_id}/", jsonDocument);
50+
public Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, PublicKey readerKey, string readerKeyId)
51+
=> _node.PostAsync<JsonDocumentModel>($"/jsonDocuments@{_id}/withKey",
52+
jsonDocument,
53+
extraHeaders: [$"X-PubKey: {readerKey.ToInterlockLedgerPublicKeyRepresentation()}", $"X-PubKeyId: {readerKeyId}"]);
54+
public Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, RecordReference[] allowedReadersReferences) =>
55+
_node.PostAsync<JsonDocumentModel>($"/jsonDocuments@{_id}/withIndirectKeys",
56+
jsonDocument,
57+
extraHeaders: [$"X-PubKeyReferences: {allowedReadersReferences.NonEmpty().JoinedBy(",")}"]);
58+
59+
public Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, string[] idOfChainsWithAllowedReaders)
60+
=> _node.PostAsync<JsonDocumentModel>($"/jsonDocuments@{_id}/withChainKeys",
61+
jsonDocument,
62+
extraHeaders: [$"X-PubKeyChains: {idOfChainsWithAllowedReaders.NonEmpty().JoinedBy(",")}"]);
63+
public Task<AllowedReadersRecordModel?> AddAllowedReadersAsync(AllowedReadersModel allowedReaders)
64+
=> _node.PostAsync<AllowedReadersRecordModel>($"/jsonDocuments@{_id}/allow", allowedReaders);
4865

4966
private readonly string _id;
5067
private readonly RestChainV14_2_2 _parent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace InterlockLedger.Rest.Client.V14_2_2;
34+
35+
public static class PublicKeyExtensions
36+
{
37+
public static string ToInterlockLedgerPublicKeyRepresentation(this PublicKey publicKey) => throw new NotImplementedException();
38+
}

InterlockLedger.Rest.Client/V14_2_2/Interfaces/IJsonStore.cs

+6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@
3232

3333
namespace InterlockLedger.Rest.Client.V14_2_2;
3434

35+
3536
public interface IJsonStore
3637
{
38+
Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument);
39+
Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, PublicKey readerKey, string readerKeyId);
40+
Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, RecordReference[] allowedReadersReferences);
41+
Task<JsonDocumentModel?> AddAsync<T>(T jsonDocument, string[] idOfChainsWithAllowedReaders);
3742
Task<JsonDocumentModel?> RetrieveAsync(ulong serial);
3843

44+
Task<AllowedReadersRecordModel?> AddAllowedReadersAsync(AllowedReadersModel allowedReaders);
3945
Task<PageOfAllowedReadersRecordModel?> RetrieveAllowedReadersAsync(string chain, string? contextId = null, bool lastToFirst = false, int page = 0, int pageSize = 10);
4046
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace InterlockLedger.Rest.Client.V14_2_2;
34+
35+
/// <summary>
36+
/// Model to insert sets of allowed readers
37+
/// </summary>
38+
/// <param name="ContextId">[Optional] Context identifier, to group multiple sets</param>
39+
/// <param name="Readers">List of readers</param>
40+
public record AllowedReadersModel(string? ContextId, IEnumerable<ReaderModel> Readers);

0 commit comments

Comments
 (0)