Ejemplo n.º 1
0
        public async Task RemoveBitGoUser(RemoveBitGoUserRequest request)
        {
            using var action = MyTelemetry.StartActivity("Remove BitGo user");
            request.AddToActivityAsJsonTag("request");
            try
            {
                _logger.LogInformation("Remove BitGo user: {jsonText}",
                                       JsonConvert.SerializeObject(request));

                var entity = await _writer.DeleteAsync(BitGoUserNoSqlEntity.GeneratePartitionKey(request.BrokerId),
                                                       BitGoUserNoSqlEntity.GenerateRowKey(request.UserId, request.CoinId));

                if (entity != null)
                {
                    _logger.LogInformation("Removed BitGo user: {jsonText}",
                                           JsonConvert.SerializeObject(entity, new ApiKeyHiddenJsonConverter(typeof(BitGoUser))));
                }
                else
                {
                    _logger.LogInformation("Unable to remove BitGo user, do not exist: {jsonText}",
                                           JsonConvert.SerializeObject(request));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Cannot remove BitGo user: {requestJson}",
                                 JsonConvert.SerializeObject(request));
                ex.FailActivity();
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <BitGoUser> GetBitGoUser(GetBitGoUserRequest request)
        {
            var user = (await _writer.GetAsync(BitGoUserNoSqlEntity.GeneratePartitionKey(request.BrokerId),
                                               BitGoUserNoSqlEntity.GenerateRowKey(request.UserId, request.CoinId))).User;

            if (user != null)
            {
                user.ApiKey = "***";
            }

            return(user);
        }
Ejemplo n.º 3
0
        public IBitGoApi GetByUser(string brokerId, string userId, string coinId, string newApiKey)
        {
            var bitGoUser = _bitgoUserReader.Get(
                BitGoUserNoSqlEntity.GeneratePartitionKey(brokerId),
                BitGoUserNoSqlEntity.GenerateRowKey(BitGoUserNoSqlEntity.TechSignerId,
                                                    coinId)) ??
                            _bitgoUserReader.Get(
                BitGoUserNoSqlEntity.GeneratePartitionKey(brokerId),
                BitGoUserNoSqlEntity.GenerateRowKey(BitGoUserNoSqlEntity.TechSignerId,
                                                    BitGoUserNoSqlEntity.DefaultCoin));

            var apiKeyEnc = bitGoUser?.User?.ApiKey;

            if (string.IsNullOrEmpty(apiKeyEnc) && string.IsNullOrEmpty(newApiKey))
            {
                _logger.LogError("Tech account is not configured, id = {techSignerName}",
                                 BitGoUserNoSqlEntity.TechSignerId);
                return(null);
            }

            lock (_clients)
            {
                if (!string.IsNullOrEmpty(apiKeyEnc) && _clients.TryGetValue(apiKeyEnc, out var api))
                {
                    return(api);
                }
            }

            var coin = _bitgoCointReader.Get(BitgoCoinEntity.GeneratePartitionKey(),
                                             BitgoCoinEntity.GenerateRowKey(coinId));

            if (coin == null)
            {
                _logger.LogError("Cannot fond bitgo coin id = {symbol}", coinId);
                return(null);
            }

            var apiKey = string.IsNullOrEmpty(apiKeyEnc) ? newApiKey : _encryptionService.Decrypt(apiKeyEnc);

            var client = new BitGoClient(
                apiKey, Program.Settings.BitgoExpressUrlMainNet,
                apiKey, Program.Settings.BitgoExpressUrlTestNet);

            lock (_clients)
            {
                _clients[apiKey] = coin.IsMainNet ? client.MainNet : client.TestNet;
                return(_clients[apiKey]);
            }
        }
Ejemplo n.º 4
0
        public async Task UpdateBitGoUser(BitGoUser user)
        {
            using var action = MyTelemetry.StartActivity("Update BitGo user");
            try
            {
                _logger.LogInformation("Update BitGoUser: {jsonText}",
                                       JsonConvert.SerializeObject(user, new ApiKeyHiddenJsonConverter(typeof(BitGoUser))));

                user.UpdatedDate = DateTime.Now;
                await SetUserId(user);

                user.ApiKey = _encryptionService.Encrypt(user.ApiKey);

                ValidateUser(user);

                var entity = BitGoUserNoSqlEntity.Create(user);

                var existingItem = await _writer.GetAsync(entity.PartitionKey, entity.RowKey);

                if (existingItem == null)
                {
                    throw new Exception("Cannot update BitGo user. Do not exist");
                }

                await _writer.InsertOrReplaceAsync(entity);

                _logger.LogInformation("Updated BitGo user: {jsonText}",
                                       JsonConvert.SerializeObject(user, new ApiKeyHiddenJsonConverter(typeof(BitGoUser))));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Cannot update BitGo user: {requestJson}",
                                 JsonConvert.SerializeObject(user, new ApiKeyHiddenJsonConverter(typeof(BitGoUser))));
                ex.FailActivity();
                throw;
            }
        }