Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 9 additions & 33 deletions src/Hyperledger.Aries/Storage/DefaultWalletRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Hyperledger.Indy.NonSecretsApi;
using Hyperledger.Indy.WalletApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Hyperledger.Aries.Storage
{
Expand All @@ -34,7 +33,7 @@ public DefaultWalletRecordService()
}

/// <inheritdoc />
public virtual Task AddAsync<T>(Wallet wallet, T record, Func<T, JObject>? encode = null) where T : RecordBase, new()
public virtual Task AddAsync<T>(Wallet wallet, T record) where T : RecordBase, new()
{
record.CreatedAtUtc = DateTime.UtcNow;

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

var recordJson = encode is null
? record.ToJson(_jsonSettings)
: encode(record).ToString();
var recordJson = record.ToJson(_jsonSettings);

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

var records = searchResult.Records.Select(searchItem =>
{
T record;
if (decode is null)
{
record = JsonConvert.DeserializeObject<T>(searchItem.Value, _jsonSettings)!;
}
else
{
var json = JObject.Parse(searchItem.Value);
record = decode(json);
}
var record = JsonConvert.DeserializeObject<T>(searchItem.Value, _jsonSettings)!;

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

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

var recordJson = encode is null
? record.ToJson(_jsonSettings)
: encode(record).ToString();
var recordJson = record.ToJson(_jsonSettings);

await NonSecrets.UpdateRecordValueAsync(wallet,
record.TypeName,
Expand All @@ -146,7 +131,7 @@ await NonSecrets.UpdateRecordTagsAsync(wallet,
}

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

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

T record;
if (decode is null)
{
record = JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings)!;
}
else
{
var json = JObject.Parse(item.Value);
record = decode(json);
}
var record = JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings)!;

foreach (var tag in item.Tags)
record.Tags[tag.Key] = tag.Value;
Expand All @@ -190,7 +166,7 @@ record = decode(json);
try
{
var record = await GetAsync<T>(wallet, id);
var typeName = record.TypeName;
var typeName = record!.TypeName;

await NonSecrets.DeleteRecordTagsAsync(
wallet: wallet,
Expand Down
17 changes: 5 additions & 12 deletions src/Hyperledger.Aries/Storage/IWalletRecordService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Hyperledger.Indy.WalletApi;
using Newtonsoft.Json.Linq;

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

/// <summary>
/// Searches the records async.
Expand All @@ -30,15 +27,13 @@ public interface IWalletRecordService
/// <param name="options">Options.</param>
/// <param name="count">The number of items to return</param>
/// <param name="skip">The number of items to skip</param>
/// <param name="decode">Func for decoding the JSON to the record</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
Task<List<T>> SearchAsync<T>(
Wallet wallet,
ISearchQuery? query = null,
SearchOptions? options = null,
int count = 10,
int skip = 0,
Func<JObject, T>? decode = null) where T : RecordBase, new();
int skip = 0) where T : RecordBase, new();

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

/// <summary>
/// Gets the record async.
/// </summary>
/// <returns>The record async.</returns>
/// <param name="wallet">Wallet.</param>
/// <param name="id">Identifier.</param>
/// <param name="decode">Func for decoding the JSON to the record</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
Task<T?> GetAsync<T>(Wallet wallet, string id, Func<JObject, T>? decode = null) where T : RecordBase, new();
Task<T?> GetAsync<T>(Wallet wallet, string id) where T : RecordBase, new();

/// <summary>
/// Deletes the record async.
Expand Down
80 changes: 51 additions & 29 deletions src/WalletFramework.MdocVc/MdocRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hyperledger.Aries.Storage.Models;
using Hyperledger.Aries.Storage.Models.Interfaces;
using LanguageExt;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WalletFramework.Core.Credentials;
using WalletFramework.Core.Functional;
Expand All @@ -11,6 +12,7 @@

namespace WalletFramework.MdocVc;

[JsonConverter(typeof(MdocRecordJsonConverter))]
public sealed class MdocRecord : RecordBase, ICredential
{
public CredentialId CredentialId
Expand All @@ -20,12 +22,11 @@ public CredentialId CredentialId
.UnwrapOrThrow(new InvalidOperationException("The Id is corrupt"));
private set => Id = value;
}


[RecordTag] public DocType DocType => Mdoc.DocType;

public Mdoc Mdoc { get; }

[RecordTag]
public DocType DocType => Mdoc.DocType;


public Option<List<MdocDisplay>> Displays { get; }

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

public static class MdocRecordFun
public class MdocRecordJsonConverter : JsonConverter<MdocRecord>
{
public const string MdocJsonKey = "mdoc";
private const string MdocDisplaysJsonKey = "displays";

public static JObject EncodeToJson(this MdocRecord record)
public override MdocRecord ReadJson(
JsonReader reader,
Type objectType,
MdocRecord? existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
var result = new JObject
{
{nameof(RecordBase.Id), record.Id},
{MdocJsonKey, record.Mdoc.Encode()}
};

record.Displays.IfSome(displays =>
{
var displaysJson = new JArray();
foreach (var display in displays)
{
displaysJson.Add(display.EncodeToJson());
}
result.Add(MdocDisplaysJsonKey, displaysJson);
});
var json = JObject.Load(reader);
return DecodeFromJson(json);
}

return result;
public override void WriteJson(JsonWriter writer, MdocRecord? value, JsonSerializer serializer)
{
var json = value!.EncodeToJson();
json.WriteTo(writer);
}

}

public static class MdocRecordFun
{
private const string MdocDisplaysJsonKey = "displays";
public const string MdocJsonKey = "mdoc";

public static MdocRecord DecodeFromJson(JObject json)
{
var id = json[nameof(RecordBase.Id)]!.ToString();

var mdocStr = json[MdocJsonKey]!.ToString();
var mdoc = Mdoc
.ValidMdoc(mdocStr)
Expand All @@ -86,7 +86,7 @@ from jToken in json.GetByKey(MdocDisplaysJsonKey).ToOption()
from jArray in jToken.ToJArray().ToOption()
from mdocDisplays in MdocDisplayFun.DecodeFromJson(jArray)
select mdocDisplays;

var result = new MdocRecord(mdoc, displays)
{
Id = id
Expand All @@ -95,5 +95,27 @@ from mdocDisplays in MdocDisplayFun.DecodeFromJson(jArray)
return result;
}

public static JObject EncodeToJson(this MdocRecord record)
{
var result = new JObject
{
{ nameof(RecordBase.Id), record.Id },
{ MdocJsonKey, record.Mdoc.Encode() }
};

record.Displays.IfSome(displays =>
{
var displaysJson = new JArray();
foreach (var display in displays)
{
displaysJson.Add(display.EncodeToJson());
}

result.Add(MdocDisplaysJsonKey, displaysJson);
});

return result;
}

public static MdocRecord ToRecord(this Mdoc mdoc, Option<List<MdocDisplay>> displays) => new(mdoc, displays);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public async Task<string> StoreAsync(
authorizationCodeParameters,
authFlowSessionState);

await _recordService.AddAsync(agentContext.Wallet, record, AuthFlowSessionRecordFun.EncodeToJson);
await _recordService.AddAsync(agentContext.Wallet, record);

return record.Id;
}

/// <inheritdoc />
public async Task<AuthFlowSessionRecord> GetAsync(IAgentContext context, AuthFlowSessionState authFlowSessionState)
{
var record = await _recordService.GetAsync(context.Wallet, authFlowSessionState, AuthFlowSessionRecordFun.DecodeFromJson);
var record = await _recordService.GetAsync<AuthFlowSessionRecord>(context.Wallet, authFlowSessionState);
return record!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace WalletFramework.Oid4Vc.Oid4Vci.AuthFlow.Records;
/// <summary>
/// Represents the authorization session record. Used during the VCI Authorization Code Flow to hold session relevant information.
/// </summary>
[JsonConverter(typeof(AuthFlowSessionRecordConverter))]
public sealed class AuthFlowSessionRecord : RecordBase
{
/// <summary>
Expand Down Expand Up @@ -69,6 +70,26 @@ public AuthFlowSessionRecord(
}
}

public class AuthFlowSessionRecordConverter : JsonConverter<AuthFlowSessionRecord>
{
public override void WriteJson(JsonWriter writer, AuthFlowSessionRecord? value, JsonSerializer serializer)
{
var json = value!.EncodeToJson();
json.WriteTo(writer);
}

public override AuthFlowSessionRecord ReadJson(
JsonReader reader,
Type objectType,
AuthFlowSessionRecord? existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
var json = JObject.Load(reader);
return AuthFlowSessionRecordFun.DecodeFromJson(json);
}
}

public static class AuthFlowSessionRecordFun
{
private const string AuthorizationDataJsonKey = "authorization_data";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public MdocStorage(IAgentProvider agentProvider, IWalletRecordService recordServ
public async Task<Unit> Add(MdocRecord record)
{
var context = await _agentProvider.GetContextAsync();
await _recordService.AddAsync(context.Wallet, record, MdocRecordFun.EncodeToJson);
await _recordService.AddAsync(context.Wallet, record);
return Unit.Default;
}

public async Task<Option<MdocRecord>> Get(CredentialId id)
{
var context = await _agentProvider.GetContextAsync();
return await _recordService.GetAsync(context.Wallet, id, MdocRecordFun.DecodeFromJson);
return await _recordService.GetAsync<MdocRecord>(context.Wallet, id);
}

public async Task<Option<IEnumerable<MdocRecord>>> List(
Expand All @@ -38,13 +38,12 @@ public async Task<Option<IEnumerable<MdocRecord>>> List(
int skip = 0)
{
var context = await _agentProvider.GetContextAsync();
var list = await _recordService.SearchAsync(
var list = await _recordService.SearchAsync<MdocRecord>(
context.Wallet,
query.ToNullable(),
null,
count,
skip,
MdocRecordFun.DecodeFromJson);
skip);

if (list.Count == 0)
return Option<IEnumerable<MdocRecord>>.None;
Expand All @@ -55,7 +54,7 @@ public async Task<Option<IEnumerable<MdocRecord>>> List(
public async Task<Unit> Update(MdocRecord record)
{
var context = await _agentProvider.GetContextAsync();
await _recordService.Update(context.Wallet, record, MdocRecordFun.EncodeToJson);
await _recordService.Update(context.Wallet, record);
return Unit.Default;
}

Expand Down
Loading
Loading