示例#1
0
        public static bool IsSwapRedeem(TezosTransaction tx, byte[] secretHash)
        {
            try
            {
                if (tx.Params == null)
                {
                    return(false);
                }

                var entrypoint = tx.Params?["entrypoint"]?.ToString();

                var paramSecretHex = entrypoint switch
                {
                    "default" => GetSecret(tx.Params?["value"]?["args"]?[0]?["args"]?[0]),
                    "withdraw" => GetSecret(tx.Params?["value"]?["args"]?[0]),
                    "redeem" => GetSecret(tx.Params?["value"]),
                    _ => ""
                };

                var paramSecretBytes     = Hex.FromString(paramSecretHex);
                var paramSecretHashBytes = CurrencySwap.CreateSwapSecretHash(paramSecretBytes);

                return(paramSecretHashBytes.SequenceEqual(secretHash));
            }
            catch (Exception)
            {
                return(false);
            }
        }
示例#2
0
        public static bool IsSwapRedeem(TezosTransaction tx, byte[] secretHash)
        {
            try
            {
                var secretBytes     = Hex.FromString(tx.Params["value"]["bytes"].ToString());
                var secretHashBytes = CurrencySwap.CreateSwapSecretHash(secretBytes);

                return(secretHashBytes.SequenceEqual(secretHash));
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public bool IsSwapRedeem(byte[] secretHash)
        {
            try
            {
                var secretBytes     = Hex.FromString(Params["args"][0]["args"][0]["bytes"].ToString());
                var secretHashBytes = CurrencySwap.CreateSwapSecretHash(secretBytes);

                return(secretHashBytes.SequenceEqual(secretHash));
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public void ExtractSecretTest()
        {
            var redeemScript = new Script(SwapRedeemScript);

            Assert.NotNull(redeemScript);

            var secret = BitcoinBasedSwapTemplate.ExtractSecretFromP2PkhSwapRedeem(redeemScript);

            Assert.NotNull(secret);

            var secretHash = Convert.ToBase64String(CurrencySwap.CreateSwapSecretHash160(secret));

            Assert.Equal(SwapSecretHash, secretHash);
        }
示例#5
0
        private async Task InitiateSwapAsync(Swap swap)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            var soldCurrency = _account.Currencies.GetByName(swap.SoldCurrency);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(soldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecret);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecretHash);
            }

            WalletAddress walletAddress;

            if (swap.ToAddress == null)
            {
                walletAddress = await _account
                                .GetRedeemAddressAsync(swap.PurchasedCurrency)
                                .ConfigureAwait(false);

                swap.ToAddress = walletAddress.Address;

                RaiseSwapUpdated(swap, SwapStateFlags.Empty);
            }
            else
            {
                walletAddress = await _account
                                .GetAddressAsync(swap.PurchasedCurrency, swap.ToAddress)
                                .ConfigureAwait(false);
            }

            swap.RewardForRedeem = await GetRewardForRedeemAsync(walletAddress)
                                   .ConfigureAwait(false);

            RaiseSwapUpdated(swap, SwapStateFlags.Empty);

            _swapClient.SwapInitiateAsync(swap);
        }
示例#6
0
        private async Task InitiateSwapAsync(ClientSwap swap)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(swap.SoldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecret);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecretHash);
            }

            if (swap.ToAddress == null)
            {
                swap.ToAddress = (await _account
                                  .GetRedeemAddressAsync(swap.PurchasedCurrency)
                                  .ConfigureAwait(false))
                                 .Address;
                RaiseSwapUpdated(swap, SwapStateFlags.Empty);
            }

            var purchasedCurrencyBalance = await _account
                                           .GetBalanceAsync(swap.PurchasedCurrency)
                                           .ConfigureAwait(false);

            swap.RewardForRedeem = purchasedCurrencyBalance.Available < swap.PurchasedCurrency.GetDefaultRedeemFee() &&
                                   !(swap.PurchasedCurrency is BitcoinBasedCurrency)
                ? swap.PurchasedCurrency.GetDefaultRedeemFee() * 2
                : 0;
            RaiseSwapUpdated(swap, SwapStateFlags.Empty);

            _swapClient.SwapInitiateAsync(swap);
        }
示例#7
0
        private async Task InitiateSwapAsync(
            Swap swap,
            CancellationToken cancellationToken = default)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            var soldCurrency = _account.Currencies.GetByName(swap.SoldCurrency);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(soldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);

                await UpdateSwapAsync(swap, SwapStateFlags.HasSecret, cancellationToken)
                .ConfigureAwait(false);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);

                await UpdateSwapAsync(swap, SwapStateFlags.HasSecretHash, cancellationToken)
                .ConfigureAwait(false);
            }

            var redeemFromWalletAddress = swap.RedeemFromAddress != null
                ? await _account
                                          .GetAddressAsync(swap.PurchasedCurrency, swap.RedeemFromAddress, cancellationToken)
                                          .ConfigureAwait(false)
                : null;

            var purchasedCurrency = _account.Currencies.GetByName(swap.PurchasedCurrency);

            swap.RewardForRedeem = await RewardForRedeemHelper
                                   .EstimateAsync(
                account : _account,
                quotesProvider : _quotesProvider,
                feeCurrencyQuotesProvider : symbol => _marketDataRepository
                ?.OrderBookBySymbol(symbol)
                ?.TopOfBook(),
                redeemableCurrency : purchasedCurrency,
                redeemFromAddress : redeemFromWalletAddress,
                cancellationToken : cancellationToken)
                                   .ConfigureAwait(false);

            // select refund address for bitcoin based currency
            if (soldCurrency is BitcoinBasedConfig && swap.RefundAddress == null)
            {
                swap.RefundAddress = (await _account
                                      .GetCurrencyAccount <BitcoinBasedAccount>(soldCurrency.Name)
                                      .GetRefundAddressAsync(cancellationToken)
                                      .ConfigureAwait(false))
                                     ?.Address;
            }

            await UpdateSwapAsync(swap, SwapStateFlags.Empty, cancellationToken)
            .ConfigureAwait(false);

            _swapClient.SwapInitiateAsync(swap);
        }