private void OnRegisterCandidateCommand(UInt160 account) { var testGas = NativeContract.NEO.GetRegisterPrice(NeoSystem.StoreView) + (BigInteger)Math.Pow(10, NativeContract.GAS.Decimals) * 10; if (NoWallet()) { return; } WalletAccount currentAccount = CurrentWallet.GetAccount(account); if (currentAccount == null) { ConsoleHelper.Warning("This address isn't in your wallet!"); return; } else { if (currentAccount.Lock || currentAccount.WatchOnly) { ConsoleHelper.Warning("Locked or WatchOnly address."); return; } } ECPoint publicKey = currentAccount.GetKey()?.PublicKey; byte[] script; using (ScriptBuilder scriptBuilder = new ScriptBuilder()) { scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "registerCandidate", publicKey); script = scriptBuilder.ToArray(); } SendTransaction(script, account, (long)testGas); }
private void OnUnregisterCandidateCommand(UInt160 account) { if (NoWallet()) { return; } WalletAccount currentAccount = CurrentWallet.GetAccount(account); if (currentAccount == null) { ConsoleHelper.Warning("This address isn't in your wallet!"); return; } else { if (currentAccount.Lock || currentAccount.WatchOnly) { ConsoleHelper.Warning("Locked or WatchOnly address."); return; } } ECPoint publicKey = currentAccount?.GetKey()?.PublicKey; byte[] script; using (ScriptBuilder scriptBuilder = new ScriptBuilder()) { scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "unregisterCandidate", publicKey); script = scriptBuilder.ToArray(); } SendTransaction(script, account); }
/// <summary> /// show private key /// </summary> /// <param name="address"></param> /// <returns></returns> public async Task <object> ShowPrivateKey(UInt160 address) { if (CurrentWallet == null) { return(Error(ErrorCode.WalletNotOpen)); } var account = CurrentWallet.GetAccount(address); if (account == null) { return(Error(ErrorCode.AddressNotFound)); } if (!account.HasKey) { return(Error(ErrorCode.AddressNotFoundPrivateKey)); } var key = account.GetKey(); return(new PrivateKeyModel() { ScriptHash = address, PublicKey = key.PublicKey.EncodePoint(true).ToHexString(), PrivateKey = key.PrivateKey.ToHexString(), Wif = key.Export(), }); }
private void OnImportWatchOnlyCommand(string addressOrFile) { if (NoWallet()) { return; } UInt160 address = null; try { address = StringToAddress(addressOrFile, NeoSystem.Settings.AddressVersion); } catch (FormatException) { } if (address is null) { var fileInfo = new FileInfo(addressOrFile); if (!fileInfo.Exists) { ConsoleHelper.Warning($"File '{fileInfo.FullName}' doesn't exists"); return; } if (fileInfo.Length > 1024 * 1024) { if (!ReadUserInput($"The file '{fileInfo.FullName}' is too big, do you want to continue? (yes|no)", false).IsYes()) { return; } } string[] lines = File.ReadAllLines(fileInfo.FullName).Where(u => !string.IsNullOrEmpty(u)).ToArray(); using (var percent = new ConsolePercent(0, lines.Length)) { for (int i = 0; i < lines.Length; i++) { address = StringToAddress(lines[i], NeoSystem.Settings.AddressVersion); CurrentWallet.CreateAccount(address); percent.Value++; } } } else { WalletAccount account = CurrentWallet.GetAccount(address); if (account is not null) { ConsoleHelper.Warning("This address is already in your wallet"); } else { account = CurrentWallet.CreateAccount(address); ConsoleHelper.Info("Address: ", account.Address); } } if (CurrentWallet is NEP6Wallet wallet) { wallet.Save(); } }
/// <summary> /// append signature for text /// </summary> /// <param name="text"></param> /// <param name="address"></param> /// <returns></returns> public async Task <object> AppendTextSignature(string text, UInt160 address) { if (CurrentWallet == null) { return(Error(ErrorCode.WalletNotOpen)); } var account = CurrentWallet.GetAccount(address); if (!account.HasKey) { return(Error(ErrorCode.AddressNotFoundPrivateKey)); } try { KeyPair key = account.GetKey(); byte[] signature = Crypto.Sign(Encoding.UTF8.GetBytes(text), key.PrivateKey, key.PublicKey.EncodePoint(false)[1..]);
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 OnUnregisterCandidateCommand(UInt160 account) { if (NoWallet()) { Console.WriteLine("Need open wallet!"); return; } ECPoint publicKey = CurrentWallet.GetAccount(account)?.GetKey()?.PublicKey; byte[] script; using (ScriptBuilder scriptBuilder = new ScriptBuilder()) { scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "unregisterCandidate", publicKey); script = scriptBuilder.ToArray(); } SendTransaction(script, account); }
private void OnRegisterCandidateCommand(UInt160 account, decimal maxGas = RegisterGas) { var gas = new BigDecimal(maxGas, NativeContract.GAS.Decimals); if (NoWallet()) { Console.WriteLine("Need open wallet!"); return; } WalletAccount currentAccount = CurrentWallet.GetAccount(account); if (currentAccount == null) { Console.WriteLine("This address isn't in your wallet!"); return; } else { if (currentAccount.Lock || currentAccount.WatchOnly) { Console.WriteLine("Locked or WatchOnly address."); return; } } ECPoint publicKey = currentAccount?.GetKey()?.PublicKey; byte[] script; using (ScriptBuilder scriptBuilder = new ScriptBuilder()) { scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "registerCandidate", publicKey); script = scriptBuilder.ToArray(); } SendTransaction(script, account, (long)gas.Value); }
/// <summary> /// send asset /// </summary> /// <param name="sender"></param> /// <param name="receivers"></param> /// <param name="asset"></param> /// <returns></returns> public async Task <object> SendToMultiAddress(MultiReceiverRequest[] receivers, string asset = "neo", UInt160 sender = null) { if (CurrentWallet == null) { return(Error(ErrorCode.WalletNotOpen)); } if (receivers.IsEmpty()) { return(Error(ErrorCode.ParameterIsNull, $"receivers is null!")); } UInt160 assetHash = ConvertToAssetId(asset, out var convertError); if (assetHash == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } var assetInfo = AssetCache.GetAssetInfo(assetHash); if (assetInfo == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } if (sender != null) { var account = CurrentWallet.GetAccount(sender); if (account == null) { return(Error(ErrorCode.AddressNotFound)); } } var toes = new List <(UInt160 scriptHash, BigDecimal amount)>(); foreach (var receiver in receivers) { if (!BigDecimal.TryParse(receiver.Amount, assetInfo.Decimals, out BigDecimal sendAmount) || sendAmount.Sign <= 0) { return(Error(ErrorCode.InvalidPara, $"Incorrect Amount Format:{receiver.Amount}")); } toes.Add((receiver.Address, sendAmount)); } var outputs = toes.Select(t => new TransferOutput() { AssetId = assetHash, Value = t.amount, ScriptHash = t.scriptHash, }).ToArray(); try { Transaction tx = CurrentWallet.MakeTransaction(Helpers.GetDefaultSnapshot(), outputs, sender); if (tx == null) { return(Error(ErrorCode.BalanceNotEnough, "Insufficient funds")); } var(signSuccess, context) = CurrentWallet.TrySignTx(tx); if (!signSuccess) { return(Error(ErrorCode.SignFail, context.SafeSerialize())); } await tx.Broadcast(); return(new TransactionModel(tx)); } catch (Exception ex) { if (ex.Message.Contains("Insufficient GAS")) { return(Error(ErrorCode.GasNotEnough)); } return(Error(ErrorCode.TransferError, ex.Message)); } }
/// <summary> /// send asset /// </summary> /// <param name="sender"></param> /// <param name="receiver"></param> /// <param name="amount"></param> /// <param name="asset"></param> /// <returns></returns> public async Task <object> SendToAddress(UInt160 receiver, string amount, string asset = "neo", UInt160 sender = null) { if (CurrentWallet == null) { return(Error(ErrorCode.WalletNotOpen)); } if (receiver == null) { return(Error(ErrorCode.ParameterIsNull, $"receiver address is null!")); } UInt160 assetHash = ConvertToAssetId(asset, out var convertError); if (assetHash == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } var assetInfo = AssetCache.GetAssetInfo(assetHash); if (assetInfo == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } if (!BigDecimal.TryParse(amount, assetInfo.Decimals, out BigDecimal sendAmount) || sendAmount.Sign <= 0) { return(Error(ErrorCode.InvalidPara, "Incorrect Amount Format")); } if (sender != null) { var account = CurrentWallet.GetAccount(sender); if (account == null) { return(Error(ErrorCode.AddressNotFound)); } var balance = sender.GetBalanceOf(assetHash); if (balance.Value < sendAmount.Value) { return(Error(ErrorCode.BalanceNotEnough)); } } try { Transaction tx = CurrentWallet.MakeTransaction(Helpers.GetDefaultSnapshot(), new[] { new TransferOutput { AssetId = assetHash, Value = sendAmount, ScriptHash = receiver } }, sender); if (tx == null) { return(Error(ErrorCode.BalanceNotEnough, "Insufficient funds")); } var(signSuccess, context) = CurrentWallet.TrySignTx(tx); if (!signSuccess) { return(Error(ErrorCode.SignFail, context.SafeSerialize())); } await tx.Broadcast(); return(new TransactionModel(tx)); } catch (Exception ex) { if (ex.Message.Contains("Insufficient GAS")) { return(Error(ErrorCode.GasNotEnough)); } return(Error(ErrorCode.TransferError, ex.Message)); } }
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); }