-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine/Refactor Edge Provisioning (#23)
* Introduce alternative EdgeProvisioning flow (#15) * Introduce alternative EdgeProvisioning flow Signed-off-by: Johannes Tuerk <[email protected]> * refactor privisoing process Signed-off-by: Johannes Tuerk <[email protected]> --------- Signed-off-by: Johannes Tuerk <[email protected]> * expose GetMediatorConnection in Interface Signed-off-by: Johannes Tuerk <[email protected]> * revert EdgeProvisoningService removal Signed-off-by: Johannes Tuerk <[email protected]> * cleaning Signed-off-by: Johannes Tuerk <[email protected]> * cleaning Signed-off-by: Johannes Tuerk <[email protected]> * cleaning Signed-off-by: Johannes Tuerk <[email protected]> * Refactor Signed-off-by: Johannes Tuerk <[email protected]> * minor cleaning Signed-off-by: Johannes Tuerk <[email protected]> --------- Signed-off-by: Johannes Tuerk <[email protected]>
- Loading branch information
Showing
5 changed files
with
176 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 105 additions & 97 deletions
202
src/Hyperledger.Aries.Routing.Edge/EdgeProvisioningService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,105 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Hyperledger.Aries.Configuration; | ||
using Hyperledger.Aries.Features.Handshakes.Connection; | ||
using Hyperledger.Aries.Features.Handshakes.Connection.Models; | ||
using Hyperledger.Aries.Routing; | ||
using Hyperledger.Aries.Storage; | ||
using Hyperledger.Indy.WalletApi; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
|
||
[assembly: InternalsVisibleTo("Hyperledger.Aries.Tests")] | ||
|
||
namespace Hyperledger.Aries.Agents.Edge | ||
{ | ||
internal class EdgeProvisioningService : IHostedService, IEdgeProvisioningService | ||
{ | ||
internal const string MediatorConnectionIdTagName = "MediatorConnectionId"; | ||
private const string MediatorInboxIdTagName = "MediatorInboxId"; | ||
|
||
private readonly IProvisioningService provisioningService; | ||
private readonly IConnectionService connectionService; | ||
private readonly IMessageService messageService; | ||
private readonly IEdgeClientService edgeClientService; | ||
private readonly IWalletRecordService recordService; | ||
private readonly IAgentProvider agentProvider; | ||
private readonly AgentOptions options; | ||
|
||
public EdgeProvisioningService( | ||
IProvisioningService provisioningService, | ||
IConnectionService connectionService, | ||
IMessageService messageService, | ||
IEdgeClientService edgeClientService, | ||
IWalletRecordService recordService, | ||
IAgentProvider agentProvider, | ||
IOptions<AgentOptions> options) | ||
{ | ||
this.provisioningService = provisioningService; | ||
this.connectionService = connectionService; | ||
this.messageService = messageService; | ||
this.edgeClientService = edgeClientService; | ||
this.recordService = recordService; | ||
this.agentProvider = agentProvider; | ||
this.options = options.Value; | ||
} | ||
|
||
public async Task ProvisionAsync(AgentOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var discovery = await edgeClientService.DiscoverConfigurationAsync(options.EndpointUri); | ||
|
||
try | ||
{ | ||
options.AgentKey = discovery.RoutingKey; | ||
options.EndpointUri = discovery.ServiceEndpoint; | ||
|
||
await provisioningService.ProvisionAgentAsync(options); | ||
} | ||
catch(WalletStorageException) | ||
{ | ||
// OK | ||
} | ||
catch (WalletExistsException) | ||
{ | ||
// OK | ||
} | ||
var agentContext = await agentProvider.GetContextAsync(); | ||
var provisioning = await provisioningService.GetProvisioningAsync(agentContext.Wallet); | ||
|
||
// Check if connection has been established with mediator agent | ||
if (provisioning.GetTag(MediatorConnectionIdTagName) == null) | ||
{ | ||
var (request, record) = await connectionService.CreateRequestAsync(agentContext, discovery.Invitation); | ||
var response = await messageService.SendReceiveAsync<ConnectionResponseMessage>(agentContext, request, record); | ||
|
||
await connectionService.ProcessResponseAsync(agentContext, response, record); | ||
|
||
// Remove the routing key explicitly as it won't ever be needed. | ||
// Messages will always be sent directly with return routing enabled | ||
record = await connectionService.GetAsync(agentContext, record.Id); | ||
record.Endpoint = new AgentEndpoint(record.Endpoint.Uri, null, null); | ||
await recordService.UpdateAsync(agentContext.Wallet, record); | ||
|
||
provisioning.SetTag(MediatorConnectionIdTagName, record.Id); | ||
await recordService.UpdateAsync(agentContext.Wallet, provisioning); | ||
} | ||
|
||
await edgeClientService.CreateInboxAsync(agentContext, options.MetaData); | ||
} | ||
|
||
public Task ProvisionAsync(CancellationToken cancellationToken = default) => ProvisionAsync(options, cancellationToken); | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) => ProvisionAsync(cancellationToken); | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Hyperledger.Aries.Configuration; | ||
using Hyperledger.Aries.Features.Handshakes.Connection; | ||
using Hyperledger.Aries.Features.Handshakes.Connection.Models; | ||
using Hyperledger.Aries.Routing; | ||
using Hyperledger.Aries.Storage; | ||
using Hyperledger.Indy.WalletApi; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
|
||
[assembly: InternalsVisibleTo("Hyperledger.Aries.Tests")] | ||
|
||
namespace Hyperledger.Aries.Agents.Edge | ||
{ | ||
internal class EdgeProvisioningService : IHostedService, IEdgeProvisioningService | ||
{ | ||
internal const string MediatorConnectionIdTagName = "MediatorConnectionId"; | ||
|
||
private readonly IProvisioningService _provisioningService; | ||
private readonly IConnectionService _connectionService; | ||
private readonly IMessageService _messageService; | ||
private readonly IEdgeClientService _edgeClientService; | ||
private readonly IWalletRecordService _recordService; | ||
private readonly IAgentProvider _agentProvider; | ||
private readonly AgentOptions _agentOptions; | ||
|
||
public EdgeProvisioningService( | ||
IProvisioningService provisioningService, | ||
IConnectionService connectionService, | ||
IMessageService messageService, | ||
IEdgeClientService edgeClientService, | ||
IWalletRecordService recordService, | ||
IAgentProvider agentProvider, | ||
IOptions<AgentOptions> agentOptions) | ||
{ | ||
_provisioningService = provisioningService; | ||
_connectionService = connectionService; | ||
_messageService = messageService; | ||
_agentProvider = agentProvider; | ||
_agentOptions = agentOptions.Value; | ||
_edgeClientService = edgeClientService; | ||
_recordService = recordService; | ||
} | ||
|
||
public async Task EnsureMediatorConnectionAndInboxAsync(AgentOptions agentOptions, CancellationToken cancellationToken = default) | ||
{ | ||
var agentContext = await _agentProvider.GetContextAsync(); | ||
if (_edgeClientService.GetMediatorConnectionAsync(agentContext) != null) | ||
return; | ||
|
||
await CreateMediatorConnection(agentContext, agentOptions); | ||
|
||
await _edgeClientService.CreateInboxAsync(agentContext, agentOptions.MetaData); | ||
} | ||
|
||
private async Task CreateMediatorConnection(IAgentContext agentContext, AgentOptions agentOptions) | ||
{ | ||
var discovery = await _edgeClientService.DiscoverConfigurationAsync(agentOptions.EndpointUri); | ||
|
||
await _provisioningService.UpdateEndpointAsync(agentContext.Wallet, new AgentEndpoint | ||
{ | ||
Uri = discovery.ServiceEndpoint, | ||
Verkey = new[] { discovery.RoutingKey}, | ||
Did = agentOptions.AgentDid | ||
}); | ||
|
||
var (request, record) = await _connectionService.CreateRequestAsync(agentContext, discovery.Invitation); | ||
var response = await _messageService.SendReceiveAsync<ConnectionResponseMessage>(agentContext, request, record); | ||
|
||
await _connectionService.ProcessResponseAsync(agentContext, response, record); | ||
|
||
// Remove the routing key explicitly as it won't ever be needed. | ||
// Messages will always be sent directly with return routing enabled | ||
record = await _connectionService.GetAsync(agentContext, record.Id); | ||
record.Endpoint = new AgentEndpoint(record.Endpoint.Uri, null, null); | ||
await _recordService.UpdateAsync(agentContext.Wallet, record); | ||
|
||
var provisioning = await _provisioningService.GetProvisioningAsync(agentContext.Wallet); | ||
provisioning.SetTag(MediatorConnectionIdTagName, record.Id); | ||
await _recordService.UpdateAsync(agentContext.Wallet, provisioning); | ||
} | ||
|
||
public async Task ProvisionAsync(AgentOptions agentOptions, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await _provisioningService.ProvisionAgentAsync(agentOptions); | ||
} | ||
catch (WalletExistsException) | ||
{ | ||
// OK | ||
} | ||
} | ||
|
||
public Task ProvisionAsync(CancellationToken cancellationToken = default) => ProvisionAsync(_agentOptions, cancellationToken); | ||
|
||
public Task EnsureMediatorConnectionAndInboxAsync(CancellationToken cancellationToken = default) => ProvisionAsync(_agentOptions, cancellationToken); | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) => ProvisionAsync(cancellationToken); | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} |
Oops, something went wrong.