Skip to content

Commit 659e92d

Browse files
Send transaction private key
1 parent ef8483d commit 659e92d

7 files changed

+146
-70
lines changed

Nethereum.Console/App.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public App()
1010

1111
Commands.Add(new CreateAccountCommand());
1212
Commands.Add(new SendTransactionFromAccountFileCommand(accountService));
13+
Commands.Add(new SendTransactionFromAccountPrivateKeyCommand(accountService));
1314
Commands.Add(new TransferEtherFromAccountFileCommand(accountService));
1415
Commands.Add(new TransferEtherFromAccountPrivateKeyCommand(accountService));
1516
Commands.Add(new CreateAccountsAndMixBalancesCommand());

Nethereum.Console/CommandOptions/TransactionCommandOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public override void ParseAndValidateInput()
3232
base.ParseAndValidateInput();
3333
Data = DataOption.Value();
3434
Gas = GasOption.TryParseHexBigIntegerValue(HasInputErrors, false);
35+
if (Gas != null && Gas.Value == 0) Gas = null;
3536
GasPrice = GasPriceOption.TryParseHexBigIntegerValue(HasInputErrors, false);
37+
if (GasPrice != null && GasPrice.Value == 0) GasPrice = null;
3638
}
3739

3840
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Extensions.CommandLineUtils;
5+
using Nethereum.Web3.Accounts;
6+
7+
namespace Nethereum.Console
8+
{
9+
public abstract class SendTransactionBaseCommand : CommandLineApplication
10+
{
11+
protected TransactionCommandOptions _transactionCommandOptions;
12+
protected IAccountService accountService;
13+
14+
public SendTransactionBaseCommand(IAccountService accountService)
15+
{
16+
this.accountService = accountService;
17+
InitOptions();
18+
HelpOption("-? | -h | --help");
19+
OnExecute((Func<int>)RunCommand);
20+
}
21+
22+
protected virtual void InitOptions()
23+
{
24+
_transactionCommandOptions = new TransactionCommandOptions(accountService);
25+
_transactionCommandOptions.AddOptionToCommandLineApplication(this);
26+
}
27+
28+
protected abstract int RunCommand();
29+
30+
}
31+
}
Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,64 @@
11
using System;
22
using Microsoft.Extensions.CommandLineUtils;
3+
using Nethereum.Web3.Accounts;
34

45
namespace Nethereum.Console
56
{
6-
public abstract class SendTransactionBaseCommand : CommandLineApplication
7-
{
8-
protected TransactionCommandOptions _transactionCommandOptions;
9-
protected IAccountService accountService;
10-
11-
public SendTransactionBaseCommand(IAccountService accountService)
7+
public class SendTransactionFromAccountFileCommand : SendTransactionBaseCommand
128
{
13-
this.accountService = accountService;
14-
InitOptions();
15-
HelpOption("-? | -h | --help");
16-
OnExecute((Func<int>)RunCommand);
17-
}
9+
protected AccountKeyStoreCommandOption _accountKeyStoreCommandOption;
1810

19-
protected virtual void InitOptions()
20-
{
21-
_transactionCommandOptions = new TransactionCommandOptions(accountService);
22-
_transactionCommandOptions.AddOptionToCommandLineApplication(this);
23-
}
24-
25-
protected abstract int RunCommand();
26-
27-
}
28-
29-
public class SendTransactionFromAccountFileCommand : SendTransactionBaseCommand
30-
{
31-
protected AccountKeyStoreCommandOption _accountKeyStoreCommandOption;
32-
33-
public SendTransactionFromAccountFileCommand(IAccountService accountService):base(accountService)
34-
{
35-
Name = "send-transaction-from-account-file";
36-
Description = "Sends a transaction using the account's key store file";
37-
38-
HelpOption("-? | -h | --help");
39-
OnExecute((Func<int>)RunCommand);
40-
}
41-
42-
protected override void InitOptions()
43-
{
44-
base.InitOptions();
45-
_accountKeyStoreCommandOption = new AccountKeyStoreCommandOption();
46-
_accountKeyStoreCommandOption.AddOptionToCommandLineApplication(this);
47-
}
48-
49-
protected override int RunCommand()
50-
{
51-
try
11+
public SendTransactionFromAccountFileCommand(IAccountService accountService) : base(accountService)
5212
{
53-
_transactionCommandOptions.ParseAndValidateInput();
54-
_accountKeyStoreCommandOption.ParseAndValidateInput();
13+
Name = "send-transaction-from-account-file";
14+
Description = "Sends a transaction using the account's key store file";
5515

56-
if (_transactionCommandOptions.HasInputErrors) return 1;
57-
if (_accountKeyStoreCommandOption.HasInputErrors) return 1;
16+
HelpOption("-? | -h | --help");
17+
OnExecute((Func<int>)RunCommand);
18+
}
5819

20+
protected override void InitOptions()
21+
{
22+
base.InitOptions();
23+
_accountKeyStoreCommandOption = new AccountKeyStoreCommandOption();
24+
_accountKeyStoreCommandOption.AddOptionToCommandLineApplication(this);
25+
}
5926

60-
var txn = accountService.SendTransaction(_accountKeyStoreCommandOption.AccountFile,
61-
_accountKeyStoreCommandOption.Password,
62-
_transactionCommandOptions.ToAddress,
63-
_transactionCommandOptions.Amount ?? _transactionCommandOptions.Amount.Value,
64-
_transactionCommandOptions.RpcAddress,
65-
_transactionCommandOptions.Gas,
66-
_transactionCommandOptions.GasPrice,
67-
_transactionCommandOptions.Data
68-
).Result;
27+
protected override int RunCommand()
28+
{
29+
try
30+
{
31+
_transactionCommandOptions.ParseAndValidateInput();
32+
_accountKeyStoreCommandOption.ParseAndValidateInput();
33+
34+
if (_transactionCommandOptions.HasInputErrors) return 1;
35+
if (_accountKeyStoreCommandOption.HasInputErrors) return 1;
36+
37+
38+
var txn = accountService.SendTransaction(_accountKeyStoreCommandOption.AccountFile,
39+
_accountKeyStoreCommandOption.Password,
40+
_transactionCommandOptions.ToAddress,
41+
_transactionCommandOptions.Amount ?? _transactionCommandOptions.Amount.Value,
42+
_transactionCommandOptions.RpcAddress,
43+
_transactionCommandOptions.Gas,
44+
_transactionCommandOptions.GasPrice,
45+
_transactionCommandOptions.Data
46+
).Result;
47+
48+
System.Console.WriteLine(txn + " transaction submitted");
49+
return 0;
50+
51+
}
52+
catch (Exception ex)
53+
{
54+
System.Console.WriteLine("Error: " + ex.Message);
55+
System.Console.WriteLine("Stack trace: " + ex.StackTrace);
56+
return 1;
57+
}
6958

70-
System.Console.WriteLine(txn + " sent");
7159
return 0;
72-
73-
}
74-
catch (Exception ex)
75-
{
76-
System.Console.WriteLine("Error: " + ex.Message);
77-
System.Console.WriteLine("Stack trace: " + ex.StackTrace);
78-
return 1;
7960
}
8061

81-
return 0;
82-
}
8362

84-
63+
}
8564
}
86-
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Extensions.CommandLineUtils;
5+
using Nethereum.Web3.Accounts;
6+
7+
namespace Nethereum.Console
8+
{
9+
public class SendTransactionFromAccountPrivateKeyCommand : SendTransactionBaseCommand
10+
{
11+
protected PrivateKeyCommandOption _privateKeyCommandOption;
12+
13+
public SendTransactionFromAccountPrivateKeyCommand(IAccountService accountService) : base(accountService)
14+
{
15+
Name = "send-transaction-from-account-private-key";
16+
Description = "Sends a transaction using the account's private key";
17+
18+
HelpOption("-? | -h | --help");
19+
OnExecute((Func<int>)RunCommand);
20+
}
21+
22+
protected override void InitOptions()
23+
{
24+
base.InitOptions();
25+
_privateKeyCommandOption = new PrivateKeyCommandOption();
26+
_privateKeyCommandOption.AddOptionToCommandLineApplication(this);
27+
}
28+
29+
protected override int RunCommand()
30+
{
31+
try
32+
{
33+
_transactionCommandOptions.ParseAndValidateInput();
34+
_privateKeyCommandOption.ParseAndValidateInput();
35+
36+
if (_transactionCommandOptions.HasInputErrors) return 1;
37+
if (_privateKeyCommandOption.HasInputErrors) return 1;
38+
39+
40+
var txn = accountService.SendTransaction(new Account(_privateKeyCommandOption.PrivateKey),
41+
_transactionCommandOptions.ToAddress,
42+
_transactionCommandOptions.Amount ?? _transactionCommandOptions.Amount.Value,
43+
_transactionCommandOptions.RpcAddress,
44+
_transactionCommandOptions.Gas,
45+
_transactionCommandOptions.GasPrice,
46+
_transactionCommandOptions.Data
47+
).Result;
48+
49+
System.Console.WriteLine(txn + " transaction submitted");
50+
return 0;
51+
52+
}
53+
catch (Exception ex)
54+
{
55+
System.Console.WriteLine("Error: " + ex.Message);
56+
System.Console.WriteLine("Stack trace: " + ex.StackTrace);
57+
return 1;
58+
}
59+
60+
return 0;
61+
}
62+
}
63+
}

Nethereum.Console/Commands/TransferEtherFromAccountPrivateKeyCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TransferEtherFromAccountPrivateKeyCommand : TransferEtherBaseComman
1010
public TransferEtherFromAccountPrivateKeyCommand(IAccountService accountService) : base(accountService)
1111
{
1212
Name = "account-transfer-from-account-private-key";
13-
Description = "Transfers ether from a given acccount file (key store file) to another account";
13+
Description = "Transfers ether using the account's private key";
1414
}
1515

1616
protected override void InitOptions()

Nethereum.Console/Services/IAccountService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IAccountService
1515
Task<string> TransferEther(string keyStoreFilePath, string keyStorePassword, string addressTo, decimal etherAmount, string rpcUrl);
1616
Task<string> TransferEther(Account account, string addressTo, decimal etherAmount, string rpcUrl);
1717
Task<string> SendTransaction(string keyStoreFilePath, string keyStorePassword, string addressTo, decimal etherAmount, string rpcUrl, HexBigInteger gas, HexBigInteger gasPrice, string data);
18+
Task<string> SendTransaction(Account account, string addressTo, decimal etherAmount, string rpcUrl, HexBigInteger gas, HexBigInteger gasPrice, string data);
1819

1920
}
2021
}

0 commit comments

Comments
 (0)