Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge with development #5

Merged
merged 5 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/NeoSharp.Application/Client/ConsoleReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void AppendInputs(params string[] inputs)
/// </summary>
/// <param name="promptLabel">Prompt label</param>
/// <returns>Reteurn Secure string password</returns>
public SecureString ReadPassword(bool promptLabel = true)
public SecureString ReadPassword(string promptLabel = "Password: ")
{
if (promptLabel)
if (promptLabel != null)
{
_consoleWriter.WriteLine("Password: ");
_consoleWriter.WriteLine(promptLabel, ConsoleOutputStyle.Information);
}

State = ConsoleReaderState.ReadingPassword;
Expand Down
2 changes: 1 addition & 1 deletion src/NeoSharp.Application/Client/IConsoleReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IConsoleReader
/// </summary>
/// <param name="promptLabel">Prompt label</param>
/// <returns>Reteurn Secure string password</returns>
SecureString ReadPassword(bool promptLabel = true);
SecureString ReadPassword(string promptLabel = "Password: ");
/// <summary>
/// Read string from console
/// </summary>
Expand Down
20 changes: 15 additions & 5 deletions src/NeoSharp.Application/Controllers/PromptBlockchainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NeoSharp.Core.Blockchain;
using NeoSharp.Core.Blockchain.Processing;
using NeoSharp.Core.Extensions;
using NeoSharp.Core.Network;
using NeoSharp.Core.Types;

namespace NeoSharp.Application.Controllers
Expand All @@ -16,6 +17,7 @@ public class PromptBlockchainController : IPromptController
private readonly IBlockPool _blockPool;
private readonly ITransactionPool _transactionPool;
private readonly IBlockchain _blockchain;
private readonly IBlockchainContext _blockchainContext;
private readonly IConsoleWriter _consoleWriter;
private readonly IConsoleReader _consoleReader;

Expand All @@ -25,13 +27,21 @@ public class PromptBlockchainController : IPromptController
/// Constructor
/// </summary>
/// <param name="blockchain">Blockchain</param>
/// <param name="blockchainContext">The block chain context class.</param>
/// <param name="blockPool">Block pool</param>
/// <param name="transactionPool">Transaction Pool</param>
/// <param name="consoleWriter">Console writter</param>
/// <param name="consoleReader">Console reader</param>
public PromptBlockchainController(IBlockchain blockchain, IBlockPool blockPool, ITransactionPool transactionPool, IConsoleWriter consoleWriter, IConsoleReader consoleReader)
public PromptBlockchainController(
IBlockchain blockchain,
IBlockchainContext blockchainContext,
IBlockPool blockPool,
ITransactionPool transactionPool,
IConsoleWriter consoleWriter,
IConsoleReader consoleReader)
{
_blockchain = blockchain;
_blockchainContext = blockchainContext;
_blockPool = blockPool;
_transactionPool = transactionPool;
_consoleReader = consoleReader;
Expand Down Expand Up @@ -67,8 +77,8 @@ public void StateCommand()
{
var memStr = FormatState(_transactionPool.Size);
var blockStr = FormatState(_blockPool.Size);
var headStr = FormatState(_blockchain.LastBlockHeader?.Index);
var blStr = FormatState(_blockchain.CurrentBlock?.Index);
var headStr = FormatState(this._blockchainContext.LastBlockHeader?.Index);
var blStr = FormatState(this._blockchainContext.CurrentBlock?.Index);
var blIndex = FormatState(0); // TODO #398: Change me

var numSpaces = new int[] { memStr.Length, blockStr.Length, blIndex.Length, headStr.Length, blStr.Length }.Max() + 1;
Expand All @@ -85,8 +95,8 @@ public void StateCommand()

_consoleWriter.WriteLine("Headers: " + headStr.PadLeft(numSpaces, ' ') + " ");

WriteStatePercent(" Blocks", blStr.PadLeft(numSpaces, ' '), _blockchain.CurrentBlock?.Index, _blockchain.LastBlockHeader?.Index);
WriteStatePercent(" Index", blIndex.PadLeft(numSpaces, ' '), 0, _blockchain.CurrentBlock?.Index);
WriteStatePercent(" Blocks", blStr.PadLeft(numSpaces, ' '), this._blockchainContext.CurrentBlock?.Index, this._blockchainContext.LastBlockHeader?.Index);
WriteStatePercent(" Index", blIndex.PadLeft(numSpaces, ' '), 0, this._blockchainContext.CurrentBlock?.Index);
}

/// <summary>
Expand Down
142 changes: 106 additions & 36 deletions src/NeoSharp.Application/Controllers/PromptWalletController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Linq;
using System;
using System.Linq;
using NeoSharp.Application.Attributes;
using NeoSharp.Application.Client;
using NeoSharp.Core.Extensions;
using NeoSharp.Core.Types;
using NeoSharp.Core.Wallet;
using NeoSharp.Core.Wallet.Exceptions;
using NeoSharp.Core.Wallet.Helpers;

namespace NeoSharp.Application.Controllers
Expand Down Expand Up @@ -34,24 +36,24 @@ public PromptWalletController(IWalletManager walletManager, IConsoleWriter conso
_consoleWriter = consoleWriter;
}

[PromptCommand("wallet create", Category = "Wallet", Help = "Create a new wallet")]
public void WalletCreateCommand(string fileName)
{
_walletManager.CreateWallet(fileName);
var secureString = _consoleReader.ReadPassword();
_consoleWriter.WriteLine("\n", ConsoleOutputStyle.Information); //How these line breaks can be improved?
var confirmationString = _consoleReader.ReadPassword();
if (secureString.ToByteArray().SequenceEqual(confirmationString.ToByteArray()))
{
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
{
_consoleWriter.WriteLine("\nPasswords don't match.", ConsoleOutputStyle.Information);
}

[PromptCommand("wallet create", Category = "Wallet", Help = "Create a new wallet")]
public void WalletCreateCommand(string fileName)
{
var secureString = _consoleReader.ReadPassword();
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
var confirmationString = _consoleReader.ReadPassword("\nConfirm your password:");
if (secureString.ToByteArray().SequenceEqual(confirmationString.ToByteArray()))
{
_walletManager.CreateWallet(fileName);
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
{
_consoleWriter.WriteLine("\nPasswords don't match.", ConsoleOutputStyle.Information);
}
}

[PromptCommand("wallet open", Category = "Wallet", Help = "Open wallet")]
Expand Down Expand Up @@ -97,19 +99,18 @@ public void WalletSaveCommand(string fileName)
[PromptCommand("account create", Category = "Account", Help = "Create a new account")]
public void AccountCreateCommand()
{
var secureString = _consoleReader.ReadPassword();
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
_consoleWriter.WriteLine("\nConfirm your password:", ConsoleOutputStyle.Information);
var confirmationString = _consoleReader.ReadPassword();
if(secureString.ToByteArray().SequenceEqual(confirmationString.ToByteArray()))
{
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
{
_consoleWriter.WriteLine("Passwords don't match.");
var secureString = _consoleReader.ReadPassword("Wallet password:");
try
{
_walletManager.CheckIfPasswordMatchesOpenWallet(secureString);
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
catch(AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nInvalid password.");
}
}

Expand All @@ -121,18 +122,87 @@ public void AccountDeleteCommand(string address)
_consoleWriter.WriteLine("Account deleted.");
}

[PromptCommand("account export nep2", Category = "Account", Help = "Exports an account in nep-2 format")]
public void AccountExportNep2(string address)
{
var walletAccount = _walletManager.GetAccount(address.ToScriptHash());
if (walletAccount != null)
{
try
{
var walletPassword = _consoleReader.ReadPassword();
byte[] accountPrivateKey = _walletManager.DecryptNep2(walletAccount.Key, walletPassword);
var newKeyPassword = _consoleReader.ReadPassword("\nNew key password:");
var newKeyPasswordConfirmation = _consoleReader.ReadPassword("\nConfirm your password:");
if (newKeyPassword.ToByteArray().SequenceEqual(newKeyPasswordConfirmation.ToByteArray()))
{
string nep2Key = _walletManager.EncryptNep2(accountPrivateKey, newKeyPassword);
_consoleWriter.WriteLine("\nExported NEP-2 Key: " + nep2Key);
}
else
{
_consoleWriter.WriteLine("\nPasswords don't match.");
}
}
catch (AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nInvalid password.");
}

}
else
{
_consoleWriter.WriteLine("\nAccount not found.");
}

}

[PromptCommand("account export wif", Category = "Account", Help = "Exports an account in nep-2 format")]
public void AccountExportWif(string address)
{
var walletAccount = _walletManager.GetAccount(address.ToScriptHash());
if (walletAccount != null)
{
try
{
var walletPassword = _consoleReader.ReadPassword();
byte[] accountPrivateKey = _walletManager.DecryptNep2(walletAccount.Key, walletPassword);
string wif = _walletManager.PrivateKeyToWif(accountPrivateKey);
_consoleWriter.WriteLine("\nExported wif: " + wif);
}
catch (AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nInvalid password.");
}
}
else
{
_consoleWriter.WriteLine("\nAccount not found.");
}
}

[PromptCommand("account alias", Category = "Account", Help = "Adds a label to an account")]
public void AddAccountAlias(string address, string alias)
{
UInt160 accountScriptHash = address.ToScriptHash();
if(_walletManager.Contains(accountScriptHash))
{
_walletManager.UpdateAccountAlias(accountScriptHash, alias);
}else
{
_consoleWriter.WriteLine("\nAccount not found.");
}
}


/*
TODO #404: Implement additional wallet features
wallet delete_addr {addr}
wallet delete_token {token_contract_hash}
wallet alias {addr} {title}

import multisig_addr {pubkey in wallet} {minimum # of signatures required} {signing pubkey 1} {signing pubkey 2}...
import watch_addr {address}
import token {token_contract_hash}
export wif {address}
export nep2 {address}

*/

/*
Expand Down
3 changes: 3 additions & 0 deletions src/NeoSharp.Application/NeoSharp.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.neo-privnet.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.testnet.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
15 changes: 4 additions & 11 deletions src/NeoSharp.Application/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
{
"network": {
"magic": 7630401,
"magic": 1953787457,
"port": 8000,
"forceIPv6": false,
"peerEndPoints": [
"tcp://neo-privnet:20333",
"tcp://neo-privnet:20334",
"tcp://neo-privnet:20335",
"tcp://neo-privnet:20336",
"tcp://neo-privnet:30333",
"tcp://neo-privnet:30334",
"tcp://neo-privnet:30335",
"tcp://neo-privnet:30336"
"tcp://seed1.neo.org:20333"
],
"acl": {
"path": "network-acl.json",
Expand Down Expand Up @@ -41,7 +34,7 @@
"persistence": {
"provider": "RocksDb",
"rocksDbProvider": {
"filePath": "localhost"
"filePath": "ChainTestnet"
},
"redisDbBinaryProvider": {
"connectionString": "localhost",
Expand All @@ -52,4 +45,4 @@
"databaseId": "0"
}
}
}
}
55 changes: 55 additions & 0 deletions src/NeoSharp.Application/appsettings.neo-privnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"network": {
"magic": 7630401,
"port": 8000,
"forceIPv6": false,
"peerEndPoints": [
"tcp://neo-privnet:20333",
"tcp://neo-privnet:20334",
"tcp://neo-privnet:20335",
"tcp://neo-privnet:20336",
"tcp://neo-privnet:30333",
"tcp://neo-privnet:30334",
"tcp://neo-privnet:30335",
"tcp://neo-privnet:30336"
],
"acl": {
"path": "network-acl.json",
"type": "Blacklist"
},
"standByValidators": [
"03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
"02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093",
"03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a",
"02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554",
"024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d",
"02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e",
"02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70"
]
},
"rpc": {
"listenEndPoint": "127.0.0.1,10332",
"#ssl": {
"path": "./rpc-ssl.cert",
"password": "changeme"
},
"acl": {
"path": "rpc-acl.json",
"type": "Blacklist"
}
},
"persistence": {
"provider": "RocksDb",
"rocksDbProvider": {
"filePath": "localhost"
},
"redisDbBinaryProvider": {
"connectionString": "localhost",
"databaseId": "0"
},
"redisDbJsonProvider": {
"connectionString": "localhost",
"databaseId": "0"
}
}
}