Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Export Wif / Export Nep-2 (+1 squashed commit)
Browse files Browse the repository at this point in the history
Squashed commits:
[7b76303] Export Account in NEP-2 format;
Changing ReadPassword to use custom message;
  • Loading branch information
lock9 committed Sep 24, 2018
1 parent 67915c8 commit ada3d02
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 30 deletions.
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
63 changes: 62 additions & 1 deletion src/NeoSharp.Application/Controllers/PromptWalletController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using NeoSharp.Application.Attributes;
using NeoSharp.Application.Client;
using NeoSharp.Core.Extensions;
Expand Down Expand Up @@ -121,6 +122,66 @@ 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 (Exception)
{
_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 (Exception)
{
_consoleWriter.WriteLine("\nInvalid password.");
}
}
else
{
_consoleWriter.WriteLine("\nAccount not found.");
}
}


/*
TODO #404: Implement additional wallet features
wallet delete_addr {addr}
Expand Down
42 changes: 41 additions & 1 deletion src/NeoSharp.Core/Wallet/Helpers/WalletHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public byte[] DecryptWif(string encryptedPrivateKey, SecureString passphrase)
throw new ArgumentNullException(nameof(encryptedPrivateKey));
}

if (passphrase == null || String.IsNullOrWhiteSpace(passphrase.ToString()))
if (passphrase == null || String.IsNullOrWhiteSpace(passphrase.ToString()))
{
throw new ArgumentNullException(nameof(passphrase));
}
Expand Down Expand Up @@ -162,6 +162,46 @@ public UInt160 ScriptHashFromPublicKey(ECPoint publicKey)
return ContractFactory.CreateSinglePublicKeyRedeemContract(publicKey).ScriptHash;
}

/// <summary>
/// Converts a byte array into wif string
/// </summary>
/// <returns>Wif.</returns>
/// <param name="privateKey">Private Key</param>
public string PrivateKeyToWif(byte[] privateKey)
{
byte[] data = new byte[34];
data[0] = 0x80;
Buffer.BlockCopy(privateKey, 0, data, 1, 32);
data[33] = 0x01;
string wif = Crypto.Default.Base58CheckEncode(data);
Array.Clear(data, 0, data.Length);
return wif;
}

/// <summary>
/// Gets the private key from wif.
/// </summary>
/// <returns>The private key from wif.</returns>
/// <param name="wif">Wif.</param>
public byte[] GetPrivateKeyFromWIF(string wif)
{
var internalWif = wif ?? throw new ArgumentNullException(nameof(wif));

var privateKeyByteArray = Crypto.Default.Base58CheckDecode(internalWif);

if (privateKeyByteArray.IsValidPrivateKey())
{
var privateKey = new byte[32];
Buffer.BlockCopy(privateKeyByteArray, 1, privateKey, 0, privateKey.Length);
Array.Clear(privateKeyByteArray, 0, privateKeyByteArray.Length);
return privateKey;
}
else
{
throw new FormatException();
}
}

#region Private Methods

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions src/NeoSharp.Core/Wallet/IWalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ public interface IWalletManager
/// <param name="filename">the filename</param>
void ExportWallet(string filename);

/// <summary>
/// Returns the private key from a NEP2 key and password
/// </summary>
/// <returns>The nep2 key.</returns>
/// <param name="nep2key">Nep2key.</param>
/// <param name="keyPassword">Key password.</param>
byte[] DecryptNep2(string nep2key, SecureString keyPassword);

/// <summary>
/// Returns the encrypted private key (nep-2)
/// </summary>
/// <returns>The nep2.</returns>
/// <param name="privateKey">Private key.</param>
/// <param name="keyPassword">Key password.</param>
string EncryptNep2(byte[] privateKey, SecureString keyPassword);

/// <summary>
/// Load a wallet at specified fileName.
/// </summary>
Expand All @@ -135,5 +151,20 @@ public interface IWalletManager
/// Close wallet.
/// </summary>
void Close();

/// <summary>
/// Converts a byte array into a wif string
/// </summary>
/// <returns>Private key in wif format.</returns>
/// <param name="privateKey">Private key.</param>
string PrivateKeyToWif(byte[] privateKey);

/// <summary>
/// Gets the private key from wif.
/// </summary>
/// <returns>The private key from wif.</returns>
/// <param name="wif">Wif.</param>
byte[] GetPrivateKeyFromWIF(string wif);

}
}
51 changes: 27 additions & 24 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public Nep6WalletManager(IFileWrapper fileWrapper, IJsonConverter jsonConverter)
_jsonConverter = jsonConverter;
}



/// <inheritdoc />
public void CreateWallet(string filename)
{
Expand Down Expand Up @@ -182,6 +184,18 @@ public IWalletAccount ImportPrivateKey(byte[] privateKey, SecureString passphras
return account;
}

/// <inheritdoc />
public byte[] GetPrivateKeyFromWIF(string wif)
{
return _walletHelper.GetPrivateKeyFromWIF(wif);
}

/// <inheritdoc />
public string PrivateKeyToWif(byte[] privateKey)
{
return _walletHelper.PrivateKeyToWif(privateKey);
}

/// <inheritdoc />
public IWalletAccount ImportWif(string wif, SecureString password)
{
Expand Down Expand Up @@ -240,6 +254,7 @@ public void ExportWallet(String filename)
_fileWrapper.WriteToFile(json, filename);
}


/// <inheritdoc />
public void Load(string fileName)
{
Expand Down Expand Up @@ -275,6 +290,18 @@ public ECPoint GetPublicKeyFromNep2(string nep2Key, SecureString password)
return new ECPoint(publicKeyInBytes);
}

/// <inheritdoc />
public byte[] DecryptNep2(string nep2key, SecureString keyPassword)
{
return _walletHelper.DecryptWif(nep2key, keyPassword);
}

/// <inheritdoc />
public string EncryptNep2(byte[] privateKey, SecureString keyPassword)
{
return _walletHelper.EncryptWif(privateKey, keyPassword);
}

/// <inheritdoc />
public void CheckWalletIsOpen()
{
Expand Down Expand Up @@ -379,30 +406,6 @@ private void AddAccount(IWalletAccount account)
}
}

/// <summary>
/// Gets the private key from wif.
/// </summary>
/// <returns>The private key from wif.</returns>
/// <param name="wif">Wif.</param>
private byte[] GetPrivateKeyFromWIF(string wif)
{
var internalWif = wif ?? throw new ArgumentNullException(nameof(wif));

var privateKeyByteArray = Crypto.Default.Base58CheckDecode(internalWif);

if (privateKeyByteArray.IsValidPrivateKey())
{
var privateKey = new byte[32];
Buffer.BlockCopy(privateKeyByteArray, 1, privateKey, 0, privateKey.Length);
Array.Clear(privateKeyByteArray, 0, privateKeyByteArray.Length);
return privateKey;
}
else
{
throw new FormatException();
}
}

/// <summary>
/// Creates the NEP6Account with private key.
/// </summary>
Expand Down

0 comments on commit ada3d02

Please sign in to comment.