Пример #1
0
        public void CanEasilySpendUTXOs()
        {
            using (var tester = ServerTester.Create())
            {
                var userExtKey           = new ExtKey();
                var userDerivationScheme = tester.Client.Network.DerivationStrategyFactory.CreateDirectDerivationStrategy(userExtKey.Neuter(), new DerivationStrategyOptions()
                {
                    // Use non-segwit
                    Legacy = true
                });
                tester.Client.Track(userDerivationScheme);
                var utxos = tester.Client.GetUTXOs(userDerivationScheme, null, false);

                // Send 1 BTC
                var newAddress = tester.Client.GetUnused(userDerivationScheme, DerivationFeature.Direct);
                tester.Explorer.CreateRPCClient().SendToAddress(newAddress.ScriptPubKey.GetDestinationAddress(Network.RegTest), Money.Coins(1.0m));
                utxos = tester.Client.GetUTXOs(userDerivationScheme, utxos, true);

                // Send 1 more BTC
                newAddress = tester.Client.GetUnused(userDerivationScheme, DerivationFeature.Deposit);
                tester.Explorer.CreateRPCClient().SendToAddress(newAddress.ScriptPubKey.GetDestinationAddress(Network.RegTest), Money.Coins(1.0m));
                utxos = tester.Client.GetUTXOs(userDerivationScheme, utxos, true);

                utxos = tester.Client.GetUTXOs(userDerivationScheme, null, false);
                Assert.Equal(2, utxos.GetUnspentCoins().Length);
                for (int i = 0; i < 3; i++)
                {
                    var changeAddress          = tester.Client.GetUnused(userDerivationScheme, DerivationFeature.Change);
                    var coins                  = utxos.GetUnspentCoins();
                    var keys                   = utxos.GetKeys(userExtKey);
                    TransactionBuilder builder = new TransactionBuilder();
                    builder.AddCoins(coins);
                    builder.AddKeys(keys);
                    builder.Send(new Key(), Money.Coins(0.5m));
                    builder.SetChange(changeAddress.ScriptPubKey);

                    var fallbackFeeRate = new FeeRate(Money.Satoshis(100), 1);
                    var feeRate         = tester.Client.GetFeeRate(1, fallbackFeeRate).FeeRate;

                    builder.SendEstimatedFees(feeRate);
                    var tx = builder.BuildTransaction(true);
                    Assert.True(tester.Client.Broadcast(tx).Success);

                    utxos = tester.Client.GetUTXOs(userDerivationScheme, utxos, true);
                    utxos = tester.Client.GetUTXOs(userDerivationScheme, null, false);

                    if (i == 0)
                    {
                        Assert.Equal(2, utxos.GetUnspentCoins().Length);
                    }

                    Assert.Contains(utxos.GetUnspentCoins(), u => u.ScriptPubKey == changeAddress.ScriptPubKey);
                    Assert.Contains(utxos.Unconfirmed.UTXOs, u => u.ScriptPubKey == changeAddress.ScriptPubKey && u.Feature == DerivationFeature.Change);
                }
            }
        }
Пример #2
0
        public void CanTrackSeveralTransactions()
        {
            using (var tester = ServerTester.Create())
            {
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());
                tester.Client.Track(pubkey);
                var utxo = tester.Client.Sync(pubkey, null, null, true);                 //Track things do not wait

                var addresses = new HashSet <Script>();
                tester.RPC.ImportPrivKey(PrivateKeyOf(key, "0/0"));
                var id = tester.RPC.SendToAddress(AddressOf(key, "0/0"), Money.Coins(1.0m));
                addresses.Add(AddressOf(key, "0/0").ScriptPubKey);

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.True(utxo.HasChanges);

                var coins = Money.Coins(1.0m);
                int i     = 0;
                for (i = 0; i < 20; i++)
                {
                    LockTestCoins(tester.RPC, addresses);
                    var spendable = tester.RPC.ListUnspent(0, 0);
                    coins = coins - Money.Coins(0.001m);
                    var destination = AddressOf(key, $"0/{i + 1}");

                    tester.RPC.ImportPrivKey(PrivateKeyOf(key, $"0/{i + 1}"));
                    tester.RPC.SendToAddress(destination, coins);
                    addresses.Add(destination.ScriptPubKey);
                }

                while (true)
                {
                    utxo = tester.Client.Sync(pubkey, utxo);
                    if (!utxo.HasChanges)
                    {
                        Assert.False(true, "should have changes");
                    }
                    Assert.False(utxo.Confirmed.Reset);
                    Assert.True(utxo.Unconfirmed.HasChanges);
                    Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                    if (new KeyPath($"0/{i}").Equals(utxo.Unconfirmed.UTXOs[0].KeyPath))
                    {
                        break;
                    }
                }

                tester.RPC.Generate(1);

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.True(utxo.Confirmed.HasChanges);
                Assert.Equal(0, utxo.Confirmed.SpentOutpoints.Count);
            }
        }
Пример #3
0
        public void CanTrack4()
        {
            using (var tester = ServerTester.Create())
            {
                var bob   = new BitcoinExtKey(new ExtKey(), tester.Network);
                var alice = new BitcoinExtKey(new ExtKey(), tester.Network);

                var bobPubKey   = CreateDerivationStrategy(bob.Neuter());
                var alicePubKey = CreateDerivationStrategy(alice.Neuter());

                tester.Client.Track(alicePubKey);
                var utxoAlice = tester.Client.GetUTXOs(alicePubKey, Bookmark.Start, Bookmark.Start, true); //Track things do not wait
                tester.Client.Track(bobPubKey);
                var utxoBob = tester.Client.GetUTXOs(bobPubKey, null, false);                              //Track things do not wait
                Assert.NotNull(utxoAlice.Confirmed.KnownBookmark);
                Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

                var id = tester.RPC.SendToAddress(AddressOf(alice, "0/1"), Money.Coins(1.0m));
                id        = tester.RPC.SendToAddress(AddressOf(bob, "0/2"), Money.Coins(0.1m));
                utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.GetUTXOs(bobPubKey, utxoBob);
                Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

                tester.RPC.Generate(1);

                utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.GetUTXOs(bobPubKey, utxoBob);
                Assert.NotNull(utxoAlice.Confirmed.KnownBookmark);

                LockTestCoins(tester.RPC);
                tester.RPC.ImportPrivKey(PrivateKeyOf(alice, "0/1"));
                tester.RPC.SendToAddress(AddressOf(bob, "0/3"), Money.Coins(0.6m));

                utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.GetUTXOs(bobPubKey, utxoBob);
                Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

                utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice, false);
                Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

                tester.RPC.Generate(1);

                utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.GetUTXOs(bobPubKey, utxoBob);

                Assert.NotNull(utxoAlice.Confirmed.KnownBookmark);
                Assert.Single(utxoAlice.Confirmed.SpentOutpoints);
                Assert.Empty(utxoAlice.Confirmed.UTXOs);

                Assert.Empty(utxoBob.Confirmed.SpentOutpoints);
                Assert.Single(utxoBob.Confirmed.UTXOs);
                Assert.Equal("0/3", utxoBob.Confirmed.UTXOs[0].KeyPath.ToString());
            }
        }
Пример #4
0
        public void CanTrack4()
        {
            using (var tester = ServerTester.Create())
            {
                var bob   = new BitcoinExtKey(new ExtKey(), tester.Network);
                var alice = new BitcoinExtKey(new ExtKey(), tester.Network);

                var bobPubKey   = CreateDerivationStrategy(bob.Neuter());
                var alicePubKey = CreateDerivationStrategy(alice.Neuter());

                tester.Client.Track(alicePubKey);
                var utxoAlice = tester.Client.Sync(alicePubKey, uint256.Zero, uint256.Zero, true); //Track things do not wait
                tester.Client.Track(bobPubKey);
                var utxoBob = tester.Client.Sync(bobPubKey, null, null, true);                     //Track things do not wait
                Assert.False(utxoAlice.Confirmed.Reset);
                Assert.False(utxoAlice.Unconfirmed.Reset);

                var id = tester.RPC.SendToAddress(AddressOf(alice, "0/1"), Money.Coins(1.0m));
                id        = tester.RPC.SendToAddress(AddressOf(bob, "0/2"), Money.Coins(0.1m));
                utxoAlice = tester.Client.Sync(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.Sync(bobPubKey, utxoBob);
                Assert.False(utxoAlice.Unconfirmed.Reset);

                tester.RPC.Generate(1);

                utxoAlice = tester.Client.Sync(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.Sync(bobPubKey, utxoBob);
                Assert.False(utxoAlice.Confirmed.Reset);

                LockTestCoins(tester.RPC);
                tester.RPC.ImportPrivKey(PrivateKeyOf(alice, "0/1"));
                tester.RPC.SendToAddress(AddressOf(bob, "0/3"), Money.Coins(0.6m));

                utxoAlice = tester.Client.Sync(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.Sync(bobPubKey, utxoBob);
                Assert.False(utxoAlice.Unconfirmed.Reset);

                utxoAlice = tester.Client.Sync(alicePubKey, utxoAlice, true);
                Assert.False(utxoAlice.Unconfirmed.Reset);

                tester.RPC.Generate(1);

                utxoAlice = tester.Client.Sync(alicePubKey, utxoAlice);
                utxoBob   = tester.Client.Sync(bobPubKey, utxoBob);

                Assert.False(utxoAlice.Confirmed.Reset);
                Assert.Equal(1, utxoAlice.Confirmed.SpentOutpoints.Count);
                Assert.Equal(0, utxoAlice.Confirmed.UTXOs.Count);

                Assert.Equal(0, utxoBob.Confirmed.SpentOutpoints.Count);
                Assert.Equal(1, utxoBob.Confirmed.UTXOs.Count);
                Assert.Equal("0/3", utxoBob.Confirmed.UTXOs[0].KeyPath.ToString());
            }
        }
Пример #5
0
        public void CanGetTransactionsOfDerivation()
        {
            using (var tester = ServerTester.Create())
            {
                tester.Client.WaitServerStarted(Timeout);
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());

                tester.Client.Track(pubkey);
                var utxo = tester.Client.GetUTXOs(pubkey, null, false);                 //Track things do not wait

                var txId   = tester.RPC.SendToAddress(AddressOf(key, "0/0"), Money.Coins(1.0m));
                var result = tester.Client.GetTransactions(pubkey, new[] { Bookmark.Start }, new[] { Bookmark.Start }, new[] { Bookmark.Start });
                Assert.True(result.HasChanges());
                Assert.Single(result.UnconfirmedTransactions.Transactions);
                var height          = result.Height;
                var timestampUnconf = result.UnconfirmedTransactions.Transactions[0].Timestamp;
                Assert.Null(result.UnconfirmedTransactions.Transactions[0].BlockHash);
                Assert.Null(result.UnconfirmedTransactions.Transactions[0].Height);
                Assert.Equal(0, result.UnconfirmedTransactions.Transactions[0].Confirmations);
                Assert.Equal(result.UnconfirmedTransactions.Transactions[0].Transaction.GetHash(), result.UnconfirmedTransactions.Transactions[0].TransactionId);

                tester.Client.IncludeTransaction = false;
                result = tester.Client.GetTransactions(pubkey, new[] { Bookmark.Start }, new[] { Bookmark.Start }, new[] { Bookmark.Start });
                Assert.Null(result.UnconfirmedTransactions.Transactions[0].Transaction);

                result = tester.Client.GetTransactions(pubkey, result, false);
                Assert.False(result.HasChanges());

                tester.RPC.Generate(1);
                result = tester.Client.GetTransactions(pubkey, result);
                Assert.True(result.HasChanges());
                Assert.Null(result.UnconfirmedTransactions.KnownBookmark);

                var gotConf = result.ConfirmedTransactions.Bookmark;

                var txId2 = tester.RPC.SendToAddress(AddressOf(key, "0"), Money.Coins(1.0m));
                result = tester.Client.GetTransactions(pubkey, result);
                Assert.True(result.HasChanges());
                Assert.Equal(gotConf, result.ConfirmedTransactions.KnownBookmark);
                Assert.Single(result.UnconfirmedTransactions.Transactions);
                Assert.Equal(txId2, result.UnconfirmedTransactions.Transactions[0].TransactionId);

                result = tester.Client.GetTransactions(pubkey, null, null, null, false);
                Assert.True(result.HasChanges());
                Assert.Single(result.ConfirmedTransactions.Transactions);
                Assert.Single(result.UnconfirmedTransactions.Transactions);
                Assert.Equal(txId, result.ConfirmedTransactions.Transactions[0].TransactionId);
                Assert.Equal(timestampUnconf, result.ConfirmedTransactions.Transactions[0].Timestamp);
                Assert.Equal(txId2, result.UnconfirmedTransactions.Transactions[0].TransactionId);
            }
        }
Пример #6
0
 public void ShouldBlockIfNoChange()
 {
     using (var tester = ServerTester.Create())
     {
         var       bob   = pubKey;
         var       utxo  = tester.Client.Sync(bob, null, true);
         Stopwatch watch = new Stopwatch();
         watch.Start();
         var result = tester.Client.Sync(bob, utxo);
         watch.Stop();
         Assert.True(watch.Elapsed > TimeSpan.FromSeconds(10));
     }
 }
Пример #7
0
        public void ShowRBFedTransaction()
        {
            using (var tester = ServerTester.Create())
            {
                var bob = DummyPubKey;
                tester.Client.Track(bob);
                var utxo = tester.Client.GetUTXOs(bob, null, false);                 //Track things do not wait
                var a1   = tester.Client.GetUnused(bob, DerivationFeature.Deposit, 0);

                var payment1 = Money.Coins(0.04m);
                var payment2 = Money.Coins(0.08m);

                var tx1 = new uint256(tester.RPC.SendCommand("sendtoaddress", new object[]
                {
                    a1.ScriptPubKey.GetDestinationAddress(tester.Network).ToString(),
                    payment1.ToString(),
                    null,                     //comment
                    null,                     //comment_to
                    false,                    //subtractfeefromamount
                    true,                     //replaceable
                }).ResultString);

                utxo = tester.Client.GetUTXOs(bob, utxo);                 //Wait tx received
                Assert.Equal(tx1, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);

                var tx = tester.RPC.GetRawTransaction(new uint256(tx1));
                foreach (var input in tx.Inputs)
                {
                    input.ScriptSig = Script.Empty;                     //Strip signatures
                }
                var output = tx.Outputs.First(o => o.Value == payment1);
                output.Value = payment2;
                var change = tx.Outputs.First(o => o.Value != payment1);
                change.Value -= (payment2 - payment1) * 2;                 //Add more fees
                var replacement = tester.RPC.SignRawTransaction(tx);

                tester.RPC.SendRawTransaction(replacement);

                var prevUtxo = utxo;
                utxo = tester.Client.GetUTXOs(bob, prevUtxo);                 //Wait tx received
                Assert.Null(utxo.Unconfirmed.KnownBookmark);
                Assert.Equal(replacement.GetHash(), utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);
                Assert.Single(utxo.Unconfirmed.UTXOs);

                var txs = tester.Client.GetTransactions(bob, null);
                Assert.Single(txs.UnconfirmedTransactions.Transactions);
                Assert.Equal(replacement.GetHash(), txs.UnconfirmedTransactions.Transactions[0].TransactionId);
                Assert.Single(txs.ReplacedTransactions.Transactions);
                Assert.Equal(tx1, txs.ReplacedTransactions.Transactions[0].TransactionId);
            }
        }
Пример #8
0
        public void CanTrackDirect()
        {
            using (var tester = ServerTester.Create())
            {
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());
                tester.Client.Track(pubkey);
                var utxo = tester.Client.GetUTXOs(pubkey, null, false);         //Track things do not wait
                var tx1  = tester.RPC.SendToAddress(AddressOf(key, "0"), Money.Coins(1.0m));
                utxo = tester.Client.GetUTXOs(pubkey, utxo);
                Assert.NotNull(utxo.Confirmed.KnownBookmark);
                Assert.Single(utxo.Unconfirmed.UTXOs);
                Assert.Equal(tx1, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);


                LockTestCoins(tester.RPC);
                tester.RPC.ImportPrivKey(PrivateKeyOf(key, "0"));
                var tx2 = tester.RPC.SendToAddress(AddressOf(key, "1"), Money.Coins(0.6m));

                var prevUtxo = utxo;
                utxo = tester.Client.GetUTXOs(pubkey, utxo);
                Assert.NotNull(utxo.Unconfirmed.KnownBookmark);
                Assert.Single(utxo.Unconfirmed.UTXOs);
                Assert.Equal(tx2, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);         //got the 0.6m
                Assert.Equal(Money.Coins(0.6m), utxo.Unconfirmed.UTXOs[0].Value);   //got the 0.6m

                Assert.Single(utxo.Unconfirmed.SpentOutpoints);
                Assert.Equal(tx1, utxo.Unconfirmed.SpentOutpoints[0].Hash);         //previous coin is spent

                utxo = tester.Client.GetUTXOs(pubkey, prevUtxo.Confirmed.Bookmark, null);
                Assert.Null(utxo.Unconfirmed.KnownBookmark);
                Assert.Single(utxo.Unconfirmed.UTXOs);
                Assert.Empty(utxo.Unconfirmed.SpentOutpoints);         //should be skipped as the unconf coin were not known

                tester.RPC.SendToAddress(AddressOf(key, "0"), Money.Coins(0.15m));

                utxo = tester.Client.GetUTXOs(pubkey, utxo);
                Assert.Single(utxo.Unconfirmed.UTXOs);
                Assert.IsType <Coin>(utxo.Unconfirmed.UTXOs[0].AsCoin(pubkey));
                Assert.Equal(Money.Coins(0.15m), utxo.Unconfirmed.UTXOs[0].Value);
                Assert.Empty(utxo.Unconfirmed.SpentOutpoints);

                utxo = tester.Client.GetUTXOs(pubkey, null);
                Assert.Equal(2, utxo.Unconfirmed.UTXOs.Count);         //Should have 0.15 and 0.6
                Assert.Equal(Money.Coins(0.75m), utxo.Unconfirmed.UTXOs.Select(c => c.Value).Sum());
                Assert.Empty(utxo.Unconfirmed.SpentOutpoints);
            }
        }
Пример #9
0
        public void CanGetUnusedAddresses()
        {
            using (var tester = ServerTester.Create())
            {
                var bob  = DummyPubKey;
                var utxo = tester.Client.GetUTXOs(bob, null, false);                 //Track things do not wait

                var a1 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, 0);
                Assert.Null(a1);
                tester.Client.Track(bob);
                a1 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, 0);
                Assert.NotNull(a1);
                Assert.Equal(a1.ScriptPubKey, tester.Client.GetUnused(bob, DerivationFeature.Deposit, 0).ScriptPubKey);
                Assert.Equal(a1.ScriptPubKey, bob.Root.Derive(new KeyPath("0/0")).PubKey.Hash.ScriptPubKey);

                var a2 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, skip: 1);
                Assert.Equal(a2.ScriptPubKey, bob.Root.Derive(new KeyPath("0/1")).PubKey.Hash.ScriptPubKey);

                var a3 = tester.Client.GetUnused(bob, DerivationFeature.Change, skip: 0);
                Assert.Equal(a3.ScriptPubKey, bob.Root.Derive(new KeyPath("1/0")).PubKey.Hash.ScriptPubKey);

                var a4 = tester.Client.GetUnused(bob, DerivationFeature.Direct, skip: 1);
                Assert.Equal(a4.ScriptPubKey, bob.Root.Derive(new KeyPath("1")).PubKey.Hash.ScriptPubKey);

                Assert.Null(tester.Client.GetUnused(bob, DerivationFeature.Change, skip: 30));

                a3 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, skip: 2);
                Assert.Equal(new KeyPath("0/2"), a3.KeyPath);

                //   0/0 and 0/2 used
                tester.RPC.SendToAddressAsync(a1.ScriptPubKey.GetDestinationAddress(tester.Network), Money.Coins(1.0m));
                utxo = tester.Client.GetUTXOs(bob, utxo);                 //Wait tx received
                tester.RPC.SendToAddressAsync(a3.ScriptPubKey.GetDestinationAddress(tester.Network), Money.Coins(1.0m));
                utxo = tester.Client.GetUTXOs(bob, utxo);                 //Wait tx received

                tester.RPC.SendToAddressAsync(a4.ScriptPubKey.GetDestinationAddress(tester.Network), Money.Coins(1.0m));
                utxo = tester.Client.GetUTXOs(bob, utxo);                 //Wait tx received

                a1 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, 0);
                Assert.Equal(a1.ScriptPubKey, bob.Root.Derive(new KeyPath("0/1")).PubKey.Hash.ScriptPubKey);
                a2 = tester.Client.GetUnused(bob, DerivationFeature.Deposit, skip: 1);
                Assert.Equal(a2.ScriptPubKey, bob.Root.Derive(new KeyPath("0/3")).PubKey.Hash.ScriptPubKey);

                a4 = tester.Client.GetUnused(bob, DerivationFeature.Direct, skip: 1);
                Assert.Equal(a4.ScriptPubKey, bob.Root.Derive(new KeyPath("2")).PubKey.Hash.ScriptPubKey);
            }
        }
Пример #10
0
        public void CanTrack2()
        {
            using (var tester = ServerTester.Create())
            {
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());
                tester.Client.Track(pubkey);
                var utxo = tester.Client.Sync(pubkey, null, null, true);                 //Track things do not wait
                var tx1  = tester.RPC.SendToAddress(AddressOf(key, "0/0"), Money.Coins(1.0m));
                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.False(utxo.Confirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(tx1, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);


                LockTestCoins(tester.RPC);
                tester.RPC.ImportPrivKey(PrivateKeyOf(key, "0/0"));
                var tx2 = tester.RPC.SendToAddress(AddressOf(key, "1/0"), Money.Coins(0.6m));

                var prevUtxo = utxo;
                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.False(utxo.Unconfirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(tx2, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);                 //got the 0.6m
                Assert.Equal(Money.Coins(0.6m), utxo.Unconfirmed.UTXOs[0].Output.Value);    //got the 0.6m

                Assert.Equal(1, utxo.Unconfirmed.SpentOutpoints.Count);
                Assert.Equal(tx1, utxo.Unconfirmed.SpentOutpoints[0].Hash);                 //previous coin is spent

                utxo = tester.Client.Sync(pubkey, prevUtxo.Confirmed.Hash, null);
                Assert.True(utxo.Unconfirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(0, utxo.Unconfirmed.SpentOutpoints.Count);                 //should be skipped as the unconf coin were not known

                tester.RPC.SendToAddress(AddressOf(key, "0/0"), Money.Coins(0.15m));

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(Money.Coins(0.15m), utxo.Unconfirmed.UTXOs[0].Output.Value);
                Assert.Equal(0, utxo.Unconfirmed.SpentOutpoints.Count);

                utxo = tester.Client.Sync(pubkey, null);
                Assert.Equal(2, utxo.Unconfirmed.UTXOs.Count);                 //Should have 0.15 and 0.6
                Assert.Equal(Money.Coins(0.75m), utxo.Unconfirmed.UTXOs.Select(c => c.Output.Value).Sum());
                Assert.Equal(0, utxo.Unconfirmed.SpentOutpoints.Count);
            }
        }
Пример #11
0
 public void CanGetStatus()
 {
     using (var tester = ServerTester.Create())
     {
         tester.Client.WaitServerStarted(Timeout);
         var status = tester.Client.GetStatus();
         Assert.NotNull(status.BitcoinStatus);
         Assert.True(status.IsFullySynched);
         Assert.Equal(status.BitcoinStatus.Blocks, status.BitcoinStatus.Headers);
         Assert.Equal(status.BitcoinStatus.Blocks, status.ChainHeight);
         Assert.Equal(1.0, status.BitcoinStatus.VerificationProgress);
         Assert.NotNull(status.Version);
         Assert.Equal("BTC", status.CryptoCode);
         Assert.Equal(ChainType.Regtest, status.ChainType);
         Assert.Equal("BTC", status.SupportedCryptoCodes[0]);
         Assert.Single(status.SupportedCryptoCodes);
     }
 }
Пример #12
0
        public void CanBroadcast()
        {
            using (var tester = ServerTester.Create())
            {
                var tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                var funded = tester.User1.CreateRPCClient().FundRawTransaction(tx);
                var signed = tester.User1.CreateRPCClient().SignRawTransaction(funded.Transaction);
                var result = tester.Client.Broadcast(signed);
                Assert.True(result.Success);
                signed.Inputs[0].PrevOut.N = 999;
                result = tester.Client.Broadcast(signed);
                Assert.False(result.Success);

                var ex = Assert.Throws <NBXplorerException>(() => tester.Client.GetFeeRate(5));
                Assert.Equal("fee-estimation-unavailable", ex.Error.Code);
            }
        }
Пример #13
0
 RepositoryTester(string name, bool caching)
 {
     _Name = name;
     ServerTester.DeleteFolderRecursive(name);
     _Provider = new RepositoryProvider(new NBXplorerNetworkProvider(NetworkType.Regtest),
                                        new Configuration.ExplorerConfiguration()
     {
         DataDir             = name,
         ChainConfigurations = new List <Configuration.ChainConfiguration>()
         {
             new Configuration.ChainConfiguration()
             {
                 CryptoCode = "BTC",
                 Rescan     = false
             }
         }
     });
     _Repository = _Provider.GetRepository(new NBXplorerNetworkProvider(NetworkType.Regtest).GetFromCryptoCode("BTC"));
 }
Пример #14
0
 RepositoryTester(string name, bool caching)
 {
     _Name = name;
     ServerTester.DeleteFolderRecursive(name);
     _Provider = new RepositoryProvider(new NBXplorerNetworkProvider(ChainName.Regtest),
                                        KeyPathTemplates.Default,
                                        new Configuration.ExplorerConfiguration()
     {
         DataDir             = name,
         ChainConfigurations = new List <Configuration.ChainConfiguration>()
         {
             new Configuration.ChainConfiguration()
             {
                 CryptoCode = "BTC",
                 Rescan     = false
             }
         }
     });
     _Provider.StartAsync(default).GetAwaiter().GetResult();
Пример #15
0
        public void CanReserveAddress()
        {
            using (var tester = ServerTester.Create())
            {
                //WaitServerStarted not needed, just a sanity check
                tester.Client.WaitServerStarted();
                tester.Client.Track(pubKey);
                var utxo = tester.Client.Sync(pubKey, null, null, true);                 //Track things do not wait

                var tasks = new List <Task <KeyPathInformation> >();
                for (int i = 0; i < 100; i++)
                {
                    tasks.Add(tester.Client.GetUnusedAsync(pubKey, DerivationFeature.Deposit, reserve: true));
                }
                Task.WaitAll(tasks.ToArray());

                var paths = tasks.Select(t => t.Result).ToDictionary(c => c.KeyPath);
                Assert.Equal(99U, paths.Select(p => p.Key.Indexes.Last()).Max());

                tester.Client.CancelReservation(pubKey, new[] { new KeyPath("0/0") });
                Assert.Equal(new KeyPath("0/0"), tester.Client.GetUnused(pubKey, DerivationFeature.Deposit).KeyPath);
            }
        }
Пример #16
0
        public void CanUseWebSockets()
        {
            using (var tester = ServerTester.Create())
            {
                tester.Client.WaitServerStarted();
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());
                tester.Client.Track(pubkey);
                using (var connected = tester.Client.CreateNotificationSession())
                {
                    connected.ListenNewBlock();
                    var expectedBlockId = tester.Explorer.CreateRPCClient().Generate(1)[0];
                    var blockEvent      = (Models.NewBlockEvent)connected.NextEvent(Cancel);
                    Assert.Equal(expectedBlockId, blockEvent.Hash);
                    Assert.NotEqual(0, blockEvent.Height);

                    connected.ListenDerivationSchemes(new[] { pubkey });
                    tester.Explorer.CreateRPCClient().SendToAddress(AddressOf(key, "0/1"), Money.Coins(1.0m));

                    var txEvent = (Models.NewTransactionEvent)connected.NextEvent(Cancel);
                    Assert.Equal(txEvent.DerivationStrategy, pubkey);
                }
            }
        }
Пример #17
0
 RepositoryTester(string name, bool caching)
 {
     _Name = name;
     ServerTester.DeleteRecursivelyWithMagicDust(name);
     _Repository = new Repository(new Serializer(Network.RegTest), name);
 }
Пример #18
0
        public void CanTrack()
        {
            using (var tester = ServerTester.Create())
            {
                //WaitServerStarted not needed, just a sanity check
                tester.Client.WaitServerStarted();
                var key    = new BitcoinExtKey(new ExtKey(), tester.Network);
                var pubkey = CreateDerivationStrategy(key.Neuter());

                tester.Client.Track(pubkey);
                var utxo        = tester.Client.Sync(pubkey, null, null, true);          //Track things do not wait
                var gettingUTXO = tester.Client.SyncAsync(pubkey, utxo);
                var txId        = tester.RPC.SendToAddress(AddressOf(key, "0/0"), Money.Coins(1.0m));
                utxo = gettingUTXO.GetAwaiter().GetResult();
                Assert.Equal(113, utxo.CurrentHeight);

                Assert.False(utxo.Confirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(txId, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);
                Assert.Equal(0, utxo.Unconfirmed.UTXOs[0].Confirmations);
                Assert.Equal(0, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(uint256.Zero, utxo.Confirmed.Hash);
                Assert.NotEqual(uint256.Zero, utxo.Unconfirmed.Hash);

                var tx = tester.Client.GetTransaction(utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);
                Assert.NotNull(tx);
                Assert.Equal(0, tx.Confirmations);
                Assert.Equal(utxo.Unconfirmed.UTXOs[0].Outpoint.Hash, tx.Transaction.GetHash());

                tester.RPC.Generate(1);
                var prevUtxo = utxo;
                utxo = tester.Client.Sync(pubkey, prevUtxo);
                Assert.True(utxo.Unconfirmed.Reset);
                Assert.Equal(0, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(txId, utxo.Confirmed.UTXOs[0].Outpoint.Hash);
                Assert.Equal(1, utxo.Confirmed.UTXOs[0].Confirmations);
                Assert.NotEqual(uint256.Zero, utxo.Confirmed.Hash);
                var prevConfHash = utxo.Confirmed.Hash;

                txId = tester.RPC.SendToAddress(AddressOf(key, "0/1"), Money.Coins(1.0m));
                var txId1 = txId;

                prevUtxo = utxo;
                utxo     = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(0, utxo.Confirmed.UTXOs.Count);
                Assert.False(utxo.Confirmed.Reset);
                Assert.True(utxo.HasChanges);
                Assert.False(utxo.Unconfirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(txId, utxo.Unconfirmed.UTXOs[0].Outpoint.Hash);
                utxo = tester.Client.Sync(pubkey, null, null, true);

                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/1"), utxo.Unconfirmed.UTXOs[0].KeyPath);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/0"), utxo.Confirmed.UTXOs[0].KeyPath);
                Assert.Equal(prevConfHash, utxo.Confirmed.Hash);

                utxo = tester.Client.Sync(pubkey, utxo.Confirmed.Hash, null, true);
                Assert.False(utxo.Confirmed.Reset);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(0, utxo.Confirmed.UTXOs.Count);

                utxo = tester.Client.Sync(pubkey, null, utxo.Unconfirmed.Hash, true);
                Assert.True(utxo.Confirmed.Reset);
                Assert.Equal(0, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(1, utxo.Confirmed.UTXOs[0].Confirmations);

                Assert.Null(tester.Client.GetTransaction(uint256.One));
                tx = tester.Client.GetTransaction(utxo.Confirmed.UTXOs[0].Outpoint.Hash);
                Assert.NotNull(tx);
                Assert.Equal(1, tx.Confirmations);
                Assert.Equal(utxo.Confirmed.UTXOs[0].Outpoint.Hash, tx.Transaction.GetHash());
                tester.RPC.Generate(1);

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/1"), utxo.Confirmed.UTXOs[0].KeyPath);

                var outpoint01 = utxo.Confirmed.UTXOs[0].Outpoint;

                txId = tester.RPC.SendToAddress(AddressOf(key, "0/2"), Money.Coins(1.0m));
                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(0, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/2"), utxo.Unconfirmed.UTXOs[0].KeyPath);
                tester.RPC.Generate(1);

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/2"), utxo.Confirmed.UTXOs[0].KeyPath);


                utxo = tester.Client.Sync(pubkey, utxo, true);
                Assert.True(!utxo.HasChanges);

                var before01Spend = utxo.Confirmed.Hash;

                LockTestCoins(tester.RPC);
                tester.RPC.ImportPrivKey(PrivateKeyOf(key, "0/1"));
                txId = tester.RPC.SendToAddress(AddressOf(key, "0/3"), Money.Coins(0.5m));

                utxo = tester.Client.Sync(pubkey, utxo);
                Assert.Equal(1, utxo.Unconfirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/3"), utxo.Unconfirmed.UTXOs[0].KeyPath);
                Assert.Equal(1, utxo.Unconfirmed.SpentOutpoints.Count);                 // "0/1" should be spent
                Assert.Equal(txId1, utxo.Unconfirmed.SpentOutpoints[0].Hash);           // "0/1" should be spent

                utxo = tester.Client.Sync(pubkey, utxo, true);
                Assert.False(utxo.HasChanges);
                tester.RPC.Generate(1);

                utxo = tester.Client.Sync(pubkey, before01Spend, utxo.Unconfirmed.Hash);
                Assert.True(utxo.Unconfirmed.HasChanges);
                Assert.Equal(1, utxo.Confirmed.UTXOs.Count);
                Assert.Equal(new KeyPath("0/3"), utxo.Confirmed.UTXOs[0].KeyPath);
                Assert.Equal(1, utxo.Confirmed.SpentOutpoints.Count);
                Assert.Equal(outpoint01, utxo.Confirmed.SpentOutpoints[0]);

                utxo = tester.Client.Sync(pubkey, utxo, true);
                Assert.False(utxo.HasChanges);
            }
        }
Пример #19
0
 public void Dispose()
 {
     _Provider.Dispose();
     ServerTester.DeleteRecursivelyWithMagicDust(_Name);
 }
Пример #20
0
 public void Dispose()
 {
     _Repository.Dispose();
     ServerTester.DeleteRecursivelyWithMagicDust(_Name);
 }
Пример #21
0
 public void Dispose()
 {
     _Provider.DisposeAsync().GetAwaiter().GetResult();
     ServerTester.DeleteFolderRecursive(_Name);
 }
Пример #22
0
 public void Dispose()
 {
     _Provider.Dispose();
     ServerTester.DeleteFolderRecursive(_Name);
 }