Exemplo n.º 1
0
        public void TestRpcImportPubkeyIsSuccessful()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node  = builder.CreateStratisPowNode(new BitcoinRegTest()).AlwaysFlushBlocks().WithWallet().Start();
                CoreNode node2 = builder.CreateStratisPowNode(new BitcoinRegTest()).WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Miner).Start();

                TestHelper.ConnectAndSync(node, node2);

                UnspentOutputReference tx = node2.FullNode.WalletManager().GetUnspentTransactionsInWallet("mywallet", 0, Features.Wallet.Wallet.NormalAccounts).First();

                RPCClient rpc = node.CreateRPCClient();

                PubKey pubKey  = PayToPubkeyTemplate.Instance.ExtractScriptPubKeyParameters(tx.Address.Pubkey);
                PubKey pubKey2 = new Key().PubKey;

                uint256 blockHash = rpc.GenerateToAddress(1, pubKey2.GetAddress(rpc.Network)).First();
                Block   block     = rpc.GetBlock(blockHash);
                uint256 tx2       = block.Transactions.First().GetHash();

                Assert.Throws <RPCException>(() => rpc.SendCommand(RPCOperations.gettransaction, tx.Transaction.Id.ToString(), true));
                Assert.Throws <RPCException>(() => rpc.SendCommand(RPCOperations.gettransaction, tx2.ToString(), true));;

                // Test that adding the same pubkey twice doesn't throw.
                rpc.ImportPubKey(pubKey.ToHex());
                rpc.ImportPubKey(pubKey.ToHex());

                // Add a second pubkey and ensure it doesn't throw.
                rpc.ImportPubKey(pubKey2.ToHex());

                // Add an arbitrary pubkey and ensure it doesn't throw.
                rpc.ImportPubKey(new Key().PubKey.ToHex());

                TestBase.WaitLoop(() => node.FullNode.WalletManager().WalletTipHeight == node2.FullNode.WalletManager().WalletTipHeight);

                TestBase.WaitLoop(() =>
                {
                    try
                    {
                        // Check if gettransaction can now find the transactions in the watch only account.
                        RPCResponse walletTx  = rpc.SendCommand(RPCOperations.gettransaction, tx.Transaction.Id.ToString(), true);
                        RPCResponse walletTx2 = rpc.SendCommand(RPCOperations.gettransaction, tx2.ToString(), true);

                        return(walletTx != null && walletTx2 != null);
                    }
                    catch (RPCException)
                    {
                        return(false);
                    }
                });

                // Check that when include_watchonly is not set, the watched addresses' transactions cannot be located in the normal wallet accounts.
                Assert.Throws <RPCException>(() => rpc.SendCommand(RPCOperations.gettransaction, tx.Transaction.Id.ToString(), false));
                Assert.Throws <RPCException>(() => rpc.SendCommand(RPCOperations.gettransaction, tx2.ToString(), false));
            }
        }
Exemplo n.º 2
0
        public void CanGenerateToAddress()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start();

                RPCClient rpcClient = node.CreateRPCClient();

                var privateKey = new Key();

                uint256[] blockHash = rpcClient.GenerateToAddress(1, privateKey.ScriptPubKey.GetDestinationAddress(rpcClient.Network));
                Block     block     = rpcClient.GetBlock(blockHash[0]);

                Assert.Equal(privateKey.ScriptPubKey, block.Transactions[0].Outputs[0].ScriptPubKey);
            }
        }