Exemple #1
0
        public static Transaction CreateAndSignCoinstakeTransaction(SegWitCoin kernelCoin, long totalReward, uint currentBlockTime, string passphrase, out Key privateKey)
        {
            var tx = CreateCoinstakeTransaction(kernelCoin, totalReward, currentBlockTime, passphrase, out privateKey);

            SigningService.SignInputs(tx, new[] { privateKey }, new[] { kernelCoin });

            return(tx);
        }
Exemple #2
0
        public TransactionResponse BuildTransaction(List <Recipient> recipients, bool sign, string passphrase = null, List <Burn> burns = null)
        {
            var tx = this.network.CreateTransaction();

            // add recipients
            long recipientsAmount = 0;

            foreach (Recipient recipient in recipients)
            {
                recipientsAmount += recipient.Amount;
                tx.Outputs.Add(new TxOut(recipient.Amount, recipient.Address.GetScriptPubKey()));
            }

            // add burns
            long burnAmount = 0;

            if (burns != null)
            {
                foreach (Burn burn in burns)
                {
                    burnAmount += burn.Amount;
                    tx.Outputs.Add(new TxOut(burn.Amount, TxNullDataTemplate.Instance.GenerateScriptPubKey(burn.Data)));
                }
            }

            long totalSendAmount = recipientsAmount + burnAmount;


            // calculate size, fee and change amount
            long addedFee = this.minimumPossibleFee;

            SegWitCoin[] selectedCoins;

            while (true)
            {
                tx.Inputs.Clear();

                int  selectedCount  = 0;
                long selectedAmount = 0;

                selectedCoins = AddCoins(totalSendAmount, addedFee).ToArray();

                foreach (var c in selectedCoins)
                {
                    selectedCount++;
                    selectedAmount += c.UtxoValue;
                    tx.Inputs.Add(new TxIn(new OutPoint(c.UtxoTxHash, c.UtxoTxN)));
                }

                var virtualSize = tx.GetVirtualSize() + selectedCount * SignatureVirtualSize + ChangeOutputVirtualSize;

                long requiredFee = virtualSize * this.feePer1000VBytes / 1000;

                if (addedFee == requiredFee)
                {
                    var change = selectedAmount - totalSendAmount - addedFee;
                    if (change > this.dustThreshold)
                    {
                        TxOut changeOutput = GetOutputForChange(passphrase, !sign);
                        changeOutput.Value = change;
                        tx.Outputs.Add(changeOutput);
                    }
                    break;
                }

                addedFee = requiredFee;
            }

            // signing
            if (sign)
            {
                var keys = DecryptKeys(selectedCoins, passphrase);
                SigningService.SignInputs(tx, keys, selectedCoins);
            }

            var response = new TransactionResponse
            {
                Transaction    = tx,
                Hex            = tx.ToHex(),
                Fee            = addedFee,
                VirtualSize    = tx.GetVirtualSize(),
                SerializedSize = tx.GetSerializedSize(),
                TransactionId  = tx.GetHash(),
                BroadcastState = BroadcastState.NotSet
            };

            return(response);
        }