Beispiel #1
0
        public bool PayAccountBalanceNode(SpendTx spendTx, string accountCode, string cashCodeForChange)
        {
            try
            {
                foreach (var tx in spendTx.Transactions)
                {
                    Money minerFee = tx.MinerFee;

                    for (int i = 0; i < tx.TxIds.Count; i++)
                    {
                        TxId  input    = tx.TxIds[i];
                        Money moneyOut = input.MoneyOut;

                        if (i == tx.TxIds.Count - 1)
                        {
                            moneyOut -= tx.ChangeAmount;
                        }

                        NodeCash.proc_TxPayAccount
                        (
                            tx.PaymentAddress,
                            $"{input.TransactionId}",
                            moneyOut.ToUnit(MoneyUnit.MilliBTC),
                            minerFee.ToUnit(MoneyUnit.MilliBTC),
                            accountCode
                        );

                        minerFee = minerFee <= moneyOut ? Money.Zero : minerFee - moneyOut;
                    }

                    foreach (var output in tx.Tx.Outputs)
                    {
                        string toAddress = $"{output.ScriptPubKey.GetDestinationAddress(GetNetwork)}";
                        if (toAddress == spendTx.ChangeAddress)
                        {
                            string toTxId = $"{tx.Tx.GetHash()}";

                            NodeCash.proc_ChangeTxAdd
                            (
                                toAddress,
                                toTxId,
                                (short)TxStatus.UTXO,
                                output.Value.ToUnit(MoneyUnit.MilliBTC),
                                0,
                                spendTx.TxMessage
                            );

                            NodeCash.proc_TxPayInChange(CashAccountCode, toAddress, toTxId, accountCode, cashCodeForChange, Properties.Resources.PaymentChange);
                        }
                    }
                }

                return(true);
            }
            catch (Exception err)
            {
                Console.WriteLine($"Error: {err.Message}");
                return(false);
            }
        }
Beispiel #2
0
        public void GetStatement(string keyPath)
        {
            ExtKey key = hdRoot.Derive(new KeyPath(keyPath));

            List <TxId>    txIds   = new List <TxId>();
            List <uint256> outputs = new List <uint256>();

            var script         = key.PrivateKey.GetWif(GetNetwork).GetAddress(ScriptPubKeyType.Legacy).ScriptPubKey;
            var paymentAddress = key.PrivateKey.GetWif(GetNetwork).GetAddress(ScriptPubKeyType.Legacy);

            var client = BlockchainApi;


            var balance = client.GetBalance(script, false).Result;

            foreach (var op in balance.Operations)
            {
                var transactionResponse = client.GetTransaction(op.TransactionId).Result;

                foreach (var coin in op.ReceivedCoins)
                {
                    if (coin.TxOut.ScriptPubKey == script)
                    {
                        TxId txId = txIds.Where(id => id.TransactionId == op.TransactionId).FirstOrDefault();

                        if (txId == null)
                        {
                            txId = new TxId(op.TransactionId);
                            txId.Confirmations = transactionResponse.Block == null ? 0 : transactionResponse.Block.Confirmations;
                            txIds.Add(txId);
                        }

                        txId.MoneyIn += (Money)coin.Amount;
                    }
                }

                foreach (TxIn input in transactionResponse.Transaction.Inputs)
                {
                    if (input.ScriptSig == script)
                    {
                        outputs.Add(input.PrevOut.Hash);
                    }
                }
            }

            foreach (var tx in txIds)
            {
                if (outputs.Where(id => id == tx.TransactionId).Any())
                {
                    tx.Status   = TxStatus.Spent;
                    tx.MoneyOut = tx.MoneyIn;
                }

                NodeCash.proc_ChangeTxAdd($"{paymentAddress}", $"{tx.TransactionId}", (short)tx.Status, tx.MoneyIn.ToUnit(MoneyUnit.MilliBTC), tx.Confirmations, tx.TxMessage);
            }
        }
Beispiel #3
0
        public bool KeyTransferNode(SpendTx spend, string toKey, string cashCodeFrom, string cashCodeTo)
        {
            try
            {
                string paymentRef = string.Empty;

                paymentRef = $"{spend.FromKey} -> {toKey}";

                foreach (var tx in spend.Transactions)
                {
                    Money minerFee = tx.MinerFee;

                    for (int i = 0; i < tx.TxIds.Count; i++)
                    {
                        TxId  input    = tx.TxIds[i];
                        Money moneyOut = input.MoneyOut;

                        if (i == tx.TxIds.Count - 1)
                        {
                            moneyOut -= tx.ChangeAmount;
                        }

                        NodeCash.proc_TxPayOutTransfer
                        (
                            tx.PaymentAddress,
                            $"{input.TransactionId}",
                            moneyOut.ToUnit(MoneyUnit.MilliBTC),
                            minerFee.ToUnit(MoneyUnit.MilliBTC),
                            cashCodeFrom,
                            paymentRef
                        );

                        minerFee = minerFee <= moneyOut ? Money.Zero : minerFee - moneyOut;
                    }

                    foreach (var output in tx.Tx.Outputs)
                    {
                        string toAddress = $"{output.ScriptPubKey.GetDestinationAddress(GetNetwork)}";
                        string toTxId    = $"{tx.Tx.GetHash()}";

                        NodeCash.proc_ChangeTxAdd
                        (
                            toAddress,
                            toTxId,
                            (short)TxStatus.UTXO,
                            output.Value.ToUnit(MoneyUnit.MilliBTC),
                            0,
                            spend.TxMessage
                        );

                        if (toAddress == spend.ChangeAddress)
                        {
                            paymentRef = $"{spend.FromKey} -> {spend.FromKey}";
                        }
                        else
                        {
                            paymentRef = $"{spend.FromKey} -> {toKey}";
                        }

                        TxPayIn(toAddress, toTxId, string.Empty, cashCodeTo, DateTime.Now, paymentRef);
                    }
                }

                return(true);
            }
            catch (Exception err)
            {
                Console.WriteLine($"Error: {err.Message}");
                return(false);
            }
        }