예제 #1
0
        public IActionResult GetProposedOnChainPaymentMethodPreview(
            string storeId,
            string cryptoCode,
            [FromBody] OnChainPaymentMethodDataPreview paymentMethodData,
            int offset = 0, int amount = 10)
        {
            if (!GetCryptoCodeWallet(cryptoCode, out var network, out BTCPayWallet _))
            {
                return(NotFound());
            }

            if (string.IsNullOrEmpty(paymentMethodData?.DerivationScheme))
            {
                ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme),
                                         "Missing derivationScheme");
            }

            if (!ModelState.IsValid)
            {
                return(this.CreateValidationError(ModelState));
            }
            DerivationSchemeSettings strategy;

            try
            {
                strategy = DerivationSchemeSettings.Parse(paymentMethodData.DerivationScheme, network);
            }
            catch
            {
                ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme),
                                         "Invalid Derivation Scheme");
                return(this.CreateValidationError(ModelState));
            }

            var deposit = new NBXplorer.KeyPathTemplates(null).GetKeyPathTemplate(DerivationFeature.Deposit);
            var line    = strategy.AccountDerivation.GetLineFor(deposit);
            var result  = new OnChainPaymentMethodPreviewResultData();

            for (var i = offset; i < amount; i++)
            {
                var derivation = line.Derive((uint)i);
                result.Addresses.Add(
                    new
                    OnChainPaymentMethodPreviewResultData.
                    OnChainPaymentMethodPreviewResultAddressItem()
                {
                    KeyPath = deposit.GetKeyPath((uint)i).ToString(),
                    Address = strategy.Network.NBXplorerNetwork.CreateAddress(strategy.AccountDerivation,
                                                                              line.KeyPathTemplate.GetKeyPath((uint)i),
                                                                              derivation.ScriptPubKey).ToString()
                });
            }

            return(Ok(result));
        }
예제 #2
0
        public IActionResult GetOnChainPaymentMethodPreview(
            string storeId,
            string cryptoCode,
            int offset = 0, int amount = 10)
        {
            if (!GetCryptoCodeWallet(cryptoCode, out var network, out BTCPayWallet _))
            {
                return(NotFound());
            }

            var paymentMethod = GetExistingBtcLikePaymentMethod(cryptoCode);

            if (string.IsNullOrEmpty(paymentMethod?.DerivationScheme))
            {
                return(NotFound());
            }

            try
            {
                var strategy = DerivationSchemeSettings.Parse(paymentMethod.DerivationScheme, network);
                var deposit  = new NBXplorer.KeyPathTemplates(null).GetKeyPathTemplate(DerivationFeature.Deposit);

                var line   = strategy.AccountDerivation.GetLineFor(deposit);
                var result = new OnChainPaymentMethodPreviewResultData();
                for (var i = offset; i < amount; i++)
                {
                    var address = line.Derive((uint)i);
                    result.Addresses.Add(
                        new OnChainPaymentMethodPreviewResultData.OnChainPaymentMethodPreviewResultAddressItem()
                    {
                        KeyPath = deposit.GetKeyPath((uint)i).ToString(),
                        Address = address.ScriptPubKey.GetDestinationAddress(strategy.Network.NBitcoinNetwork)
                                  .ToString()
                    });
                }

                return(Ok(result));
            }
            catch
            {
                ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme),
                                         "Invalid Derivation Scheme");
                return(this.CreateValidationError(ModelState));
            }
        }