void PrintAddressAndBlockExplorer(string desc, byte[] address) { m_paragraphTransaction.Inlines.Add(desc + ": 0x" + Encode.BytesToHex(address)); foreach (string url in m_lBlockExplorer) { if (!url.Contains("<address>")) { continue; } Uri uri = new Uri(url.Replace("<address>", Encode.BytesToHex(address))); if (uri.IsFile) { continue; } Hyperlink hyperlink = new Hyperlink(new Run(uri.Host)) { NavigateUri = uri, ToolTip = uri }; hyperlink.Click += (sender, e) => System.Diagnostics.Process.Start(uri.ToString()); m_paragraphTransaction.Inlines.Add(" "); m_paragraphTransaction.Inlines.Add(hyperlink); } m_paragraphTransaction.Inlines.Add("\n"); }
public static List <Tuple <string, string> > ReadAddressInfo(string file) { var lEntry = new List <Tuple <string, string> >(); foreach (string line in ReadTrimmedLines(file)) { // format: address [info] string address = line; string desc = ""; int index = line.IndexOf(" "); if (index != -1) { address = line.Substring(0, index); desc = line.Substring(index).Trim(); } byte[] data = Encode.HexToBytes(address, 20); if (data != null) { lEntry.Add(new Tuple <string, string>(Encode.BytesToHex(data), desc)); } } return(lEntry); }
public static bool IntegrityCheck() { string file = typeof(Org.BouncyCastle.Math.BigInteger).Assembly.Location; byte[] buffer = File.ReadAllBytes(file); byte[] hash = new System.Security.Cryptography.SHA256Managed().ComputeHash(buffer); return(Encode.BytesToHex(hash) == "83ba441c5572bba81381427c18ae36eeb9c8b831e51edd449a54a31838a5577d"); }
static void Main() { ETHTransaction tx = new ETHTransaction(); tx.m_nonce = BigInteger.Parse("16"); tx.m_gasPrice = BigInteger.Parse("10000000000"); tx.m_gasLimit = BigInteger.Parse("21000"); tx.m_to = Encode.HexToBytes("1111111111111111111111111111111111111111"); tx.m_value = BigInteger.Parse("100"); tx.m_initOrData = Encode.HexToBytes(""); tx.ECDsaSign("ETH", Encode.BytesToBigInteger(Encode.HexToBytes("1111111111111111111111111111111111111111111111111111111111111111"))); Console.Write(Encode.BytesToHex(tx.EncodeRLP())); }
void Output(BigInteger privateKey) { BigInteger publicKeyX; BigInteger publicKeyY; BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY); byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY); byte[] address = ETHAddress.PublicKeyToETHAddress(publicKey); m_paragraphAddress.Inlines.Add("Private key (confidential): 0x" + Encode.BytesToHex(Encode.BigIntegerToBytes(privateKey, 32)) + "\n"); m_paragraphAddress.Inlines.Add("Public key: 0x" + Encode.BytesToHex(publicKey) + "\n"); m_paragraphAddress.Inlines.Add("ETH address: 0x" + Encode.BytesToHex(address) + "\n"); m_paragraphAddress.Inlines.Add("ETC address: 0x" + Encode.BytesToHex(address) + "\n"); }
static void Main(string[] args) { int passwordStrengthBit = 128; // value less than 64 is strongly not recommended if (args.Length != 2) { Console.WriteLine("usage: charset number"); return; } string charset = Password.GetCharset(args[0]); if (charset.Length == 0) { Console.WriteLine("Invalid charset."); return; } ulong number; if (!ulong.TryParse(args[1], out number)) { Console.WriteLine("Invalid number."); return; } for (ulong i = 0; i < number; i++) { BigInteger publicKeyX; BigInteger publicKeyY; string password = Password.GeneratePassword(charset, BigInteger.Pow(2, passwordStrengthBit)); BigInteger privateKey = Password.PasswordToPrivateKey(password); BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY); byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY); byte[] address = ETHAddress.PublicKeyToETHAddress(publicKey); Console.WriteLine("0x" + Encode.BytesToHex(address) + " " + password); } }
void UpdateTransaction(bool loadNonce) { m_paragraphTransaction.Inlines.Clear(); // Password / Private key BigInteger privateKey; if (EllipticCurve.HexToPrivateKey(m_passwordBoxPP.Password, out privateKey)) { m_passwordBoxPP.Background = Brushes.LightSkyBlue; } else { m_passwordBoxPP.Background = Brushes.White; if (Password.GetKeySpace(m_passwordBoxPP.Password) < BigInteger.Pow(2, m_passwordStrengthBit)) { return; } privateKey = Password.PasswordToPrivateKey(m_passwordBoxPP.Password); } BigInteger publicKeyX; BigInteger publicKeyY; BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY); byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY); byte[] address = ETHAddress.PublicKeyToETHAddress(publicKey); PrintAddressAndBlockExplorer("From", address); // Nonce if (loadNonce) { string text = ""; if (m_dAddressNonce.ContainsKey(Encode.BytesToHex(address))) { text = m_dAddressNonce[Encode.BytesToHex(address)]; } Dispatcher.BeginInvoke(new Action(() => m_comboBoxNonce.Text = text)); } if (!BigInteger.TryParse(m_comboBoxNonce.Text, out m_tx.m_nonce)) { m_paragraphTransaction.Inlines.Add("Nonce: <Invalid>\n"); return; } if (m_tx.m_nonce < 0) { m_paragraphTransaction.Inlines.Add("Nonce: <Invalid>\n"); return; } m_paragraphTransaction.Inlines.Add("Nonce: " + m_tx.m_nonce + "\n"); // To string to = m_comboBoxTo.Text.Trim(); int index = to.IndexOf(" "); if (index != -1) { to = to.Substring(0, index); } m_tx.m_to = Encode.HexToBytes(to); if (m_tx.m_to == null) { m_paragraphTransaction.Inlines.Add("To: <Invalid>\n"); return; } else if (m_tx.m_to.Length == 0) { m_paragraphTransaction.Inlines.Add("To: Empty\n"); } else if (m_tx.m_to.Length == 20) { PrintAddressAndBlockExplorer("To", m_tx.m_to); } else { m_paragraphTransaction.Inlines.Add("To: <Invalid>\n"); return; } // Value if (Encode.DecimalToBigInteger(m_comboBoxValue.Text, 18, out m_tx.m_value)) { m_paragraphTransaction.Inlines.Add("Value: " + Encode.BigIntegerToDecimal(m_tx.m_value, 18) + " " + GetCurrency() + "\n"); } else { m_paragraphTransaction.Inlines.Add("Value: <Invalid>\n"); return; } // Init / Data if (m_tx.m_initOrData.Length > 0) { m_paragraphTransaction.Inlines.Add("Init / Data: " + m_tx.m_initOrData.Length + " bytes\n"); } // Gas price if (!Encode.DecimalToBigInteger(m_comboBoxGasPrice.Text, 18, out m_tx.m_gasPrice)) { m_paragraphTransaction.Inlines.Add("Gas price: <Invalid>\n"); return; } // m_paragraphTransaction.Inlines.Add("Gas price: " + Encode.BigIntegerToDecimal(m_tx.m_gasPrice, 18) + " " + GetCurrency() + "\n"); // Gas limit if (!BigInteger.TryParse(m_comboBoxGasLimit.Text, out m_tx.m_gasLimit)) { m_paragraphTransaction.Inlines.Add("Gas limit: <Invalid>\n"); return; } if (m_tx.m_gasLimit < 0) { m_paragraphTransaction.Inlines.Add("Gas limit: <Invalid>\n"); return; } // m_paragraphTransaction.Inlines.Add("Gas limit: " + m_tx.m_gasLimit + "\n"); BigInteger gasLimitMin = m_tx.GetGasLimitMin(); if (m_tx.m_gasLimit < gasLimitMin) { m_paragraphTransaction.Inlines.Add(new Run("Minimal gas limit is " + gasLimitMin + ".\n") { Foreground = Brushes.Red }); } // calculation m_paragraphTransaction.Inlines.Add(new Run("Fee: " + Encode.BigIntegerToDecimal(m_tx.m_gasPrice * m_tx.m_gasLimit, 18) + " " + GetCurrency() + "\n") { ToolTip = "Fee = Gas price * Gas limit" }); m_paragraphTransaction.Inlines.Add(new Run("Value + Fee: " + Encode.BigIntegerToDecimal(m_tx.m_value + m_tx.m_gasPrice * m_tx.m_gasLimit, 18) + " " + GetCurrency() + "\n") { ToolTip = "Fee = Gas price * Gas limit" }); // additional check if (m_tx.m_to.Length == 0 && m_tx.m_initOrData.Length == 0) { return; } // tx m_tx.ECDsaSign(GetCurrency(), privateKey); byte[] tx = m_tx.EncodeRLP(); byte[] hash = BouncyCastle.Keccak(tx); // button foreach (string node in m_lNode) { Button buttonSend = new Button() { Margin = new Thickness(2), Content = "Send to " + node, ToolTip = "Send raw transaction to " + node }; buttonSend.Click += (sender, e) => { // save string file = GetCurrency() + "_tx_" + DateTime.Now.ToString("yyMMdd_HHmmss") + "_0x" + Encode.BytesToHex(hash); File.WriteAllBytes(file, tx); // nonce read m_dAddressNonce.Clear(); foreach (var entry in ConfigFile.ReadAddressInfo(GetCurrency() + "_nonce")) { m_dAddressNonce[entry.Item1] = entry.Item2; } // nonce update m_dAddressNonce[Encode.BytesToHex(address)] = m_tx.m_nonce.ToString(); // nonce write List <string> lLine = new List <string>(); foreach (var entry in m_dAddressNonce) { lLine.Add("0x" + entry.Key + " " + entry.Value); } File.WriteAllLines(GetCurrency() + "_nonce", lLine); // send System.Diagnostics.Process.Start("send_transaction.exe", file + " " + node + " -pause"); }; m_paragraphTransaction.Inlines.Add(buttonSend); } Button buttonCopy = new Button() { Margin = new Thickness(2), Content = "Copy", ToolTip = "Copy raw transaction to clipboard" }; Button buttonSave = new Button() { Margin = new Thickness(2), Content = "Save", ToolTip = "Save raw transaction to file" }; buttonCopy.Click += (sender, e) => Clipboard.SetText(Encode.BytesToHex(tx)); buttonSave.Click += (sender, e) => { var dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = GetCurrency() + "_tx_0x" + Encode.BytesToHex(hash); if (dlg.ShowDialog() == true) { File.WriteAllBytes(dlg.FileName, tx); } }; m_paragraphTransaction.Inlines.Add(buttonCopy); m_paragraphTransaction.Inlines.Add(buttonSave); }