Skip to content

Commit de7fc93

Browse files
Adding NNS to neo-cli (#3032)
* Add NNS to UInt160 parameters for neo-cli * Fixed typo * Update src/Neo.ConsoleService/ConsoleServiceBase.cs * Added NNS hashes to config files. Updated code to reflect the changes * Updated support for neofs * Added ReadOnly Call Flag to the contract call * Updated to allow all domains * Update src/Neo.CLI/Settings.cs * null check * Update config.testnet.json with contract hash * Fixes * fixed wallet * Update src/Neo.CLI/Settings.cs Two vs one in this discussion * Update Settings.cs * Update Settings.cs * Refactor NNS definition * clean new * Show NNS Fault error --------- Co-authored-by: Shargon <shargon@gmail.com>
1 parent e04da14 commit de7fc93

File tree

9 files changed

+89
-6
lines changed

9 files changed

+89
-6
lines changed

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))
@@ -636,5 +641,45 @@ static string GetExceptionMessage(Exception exception)
636641

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

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; }
2223
public P2PSettings P2P { get; }
2324
public UnlockWalletSettings UnlockWallet { get; }
25+
public ContractsSettings Contracts { get; }
2426

2527
static Settings? s_default;
2628

@@ -51,6 +53,7 @@ public static Settings Default
5153

5254
public Settings(IConfigurationSection section)
5355
{
56+
Contracts = new(section.GetSection(nameof(Contracts)));
5457
Logger = new(section.GetSection(nameof(Logger)));
5558
Storage = new(section.GetSection(nameof(Storage)));
5659
P2P = new(section.GetSection(nameof(P2P)));
@@ -116,4 +119,22 @@ public UnlockWalletSettings(IConfigurationSection section)
116119
}
117120
}
118121
}
122+
123+
public class ContractsSettings
124+
{
125+
public UInt160 NeoNameService { get; } = UInt160.Zero;
126+
127+
public ContractsSettings(IConfigurationSection section)
128+
{
129+
if (section.Exists())
130+
{
131+
if (UInt160.TryParse(section.GetValue(nameof(NeoNameService), string.Empty), out var hash))
132+
{
133+
NeoNameService = hash;
134+
}
135+
else
136+
throw new ArgumentException("Neo Name Service (NNS): NeoNameService hash is invalid. Check your config.json.", nameof(NeoNameService));
137+
}
138+
}
139+
}
119140
}

src/Neo.CLI/config.fs.mainnet.json

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": {

src/Neo.CLI/config.fs.testnet.json

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": {

src/Neo.CLI/config.json

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.CLI/config.mainnet.json

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.CLI/config.testnet.json

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)