Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Allow smart contract verification #628

Merged
merged 20 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 0 additions & 9 deletions neo-cli/CLI/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ namespace Neo.CLI
{
internal static class Helper
{
public static bool ToBool(this string input)
{
if (input == null) return false;

input = input.ToLowerInvariant();

return input == "true" || input == "yes" || input == "1";
}

public static bool IsYes(this string input)
{
if (input == null) return false;
Expand Down
7 changes: 1 addition & 6 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using Neo.ConsoleService;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.CLI
Expand Down Expand Up @@ -53,7 +48,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra
{
Signer[] signers = Array.Empty<Signer>();
if (signerAccounts != null && !NoWallet())
signers = CurrentWallet.GetAccounts().Where(p => !p.Lock && !p.WatchOnly && signerAccounts.Contains(p.ScriptHash)).Select(p => new Signer() { Account = p.ScriptHash, Scopes = WitnessScope.CalledByEntry }).ToArray();
signers = CurrentWallet.GetAccounts().Where(p => !p.Lock && signerAccounts.Contains(p.ScriptHash)).Select(p => new Signer() { Account = p.ScriptHash, Scopes = WitnessScope.CalledByEntry }).ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why WatchOnly address can be signer?

Copy link
Member Author

@shargon shargon Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart contract withdrawal require a watch only address.


Transaction tx = new Transaction
{
Expand Down
60 changes: 59 additions & 1 deletion neo-cli/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,57 @@ private void OnImportKeyCommand(string wifOrFile)
wallet.Save();
}

/// <summary>
/// Process "import watchonly" command
/// </summary>
[ConsoleCommand("import watchonly", Category = "Wallet Commands")]
private void OnImportWatchOnlyCommand(string addressOrFile)
{
UInt160 address = null;
try
{
address = StringToAddress(addressOrFile);
}
catch (FormatException) { }
if (address == null)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
var fileInfo = new FileInfo(addressOrFile);

if (!fileInfo.Exists)
{
Console.WriteLine($"Error: File '{fileInfo.FullName}' doesn't exists");
return;
}

if (fileInfo.Length > 1024 * 1024)
{
if (!ReadUserInput($"The file '{fileInfo.FullName}' is too big, do you want to continue? (yes|no)", false).IsYes())
{
return;
}
}

string[] lines = File.ReadAllLines(fileInfo.FullName).Where(u => !string.IsNullOrEmpty(u)).ToArray();
using (var percent = new ConsolePercent(0, lines.Length))
{
for (int i = 0; i < lines.Length; i++)
{
address = StringToAddress(lines[i]);
CurrentWallet.CreateAccount(address);
percent.Value++;
}
}
}
else
{
WalletAccount account = CurrentWallet.CreateAccount(address);
Console.WriteLine($"Address: {account.Address}");
Console.WriteLine($" Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
}
if (CurrentWallet is NEP6Wallet wallet)
wallet.Save();
}

/// <summary>
/// Process "list address" command
/// </summary>
Expand All @@ -294,8 +345,15 @@ private void OnListAddressCommand()

using (var snapshot = Blockchain.Singleton.GetSnapshot())
{
foreach (Contract contract in CurrentWallet.GetAccounts().Where(p => !p.WatchOnly).Select(p => p.Contract))
foreach (var account in CurrentWallet.GetAccounts())
{
if (account.WatchOnly)
{
Console.WriteLine($"{" Address: "}{account.Address}\tWatchOnly");
continue;
}

var contract = account.Contract;
var type = "Nonstandard";

if (contract.Script.IsMultiSigContract())
Expand Down
45 changes: 21 additions & 24 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,7 @@ public MainService() : base()

RegisterCommandHander<string, UInt256>(false, (str) => UInt256.Parse(str));
RegisterCommandHander<string[], UInt256[]>((str) => str.Select(u => UInt256.Parse(u.Trim())).ToArray());
RegisterCommandHander<string[], UInt160[]>((arr) =>
{
return arr.Select(str =>
{
switch (str.ToLowerInvariant())
{
case "neo": return NativeContract.NEO.Hash;
case "gas": return NativeContract.GAS.Hash;
}

// Try to parse as UInt160

if (UInt160.TryParse(str, out var addr))
{
return addr;
}

// Accept wallet format

return str.ToScriptHash();
})
.ToArray();
});

RegisterCommandHander<string[], UInt160[]>((arr) => arr.Select(str => StringToAddress(str)).ToArray());
RegisterCommandHander<string, ECPoint>((str) => ECPoint.Parse(str.Trim(), ECCurve.Secp256r1));
RegisterCommandHander<string[], ECPoint[]>((str) => str.Select(u => ECPoint.Parse(u.Trim(), ECCurve.Secp256r1)).ToArray());
RegisterCommandHander<string, JObject>((str) => JObject.Parse(str));
Expand All @@ -124,6 +101,26 @@ public MainService() : base()
RegisterCommand(this);
}

internal static UInt160 StringToAddress(string input)
{
switch (input.ToLowerInvariant())
{
case "neo": return NativeContract.NEO.Hash;
case "gas": return NativeContract.GAS.Hash;
}

// Try to parse as UInt160

if (UInt160.TryParse(input, out var addr))
{
return addr;
}

// Accept wallet format

return input.ToScriptHash();
}

public override void RunConsole()
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Expand Down