/// <summary>
        /// Create and provision agent, wallet, etc.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected async Task ProvisionAsync(CancellationToken cancellationToken)
        {
            // Create agent wallet
            await _walletService.CreateWalletAsync(
                configuration : _walletOptions.WalletConfiguration,
                credentials : _walletOptions.WalletCredentials);

            var wallet = await _walletService.GetWalletAsync(
                configuration : _walletOptions.WalletConfiguration,
                credentials : _walletOptions.WalletCredentials);

            if (_agentOptions.AgentKeySeed == null)
            {
                _agentOptions.AgentKeySeed = CryptoUtils.GetUniqueKey(32);
            }

            // Configure agent endpoint
            AgentEndpoint endpoint = null;

            if (_agentOptions.EndpointUri != null)
            {
                endpoint = new AgentEndpoint {
                    Uri = _agentOptions.EndpointUri?.ToString()
                };
                if (_agentOptions.AgentKeySeed != null)
                {
                    var agent = await Did.CreateAndStoreMyDidAsync(
                        wallet : wallet,
                        didJson : new
                    {
                        seed = _agentOptions.AgentKeySeed,
                        did  = _agentOptions.AgentDid
                    }.ToJson());

                    endpoint.Did    = agent.Did;
                    endpoint.Verkey = agent.VerKey;
                }
                else if (_agentOptions.AgentDid != null && _agentOptions.AgentKey != null)
                {
                    endpoint.Did    = _agentOptions.AgentDid;
                    endpoint.Verkey = _agentOptions.AgentKey;
                }
            }

            var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null);

            var record = new ProvisioningRecord
            {
                MasterSecretId = masterSecretId,
                Endpoint       = endpoint,
                Owner          =
                {
                    Name     = _agentOptions.AgentName,
                    ImageUrl = _agentOptions.AgentImageUri
                }
            };

            record.SetTag("AgentKeySeed", _agentOptions.AgentKeySeed);
            await _recordService.AddAsync(wallet, record);
        }
        /// <inheritdoc />
        public virtual async Task UpdateEndpointAsync(Wallet wallet, AgentEndpoint endpoint)
        {
            var record = await GetProvisioningAsync(wallet);

            record.Endpoint = endpoint;

            await RecordService.UpdateAsync(wallet, record);
        }
        public AgentApp()
        {
            AgentPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Battle.net", "Agent", "Agent.exe");

            StartProcess();

            AgentEndpoint   = new AgentEndpoint(Requester);
            InstallEndpoint = new InstallEndpoint(Requester);
            UpdateEndpoint  = new UpdateEndpoint(Requester);
            RepairEndpoint  = new RepairEndpoint(Requester);
            GameEndpoint    = new GameEndpoint(Requester);
            VersionEndpoint = new VersionEndpoint(Requester);
        }
        /// <inheritdoc/>
        public async Task <ConnectionRecord> ProcessResponseAsync(IAgentContext agentContext, DidExchangeResponseMessage responseMessage, ConnectionRecord connectionRecord)
        {
            await connectionRecord.TriggerAsync(ConnectionTrigger.Response);

            DidDoc didDoc = null;

            if (responseMessage.DidDoc.Data.Base64 is { } data)
            {
                var isValidSignature = await responseMessage.DidDoc.Data.VerifyJsonWebSignature();

                if (isValidSignature == false)
                {
                    throw new AriesFrameworkException(ErrorCode.InvalidSignatureEncoding,
                                                      "The given JSON web signature is invalid");
                }

                var json = data.FromBase64Url();
                didDoc = json.ToObject <DidDoc>();
            }

            if (didDoc == null)
            {
                throw new NotImplementedException("Response message must provide an attached did document");
            }

            if (didDoc.Keys.All(key => key.Type == DidDocExtensions.DefaultKeyType) == false)
            {
                throw new NotImplementedException($"Only {DidDocExtensions.DefaultKeyType} is supported");
            }

            var indyService = (IndyAgentDidDocService)didDoc.Services.First(service => service is IndyAgentDidDocService);

            var agentEndpoint = new AgentEndpoint(indyService.ServiceEndpoint, null, indyService.RoutingKeys.ToArray());

            connectionRecord.TheirDid = responseMessage.Did;
            connectionRecord.TheirVk  =
                didDoc.Keys.FirstOrDefault(key => key.Controller == responseMessage.Did)?.PublicKeyBase58
                ?? throw new NullReferenceException("Missing public key for controller");
            connectionRecord.Endpoint = agentEndpoint;

            await _recordService.UpdateAsync(agentContext.Wallet, connectionRecord);

            _eventAggregator.Publish(new ServiceMessageProcessingEvent()
            {
                MessageType = responseMessage.Type,
                RecordId    = connectionRecord.Id,
                ThreadId    = responseMessage.GetThreadId()
            });

            return(connectionRecord);
        }
示例#5
0
        public AgentApp()
        {
            AgentPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Battle.net", "Agent", "Agent.exe");

            if (!StartProcess())
            {
                Console.WriteLine("Please ensure Battle.net is installed and has recently been opened.");
                Environment.Exit(0);
            }

            AgentEndpoint   = new AgentEndpoint(Requester);
            InstallEndpoint = new InstallEndpoint(Requester);
            UpdateEndpoint  = new UpdateEndpoint(Requester);
            RepairEndpoint  = new RepairEndpoint(Requester);
            GameEndpoint    = new GameEndpoint(Requester);
            VersionEndpoint = new VersionEndpoint(Requester);
        }
        /// <inheritdoc />
        public async Task ForwardAsync(IEnvelopeMessage envelope, AgentEndpoint endpoint)
        {
            _logger.LogInformation(LoggingEvents.Forward, "Envelope {0}, Endpoint {1}", envelope.Type, endpoint.Uri);

            var encrypted = await _messageSerializer.PackAsync(envelope, endpoint.Verkey);

            var request = new HttpRequestMessage
            {
                RequestUri = new Uri(endpoint.Uri),
                Method     = HttpMethod.Post,
                Content    = new ByteArrayContent(encrypted)
            };

            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            var response = await _httpClient.SendAsync(request);

            response.EnsureSuccessStatusCode();
        }
示例#7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProvisioningRecord"/> class.
 /// </summary>
 public ProvisioningRecord()
 {
     Endpoint = new AgentEndpoint();
     Owner    = new AgentOwner();
 }
        /// <inheritdoc />
        public async Task ProvisionAgentAsync(AgentOptions agentOptions)
        {
            if (agentOptions is null)
            {
                throw new ArgumentNullException(nameof(agentOptions));
            }

            // Create agent wallet
            await WalletService.CreateWalletAsync(
                configuration : agentOptions.WalletConfiguration,
                credentials : agentOptions.WalletCredentials);

            var wallet = await WalletService.GetWalletAsync(
                configuration : agentOptions.WalletConfiguration,
                credentials : agentOptions.WalletCredentials);

            // Configure agent endpoint
            AgentEndpoint endpoint = null;

            if (agentOptions.EndpointUri != null)
            {
                endpoint = new AgentEndpoint {
                    Uri = agentOptions.EndpointUri.ToString()
                };
                if (agentOptions.AgentKeySeed != null)
                {
                    var agent = await Did.CreateAndStoreMyDidAsync(wallet, new { seed = agentOptions.AgentKeySeed }.ToJson());

                    endpoint.Did    = agent.Did;
                    endpoint.Verkey = agent.VerKey;
                }
                else if (agentOptions.AgentKey != null)
                {
                    endpoint.Did    = agentOptions.AgentDid;
                    endpoint.Verkey = agentOptions.AgentKey;
                }
                else
                {
                    var agent = await Did.CreateAndStoreMyDidAsync(wallet, "{}");

                    endpoint.Did    = agent.Did;
                    endpoint.Verkey = agent.VerKey;
                }
            }
            var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null);

            var record = new ProvisioningRecord
            {
                MasterSecretId = masterSecretId,
                Endpoint       = endpoint,
                Owner          =
                {
                    Name     = agentOptions.AgentName,
                    ImageUrl = agentOptions.AgentImageUri
                }
            };

            // Issuer Configuration
            if (agentOptions.IssuerKeySeed == null)
            {
                agentOptions.IssuerKeySeed = CryptoUtils.GetUniqueKey(32);
            }

            var issuer = await Did.CreateAndStoreMyDidAsync(
                wallet : wallet,
                didJson : new
            {
                did  = agentOptions.IssuerDid,
                seed = agentOptions.IssuerKeySeed
            }.ToJson());

            record.IssuerSeed   = agentOptions.IssuerKeySeed;
            record.IssuerDid    = issuer.Did;
            record.IssuerVerkey = issuer.VerKey;

            record.SetTag("AgentKeySeed", agentOptions.AgentKeySeed);
            record.SetTag("IssuerKeySeed", agentOptions.IssuerKeySeed);

            // Add record to wallet
            await RecordService.AddAsync(wallet, record);
        }
        /// <inheritdoc />
        public virtual async Task ProvisionAgentAsync(ProvisioningConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (configuration.WalletConfiguration == null ||
                configuration.WalletCredentials == null)
            {
                throw new ArgumentNullException(nameof(configuration),
                                                "Wallet configuration and credentials must be specified");
            }

            // Create agent wallet
            await WalletService.CreateWalletAsync(configuration.WalletConfiguration, configuration.WalletCredentials);

            var wallet =
                await WalletService.GetWalletAsync(configuration.WalletConfiguration, configuration.WalletCredentials);

            // Configure agent endpoint
            AgentEndpoint endpoint = null;

            if (configuration.EndpointUri != null)
            {
                endpoint = new AgentEndpoint {
                    Uri = configuration.EndpointUri?.ToString()
                };
                if (configuration.AgentSeed != null)
                {
                    var agent = await Did.CreateAndStoreMyDidAsync(wallet, new { seed = configuration.AgentSeed }.ToJson());

                    endpoint.Did    = agent.Did;
                    endpoint.Verkey = agent.VerKey;
                }
                else if (configuration.AgentDid != null && configuration.AgentVerkey != null)
                {
                    endpoint.Did    = configuration.AgentDid;
                    endpoint.Verkey = configuration.AgentVerkey;
                }
                else
                {
                    var agent = await Did.CreateAndStoreMyDidAsync(wallet, "{}");

                    endpoint.Did    = agent.Did;
                    endpoint.Verkey = agent.VerKey;
                }
            }

            var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null);

            var record = new ProvisioningRecord
            {
                MasterSecretId = masterSecretId,
                Endpoint       = endpoint,
                Owner          =
                {
                    Name     = configuration.OwnerName,
                    ImageUrl = configuration.OwnerImageUrl
                },
                PassCode = configuration.PassCode
            };

            // Populate initial tags if any passed
            if (configuration.Tags != null && configuration.Tags.Any())
            {
                foreach (var item in configuration.Tags)
                {
                    record.Tags.Add(item.Key, item.Value);
                }
            }

            // Create issuer
            await configuration.ConfigureAsync(record, new DefaultAgentContext { Wallet = wallet });

            await RecordService.AddAsync(wallet, record);
        }
        /// <inheritdoc/>
        public async Task <ConnectionRecord> ProcessRequestAsync(IAgentContext agentContext, DidExchangeRequestMessage requestMessage)
        {
            var myDid = await Did.CreateAndStoreMyDidAsync(agentContext.Wallet, "{}");

            DidDoc didDoc = null;

            if (requestMessage.DidDoc.Data.Base64 is { } data)
            {
                var isValidSignature = await requestMessage.DidDoc.Data.VerifyJsonWebSignature();

                if (isValidSignature == false)
                {
                    throw new AriesFrameworkException(ErrorCode.InvalidSignatureEncoding,
                                                      "The given JSON web signature is invalid");
                }

                var json = data.FromBase64Url();
                didDoc = json.ToObject <DidDoc>();
            }

            // Todo: Handle resolvable Dids
            if (didDoc == null)
            {
                throw new NotImplementedException("Request message must provide an attached did document");
            }

            if (didDoc.Keys.All(key => key.Type == DidDocExtensions.DefaultKeyType) == false)
            {
                throw new NotImplementedException($"Only {DidDocExtensions.DefaultKeyType} is supported");
            }

            var indyService = (IndyAgentDidDocService)didDoc.Services.First(service => service is IndyAgentDidDocService);

            var agentEndpoint = new AgentEndpoint(indyService.ServiceEndpoint, null, indyService.RoutingKeys.ToArray());

            var connectionRecord = new ConnectionRecord
            {
                Id    = Guid.NewGuid().ToString(),
                Alias = new ConnectionAlias {
                    Name = requestMessage.Label
                },
                MyDid    = DidUtils.ConvertVerkeyToDidKey(myDid.VerKey),
                MyVk     = myDid.VerKey,
                TheirDid = requestMessage.Did,
                TheirVk  = didDoc.Keys.FirstOrDefault(key => key.Controller == requestMessage.Did)?.PublicKeyBase58
                           ?? throw new NullReferenceException("Missing public for controller"),
                                 Endpoint = agentEndpoint,
                                 State    = ConnectionState.Negotiating
            };
            await _recordService.AddAsync(agentContext.Wallet, connectionRecord);

            _eventAggregator.Publish(
                new ServiceMessageProcessingEvent
            {
                MessageType = requestMessage.Type,
                RecordId    = connectionRecord.Id,
                ThreadId    = requestMessage.GetThreadId()
            });

            return(connectionRecord);
        }