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

improve parse method in neo-cli #3204

Merged
merged 30 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e1d8e14
Update MainService.Tools.cs
chenzhitong Apr 24, 2024
25b16f6
format
chenzhitong Apr 24, 2024
0bcbcda
Update MainService.Tools.cs
chenzhitong Apr 24, 2024
0c36106
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
43faa79
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
4acbe95
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
ecc7cbb
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
9e6f07c
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
ff3406d
Update MainService.Tools.cs
chenzhitong Apr 26, 2024
214f62b
update
chenzhitong May 1, 2024
7b5c9e1
Update MainService.Tools.cs
chenzhitong May 1, 2024
7b9a17a
Update MainService.Tools.cs
chenzhitong May 6, 2024
75f4473
Merge branch 'master' into converter
chenzhitong May 6, 2024
b42fa7b
Update MainService.Tools.cs
chenzhitong May 6, 2024
7d5c5ca
Merge branch 'converter' of /~https://github.com/chenzhitong/neo into c…
chenzhitong May 6, 2024
31c226f
Merge branch 'master' into converter
chenzhitong May 7, 2024
90d10ef
format
chenzhitong May 7, 2024
010f34b
Merge branch 'master' into converter
chenzhitong May 9, 2024
cd69789
Merge branch 'master' into converter
chenzhitong May 10, 2024
819f1cf
Merge branch 'master' into converter
chenzhitong May 10, 2024
4fb2a2d
Update MainService.Tools.cs
chenzhitong May 10, 2024
fcc8cec
Merge branch 'master' into converter
cschuchardt88 May 10, 2024
484f1e6
Merge branch 'master' into converter
shargon May 13, 2024
250c8ea
Review
shargon May 13, 2024
6fdab35
fixedbug
chenzhitong May 14, 2024
17141e4
Update MainService.Tools.cs
chenzhitong May 14, 2024
63e992a
Merge branch 'master' into converter
shargon May 16, 2024
ef81c32
Merge branch 'master' into converter
Jim8y May 16, 2024
4f6afbb
Merge branch 'master' into converter
shargon May 20, 2024
f6e652e
Merge branch 'master' into converter
shargon May 20, 2024
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
46 changes: 18 additions & 28 deletions src/Neo.CLI/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@

namespace Neo.CLI
{
public class ParseFunctionAttribute : Attribute
{
public string Description { get; }

public ParseFunctionAttribute(string description)
{
Description = description;
}
}

partial class MainService
{
/// <summary>
Expand Down Expand Up @@ -265,7 +255,7 @@
/// Network Id to String
/// input: 860833102
/// output: NEO3
[ParseFunction("Network Id to String")]

Check warning on line 258 in src/Neo.CLI/CLI/MainService.Tools.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'

Check warning on line 258 in src/Neo.CLI/CLI/MainService.Tools.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'
private string? NumberToHexToString(string input)
{
try
Expand Down Expand Up @@ -529,6 +519,24 @@
}
}

private bool IsValidPubKey(string pubKey)
{
if (pubKey.Length != 66) return false;
if (pubKey[0] != '0') return false;
if (pubKey[1] != '2' && pubKey[1] != '3') return false;

for (var i = 2; i < pubKey.Length; i++)
{
var c = pubKey[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
{
return false;
}
}

return true;
Copy link
Member

@cschuchardt88 cschuchardt88 May 1, 2024

Choose a reason for hiding this comment

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

Suggested change
if (pubKey.Length != 66) return false;
if (pubKey[0] != '0') return false;
if (pubKey[1] != '2' && pubKey[1] != '3') return false;
for (var i = 2; i < pubKey.Length; i++)
{
var c = pubKey[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
{
return false;
}
}
return true;
return ECPoint.TryParse(pubkey, ECCurve.Secp256r1, out _);

Copy link
Member

@cschuchardt88 cschuchardt88 May 1, 2024

Choose a reason for hiding this comment

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

public keys can be many lengths and prefixes. Look at Neo.Cryptography.ECC.ECPoint class in method TryParse, Parse, DecodePoint or FromBytes

Copy link
Member Author

Choose a reason for hiding this comment

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

Already removed IsValidPubKey method

}

/// <summary>
/// Public Key to Address
/// input: 03dab84c1243ec01ab2500e1a8c7a1546a26d734628180b0cf64e72bf776536997
Expand All @@ -537,24 +545,6 @@
[ParseFunction("Public Key to Address")]
private string? PublicKeyToAddress(string pubKey)
{
bool IsValidPubKey(string pubKey)
{
if (pubKey.Length != 66) return false;
if (pubKey[0] != '0') return false;
if (pubKey[1] != '2' && pubKey[1] != '3') return false;

for (int i = 2; i < pubKey.Length; i++)
{
char c = pubKey[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
{
return false;
}
}

return true;
}

if (!IsValidPubKey(pubKey)) return null;
return Contract.CreateSignatureContract(ECPoint.Parse(pubKey, ECCurve.Secp256r1)).ScriptHash.ToAddress(NeoSystem.Settings.AddressVersion);
}
Expand Down Expand Up @@ -679,7 +669,7 @@
{
var attribute = field.GetCustomAttribute<OperandSizeAttribute>();
if (attribute == null) continue;
int index = (int)(OpCode)field.GetValue(null);

Check warning on line 672 in src/Neo.CLI/CLI/MainService.Tools.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unboxing a possibly null value.

Check warning on line 672 in src/Neo.CLI/CLI/MainService.Tools.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Unboxing a possibly null value.
OperandSizePrefixTable[index] = attribute.SizePrefix;
OperandSizeTable[index] = attribute.Size;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Neo.CLI/CLI/ParseFunctionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ParseFunctionAttribute.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;

namespace Neo.CLI
{
internal class ParseFunctionAttribute : Attribute
{
public string Description { get; }

public ParseFunctionAttribute(string description)
{
Description = description;
}
}
}