FreeMoney.BitcoinAddressForEmailService.BTCAddressForEmail C# (CSharp) Method

BTCAddressForEmail() public method

public BTCAddressForEmail ( string email_address ) : string
email_address string
return string
        public string BTCAddressForEmail(string email_address)
        {
            if (email_address == "") {
                return "";
            }

            string url = m_config["bitcoin_address_for_email_service_1_url"];

            if (url == "") {
                return "";
            }

            string payer_name = m_config["bitcoin_address_for_email_payer_name"];
            string message_text = m_config["bitcoin_address_for_email_message_text"];

            string post_data = "to=" + HttpUtility.UrlEncode(email_address)
                + "&from=" + HttpUtility.UrlEncode(payer_name)
                + "&message=" + HttpUtility.UrlEncode(message_text);

            HttpWebRequest httpWebRequest=(HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";

            ASCIIEncoding encoding = new ASCIIEncoding ();
            byte[] byte1 = encoding.GetBytes (post_data);

            m_log.Info("[FreeMoney] Sending post data"+post_data);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            httpWebRequest.ContentLength = byte1.Length;

            m_log.Info("[FreeMoney] Post length:"+httpWebRequest.ContentLength.ToString());

            Stream newStream = httpWebRequest.GetRequestStream ();
            newStream.Write (byte1, 0, byte1.Length);
            newStream.Close();

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse ();
            string response;
            using (StreamReader streamReader = new StreamReader (httpWebResponse.GetResponseStream ())) {
                response = streamReader.ReadToEnd ();
                streamReader.Close ();
            }

            if (httpWebResponse.StatusCode != HttpStatusCode.OK) {
                m_log.Warn("[FreeMoney] address service response code was ng");
                return "";
            }

            //Console.WriteLine(response);

            string pattern = "\\\"bitcoin\\:(.*?)\\\"";
            Match m = Regex.Match(response, pattern, RegexOptions.Multiline);
            if (m.Success) {

                GroupCollection gc = m.Groups;
                if (gc.Count < 2) {
                    m_log.Warn("[FreeMoney] Could not find an address in the response from the email service.");
                    return "";
                }
                string new_address = (string)gc[1].Value;
                if (!BitcoinAddress.IsValidAddress(new_address)) {
                    m_log.Warn("[FreeMoney] Created new address "+new_address+", but it did not look like a valid address.");
                    return "";
                }

                m_log.Info("[FreeMoney] Created new address "+new_address);
                return new_address;

            } else {
                m_log.Warn("[FreeMoney] Could not find an address in the response from the email service.");
            }

            return "";
        }

Usage Example

Ejemplo n.º 1
0
        public string AddressForAvatar(string user_identifier, string user_email)
        {
            string query = "select a.btc_address as btc_address from opensim_btc_addresses a left outer join opensim_btc_transactions t on a.btc_address=t.btc_address where a.user_identifier=?user_identifier AND t.confirmation_sent_ts > 0 OR t.id IS NULL limit 1;";

            using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
            {
                dbcon.Open();

                MySqlCommand cmd = new MySqlCommand(query, dbcon);

                try
                {
                    using (cmd)
                    {
                        cmd.Parameters.AddWithValue("?user_identifier", m_user_identifier);

                        using (MySqlDataReader dbReader = cmd.ExecuteReader())
                        {
                            if (dbReader.Read())
                            {
                                m_btc_address = (string)dbReader["btc_address"];
                            }
                        }

                        cmd.Dispose();

                        return(m_btc_address);
                    }
                }
                catch (Exception e)
                {
                    m_log.Error("[FreeMoney] Error fetching addresses for avatar: " + e.ToString());
                    //m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset {0} with name \"{1}\". Error: {2}",
                    //return "";
                }
            }

            if (user_email == "")
            {
                return("");
            }

            // No address yet - try to create one with an email service.
            BitcoinAddressForEmailService serv = new BitcoinAddressForEmailService(m_config);
            string new_btc_address             = serv.BTCAddressForEmail(user_email);

            if (new_btc_address != "")
            {
                // create a new object - we may want to make this whole method static.
                BitcoinAddress addr = new BitcoinAddress(m_connectionString, m_config);
                if (addr.Create(user_identifier, new_btc_address))
                {
                    return(new_btc_address);
                }
            }

            return("");
        }
All Usage Examples Of FreeMoney.BitcoinAddressForEmailService::BTCAddressForEmail