Exemplo n.º 1
0
 public IEnumerable <Label> GetLabels(WalletBlobInfo walletBlobInfo, WalletTransactionInfo transactionInfo,
                                      HttpRequest request)
 {
     foreach (var label in transactionInfo.Labels)
     {
         if (walletBlobInfo.LabelColors.TryGetValue(label, out var color))
         {
             yield return(CreateLabel(label, color, request));
         }
     }
 }
Exemplo n.º 2
0
        async public Task <RawLabel> BuildLabel(
            WalletBlobInfo walletBlobInfo,
            HttpRequest request,
            WalletTransactionInfo walletTransactionInfo,
            WalletId walletId,
            string transactionId,
            string label
            )
        {
            label = label.Trim().TrimStart('{').ToLowerInvariant().Replace(',', ' ').Truncate(MaxLabelSize);
            var labels = GetWalletColoredLabels(walletBlobInfo, request);

            if (!labels.Any(l => l.Text.Equals(label, StringComparison.OrdinalIgnoreCase)))
            {
                var chosenColor = ChooseBackgroundColor(walletBlobInfo, request);
                walletBlobInfo.LabelColors.Add(label, chosenColor);
                await _walletRepository.SetWalletInfo(walletId, walletBlobInfo);
            }

            return(new RawLabel(label));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> ModifyTransaction(
            // We need addlabel and addlabelclick. addlabel is the + button if the label does not exists,
            // addlabelclick is if the user click on existing label. For some reason, reusing the same name attribute for both
            // does not work
            [ModelBinder(typeof(WalletIdModelBinder))]
            WalletId walletId, string transactionId,
            string addlabel      = null,
            string addlabelclick = null,
            string addcomment    = null,
            string removelabel   = null)
        {
            addlabel = addlabel ?? addlabelclick;
            // Hack necessary when the user enter a empty comment and submit.
            // For some reason asp.net consider addcomment null instead of empty string...
            try
            {
                if (addcomment == null && Request?.Form?.TryGetValue(nameof(addcomment), out _) is true)
                {
                    addcomment = string.Empty;
                }
            }
            catch { }
            /////////

            DerivationSchemeSettings paymentMethod = GetDerivationSchemeSettings(walletId);

            if (paymentMethod == null)
            {
                return(NotFound());
            }

            var walletBlobInfoAsync         = WalletRepository.GetWalletInfo(walletId);
            var walletTransactionsInfoAsync = WalletRepository.GetWalletTransactionsInfo(walletId);
            var wallet                 = _walletProvider.GetWallet(paymentMethod.Network);
            var walletBlobInfo         = await walletBlobInfoAsync;
            var walletTransactionsInfo = await walletTransactionsInfoAsync;

            if (addlabel != null)
            {
                addlabel = addlabel.Trim().ToLowerInvariant().Replace(',', ' ').Truncate(MaxLabelSize);
                var labels = walletBlobInfo.GetLabels();
                if (!walletTransactionsInfo.TryGetValue(transactionId, out var walletTransactionInfo))
                {
                    walletTransactionInfo = new WalletTransactionInfo();
                }
                if (!labels.Any(l => l.Value.Equals(addlabel, StringComparison.OrdinalIgnoreCase)))
                {
                    List <string> allColors = new List <string>();
                    allColors.AddRange(LabelColorScheme);
                    allColors.AddRange(labels.Select(l => l.Color));
                    var chosenColor =
                        allColors
                        .GroupBy(k => k)
                        .OrderBy(k => k.Count())
                        .ThenBy(k => Array.IndexOf(LabelColorScheme, k.Key))
                        .First().Key;
                    walletBlobInfo.LabelColors.Add(addlabel, chosenColor);
                    await WalletRepository.SetWalletInfo(walletId, walletBlobInfo);
                }
                if (walletTransactionInfo.Labels.Add(addlabel))
                {
                    await WalletRepository.SetWalletTransactionInfo(walletId, transactionId, walletTransactionInfo);
                }
            }
            else if (removelabel != null)
            {
                removelabel = removelabel.Trim().ToLowerInvariant().Truncate(MaxLabelSize);
                if (walletTransactionsInfo.TryGetValue(transactionId, out var walletTransactionInfo))
                {
                    if (walletTransactionInfo.Labels.Remove(removelabel))
                    {
                        var canDelete = !walletTransactionsInfo.SelectMany(txi => txi.Value.Labels).Any(l => l == removelabel);
                        if (canDelete)
                        {
                            walletBlobInfo.LabelColors.Remove(removelabel);
                            await WalletRepository.SetWalletInfo(walletId, walletBlobInfo);
                        }
                        await WalletRepository.SetWalletTransactionInfo(walletId, transactionId, walletTransactionInfo);
                    }
                }
            }
            else if (addcomment != null)
            {
                addcomment = addcomment.Trim().Truncate(MaxCommentSize);
                if (!walletTransactionsInfo.TryGetValue(transactionId, out var walletTransactionInfo))
                {
                    walletTransactionInfo = new WalletTransactionInfo();
                }
                walletTransactionInfo.Comment = addcomment;
                await WalletRepository.SetWalletTransactionInfo(walletId, transactionId, walletTransactionInfo);
            }
            return(RedirectToAction(nameof(WalletTransactions), new { walletId = walletId.ToString() }));
        }
        public async Task SetWalletTransactionInfo(WalletId walletId, string transactionId, WalletTransactionInfo walletTransactionInfo)
        {
            if (walletId == null)
            {
                throw new ArgumentNullException(nameof(walletId));
            }
            if (transactionId == null)
            {
                throw new ArgumentNullException(nameof(transactionId));
            }
            using (var ctx = _ContextFactory.CreateContext())
            {
                var walletData = new WalletTransactionData()
                {
                    WalletDataId = walletId.ToString(), TransactionId = transactionId
                };
                walletData.SetBlobInfo(walletTransactionInfo);
                var entity = await ctx.WalletTransactions.AddAsync(walletData);

                entity.State = EntityState.Modified;
                try
                {
                    await ctx.SaveChangesAsync();
                }
                catch (DbUpdateException) // Does not exists
                {
                    entity.State = EntityState.Added;
                    try
                    {
                        await ctx.SaveChangesAsync();
                    }
                    catch (DbUpdateException) // the Wallet does not exists in the DB
                    {
                        await SetWalletInfo(walletId, new WalletBlobInfo());

                        await ctx.SaveChangesAsync();
                    }
                }
            }
        }
        public async Task <IActionResult> PatchOnChainWalletTransaction(
            string storeId,
            string cryptoCode,
            string transactionId,
            [FromBody] PatchOnChainTransactionRequest request
            )
        {
            if (IsInvalidWalletRequest(cryptoCode, out var network,
                                       out var derivationScheme, out var actionResult))
            {
                return(actionResult);
            }

            var wallet = _btcPayWalletProvider.GetWallet(network);
            var tx     = await wallet.FetchTransaction(derivationScheme.AccountDerivation, uint256.Parse(transactionId));

            if (tx is null)
            {
                return(this.CreateAPIError(404, "transaction-not-found", "The transaction was not found."));
            }

            var walletId = new WalletId(storeId, cryptoCode);
            var walletTransactionsInfoAsync = _walletRepository.GetWalletTransactionsInfo(walletId);

            if (!(await walletTransactionsInfoAsync).TryGetValue(transactionId, out var walletTransactionInfo))
            {
                walletTransactionInfo = new WalletTransactionInfo();
            }

            if (request.Comment != null)
            {
                walletTransactionInfo.Comment = request.Comment.Trim().Truncate(WalletTransactionDataExtensions.MaxCommentSize);
            }

            if (request.Labels != null)
            {
                var walletBlobInfo = await _walletRepository.GetWalletInfo(walletId);

                foreach (string label in request.Labels)
                {
                    var rawLabel = await _labelFactory.BuildLabel(
                        walletBlobInfo,
                        Request,
                        walletTransactionInfo,
                        walletId,
                        transactionId,
                        label
                        );

                    walletTransactionInfo.Labels.TryAdd(rawLabel.Text, rawLabel);
                }
            }

            await _walletRepository.SetWalletTransactionInfo(walletId, transactionId, walletTransactionInfo);

            var walletTransactionsInfo =
                (await _walletRepository.GetWalletTransactionsInfo(walletId, new[] { transactionId }))
                .Values
                .FirstOrDefault();

            return(Ok(ToModel(walletTransactionsInfo, tx, wallet)));
        }
Exemplo n.º 6
0
 public IEnumerable <ColoredLabel> ColorizeTransactionLabels(WalletBlobInfo walletBlobInfo, WalletTransactionInfo transactionInfo,
                                                             HttpRequest request)
 {
     foreach (var label in transactionInfo.Labels)
     {
         walletBlobInfo.LabelColors.TryGetValue(label.Value.Text, out var color);
         yield return(CreateLabel(label.Value, color, request));
     }
 }