Example #1
0
        public async Task <IActionResult> RegenerateKey([FromBody] RegenerateKeyRequestModel request)
        {
            var check2FaResult = await _google2FaService.Check2FaAsync <CreateApiKeyResponse>(_requestContext.ClientId, request.Code);

            if (check2FaResult != null)
            {
                return(Ok(check2FaResult));
            }

            var clientKeys = await _hftInternalService.Keys.GetKeys(_requestContext.ClientId);

            var existingApiKey = clientKeys.FirstOrDefault(x => x.WalletId == request.Id);

            if (existingApiKey != null)
            {
                var apiKey = await _hftInternalService.Keys.UpdateKey(new UpdateApiKeyModel
                {
                    ClientId  = _requestContext.ClientId,
                    WalletId  = existingApiKey.WalletId,
                    Apiv2Only = request.Apiv2Only
                });

                var result = new CreateApiKeyResponse {
                    ApiKey = apiKey.ApiKey, WalletId = apiKey.WalletId, Apiv2Only = request.Apiv2Only
                };
                return(Ok(Google2FaResultModel <CreateApiKeyResponse> .Success(result)));
            }

            return(NotFound());
        }
        public async Task <IActionResult> Transfer([FromBody] CreateTransferRequest cmd, [FromQuery] Guid?id)
        {
            var check2FaResult = await _google2FaService.Check2FaAsync <string>(_requestContext.ClientId, cmd.Code2Fa);

            if (check2FaResult != null)
            {
                return(Ok(check2FaResult));
            }

            var operationId = id ?? Guid.NewGuid();

            await _operationsClient.Transfer(operationId,
                                             new CreateTransferCommand
            {
                ClientId       = new Guid(_requestContext.ClientId),
                Amount         = cmd.Amount,
                SourceWalletId =
                    cmd.SourceWalletId,
                WalletId = cmd.WalletId,
                AssetId  = cmd.AssetId
            });

            return(Ok(Google2FaResultModel <string> .Success(id.ToString())));
        }
        public async Task <IActionResult> CreateWhitelistingAsync([FromBody] CreateWhitelistingRequest request)
        {
            if (string.IsNullOrWhiteSpace(request.AddressBase))
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.InvalidInput);
            }

            var check2FaResult = await _google2FaService.Check2FaAsync <string>(_requestContext.ClientId, request.Code2Fa);

            if (check2FaResult != null)
            {
                return(Ok(check2FaResult));
            }

            var asset = await _assetsHelper.GetAssetAsync(request.AssetId);

            var assetsAvailableToUser = await _assetsHelper.GetSetOfAssetsAvailableToClientAsync(_requestContext.ClientId, _requestContext.PartnerId, true);

            if (asset == null || asset.BlockchainIntegrationType != BlockchainIntegrationType.Sirius && assetsAvailableToUser.Contains(asset.Id))
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.AssetUnavailable);
            }

            var wallets = (await _clientAccountService.Wallets.GetClientWalletsFilteredAsync(_requestContext.ClientId,
                                                                                             owner: OwnerType.Spot, walletType: WalletType.Trusted)).ToList();

            if (wallets.All(x => x.Id != request.WalletId))
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.InvalidInput);
            }

            var siriusAsset = await _siriusWalletsService.GetAssetByIdAsync(asset.SiriusAssetId);

            if (siriusAsset == null)
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.AssetUnavailable);
            }

            var siriusAccount = await _siriusWalletsService.SearchAccountAsync(_requestContext.ClientId, request.WalletId);

            if (siriusAccount == null)
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.BlockchainWalletDepositAddressNotGenerated);
            }

            var whitelistedItems = await _siriusWalletsService.GetWhitelistItemsAsync(siriusAccount.Body.Items.First().Id);

            if (whitelistedItems.Any(x => x.Details.Address == request.AddressBase && x.Details.Tag == request.AddressExtension))
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.AddressAlreadyWhitelisted);
            }

            var requestId = $"lykke:hft:withdrawals:{request.WalletId}:{request.Name}:{request.AssetId}:{request.AddressBase}:{request.AddressExtension ?? string.Empty}";

            var whitelistItemRequest = new WhitelistItemCreateRequest
            {
                Name  = request.Name,
                Scope = new WhitelistItemScope {
                    BrokerAccountId = _siriusApiServiceClientSettings.BrokerAccountId,
                    AccountId       = siriusAccount.Body.Items.First().Id,
                    UserNativeId    = _requestContext.ClientId
                },
                Details = new WhitelistItemDetails
                {
                    AssetId      = siriusAsset.Id,
                    BlockchainId = siriusAsset.BlockchainId,
                    Address      = request.AddressBase,
                    Tag          = request.AddressExtension,
                    TagType      =
                        new NullableWhitelistItemTagType
                    {
                        TagType = WhitelistItemTagType.Number     //TODO: specify tag type depending on the blockchain
                    },
                    TransactionType = WhitelistTransactionType.Withdrawal
                },
                Lifespan = new WhitelistItemLifespan {
                    StartsAt = Timestamp.FromDateTime(DateTime.UtcNow.Add(_whitelistingSettings.WaitingPeriod))
                },
                RequestId = requestId
            };

            var result = await _siriusWalletsService.CreateWhitelistItemAsync(whitelistItemRequest);

            if (result == null)
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.WhitelistingError);
            }

            await SendWhitelistEmail(_requestContext.ClientId, asset.DisplayId, request.AddressBase,
                                     request.AddressExtension);

            return(Ok(Google2FaResultModel <WhitelistingResponseModel> .Success(new WhitelistingResponseModel
            {
                Id = result.WhitelistItem.Id.ToString(),
                WalletName = wallets.Single(y => y.Id == request.WalletId).Name,
                AssetName = asset.DisplayId,
                Status = WhitelistingStatus.Pending,
                AddressBase = request.AddressBase,
                AddressExtension = request.AddressExtension,
                StartsAt = result.WhitelistItem.Lifespan.StartsAt.ToDateTime(),
                CreatedAt = result.WhitelistItem.CreatedAt.ToDateTime(),
                Name = request.Name
            })));
        }