Skip to content

Commit 7bdbaa1

Browse files
authored
refactor persistence (#142)
* refactor persistence Signed-off-by: Kevin <kevin.dinh@lissi.id> * fix routing test Signed-off-by: Kevin <kevin.dinh@lissi.id> --------- Signed-off-by: Kevin <kevin.dinh@lissi.id>
1 parent c5e8be4 commit 7bdbaa1

File tree

9 files changed

+100
-89
lines changed

9 files changed

+100
-89
lines changed

src/Hyperledger.Aries/Storage/DefaultWalletRecordService.cs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Hyperledger.Indy.NonSecretsApi;
1111
using Hyperledger.Indy.WalletApi;
1212
using Newtonsoft.Json;
13-
using Newtonsoft.Json.Linq;
1413

1514
namespace Hyperledger.Aries.Storage
1615
{
@@ -34,7 +33,7 @@ public DefaultWalletRecordService()
3433
}
3534

3635
/// <inheritdoc />
37-
public virtual Task AddAsync<T>(Wallet wallet, T record, Func<T, JObject>? encode = null) where T : RecordBase, new()
36+
public virtual Task AddAsync<T>(Wallet wallet, T record) where T : RecordBase, new()
3837
{
3938
record.CreatedAtUtc = DateTime.UtcNow;
4039

@@ -49,9 +48,7 @@ public DefaultWalletRecordService()
4948
record.SetTag(property.Name, value.ToString(), false);
5049
}
5150

52-
var recordJson = encode is null
53-
? record.ToJson(_jsonSettings)
54-
: encode(record).ToString();
51+
var recordJson = record.ToJson(_jsonSettings);
5552

5653
return NonSecrets.AddRecordAsync(wallet,
5754
record.TypeName,
@@ -66,8 +63,7 @@ public virtual async Task<List<T>> SearchAsync<T>(
6663
ISearchQuery? query = null,
6764
SearchOptions? options = null,
6865
int count = 10,
69-
int skip = 0,
70-
Func<JObject, T>? decode = null) where T : RecordBase, new()
66+
int skip = 0) where T : RecordBase, new()
7167
{
7268
using var search = await NonSecrets.OpenSearchAsync(
7369
wallet,
@@ -90,16 +86,7 @@ public virtual async Task<List<T>> SearchAsync<T>(
9086

9187
var records = searchResult.Records.Select(searchItem =>
9288
{
93-
T record;
94-
if (decode is null)
95-
{
96-
record = JsonConvert.DeserializeObject<T>(searchItem.Value, _jsonSettings)!;
97-
}
98-
else
99-
{
100-
var json = JObject.Parse(searchItem.Value);
101-
record = decode(json);
102-
}
89+
var record = JsonConvert.DeserializeObject<T>(searchItem.Value, _jsonSettings)!;
10390

10491
foreach (var tag in searchItem.Tags)
10592
record.Tags[tag.Key] = tag.Value;
@@ -126,13 +113,11 @@ await NonSecrets.UpdateRecordTagsAsync(wallet,
126113
record.Tags.ToJson(_jsonSettings));
127114
}
128115

129-
public async Task Update<T>(Wallet wallet, T record, Func<T, JObject>? encode = null) where T : RecordBase
116+
public async Task Update<T>(Wallet wallet, T record) where T : RecordBase
130117
{
131118
record.UpdatedAtUtc = DateTime.UtcNow;
132119

133-
var recordJson = encode is null
134-
? record.ToJson(_jsonSettings)
135-
: encode(record).ToString();
120+
var recordJson = record.ToJson(_jsonSettings);
136121

137122
await NonSecrets.UpdateRecordValueAsync(wallet,
138123
record.TypeName,
@@ -146,7 +131,7 @@ await NonSecrets.UpdateRecordTagsAsync(wallet,
146131
}
147132

148133
/// <inheritdoc />
149-
public async Task<T?> GetAsync<T>(Wallet wallet, string id, Func<JObject, T>? decode = null) where T : RecordBase, new()
134+
public async Task<T?> GetAsync<T>(Wallet wallet, string id) where T : RecordBase, new()
150135
{
151136
try
152137
{
@@ -162,16 +147,7 @@ await NonSecrets.UpdateRecordTagsAsync(wallet,
162147

163148
var item = JsonConvert.DeserializeObject<SearchItem>(searchItemJson, _jsonSettings)!;
164149

165-
T record;
166-
if (decode is null)
167-
{
168-
record = JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings)!;
169-
}
170-
else
171-
{
172-
var json = JObject.Parse(item.Value);
173-
record = decode(json);
174-
}
150+
var record = JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings)!;
175151

176152
foreach (var tag in item.Tags)
177153
record.Tags[tag.Key] = tag.Value;
@@ -190,7 +166,7 @@ record = decode(json);
190166
try
191167
{
192168
var record = await GetAsync<T>(wallet, id);
193-
var typeName = record.TypeName;
169+
var typeName = record!.TypeName;
194170

195171
await NonSecrets.DeleteRecordTagsAsync(
196172
wallet: wallet,

src/Hyperledger.Aries/Storage/IWalletRecordService.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using Hyperledger.Indy.WalletApi;
5-
using Newtonsoft.Json.Linq;
64

75
namespace Hyperledger.Aries.Storage
86
{
@@ -17,9 +15,8 @@ public interface IWalletRecordService
1715
/// <returns>The record async.</returns>
1816
/// <param name="wallet">Wallet.</param>
1917
/// <param name="record">Record.</param>
20-
/// <param name="encode">The func for encoding the record to JSON format</param>
2118
/// <typeparam name="T">The 1st type parameter.</typeparam>
22-
Task AddAsync<T>(Wallet wallet, T record, Func<T, JObject>? encode = null) where T : RecordBase, new();
19+
Task AddAsync<T>(Wallet wallet, T record) where T : RecordBase, new();
2320

2421
/// <summary>
2522
/// Searches the records async.
@@ -30,15 +27,13 @@ public interface IWalletRecordService
3027
/// <param name="options">Options.</param>
3128
/// <param name="count">The number of items to return</param>
3229
/// <param name="skip">The number of items to skip</param>
33-
/// <param name="decode">Func for decoding the JSON to the record</param>
3430
/// <typeparam name="T">The 1st type parameter.</typeparam>
3531
Task<List<T>> SearchAsync<T>(
3632
Wallet wallet,
3733
ISearchQuery? query = null,
3834
SearchOptions? options = null,
3935
int count = 10,
40-
int skip = 0,
41-
Func<JObject, T>? decode = null) where T : RecordBase, new();
36+
int skip = 0) where T : RecordBase, new();
4237

4338
/// <summary>
4439
/// Updates the record async.
@@ -54,18 +49,16 @@ Task<List<T>> SearchAsync<T>(
5449
/// <returns>The record async.</returns>
5550
/// <param name="wallet">Wallet.</param>
5651
/// <param name="record">Credential record.</param>
57-
/// <param name="encode">The func for encoding the record to JSON format</param>
58-
Task Update<T>(Wallet wallet, T record, Func<T, JObject>? encode = null) where T : RecordBase;
52+
Task Update<T>(Wallet wallet, T record) where T : RecordBase;
5953

6054
/// <summary>
6155
/// Gets the record async.
6256
/// </summary>
6357
/// <returns>The record async.</returns>
6458
/// <param name="wallet">Wallet.</param>
6559
/// <param name="id">Identifier.</param>
66-
/// <param name="decode">Func for decoding the JSON to the record</param>
6760
/// <typeparam name="T">The 1st type parameter.</typeparam>
68-
Task<T?> GetAsync<T>(Wallet wallet, string id, Func<JObject, T>? decode = null) where T : RecordBase, new();
61+
Task<T?> GetAsync<T>(Wallet wallet, string id) where T : RecordBase, new();
6962

7063
/// <summary>
7164
/// Deletes the record async.

src/WalletFramework.MdocVc/MdocRecord.cs

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Hyperledger.Aries.Storage.Models;
33
using Hyperledger.Aries.Storage.Models.Interfaces;
44
using LanguageExt;
5+
using Newtonsoft.Json;
56
using Newtonsoft.Json.Linq;
67
using WalletFramework.Core.Credentials;
78
using WalletFramework.Core.Functional;
@@ -11,6 +12,7 @@
1112

1213
namespace WalletFramework.MdocVc;
1314

15+
[JsonConverter(typeof(MdocRecordJsonConverter))]
1416
public sealed class MdocRecord : RecordBase, ICredential
1517
{
1618
public CredentialId CredentialId
@@ -20,12 +22,11 @@ public CredentialId CredentialId
2022
.UnwrapOrThrow(new InvalidOperationException("The Id is corrupt"));
2123
private set => Id = value;
2224
}
23-
25+
26+
[RecordTag] public DocType DocType => Mdoc.DocType;
27+
2428
public Mdoc Mdoc { get; }
25-
26-
[RecordTag]
27-
public DocType DocType => Mdoc.DocType;
28-
29+
2930
public Option<List<MdocDisplay>> Displays { get; }
3031

3132
public override string TypeName => "WF.MdocRecord";
@@ -46,36 +47,35 @@ public MdocRecord()
4647
public static implicit operator Mdoc(MdocRecord record) => record.Mdoc;
4748
}
4849

49-
public static class MdocRecordFun
50+
public class MdocRecordJsonConverter : JsonConverter<MdocRecord>
5051
{
51-
public const string MdocJsonKey = "mdoc";
52-
private const string MdocDisplaysJsonKey = "displays";
53-
54-
public static JObject EncodeToJson(this MdocRecord record)
52+
public override MdocRecord ReadJson(
53+
JsonReader reader,
54+
Type objectType,
55+
MdocRecord? existingValue,
56+
bool hasExistingValue,
57+
JsonSerializer serializer)
5558
{
56-
var result = new JObject
57-
{
58-
{nameof(RecordBase.Id), record.Id},
59-
{MdocJsonKey, record.Mdoc.Encode()}
60-
};
61-
62-
record.Displays.IfSome(displays =>
63-
{
64-
var displaysJson = new JArray();
65-
foreach (var display in displays)
66-
{
67-
displaysJson.Add(display.EncodeToJson());
68-
}
69-
result.Add(MdocDisplaysJsonKey, displaysJson);
70-
});
59+
var json = JObject.Load(reader);
60+
return DecodeFromJson(json);
61+
}
7162

72-
return result;
63+
public override void WriteJson(JsonWriter writer, MdocRecord? value, JsonSerializer serializer)
64+
{
65+
var json = value!.EncodeToJson();
66+
json.WriteTo(writer);
7367
}
74-
68+
}
69+
70+
public static class MdocRecordFun
71+
{
72+
private const string MdocDisplaysJsonKey = "displays";
73+
public const string MdocJsonKey = "mdoc";
74+
7575
public static MdocRecord DecodeFromJson(JObject json)
7676
{
7777
var id = json[nameof(RecordBase.Id)]!.ToString();
78-
78+
7979
var mdocStr = json[MdocJsonKey]!.ToString();
8080
var mdoc = Mdoc
8181
.ValidMdoc(mdocStr)
@@ -86,7 +86,7 @@ from jToken in json.GetByKey(MdocDisplaysJsonKey).ToOption()
8686
from jArray in jToken.ToJArray().ToOption()
8787
from mdocDisplays in MdocDisplayFun.DecodeFromJson(jArray)
8888
select mdocDisplays;
89-
89+
9090
var result = new MdocRecord(mdoc, displays)
9191
{
9292
Id = id
@@ -95,5 +95,27 @@ from mdocDisplays in MdocDisplayFun.DecodeFromJson(jArray)
9595
return result;
9696
}
9797

98+
public static JObject EncodeToJson(this MdocRecord record)
99+
{
100+
var result = new JObject
101+
{
102+
{ nameof(RecordBase.Id), record.Id },
103+
{ MdocJsonKey, record.Mdoc.Encode() }
104+
};
105+
106+
record.Displays.IfSome(displays =>
107+
{
108+
var displaysJson = new JArray();
109+
foreach (var display in displays)
110+
{
111+
displaysJson.Add(display.EncodeToJson());
112+
}
113+
114+
result.Add(MdocDisplaysJsonKey, displaysJson);
115+
});
116+
117+
return result;
118+
}
119+
98120
public static MdocRecord ToRecord(this Mdoc mdoc, Option<List<MdocDisplay>> displays) => new(mdoc, displays);
99121
}

src/WalletFramework.Oid4Vc/Oid4Vci/AuthFlow/Implementations/AuthFlowSessionStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public async Task<string> StoreAsync(
3333
authorizationCodeParameters,
3434
authFlowSessionState);
3535

36-
await _recordService.AddAsync(agentContext.Wallet, record, AuthFlowSessionRecordFun.EncodeToJson);
36+
await _recordService.AddAsync(agentContext.Wallet, record);
3737

3838
return record.Id;
3939
}
4040

4141
/// <inheritdoc />
4242
public async Task<AuthFlowSessionRecord> GetAsync(IAgentContext context, AuthFlowSessionState authFlowSessionState)
4343
{
44-
var record = await _recordService.GetAsync(context.Wallet, authFlowSessionState, AuthFlowSessionRecordFun.DecodeFromJson);
44+
var record = await _recordService.GetAsync<AuthFlowSessionRecord>(context.Wallet, authFlowSessionState);
4545
return record!;
4646
}
4747

src/WalletFramework.Oid4Vc/Oid4Vci/AuthFlow/Records/AuthFlowSessionRecord.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace WalletFramework.Oid4Vc.Oid4Vci.AuthFlow.Records;
99
/// <summary>
1010
/// Represents the authorization session record. Used during the VCI Authorization Code Flow to hold session relevant information.
1111
/// </summary>
12+
[JsonConverter(typeof(AuthFlowSessionRecordConverter))]
1213
public sealed class AuthFlowSessionRecord : RecordBase
1314
{
1415
/// <summary>
@@ -69,6 +70,26 @@ public AuthFlowSessionRecord(
6970
}
7071
}
7172

73+
public class AuthFlowSessionRecordConverter : JsonConverter<AuthFlowSessionRecord>
74+
{
75+
public override void WriteJson(JsonWriter writer, AuthFlowSessionRecord? value, JsonSerializer serializer)
76+
{
77+
var json = value!.EncodeToJson();
78+
json.WriteTo(writer);
79+
}
80+
81+
public override AuthFlowSessionRecord ReadJson(
82+
JsonReader reader,
83+
Type objectType,
84+
AuthFlowSessionRecord? existingValue,
85+
bool hasExistingValue,
86+
JsonSerializer serializer)
87+
{
88+
var json = JObject.Load(reader);
89+
return AuthFlowSessionRecordFun.DecodeFromJson(json);
90+
}
91+
}
92+
7293
public static class AuthFlowSessionRecordFun
7394
{
7495
private const string AuthorizationDataJsonKey = "authorization_data";

src/WalletFramework.Oid4Vc/Oid4Vci/Implementations/MdocStorage.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public MdocStorage(IAgentProvider agentProvider, IWalletRecordService recordServ
2222
public async Task<Unit> Add(MdocRecord record)
2323
{
2424
var context = await _agentProvider.GetContextAsync();
25-
await _recordService.AddAsync(context.Wallet, record, MdocRecordFun.EncodeToJson);
25+
await _recordService.AddAsync(context.Wallet, record);
2626
return Unit.Default;
2727
}
2828

2929
public async Task<Option<MdocRecord>> Get(CredentialId id)
3030
{
3131
var context = await _agentProvider.GetContextAsync();
32-
return await _recordService.GetAsync(context.Wallet, id, MdocRecordFun.DecodeFromJson);
32+
return await _recordService.GetAsync<MdocRecord>(context.Wallet, id);
3333
}
3434

3535
public async Task<Option<IEnumerable<MdocRecord>>> List(
@@ -38,13 +38,12 @@ public async Task<Option<IEnumerable<MdocRecord>>> List(
3838
int skip = 0)
3939
{
4040
var context = await _agentProvider.GetContextAsync();
41-
var list = await _recordService.SearchAsync(
41+
var list = await _recordService.SearchAsync<MdocRecord>(
4242
context.Wallet,
4343
query.ToNullable(),
4444
null,
4545
count,
46-
skip,
47-
MdocRecordFun.DecodeFromJson);
46+
skip);
4847

4948
if (list.Count == 0)
5049
return Option<IEnumerable<MdocRecord>>.None;
@@ -55,7 +54,7 @@ public async Task<Option<IEnumerable<MdocRecord>>> List(
5554
public async Task<Unit> Update(MdocRecord record)
5655
{
5756
var context = await _agentProvider.GetContextAsync();
58-
await _recordService.Update(context.Wallet, record, MdocRecordFun.EncodeToJson);
57+
await _recordService.Update(context.Wallet, record);
5958
return Unit.Default;
6059
}
6160

0 commit comments

Comments
 (0)