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

Use testnet/mainnet network configuration #335

Merged
merged 7 commits into from
Nov 15, 2023
Merged
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
39 changes: 34 additions & 5 deletions src/neoxp/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
// modifications are permitted.

using McMaster.Extensions.CommandLineUtils;
using Neo.BlockchainToolkit;
using Neo.Network.RPC;
using System.Diagnostics;
using System.Security.Principal;
using static Neo.BlockchainToolkit.Constants;
using static Neo.BlockchainToolkit.Utility;

namespace NeoExpress.Commands
{
[Command("create", Description = "Create new neo-express instance")]
internal class CreateCommand
{
readonly ExpressChainManagerFactory chainManagerFactory;
readonly TransactionExecutorFactory txExecutorFactory;

public CreateCommand(ExpressChainManagerFactory chainManagerFactory)
public CreateCommand(ExpressChainManagerFactory chainManagerFactory, TransactionExecutorFactory txExecutorFactory)
{
this.chainManagerFactory = chainManagerFactory;
this.txExecutorFactory = txExecutorFactory;
}

[Argument(0, Description = $"Name of {EXPRESS_EXTENSION} file to create\nDefault location is home directory as:\nLinux: $HOME/.neo-express/{DEFAULT_EXPRESS_FILENAME}\nWindows: %UserProfile%\\.neo-express\\{DEFAULT_EXPRESS_FILENAME}")]
Expand All @@ -40,20 +47,42 @@ public CreateCommand(ExpressChainManagerFactory chainManagerFactory)
[Option(Description = "Private key for default dev account (Default: Random)")]
internal string PrivateKey { get; set; } = string.Empty;

internal int OnExecute(CommandLineApplication app, IConsole console)
[Option(Description = "Synchronize local policy with URL of Neo JSON-RPC Node\nSpecify MainNet, TestNet or JSON-RPC URL\nDefault is not to sync")]
internal string RpcUri { get; set; } = string.Empty;

internal async Task<int> OnExecuteAsync(CommandLineApplication app, IConsole console)
{
try
{
Models.PolicyValues? policyValues = null;

if (string.IsNullOrEmpty(RpcUri) == false)
{
if (TryParseRpcUri(RpcUri, out var uri) == false)
throw new ArgumentException($"Invalid RpcUri value \"{RpcUri}\"");
else
{
using var rpcClient = new RpcClient(uri);
policyValues = await rpcClient.GetPolicyAsync().ConfigureAwait(false);
}
}

byte[]? priKey = null;
if (string.IsNullOrEmpty(PrivateKey) == false)
priKey = Convert.FromHexString(PrivateKey);

var (chainManager, outputPath) = chainManagerFactory.CreateChain(Count, AddressVersion, Output, Force, privateKey: priKey);
chainManager.SaveChain(outputPath);

console.Out.WriteLine($"Created {Count} node privatenet at {outputPath}");
console.Out.WriteLine(" Note: The private keys for the accounts in this file are are *not* encrypted.");
console.Out.WriteLine(" Do not use these accounts on MainNet or in any other system where security is a concern.");
await console.Out.WriteLineAsync($"Created {Count} node privatenet at {outputPath}").ConfigureAwait(false);
await console.Out.WriteLineAsync(" Note: The private keys for the accounts in this file are are *not* encrypted.").ConfigureAwait(false);
await console.Out.WriteLineAsync(" Do not use these accounts on MainNet or in any other system where security is a concern.\n").ConfigureAwait(false);

if (policyValues != null)
{
using var txExec = txExecutorFactory.Create(chainManager, false, false);
await txExec.SetPolicyAsync(policyValues!, ExpressChainExtensions.GENESIS, string.Empty).ConfigureAwait(false);
}

return 0;
}
Expand Down