/// <summary>
        /// Adds a spendable cold staking transaction to a normal account, as oppose to dedicated special account.
        /// </summary>
        /// <param name="wallet">Wallet to add the transaction to.</param>
        /// <returns>The spendable transaction that was added to the wallet.</returns>
        private Transaction AddSpendableColdstakingTransactionToNormalWallet(Wallet.Wallet wallet, bool script = false)
        {
            // This will always be added to the secondary address.
            HdAddress address = wallet.GetAllAddresses().ToArray()[1];

            var transaction = this.Network.CreateTransaction();

            // Use the normal wallet address here.
            TxDestination hotPubKey  = BitcoinAddress.Create(address.Address, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);
            TxDestination coldPubKey = BitcoinAddress.Create(coldWalletAddress2, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);

            var scriptPubKey = new Script(OpcodeType.OP_DUP, OpcodeType.OP_HASH160, OpcodeType.OP_ROT, OpcodeType.OP_IF,
                                          OpcodeType.OP_CHECKCOLDSTAKEVERIFY, Op.GetPushOp(hotPubKey.ToBytes()), OpcodeType.OP_ELSE, Op.GetPushOp(coldPubKey.ToBytes()),
                                          OpcodeType.OP_ENDIF, OpcodeType.OP_EQUALVERIFY, OpcodeType.OP_CHECKSIG);

            transaction.Outputs.Add(new TxOut(Money.Coins(202), script ? scriptPubKey.WitHash.ScriptPubKey : scriptPubKey));

            address.Transactions.Add(new TransactionData()
            {
                Hex             = transaction.ToHex(this.Network),
                Amount          = transaction.Outputs[0].Value,
                Id              = transaction.GetHash(),
                BlockHeight     = 0,
                Index           = 0,
                IsCoinBase      = false,
                IsCoinStake     = false,
                IsColdCoinStake = true,
                IsPropagated    = true,
                BlockHash       = this.Network.GenesisHash,
                ScriptPubKey    = script ? scriptPubKey.WitHash.ScriptPubKey : scriptPubKey,
            });

            return(transaction);
        }
Exemplo n.º 2
0
        public List <Transaction> RetrieveFilteredUtxos(string walletName, string walletPassword, string transactionHex, FeeRate feeRate, string walletAccount = null)
        {
            var retrievalTransactions = new List <Transaction>();

            Transaction transactionToReclaim = this.network.Consensus.ConsensusFactory.CreateTransaction(transactionHex);

            foreach (TxOut output in transactionToReclaim.Outputs)
            {
                Wallet.Wallet wallet = this.GetWallet(walletName);

                HdAddress address = wallet.GetAllAddresses(Wallet.Wallet.AllAccounts).FirstOrDefault(a => a.ScriptPubKey == output.ScriptPubKey);

                // The address is not in the wallet so ignore this output.
                if (address == null)
                {
                    continue;
                }

                HdAccount destinationAccount = wallet.GetAccounts(Wallet.Wallet.NormalAccounts).First();

                // This shouldn't really happen unless the user has no proper accounts in the wallet.
                if (destinationAccount == null)
                {
                    continue;
                }

                Script destination = destinationAccount.GetFirstUnusedReceivingAddress().ScriptPubKey;

                ISecret extendedPrivateKey = wallet.GetExtendedPrivateKeyForAddress(walletPassword, address);

                Key privateKey = extendedPrivateKey.PrivateKey;

                var builder = new TransactionBuilder(this.network);

                var coin = new Coin(transactionToReclaim, output);

                builder.AddCoins(coin);
                builder.AddKeys(privateKey);
                builder.Send(destination, output.Value);
                builder.SubtractFees();
                builder.SendEstimatedFees(feeRate);

                Transaction builtTransaction = builder.BuildTransaction(true);

                retrievalTransactions.Add(builtTransaction);
            }

            return(retrievalTransactions);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a spendable transaction to a wallet.
        /// </summary>
        /// <param name="wallet">Wallet to add the transaction to.</param>
        /// <returns>The spendable transaction that was added to the wallet.</returns>
        private Transaction AddSpendableTransactionToWallet(Wallet.Wallet wallet)
        {
            HdAddress address = wallet.GetAllAddresses().FirstOrDefault();

            Transaction transaction = this.Network.CreateTransaction();

            transaction.Outputs.Add(new TxOut(Money.Coins(101), address.ScriptPubKey));

            address.Transactions.Add(new TransactionData()
            {
                Hex          = transaction.ToHex(this.Network),
                Amount       = transaction.Outputs[0].Value,
                Id           = transaction.GetHash(),
                BlockHeight  = 0,
                Index        = 0,
                IsCoinBase   = false,
                IsCoinStake  = false,
                IsPropagated = true,
                BlockHash    = this.Network.GenesisHash,
                ScriptPubKey = address.ScriptPubKey
            });

            return(transaction);
        }