Skip to content

Commit 141ec56

Browse files
create account key pair
1 parent 2f90866 commit 141ec56

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

Nethereum.Console/App.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public App()
1717
Commands.Add(new CalculateAccountsFolderTotalBalanceCommand());
1818
Commands.Add(new CalculateAccountsTotalBalanceCommand());
1919
Commands.Add(new AccountTokenBalanceCommand(accountService));
20+
Commands.Add(new CreateAccountKeyPairCommand(accountService));
2021

2122
HelpOption("-h | -? | --help");
2223
}

Nethereum.Console/Commands/AccountTokenBalanceCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Nethereum.Console
55
{
6+
67
/// <example>
78
/// dotnet run account-total-token-balance --url "https://mainnet.infura.io:8545" -a 0xd0a6e6c54dbc68db5db3a091b171a77407ff7ccf -ca 0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0
89
/// </example>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Microsoft.Extensions.CommandLineUtils;
3+
4+
namespace Nethereum.Console
5+
{
6+
public class CreateAccountKeyPairCommand: CommandLineApplication
7+
{
8+
private IAccountService accountService;
9+
public CreateAccountKeyPairCommand(IAccountService accountService)
10+
{
11+
this.accountService = accountService;
12+
Name = "create-account-key-pair";
13+
Description = "Generates a new private key and ethereum address";
14+
15+
HelpOption("-? | -h | --help");
16+
OnExecute((Func<int>)RunCommand);
17+
}
18+
19+
private int RunCommand()
20+
{
21+
var key = accountService.GenerateNewAccount();
22+
System.Console.WriteLine("Account Address: " + key.GetPublicAddress());
23+
System.Console.WriteLine("Private Key: " + key.GetPrivateKey());
24+
return 0;
25+
}
26+
27+
}
28+
}

Nethereum.Console/Services/AccountService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Nethereum.Hex.HexTypes;
33
using Nethereum.KeyStore;
44
using Nethereum.RPC.Eth.DTOs;
5+
using Nethereum.Signer;
56
using Nethereum.Web3.Accounts;
67
using System;
78
using System.Collections.Generic;
@@ -14,14 +15,18 @@ namespace Nethereum.Console
1415
{
1516
public class AccountService : IAccountService
1617
{
18+
public EthECKey GenerateNewAccount()
19+
{
20+
//Generate a private key pair using SecureRandom
21+
return Nethereum.Signer.EthECKey.GenerateKey();
22+
}
1723
public Account CreateAccount(string password, string path)
1824
{
1925
if (!Directory.Exists(path)){
2026
Directory.CreateDirectory(path);
2127
}
22-
//Generate a private key pair using SecureRandom
23-
var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
24-
//Get the public address (derivied from the public key)
28+
29+
var ecKey = GenerateNewAccount();
2530
var address = ecKey.GetPublicAddress();
2631

2732
//Create a store service, to encrypt and save the file using the web3 standard

Nethereum.Console/Services/IAccountService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Nethereum.Hex.HexTypes;
5+
using Nethereum.Signer;
56

67
namespace Nethereum.Console
78
{
@@ -17,5 +18,6 @@ public interface IAccountService
1718
Task<string> SendTransactionAsync(string keyStoreFilePath, string keyStorePassword, string addressTo, decimal etherAmount, string rpcUrl, HexBigInteger gas, HexBigInteger gasPrice, string data);
1819
Task<string> SendTransactionAsync(Account account, string addressTo, decimal etherAmount, string rpcUrl, HexBigInteger gas, HexBigInteger gasPrice, string data);
1920
Task<decimal> GetTokenBalanceAsync(string adddress, string contractAddress, string rpcUrl, int numberOfDecimalPlaces = 18);
21+
EthECKey GenerateNewAccount();
2022
}
2123
}

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ create-acccounts-mix-balances
113113
* -? | -h | --help Show help information
114114

115115
# TODO
116-
* ERC20 balance, transfer
116+
* SendTransaction (added for private key and key store file, this is done add readme usage)
117+
* ERC20 balance (done, add readme usage), transfer
117118
* Generare keystore file from private key, password
118-
* Generate private key, address
119+
* Generate private key, address (done, todo add readme usage)
120+
* Retrieve private key from key storage
119121
* Message sign
120-
* Transfer overloads
122+
* Deploy contract
121123
* Generic Smart contract call / transfer using abi and method
122124

123125
# License

0 commit comments

Comments
 (0)