public DC.Common.Models.UnspentResponse GetUnspentTxns(String address) { String url = String.Format("https://blockchain.info/unspent?active={0}", address); WebRequest request = WebRequest.Create(url); //request.UseDefaultCredentials = true; try { using (Stream objStream = request.GetResponse().GetResponseStream()) { if (objStream != null) { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(DC.Common.Models.UnspentResponse)); DC.Common.Models.UnspentResponse response = (DC.Common.Models.UnspentResponse)jsonSerializer.ReadObject(objStream); //TODO, ORDER BY return(response); } else { return(null); } } } catch (Exception ex) { //An internal server error is thrown here usually because there are no free outputs to spend throw new InvalidOperationException("There are insufficient cleared funds for this transaction. Please try again later", ex); } }
public int GetConfirmations(string publicKey) { if (!String.IsNullOrEmpty(publicKey)) { try { DC.Common.Models.UnspentResponse utxoResponse = Helpers.Factory.GetTransactionProvider().GetUnspentTxns(publicKey); //Get unspent txs, largest to smallest IEnumerable <DC.Common.Models.UnspentOutput> outputs = (from u in utxoResponse.UnspentOutputs where u.Confirmations > 0 select u).OrderByDescending(u => u.Value); if (outputs.Count() > 0) { return(outputs.First().Confirmations); } else { return(0); } } catch { //Returns return(0); } } else { throw new ArgumentException("Public Key cannot be null"); } }
/// <summary> /// Sign and send bitcoins using bitcoin core /// </summary> /// <param name="privateKey">Private key as decimal</param> /// <param name="sourceAddress">Bitcoin address to receive change</param> /// <param name="destinationAddress"></param> /// <param name="amount"></param> private String SignAndTransfer(String privateKey, String destinationAddress, Decimal amount) { DC.Common.Models.Address sourceAddress = new Common.Models.Address(privateKey); DC.Common.Models.UnspentResponse utxoResponse = Helpers.Factory.GetTransactionProvider().GetUnspentTxns (sourceAddress.BTCAddress); Decimal fee = Convert.ToDecimal(ConfigurationManager.AppSettings["fee"]); fee = 0.0001M; //Get unspent txs, largest to smallest IEnumerable <DC.Common.Models.UnspentOutput> outputs = (from u in utxoResponse.UnspentOutputs where u.Confirmations > 0 select u).OrderByDescending(u => u.Value); //There needs to be sufficient balance for the amount plus the fee Int64 target = Convert.ToInt64((amount + fee) * DC.Common.Math.SATOSHI); if (outputs.Count() > 0) { bool sufficientFunds = false; IList <DC.Common.Models.UnspentOutput> outputsToSpend = new List <Common.Models.UnspentOutput>(); foreach (DC.Common.Models.UnspentOutput output in outputs) { outputsToSpend.Add(output); Int64 i = outputsToSpend.Sum(o => o.Value); if (i >= target) { sufficientFunds = true; break; } } if (!sufficientFunds) { string message; if (outputsToSpend.Sum(o => o.Value) == amount * DC.Common.Math.SATOSHI) { message = "There are insufficient funds to cover the transaction amount plus the bitcoin miner fee of 0.0001 Bitcoin"; } else { message = "There are insufficient funds to complete this transaction"; } throw new InvalidOperationException(message); } DC.Providers.IAddress provider = new DC.Providers.BlockChainInfo(); String destinationHash160 = provider.GetHash160(destinationAddress); Helpers.Transaction Transaction = new Helpers.Transaction(outputsToSpend, sourceAddress, destinationAddress, destinationHash160, amount, fee); Byte[] signedTransaction = Transaction.CreateAndSignTransaction(sourceAddress, destinationAddress, destinationHash160, amount, fee); String signature = DC.Common.StringHelper.ByteArrayToHexString(signedTransaction); // Providers.IWallet wallet = Helpers.Factory.GetWallet(); Providers.IWallet wallet = new Providers.BlockChainInfo() { MerchantId = "4ea10592-e7cb-4170-9b5b-6e94e3236bb1", Password = "******" }; //ADD YOUR BLOCK CHAIN INFO CREDS HERE! String txnHash = wallet.SendRawTransaction(signature); return(txnHash); } else { throw new ArgumentException("The address does not have any unspent transactions"); } }