Ejemplo n.º 1
0
        public string NewKeyPath(string parentName, string childName)
        {
            string childHDPath = string.Empty;

            NodeCash.proc_AccountKeyAdd(CashAccountCode, parentName, childName, ref childHDPath);
            return(childHDPath);
        }
Ejemplo n.º 2
0
        public bool ReceiveCoins(BackgroundWorker sender)
        {
            try
            {
                float count = 0, percentComplete = 0;

                var hdPaths = NodeCash.vwChangeCollections.Select(rec => rec);

                foreach (var hdpath in hdPaths)
                {
                    count++;
                    percentComplete = (count / hdPaths.Count()) * 100;
                    sender?.ReportProgress((int)percentComplete);

                    GetStatement(hdpath.FullHDPath);
                }

                return(NodeCash.proc_PaymentPost() == 0);
            }
            catch (Exception e)
            {
                //ErrorLog(e);
                throw e;
            }
        }
Ejemplo n.º 3
0
        public string RenameKey(string oldName, string newName)
        {
            string keyNamespace = string.Empty;

            NodeCash.proc_AccountKeyRename(CashAccountCode, oldName, newName, ref keyNamespace);
            return(keyNamespace);
        }
Ejemplo n.º 4
0
 public void PayOutCancel(SpendTx spend)
 {
     if (spend.ChangeAddress.Length > 0)
     {
         NodeCash.DeleteChangeKey(spend.ChangeAddress);
     }
 }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
0
 public void KeyTransferCancel(SpendTx spend)
 {
     NodeCash.DeleteChangeKey(spend.ToAddress);
     if (spend.ChangeAddress.Length > 0)
     {
         NodeCash.DeleteChangeKey(spend.ChangeAddress);
     }
 }
Ejemplo n.º 7
0
        public async Task <SpendTx> PayOutTx(AccountKey fromKey, string toAddress, decimal payAmount, int minersRate, string txMessage)
        {
            try
            {
                int?addressIndex = 0;

                string changeAddress  = string.Empty;
                double accountBalance = await KeyNameBalance(fromKey.KeyName);

                if ((decimal)accountBalance > payAmount)
                {
                    changeAddress = NewChangeKey(fromKey.KeyName, fromKey.HDPath, CoinChangeType.Change, ref addressIndex);

                    if (!AddChangeKey(fromKey.KeyName, changeAddress, (int)addressIndex, null))
                    {
                        return(null);
                    }
                }

                string changePath = $"{fromKey.HDPath}{(short)CoinChangeType.Change}/{addressIndex}/";

                Money spendAmount = new Money(payAmount, MoneyUnit.MilliBTC);

                SpendTx spend = new SpendTx(BlockchainApi)
                {
                    ToAddress      = toAddress,
                    ChangeAddress  = changeAddress,
                    FromKey        = fromKey.KeyName,
                    ChangeKey      = fromKey.KeyName,
                    MinerRate      = minersRate,
                    TxMessage      = txMessage,
                    TargetAmount   = spendAmount,
                    AccountBalance = new Money((decimal)accountBalance, MoneyUnit.MilliBTC)
                };

                var keyAddresses = NodeCash.fnKeyAddresses(CashAccountCode, fromKey.KeyName)
                                   .OrderBy(tb => tb.AddressIndex)
                                   .Select(tb => tb);

                foreach (var key in keyAddresses)
                {
                    spend.AddKey(GetExtendedKey(key.HDPath), GetExtendedKey(changePath));

                    if (spend.IsSatisfied)
                    {
                        break;
                    }
                }

                return(spend);
            }
            catch (Exception err)
            {
                Console.WriteLine($"Error: {err.Message}");
                return(null);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
 public bool TxPayIn(string paymentAddress, string txId, string accountCode, string cashCode, DateTime paidOn, string paymentReference)
 {
     try
     {
         return(NodeCash.TxPayIn(CashAccountCode, paymentAddress, txId, accountCode, cashCode, paidOn, paymentReference));
     }
     catch (Exception err)
     {
         Console.WriteLine($"Error: {err.Message}");
         return(false);
     }
 }
Ejemplo n.º 10
0
 public string NewChangeKey(string keyName, string hdPath, CoinChangeType changeType, ref int?addressIndex)
 {
     if (IsWalletInitialised)
     {
         NodeCash.proc_ChangeAddressIndex(CashAccountCode, keyName, (short)changeType, ref addressIndex);
         string changePath = $"{hdPath}{(short)changeType}/{addressIndex}/";
         ExtKey privateKey = hdRoot.Derive(new KeyPath(changePath));
         return($"{privateKey.PrivateKey.GetWif(GetNetwork).GetAddress(ScriptPubKeyType.Legacy)}");
     }
     else
     {
         return(string.Empty);
     }
 }
Ejemplo n.º 11
0
 public bool ChangeKeyNote(string paymentAddress, string note)
 {
     return(NodeCash.ChangeKeyNote(paymentAddress, note));
 }
Ejemplo n.º 12
0
 public bool DeleteReceiptKey(string paymentAddress)
 {
     return(NodeCash.DeleteChangeKey(paymentAddress));
 }
Ejemplo n.º 13
0
 public bool AssignReceiptKey(string keyName, string paymentAddress, string invoiceNumber, string note)
 {
     return(NodeCash.proc_ChangeAssign(CashAccountCode, keyName, paymentAddress, invoiceNumber, note) == 0);
 }
Ejemplo n.º 14
0
 public bool AddChangeKey(string keyName, CoinChangeType changeType, string paymentAddress, int addressIndex, string note, string invoiceNumber)
 {
     return(NodeCash.proc_ChangeNew(CashAccountCode, keyName, (short)changeType, paymentAddress, addressIndex, invoiceNumber, note?.Length == 0 ? null : note) == 0);
 }
Ejemplo n.º 15
0
 public bool DeleteKey(string keyName)
 {
     return(NodeCash.proc_AccountKeyDelete(CashAccountCode, keyName) == 0);
 }
Ejemplo n.º 16
0
        public async Task <double> KeyNameBalance(string keyName)
        {
            double balance = await NodeCash.KeyNameBalance(CashAccountCode, keyName);

            return(balance);
        }
Ejemplo n.º 17
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);
            }
        }