Пример #1
0
        public override TrackedTransaction CreateTrackedTransaction(TrackedSource trackedSource, ITrackedTransactionSerializable tx)
        {
            var trackedTransaction = (ElementsTrackedTransaction)base.CreateTrackedTransaction(trackedSource, tx);

            trackedTransaction.Unblind(((ElementsTransactionMatchData)tx).Unblind);
            return(trackedTransaction);
        }
Пример #2
0
        public async Task <UTXOChanges> GetUTXOsAsync(TrackedSource trackedSource, Bookmark[] confirmedBookmarks, Bookmark[] unconfirmedBookmarks, bool longPolling = true, CancellationToken cancellation = default)
        {
            if (trackedSource == null)
            {
                throw new ArgumentNullException(nameof(trackedSource));
            }
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            if (confirmedBookmarks != null)
            {
                parameters.Add("confirmedBookmarks", String.Join(",", confirmedBookmarks.Select(b => b.ToString())));
            }
            if (unconfirmedBookmarks != null)
            {
                parameters.Add("unconfirmedBookmarks", String.Join(",", unconfirmedBookmarks.Select(b => b.ToString())));
            }
            parameters.Add("longPolling", longPolling.ToString());

            var query = String.Join("&", parameters.Select(p => p.Key + "=" + p.Value).ToArray());

            if (trackedSource is DerivationSchemeTrackedSource dsts)
            {
                return(await SendAsync <UTXOChanges>(HttpMethod.Get, null, "v1/cryptos/{0}/derivations/{1}/utxos?" + query, new object[] { CryptoCode, dsts.DerivationStrategy.ToString() }, cancellation).ConfigureAwait(false));
            }
            else if (trackedSource is AddressTrackedSource asts)
            {
                return(await SendAsync <UTXOChanges>(HttpMethod.Get, null, "v1/cryptos/{0}/addresses/{1}/utxos?" + query, new object[] { CryptoCode, asts.Address }, cancellation).ConfigureAwait(false));
            }
            else
            {
                throw UnSupported(trackedSource);
            }
        }
Пример #3
0
        public async Task <IActionResult> TrackWallet(
            string cryptoCode,
            [ModelBinder(BinderType = typeof(DerivationStrategyModelBinder))]
            DerivationStrategyBase derivationScheme,
            [ModelBinder(BinderType = typeof(BitcoinAddressModelBinder))]
            BitcoinAddress address)
        {
            TrackedSource trackedSource = GetTrackedSource(derivationScheme, address);

            if (trackedSource == null)
            {
                return(NotFound());
            }
            var network = GetNetwork(cryptoCode, false);

            if (trackedSource is DerivationSchemeTrackedSource dts)
            {
                foreach (var feature in Enum.GetValues(typeof(DerivationFeature)).Cast <DerivationFeature>())
                {
                    await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, new GenerateAddressQuery(minAddresses: 3, null));
                }
                foreach (var feature in Enum.GetValues(typeof(DerivationFeature)).Cast <DerivationFeature>())
                {
                    _ = AddressPoolService.GenerateAddresses(network, dts.DerivationStrategy, feature);
                }
            }
            else if (trackedSource is IDestination ats)
            {
                await RepositoryProvider.GetRepository(network).Track(ats);
            }
            return(Ok());
        }
Пример #4
0
 public TrackedTransaction(TrackedTransactionKey key, TrackedSource trackedSource, IEnumerable <Coin> receivedCoins, Dictionary <Script, KeyPath> knownScriptMapping)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     if (!key.IsPruned)
     {
         throw new ArgumentException("The key should be pruned", nameof(key));
     }
     if (trackedSource == null)
     {
         throw new ArgumentNullException(nameof(trackedSource));
     }
     TrackedSource = trackedSource;
     Key           = key;
     if (knownScriptMapping != null)
     {
         KnownKeyPathMapping = knownScriptMapping;
     }
     if (receivedCoins != null)
     {
         ReceivedCoins.AddRange(receivedCoins);
     }
 }
Пример #5
0
 public Task <GetTransactionsResponse> GetTransactionsAsync(TrackedSource trackedSource, GetTransactionsResponse previous, bool longPolling, CancellationToken cancellation = default)
 {
     return(GetTransactionsAsync(trackedSource,
                                 previous == null ? null : new[] { previous.ConfirmedTransactions.Bookmark },
                                 previous == null ? null : new[] { previous.UnconfirmedTransactions.Bookmark },
                                 previous == null ? null : new[] { previous.ReplacedTransactions.Bookmark }, longPolling, cancellation));
 }
Пример #6
0
        public TrackedTransaction(TrackedTransactionKey key, TrackedSource trackedSource, Transaction transaction, Dictionary <Script, KeyPath> knownScriptMapping)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (knownScriptMapping == null)
            {
                throw new ArgumentNullException(nameof(knownScriptMapping));
            }
            if (trackedSource == null)
            {
                throw new ArgumentNullException(nameof(trackedSource));
            }
            if (key.IsPruned)
            {
                throw new ArgumentException("The key should not be pruned", nameof(key));
            }
            Key           = key;
            TrackedSource = trackedSource;
            Transaction   = transaction;
            transaction.PrecomputeHash(false, true);
            KnownKeyPathMapping = knownScriptMapping;

            KnownKeyPathMappingUpdated();
        }
Пример #7
0
 public BitcoinConnector(string oracleAddress, string endpoint, Channel <Deposit> depositStream, Channel <WithdrawInfo> withdrawStream)
 {
     this.depositStream  = depositStream;
     this.withdrawStream = withdrawStream;
     this.oracleAddress  = TrackedSource.Create(new BitcoinPubKeyAddress(oracleAddress));
     client = new ExplorerClient(new NBXplorerNetworkProvider(NetworkType.Testnet).GetBTC(), new Uri(endpoint));
     client.SetNoAuth();
 }
Пример #8
0
        private void UpdateTxConfirmations(WalletAddr addr)
        {
            var baddr         = BitcoinAddress.Create(addr.Address, GetNetwork());
            var trackedSource = TrackedSource.Create(baddr);
            var txs           = GetClient().GetTransactions(trackedSource);

            UpdateTxConfirmations(txs);
        }
Пример #9
0
 public Task <UTXOChanges> GetUTXOsAsync(DerivationStrategyBase extKey, Bookmark[] confirmedBookmarks, Bookmark[] unconfirmedBookmarks, bool longPolling = true, CancellationToken cancellation = default)
 {
     if (extKey == null)
     {
         throw new ArgumentNullException(nameof(extKey));
     }
     return(GetUTXOsAsync(TrackedSource.Create(extKey), confirmedBookmarks, unconfirmedBookmarks, longPolling, cancellation));
 }
Пример #10
0
 public void RebroadcastPeriodically(NBXplorerNetwork network, TrackedSource trackedSource, params TrackedTransactionKey[] txIds)
 {
     if (network == null)
     {
         throw new ArgumentNullException(nameof(network));
     }
     _BroadcastedTransactionsByCryptoCode[network].RebroadcastPeriodically(trackedSource, txIds);
 }
Пример #11
0
 public Task <UTXOChanges> GetUTXOsAsync(DerivationStrategyBase extKey, CancellationToken cancellation = default)
 {
     if (extKey == null)
     {
         throw new ArgumentNullException(nameof(extKey));
     }
     return(GetUTXOsAsync(TrackedSource.Create(extKey), cancellation));
 }
Пример #12
0
        public async Task RebroadcastPeriodically(NBXplorerNetwork network, TrackedSource trackedSource, params uint256[] txIds)
        {
            List <TrackedTransactionKey> keys = new List <TrackedTransactionKey>();

            foreach (var txId in txIds)
            {
                keys.AddRange((await _Repositories.GetRepository(network).GetTransactions(trackedSource, txId)).Select(k => k.Key));
            }
            RebroadcastPeriodically(network, trackedSource, keys.ToArray());
        }
    public async Task TrackClaim(PaymentMethodId paymentMethodId, IClaimDestination claimDestination)
    {
        var network        = _btcPayNetworkProvider.GetNetwork <BTCPayNetwork>(paymentMethodId.CryptoCode);
        var explorerClient = _explorerClientProvider.GetExplorerClient(network);

        if (claimDestination is IBitcoinLikeClaimDestination bitcoinLikeClaimDestination)
        {
            await explorerClient.TrackAsync(TrackedSource.Create(bitcoinLikeClaimDestination.Address));
        }
    }
Пример #14
0
        internal static uint160 GetHash(this TrackedSource trackedSource)
        {
            if (trackedSource is DerivationSchemeTrackedSource t)
            {
                return(t.DerivationStrategy.GetHash());
            }
            var data = Encoding.UTF8.GetBytes(trackedSource.ToString());

            return(new uint160(Hashes.RIPEMD160(data, data.Length)));
        }
Пример #15
0
        private static TrackedSource GetTrackedSource(DerivationStrategyBase derivationScheme, BitcoinAddress address)
        {
            TrackedSource trackedSource = null;

            if (address != null)
            {
                trackedSource = new AddressTrackedSource(address);
            }
            if (derivationScheme != null)
            {
                trackedSource = new DerivationSchemeTrackedSource(derivationScheme);
            }
            return(trackedSource);
        }
Пример #16
0
 public IEnumerable <MatchedOutput> GetReceivedOutputs(TrackedSource trackedSource)
 {
     return(this.ReceivedCoins
            .Select(o => (Index: (int)o.Outpoint.N,
                          Output: o,
                          KeyPath: KnownKeyPathMapping.TryGet(o.ScriptPubKey)))
            .Where(o => o.KeyPath != null || o.Output.ScriptPubKey == (trackedSource as IDestination)?.ScriptPubKey)
            .Select(o => new MatchedOutput()
     {
         Index = o.Index,
         Value = o.Output.Amount,
         KeyPath = o.KeyPath,
         ScriptPubKey = o.Output.ScriptPubKey
     }));
 }
Пример #17
0
 public void RebroadcastPeriodically(TrackedSource trackedSource, params TrackedTransactionKey[] txIds)
 {
     lock (CollectionLock)
     {
         if (TransactionsHashSet.TryGetValue(trackedSource, out var v))
         {
             v.AddKeys(txIds);
         }
         else
         {
             TransactionsHashSet.Add(trackedSource, new RebroadcastedTransaction(txIds)
             {
                 TrackedSource = trackedSource
             });
         }
     }
 }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            if (reader.TokenType != JsonToken.String)
            {
                return(null);
            }

            if (TrackedSource.TryParse(reader.Value.ToString(), out var v, Network))
            {
                return(v);
            }
            throw new JsonObjectException("Invalid TrackedSource", reader);
        }
Пример #19
0
 public Task <GetTransactionsResponse> GetTransactionsAsync(TrackedSource trackedSource, CancellationToken cancellation = default)
 {
     if (trackedSource == null)
     {
         throw new ArgumentNullException(nameof(trackedSource));
     }
     if (trackedSource is DerivationSchemeTrackedSource dsts)
     {
         return(SendAsync <GetTransactionsResponse>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/derivations/{dsts.DerivationStrategy}/transactions", null, cancellation));
     }
     else if (trackedSource is AddressTrackedSource asts)
     {
         return(SendAsync <GetTransactionsResponse>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/addresses/{asts.Address}/transactions", null, cancellation));
     }
     else
     {
         throw UnSupported(trackedSource);
     }
 }
Пример #20
0
 public Task TrackAsync(TrackedSource trackedSource, CancellationToken cancellation = default)
 {
     if (trackedSource == null)
     {
         throw new ArgumentNullException(nameof(trackedSource));
     }
     if (trackedSource is DerivationSchemeTrackedSource dsts)
     {
         return(SendAsync <string>(HttpMethod.Post, null, "v1/cryptos/{0}/derivations/{1}", new[] { CryptoCode, dsts.DerivationStrategy.ToString() }, cancellation));
     }
     else if (trackedSource is AddressTrackedSource asts)
     {
         return(SendAsync <string>(HttpMethod.Post, null, "v1/cryptos/{0}/addresses/{1}", new[] { CryptoCode, asts.Address.ToString() }, cancellation));
     }
     else
     {
         throw UnSupported(trackedSource);
     }
 }
Пример #21
0
 public async Task <UTXOChanges> GetUTXOsAsync(TrackedSource trackedSource, CancellationToken cancellation = default)
 {
     if (trackedSource == null)
     {
         throw new ArgumentNullException(nameof(trackedSource));
     }
     if (trackedSource is DerivationSchemeTrackedSource dsts)
     {
         return(await SendAsync <UTXOChanges>(HttpMethod.Get, null, "v1/cryptos/{0}/derivations/{1}/utxos", new object[] { CryptoCode, dsts.DerivationStrategy.ToString() }, cancellation).ConfigureAwait(false));
     }
     else if (trackedSource is AddressTrackedSource asts)
     {
         return(await SendAsync <UTXOChanges>(HttpMethod.Get, null, "v1/cryptos/{0}/addresses/{1}/utxos", new object[] { CryptoCode, asts.Address }, cancellation).ConfigureAwait(false));
     }
     else
     {
         throw UnSupported(trackedSource);
     }
 }
Пример #22
0
        public async Task <IActionResult> TrackWallet(
            string cryptoCode,
            [ModelBinder(BinderType = typeof(DerivationStrategyModelBinder))]
            DerivationStrategyBase derivationScheme,
            [ModelBinder(BinderType = typeof(BitcoinAddressModelBinder))]
            BitcoinAddress address, [FromBody] TrackWalletRequest request = null)
        {
            request = request ?? new TrackWalletRequest();
            TrackedSource trackedSource = GetTrackedSource(derivationScheme, address);

            if (trackedSource == null)
            {
                return(NotFound());
            }
            var network = GetNetwork(cryptoCode, false);

            if (trackedSource is DerivationSchemeTrackedSource dts)
            {
                if (request.Wait)
                {
                    foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
                    {
                        await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
                    }
                }
                else
                {
                    foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
                    {
                        await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, new GenerateAddressQuery(minAddresses: 3, null));
                    }
                    foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
                    {
                        _ = AddressPoolService.GenerateAddresses(network, dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
                    }
                }
            }
            else if (trackedSource is IDestination ats)
            {
                await RepositoryProvider.GetRepository(network).Track(ats);
            }
            return(Ok());
        }
        public Task <TrackedSource> ConstructTrackedSource()
        {
            var data = GetData();

            var explorerClient = _nbXplorerClientProvider.GetClient(data.CryptoCode);
            var factory        = explorerClient.Network.DerivationStrategyFactory;

            if (string.IsNullOrEmpty(data
                                     .DerivationStrategy))
            {
                return(Task.FromResult <TrackedSource>(TrackedSource.Create(BitcoinAddress.Create(
                                                                                data.Address,
                                                                                explorerClient.Network.NBitcoinNetwork))));
            }
            else
            {
                return(Task.FromResult <TrackedSource>(TrackedSource.Create(_derivationSchemeParser.Parse(factory,
                                                                                                          data.DerivationStrategy))));
            }
        }
Пример #24
0
 internal static string ToPrettyString(this TrackedSource trackedSource)
 {
     if (trackedSource is DerivationSchemeTrackedSource derivation)
     {
         var strategy = derivation.DerivationStrategy.ToString();
         if (strategy.Length > 35)
         {
             strategy = strategy.Substring(0, 10) + "..." + strategy.Substring(strategy.Length - 20);
         }
         return(strategy);
     }
     else if (trackedSource is AddressTrackedSource addressDerivation)
     {
         return(addressDerivation.Address.ToString());
     }
     else
     {
         return(trackedSource.ToString());
     }
 }
Пример #25
0
        public Task <GetTransactionsResponse> GetTransactionsAsync(TrackedSource trackedSource, Bookmark[] confirmedBookmarks, Bookmark[] unconfirmedBookmarks, Bookmark[] replacedBookmarks, bool longPolling, CancellationToken cancellation = default)
        {
            if (trackedSource == null)
            {
                throw new ArgumentNullException(nameof(trackedSource));
            }
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            if (confirmedBookmarks != null)
            {
                parameters.Add("confirmedBookmarks", String.Join(",", confirmedBookmarks.Select(b => b.ToString())));
            }
            if (unconfirmedBookmarks != null)
            {
                parameters.Add("unconfirmedBookmarks", String.Join(",", unconfirmedBookmarks.Select(b => b.ToString())));
            }
            if (replacedBookmarks != null)
            {
                parameters.Add("replacedBookmarks", String.Join(",", replacedBookmarks.Select(b => b.ToString())));
            }
            parameters.Add("longPolling", longPolling.ToString());
            var query = String.Join("&", parameters.Select(p => p.Key + "=" + p.Value).ToArray());


            if (trackedSource is DerivationSchemeTrackedSource dsts)
            {
                return(SendAsync <GetTransactionsResponse>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/derivations/{dsts.DerivationStrategy}/transactions?" + query, null, cancellation));
            }
            else if (trackedSource is AddressTrackedSource asts)
            {
                return(SendAsync <GetTransactionsResponse>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/addresses/{asts.Address}/transactions?" + query, null, cancellation));
            }
            else
            {
                throw UnSupported(trackedSource);
            }
        }
Пример #26
0
        List <TransactionInformationMatch> ToMatch(AnnotatedTransactionCollection txs,
                                                   List <TxOut> outputs,
                                                   TrackedSource derivation)
        {
            var result = new List <TransactionInformationMatch>();

            for (int i = 0; i < outputs.Count; i++)
            {
                if (outputs[i] == null)
                {
                    continue;
                }
                if (!IsMatching(derivation, outputs[i].ScriptPubKey, txs))
                {
                    continue;
                }
                var keyPath = txs.GetKeyPath(outputs[i].ScriptPubKey);
                result.Add(new TransactionInformationMatch()
                {
                    Index = i, KeyPath = keyPath, Value = outputs[i].Value
                });
            }
            return(result);
        }
Пример #27
0
 public TrackedTransaction(TrackedTransactionKey key, TrackedSource trackedSource, IEnumerable <Coin> receivedCoins, IEnumerable <KeyPathInformation> knownScriptMapping)
     : this(key, trackedSource, receivedCoins, ToDictionary(knownScriptMapping))
 {
 }
Пример #28
0
 public TrackedTransaction(TrackedTransactionKey key, TrackedSource trackedSource) : this(key, trackedSource, null as Coin[], null as Dictionary <Script, KeyPath>)
 {
 }
Пример #29
0
 public TransactionInformation GetTransaction(TrackedSource trackedSource, uint256 txId, CancellationToken cancellation = default)
 {
     return(this.GetTransactionAsync(trackedSource, txId, cancellation).GetAwaiter().GetResult());
 }
Пример #30
0
 public Task <GetTransactionsResponse> GetTransactionsAsync(DerivationStrategyBase strategy, Bookmark[] confirmedBookmarks, Bookmark[] unconfirmedBookmarks, Bookmark[] replacedBookmarks, bool longPolling, CancellationToken cancellation = default)
 {
     return(GetTransactionsAsync(TrackedSource.Create(strategy), confirmedBookmarks, unconfirmedBookmarks, replacedBookmarks, longPolling, cancellation));
 }