Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,39 @@ public IActionResult GetExtPubKey([FromQuery] GetExtPubKeyModel request)
}
}

/// <summary>
/// Gets the private key of a specified wallet address.
/// </summary>
/// <param name="request">An object containing the necessary parameters to retrieve.</param>
/// <param name="cancellationToken">The Cancellation Token</param>
/// <returns>A JSON object containing the private key of the address in WIF representation.</returns>
/// <response code="200">Returns private key</response>
/// <response code="400">Invalid request, or unexpected exception occurred</response>
/// <response code="500">Request is null</response>
[Route("privatekey")]
[HttpPost]
public IActionResult RetrievePrivateKey([FromBody] RetrievePrivateKeyModel request)
{
Guard.NotNull(request, nameof(request));

// checks the request is valid
if (!this.ModelState.IsValid)
{
return ModelStateErrors.BuildErrorResponse(this.ModelState);
}

try
{
string result = this.walletManager.RetrievePrivateKey(request.Password, request.WalletName, request.AccountName, request.Address);
return this.Json(result);
}
catch (Exception e)
{
this.logger.LogError("Exception occurred: {0}", e.ToString());
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}

/// <summary>
/// Requests the node resyncs from a block specified by its block hash.
/// Internally, the specified block is taken as the new wallet tip
Expand Down
34 changes: 34 additions & 0 deletions src/Features/Blockcore.Features.Wallet/Api/Models/RequestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,40 @@ public GetExtPubKeyModel()
public string AccountName { get; set; }
}

/// <summary>
/// A class containing the necessary parameters for a private key retrieval request.
/// </summary>
public class RetrievePrivateKeyModel : RequestModel
{
public RetrievePrivateKeyModel()
{
this.AccountName = WalletManager.DefaultAccount;
}

/// <summary>
/// The password for the wallet.
/// </summary>
[Required]
public string Password { get; set; }

/// <summary>
/// The name of the wallet from which to get the private key.
/// </summary>
[Required]
public string WalletName { get; set; }

/// <summary>
/// The name of the account for which to get the private key.
/// <summary>
public string AccountName { get; set; }

/// <summary>
/// The address to retrieve the private key for.
/// </summary>
[Required]
public string Address { get; set; }
}

/// <summary>
/// A class containing the necessary parameters for a new account request.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public interface IWalletManager
/// <returns>A mnemonic defining the wallet's seed used to generate addresses.</returns>
Mnemonic CreateWallet(string password, string name, string passphrase = null, Mnemonic mnemonic = null, int? coinType = null);

/// <summary>
/// Gets the private key associated with an address in the wallet.
/// </summary>
/// <param name="password">The user's password.</param>
/// <param name="walletName">The name of the wallet.</param>
/// <param name="accountName">The name of the account.</param>
/// <param name="address">Address to extract the private key of.</param>
/// <returns>The private key associated with the given address, in WIF representation.</returns>
string RetrievePrivateKey(string password, string walletName, string accountName, string address);

/// <summary>
/// Signs a string message.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Features/Blockcore.Features.Wallet/WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ public Mnemonic CreateWallet(string password, string name, string passphrase, Mn
return mnemonic;
}

/// <inheritdoc />
public string RetrievePrivateKey(string password, string walletName, string accountName, string address)
{
Guard.NotEmpty(password, nameof(password));
Guard.NotEmpty(walletName, nameof(walletName));
Guard.NotEmpty(address, nameof(address));

Types.Wallet wallet = this.GetWallet(walletName);

// Locate the address based on its base58 string representation.
HdAddress hdAddress = wallet.GetAddress(address, account => account.Name.Equals(accountName));

ISecret privateKey = wallet.GetExtendedPrivateKeyForAddress(password, hdAddress).PrivateKey.GetWif(this.network);
return privateKey.ToString();
}

/// <inheritdoc />
public SignMessageResult SignMessage(string password, string walletName, string accountName, string externalAddress, string message)
{
Expand Down