private bool OnCreateAddressCommand(string[] args) { if (NoWallet()) { return(true); } if (args.Length > 3) { Console.WriteLine("error"); return(true); } string path = "address.txt"; if (File.Exists(path)) { if (!ReadUserInput($"The file '{path}' already exists, do you want to overwrite it? (yes|no)", false).IsYes()) { return(true); } } ushort count; if (args.Length >= 3) { count = ushort.Parse(args[2]); } else { count = 1; } List <string> addresses = new List <string>(); using (var percent = new ConsolePercent(0, count)) { Parallel.For(0, count, (i) => { WalletAccount account = CurrentWallet.CreateAccount(); addresses.Add(account.Address); lock (addresses) { addresses.Add(account.Address); percent.Value++; } }); } if (CurrentWallet is NEP6Wallet wallet) { wallet.Save(); } Console.WriteLine($"export addresses to {path}"); File.WriteAllLines(path, addresses); return(true); }
private void WriteBlocks(uint start, uint count, string path, bool writeStart) { uint end = start + count - 1; using FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.WriteThrough); if (fs.Length > 0) { byte[] buffer = new byte[sizeof(uint)]; if (writeStart) { fs.Seek(sizeof(uint), SeekOrigin.Begin); fs.Read(buffer, 0, buffer.Length); start += BitConverter.ToUInt32(buffer, 0); fs.Seek(sizeof(uint), SeekOrigin.Begin); } else { fs.Read(buffer, 0, buffer.Length); start = BitConverter.ToUInt32(buffer, 0); fs.Seek(0, SeekOrigin.Begin); } } else { if (writeStart) { fs.Write(BitConverter.GetBytes(start), 0, sizeof(uint)); } } if (start <= end) { fs.Write(BitConverter.GetBytes(count), 0, sizeof(uint)); } fs.Seek(0, SeekOrigin.End); Console.WriteLine("Export block from " + start + " to " + end); using (var percent = new ConsolePercent(start, end)) { for (uint i = start; i <= end; i++) { Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i); byte[] array = block.ToArray(); fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int)); fs.Write(array, 0, array.Length); percent.Value = i; } } }
private bool OnImportKeyCommand(string[] args) { if (args.Length > 3) { Console.WriteLine("error"); return(true); } byte[] prikey = null; try { prikey = Wallet.GetPrivateKeyFromWIF(args[2]); } catch (FormatException) { } if (prikey == null) { var file = new FileInfo(args[2]); if (!file.Exists) { Console.WriteLine($"Error: File '{file.FullName}' doesn't exists"); return(true); } if (file.Length > 1024 * 1024) { if (!ReadUserInput($"The file '{file.FullName}' is too big, do you want to continue? (yes|no)", false).IsYes()) { return(true); } } string[] lines = File.ReadAllLines(args[2]).Where(u => !string.IsNullOrEmpty(u)).ToArray(); using (var percent = new ConsolePercent(0, lines.Length)) { for (int i = 0; i < lines.Length; i++) { if (lines[i].Length == 64) { prikey = lines[i].HexToBytes(); } else { prikey = Wallet.GetPrivateKeyFromWIF(lines[i]); } CurrentWallet.CreateAccount(prikey); Array.Clear(prikey, 0, prikey.Length); percent.Value++; } } } else { WalletAccount account = CurrentWallet.CreateAccount(prikey); Array.Clear(prikey, 0, prikey.Length); Console.WriteLine($"address: {account.Address}"); Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); } if (CurrentWallet is NEP6Wallet wallet) { wallet.Save(); } return(true); }