Skip to content

Commit b558c50

Browse files
committed
Merge branch 'master' into commandline
* master: Adding NNS to `neo-cli` (neo-project#3032) # Conflicts: # src/Neo.CLI/Settings.cs
2 parents 392ba58 + de7fc93 commit b558c50

File tree

10 files changed

+91
-7
lines changed

10 files changed

+91
-7
lines changed

src/Neo.CLI/CLI/MainService.CommandLine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ private static void CustomApplicationSettings(CommandLineOptions options, Settin
8484
{
8585
Path = options.Wallet ?? tempSetting.UnlockWallet.Path,
8686
Password = options.Password ?? tempSetting.UnlockWallet.Password
87-
}
87+
},
88+
Contracts = tempSetting.Contracts
8889
};
8990
if (options.IsValid) Settings.Custom = customSetting;
9091
}

src/Neo.CLI/CLI/MainService.Wallet.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160? fr
511511
ConsoleHelper.Error("Incorrect password");
512512
return;
513513
}
514+
514515
var snapshot = NeoSystem.StoreView;
515516
Transaction tx;
516517
AssetDescriptor descriptor = new(snapshot, NeoSystem.Settings, asset);
@@ -550,10 +551,10 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160? fr
550551
return;
551552
}
552553

553-
ConsoleHelper.Info("Network fee: ",
554-
$"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}\t",
555-
"Total fee: ",
556-
$"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
554+
ConsoleHelper.Info(
555+
"Send To: ", $"{to.ToAddress(NeoSystem.Settings.AddressVersion)}\n",
556+
"Network fee: ", $"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}\t",
557+
"Total fee: ", $"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
557558
if (!ConsoleHelper.ReadUserInput("Relay tx? (no|yes)").IsYes())
558559
{
559560
return;

src/Neo.CLI/CLI/MainService.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,19 @@ public MainService() : base()
9696
Initialize_Logger();
9797
}
9898

99-
internal static UInt160 StringToAddress(string input, byte version)
99+
internal UInt160 StringToAddress(string input, byte version)
100100
{
101101
switch (input.ToLowerInvariant())
102102
{
103103
case "neo": return NativeContract.NEO.Hash;
104104
case "gas": return NativeContract.GAS.Hash;
105105
}
106106

107+
if (input.IndexOf('.') > 0 && input.LastIndexOf('.') < input.Length)
108+
{
109+
return ResolveNeoNameServiceAddress(input);
110+
}
111+
107112
// Try to parse as UInt160
108113

109114
if (UInt160.TryParse(input, out var addr))
@@ -633,5 +638,45 @@ static string GetExceptionMessage(Exception exception)
633638

634639
return exception.Message;
635640
}
641+
642+
public UInt160 ResolveNeoNameServiceAddress(string domain)
643+
{
644+
if (Settings.Default.Contracts.NeoNameService == UInt160.Zero)
645+
throw new Exception("Neo Name Service (NNS): is disabled on this network.");
646+
647+
using var sb = new ScriptBuilder();
648+
sb.EmitDynamicCall(Settings.Default.Contracts.NeoNameService, "resolve", CallFlags.ReadOnly, domain, 16);
649+
650+
using var appEng = ApplicationEngine.Run(sb.ToArray(), NeoSystem.StoreView, settings: NeoSystem.Settings);
651+
if (appEng.State == VMState.HALT)
652+
{
653+
var data = appEng.ResultStack.Pop();
654+
if (data is ByteString)
655+
{
656+
try
657+
{
658+
var addressData = data.GetString();
659+
if (UInt160.TryParse(addressData, out var address))
660+
return address;
661+
else
662+
return addressData.ToScriptHash(NeoSystem.Settings.AddressVersion);
663+
}
664+
catch { }
665+
}
666+
else if (data is Null)
667+
{
668+
throw new Exception($"Neo Name Service (NNS): \"{domain}\" domain not found.");
669+
}
670+
throw new Exception("Neo Name Service (NNS): Record invalid address format.");
671+
}
672+
else
673+
{
674+
if (appEng.FaultException is not null)
675+
{
676+
throw new Exception($"Neo Name Service (NNS): \"{appEng.FaultException.Message}\".");
677+
}
678+
}
679+
throw new Exception($"Neo Name Service (NNS): \"{domain}\" domain not found.");
680+
}
636681
}
637682
}

src/Neo.CLI/Settings.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using Microsoft.Extensions.Configuration;
1313
using Neo.Network.P2P;
14+
using System;
1415
using System.Threading;
1516

1617
namespace Neo
@@ -21,6 +22,7 @@ public class Settings
2122
public StorageSettings Storage { get; init; }
2223
public P2PSettings P2P { get; init; }
2324
public UnlockWalletSettings UnlockWallet { get; init; }
25+
public ContractsSettings Contracts { get; init;}
2426

2527
static Settings? s_default;
2628

@@ -52,6 +54,7 @@ public static Settings Default
5254

5355
public Settings(IConfigurationSection section)
5456
{
57+
Contracts = new(section.GetSection(nameof(Contracts)));
5558
Logger = new(section.GetSection(nameof(Logger)));
5659
Storage = new(section.GetSection(nameof(Storage)));
5760
P2P = new(section.GetSection(nameof(P2P)));
@@ -133,4 +136,22 @@ public UnlockWalletSettings(IConfigurationSection section)
133136

134137
public UnlockWalletSettings() { }
135138
}
139+
140+
public class ContractsSettings
141+
{
142+
public UInt160 NeoNameService { get; } = UInt160.Zero;
143+
144+
public ContractsSettings(IConfigurationSection section)
145+
{
146+
if (section.Exists())
147+
{
148+
if (UInt160.TryParse(section.GetValue(nameof(NeoNameService), string.Empty), out var hash))
149+
{
150+
NeoNameService = hash;
151+
}
152+
else
153+
throw new ArgumentException("Neo Name Service (NNS): NeoNameService hash is invalid. Check your config.json.", nameof(NeoNameService));
154+
}
155+
}
156+
}
136157
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"Path": "",
2020
"Password": "",
2121
"IsActive": false
22+
},
23+
"Contracts": {
24+
"NeoNameService": "0x7061fbd31562664b58f422c3dee4acfd70dba8af"
2225
}
2326
},
2427
"ProtocolConfiguration": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"Path": "",
2020
"Password": "",
2121
"IsActive": false
22+
},
23+
"Contracts": {
24+
"NeoNameService": "0xfb08ccf30ab534a871b7b092a49fe70c154ed678"
2225
}
2326
},
2427
"ProtocolConfiguration": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"Path": "",
2020
"Password": "",
2121
"IsActive": false
22+
},
23+
"Contracts": {
24+
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
2225
}
2326
},
2427
"ProtocolConfiguration": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"Path": "",
2020
"Password": "",
2121
"IsActive": false
22+
},
23+
"Contracts": {
24+
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
2225
}
2326
},
2427
"ProtocolConfiguration": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"Path": "",
2020
"Password": "",
2121
"IsActive": false
22+
},
23+
"Contracts": {
24+
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
2225
}
2326
},
2427
"ProtocolConfiguration": {

src/Neo.ConsoleService/ConsoleServiceBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ private bool OnCommand(string commandLine)
8888

8989
availableCommands.Add((command, arguments.ToArray()));
9090
}
91-
catch
91+
catch (Exception ex)
9292
{
9393
// Skip parse errors
9494
possibleHelp = command.Key;
95+
ConsoleHelper.Error($"{ex.InnerException?.Message ?? ex.Message}");
9596
}
9697
}
9798
}

0 commit comments

Comments
 (0)