private void OnSendCommand(UInt160 asset, UInt160 to, string amount) { if (NoWallet()) { return; } string password = ReadUserInput("password", true); if (password.Length == 0) { Console.WriteLine("Cancelled"); return; } if (!CurrentWallet.VerifyPassword(password)) { Console.WriteLine("Incorrect password"); return; } Transaction tx; AssetDescriptor descriptor = new AssetDescriptor(asset); if (!BigDecimal.TryParse(amount, descriptor.Decimals, out BigDecimal decimalAmount) || decimalAmount.Sign <= 0) { Console.WriteLine("Incorrect Amount Format"); return; } tx = CurrentWallet.MakeTransaction(new[] { new TransferOutput { AssetId = asset, Value = decimalAmount, ScriptHash = to } }); if (tx == null) { Console.WriteLine("Insufficient funds"); return; } ContractParametersContext context = new ContractParametersContext(tx); CurrentWallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); NeoSystem.LocalNode.Tell(new LocalNode.Relay { Inventory = tx }); Console.WriteLine($"TXID: {tx.Hash}"); } else { Console.WriteLine("SignatureContext:"); Console.WriteLine(context.ToString()); } }
private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null) { if (NoWallet()) { return; } if (path != null && File.Exists(path)) { Console.WriteLine($"Error: File '{path}' already exists"); return; } string password = ReadUserInput("password", true); if (password.Length == 0) { Console.WriteLine("Cancelled"); return; } if (!CurrentWallet.VerifyPassword(password)) { Console.WriteLine("Incorrect password"); return; } IEnumerable <KeyPair> keys; if (scriptHash == null) { keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey()); } else { var account = CurrentWallet.GetAccount(scriptHash); keys = account?.HasKey != true?Array.Empty <KeyPair>() : new[] { account.GetKey() }; } if (path == null) { foreach (KeyPair key in keys) { Console.WriteLine(key.Export()); } } else { File.WriteAllLines(path, keys.Select(p => p.Export())); } }
private void OnChangePasswordCommand() { if (NoWallet()) { return; } string oldPassword = ReadUserInput("password", true); if (oldPassword.Length == 0) { Console.WriteLine("Cancelled"); return; } if (!CurrentWallet.VerifyPassword(oldPassword)) { Console.WriteLine("Incorrect password"); return; } string newPassword = ReadUserInput("New password", true); string newPasswordReEntered = ReadUserInput("Re-Enter Password", true); if (!newPassword.Equals(newPasswordReEntered)) { Console.WriteLine("Two passwords entered are inconsistent!"); return; } if (CurrentWallet is NEP6Wallet wallet) { string backupFile = wallet.Path + ".bak"; if (!File.Exists(wallet.Path) || File.Exists(backupFile)) { Console.WriteLine("Wallet backup fail"); return; } try { File.Copy(wallet.Path, backupFile); } catch (IOException) { Console.WriteLine("Wallet backup fail"); return; } } bool succeed = CurrentWallet.ChangePassword(oldPassword, newPassword); if (succeed) { if (CurrentWallet is NEP6Wallet nep6Wallet) { nep6Wallet.Save(); } Console.WriteLine("Password changed successfully"); } else { Console.WriteLine("Failed to change password"); } }
private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 from = null, UInt160[] signerAccounts = null) { if (NoWallet()) { return; } string password = ReadUserInput("password", true); if (password.Length == 0) { Console.WriteLine("Cancelled"); return; } if (!CurrentWallet.VerifyPassword(password)) { Console.WriteLine("Incorrect password"); return; } Transaction tx; AssetDescriptor descriptor = new AssetDescriptor(asset); if (!BigDecimal.TryParse(amount, descriptor.Decimals, out BigDecimal decimalAmount) || decimalAmount.Sign <= 0) { Console.WriteLine("Incorrect Amount Format"); return; } try { tx = CurrentWallet.MakeTransaction(new[] { new TransferOutput { AssetId = asset, Value = decimalAmount, ScriptHash = to } }, from: from, cosigners: signerAccounts?.Select(p => new Signer { // default access for transfers should be valid only for first invocation Scopes = WitnessScope.CalledByEntry, Account = p }) .ToArray() ?? new Signer[0]); } catch (Exception e) { Console.WriteLine("Error: " + GetExceptionMessage(e)); return; } if (tx == null) { Console.WriteLine("Insufficient funds"); return; } ContractParametersContext context = new ContractParametersContext(tx); CurrentWallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); NeoSystem.Blockchain.Tell(tx); Console.WriteLine($"TXID: {tx.Hash}"); } else { Console.WriteLine("SignatureContext:"); Console.WriteLine(context.ToString()); } }
private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 from = null, string data = null, UInt160[] signerAccounts = null) { if (NoWallet()) { return; } string password = ReadUserInput("password", true); if (password.Length == 0) { ConsoleHelper.Info("Cancelled"); return; } if (!CurrentWallet.VerifyPassword(password)) { ConsoleHelper.Error("Incorrect password"); return; } var snapshot = NeoSystem.StoreView; Transaction tx; AssetDescriptor descriptor = new(snapshot, NeoSystem.Settings, asset); if (!BigDecimal.TryParse(amount, descriptor.Decimals, out BigDecimal decimalAmount) || decimalAmount.Sign <= 0) { ConsoleHelper.Error("Incorrect Amount Format"); return; } try { tx = CurrentWallet.MakeTransaction(snapshot, new[] { new TransferOutput { AssetId = asset, Value = decimalAmount, ScriptHash = to, Data = data } }, from: from, cosigners: signerAccounts?.Select(p => new Signer { // default access for transfers should be valid only for first invocation Scopes = WitnessScope.CalledByEntry, Account = p }) .ToArray() ?? Array.Empty <Signer>()); } catch (Exception e) { ConsoleHelper.Error(GetExceptionMessage(e)); return; } if (tx == null) { ConsoleHelper.Warning("Insufficient funds"); return; } ConsoleHelper.Info("Network fee: ", $"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}\t", "Total fee: ", $"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS"); if (!ReadUserInput("Relay tx? (no|yes)").IsYes()) { return; } SignAndSendTx(NeoSystem.StoreView, tx); }
private bool OnExportKeyCommand(string[] args) { if (NoWallet()) { return(true); } if (args.Length < 2 || args.Length > 4) { Console.WriteLine("error"); return(true); } UInt160 scriptHash = null; string path = null; if (args.Length == 3) { try { scriptHash = args[2].ToScriptHash(); } catch (FormatException) { path = args[2]; } } else if (args.Length == 4) { scriptHash = args[2].ToScriptHash(); path = args[3]; } if (File.Exists(path)) { Console.WriteLine($"Error: File '{path}' already exists"); return(true); } string password = ReadUserInput("password", true); if (password.Length == 0) { Console.WriteLine("cancelled"); return(true); } if (!CurrentWallet.VerifyPassword(password)) { Console.WriteLine("Incorrect password"); return(true); } IEnumerable <KeyPair> keys; if (scriptHash == null) { keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey()); } else { keys = new[] { CurrentWallet.GetAccount(scriptHash).GetKey() } }; if (path == null) { foreach (KeyPair key in keys) { Console.WriteLine(key.Export()); } } else { File.WriteAllLines(path, keys.Select(p => p.Export())); } return(true); }
private bool OnSendCommand(string[] args) { if (args.Length != 4) { Console.WriteLine("error"); return(true); } if (NoWallet()) { return(true); } string password = ReadUserInput("password", true); if (password.Length == 0) { Console.WriteLine("cancelled"); return(true); } if (!CurrentWallet.VerifyPassword(password)) { Console.WriteLine("Incorrect password"); return(true); } UInt160 assetId; switch (args[1].ToLower()) { case "neo": assetId = NativeContract.NEO.Hash; break; case "gas": assetId = NativeContract.GAS.Hash; break; default: assetId = UInt160.Parse(args[1]); break; } UInt160 to = args[2].ToScriptHash(); var snapshot = NeoSystem.StoreView; Transaction tx; AssetDescriptor descriptor = new AssetDescriptor(snapshot, CliSettings.Default.Protocol, assetId); if (!BigDecimal.TryParse(args[3], descriptor.Decimals, out BigDecimal amount) || amount.Sign <= 0) { Console.WriteLine("Incorrect Amount Format"); return(true); } tx = CurrentWallet.MakeTransaction(snapshot, new[] { new TransferOutput { AssetId = assetId, Value = amount, ScriptHash = to } }); if (tx == null) { Console.WriteLine("Insufficient funds"); return(true); } ContractParametersContext context = new ContractParametersContext(snapshot, tx, CliSettings.Default.Protocol.Network); CurrentWallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); NeoSystem.Blockchain.Tell(tx); //NeoSystem.LocalNode.Tell(new LocalNode.Relay { Inventory = tx }); Console.WriteLine($"TXID: {tx.Hash}"); } else { Console.WriteLine("SignatureContext:"); Console.WriteLine(context.ToString()); } return(true); }