Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Extensions.cs file belongs to the neo project and is free
// AssemblyExtensions.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
Expand All @@ -17,7 +17,7 @@ namespace Neo
/// <summary>
/// Extension methods
/// </summary>
internal static class Extensions
internal static class AssemblyExtensions
{
public static string GetVersion(this Assembly assembly)
{
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.SmartContract;
using Neo.VM;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Vote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.Actor;
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.Actor;
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/Neo.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<ProjectReference Include="..\Neo.ConsoleService\Neo.ConsoleService.csproj" />
<ProjectReference Include="..\Neo.Extensions\Neo.Extensions.csproj" />
<ProjectReference Include="..\Neo\Neo.csproj" />
</ItemGroup>

Expand Down
59 changes: 59 additions & 0 deletions src/Neo.Extensions/ByteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ByteExtensions.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;
using System.Text;

namespace Neo.Extensions
{
public static class ByteExtensions
{
/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this byte[] value)
{
StringBuilder sb = new();
foreach (var b in value)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}

/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <param name="reverse">Indicates whether it should be converted in the reversed byte order.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this byte[] value, bool reverse = false)
{
StringBuilder sb = new();
for (var i = 0; i < value.Length; i++)
sb.AppendFormat("{0:x2}", value[reverse ? value.Length - i - 1 : i]);
return sb.ToString();
}

/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this ReadOnlySpan<byte> value)
{
StringBuilder sb = new();
foreach (var b in value)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}
}
}
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/DeployContractDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/InvokeContractDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Properties;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/SigningDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography;
using Neo.Extensions;
using Neo.Properties;
using Neo.Wallets;
using System;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/ViewContractDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.SmartContract;
using Neo.Wallets;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/ViewPrivateKeyDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.Wallets;
using System.Windows.Forms;

Expand Down
1 change: 1 addition & 0 deletions src/Neo.GUI/GUI/Wrappers/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using System;
using System.ComponentModel;
using System.Globalization;
Expand Down
3 changes: 1 addition & 2 deletions src/Neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Linq;
using System.Numerics;
using System.Text;
using static Neo.Helper;

namespace Neo.Cryptography
{
Expand Down Expand Up @@ -86,7 +85,7 @@ public static byte[] Decode(string input)
var leadingZeros = new byte[leadingZeroCount];
if (bi.IsZero) return leadingZeros;
var bytesWithoutLeadingZeros = bi.ToByteArray(isUnsigned: true, isBigEndian: true);
return Concat(leadingZeros, bytesWithoutLeadingZeros);
return [.. leadingZeros, .. bytesWithoutLeadingZeros];
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System;
using System.IO;
using System.Numerics;
using static Neo.Helper;

namespace Neo.Cryptography.ECC
{
Expand Down Expand Up @@ -225,8 +224,8 @@ public static ECPoint FromBytes(byte[] bytes, ECCurve curve)
return bytes.Length switch
{
33 or 65 => DecodePoint(bytes, curve),
64 or 72 => DecodePoint(Concat(new byte[] { 0x04 }, bytes[^64..]), curve),
96 or 104 => DecodePoint(Concat(new byte[] { 0x04 }, bytes[^96..^32]), curve),
64 or 72 => DecodePoint([.. new byte[] { 0x04 }, .. bytes[^64..]], curve),
96 or 104 => DecodePoint([.. new byte[] { 0x04 }, .. bytes[^96..^32]], curve),
_ => throw new FormatException(),
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/Neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static Neo.Helper;
using ECPoint = Neo.Cryptography.ECC.ECPoint;

namespace Neo.Cryptography
Expand Down Expand Up @@ -213,7 +212,7 @@ public static byte[] AES256Encrypt(this byte[] plainData, byte[] key, byte[] non
var length = cipher.ProcessBytes(plainData, 0, plainData.Length, cipherBytes, 0);
cipher.DoFinal(cipherBytes, length);
}
return Concat(nonce, cipherBytes, tag);
return [.. nonce, .. cipherBytes, .. tag];
}

public static byte[] AES256Decrypt(this byte[] encryptedData, byte[] key, byte[] associatedData = null)
Expand Down
95 changes: 0 additions & 95 deletions src/Neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
using System.Net;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

namespace Neo
{
Expand All @@ -29,59 +27,6 @@ public static class Helper
{
private static readonly DateTime unixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int BitLen(int w)
Comment thread
shargon marked this conversation as resolved.
{
return (w < 1 << 15 ? (w < 1 << 7
? (w < 1 << 3 ? (w < 1 << 1
? (w < 1 << 0 ? (w < 0 ? 32 : 0) : 1)
: (w < 1 << 2 ? 2 : 3)) : (w < 1 << 5
? (w < 1 << 4 ? 4 : 5)
: (w < 1 << 6 ? 6 : 7)))
: (w < 1 << 11
? (w < 1 << 9 ? (w < 1 << 8 ? 8 : 9) : (w < 1 << 10 ? 10 : 11))
: (w < 1 << 13 ? (w < 1 << 12 ? 12 : 13) : (w < 1 << 14 ? 14 : 15)))) : (w < 1 << 23 ? (w < 1 << 19
? (w < 1 << 17 ? (w < 1 << 16 ? 16 : 17) : (w < 1 << 18 ? 18 : 19))
: (w < 1 << 21 ? (w < 1 << 20 ? 20 : 21) : (w < 1 << 22 ? 22 : 23))) : (w < 1 << 27
? (w < 1 << 25 ? (w < 1 << 24 ? 24 : 25) : (w < 1 << 26 ? 26 : 27))
: (w < 1 << 29 ? (w < 1 << 28 ? 28 : 29) : (w < 1 << 30 ? 30 : 31)))));
}

/// <summary>
/// Concatenates the specified byte arrays.
/// </summary>
/// <param name="buffers">The byte arrays to concatenate.</param>
/// <returns>The concatenated byte array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] Concat(params byte[][] buffers)
Comment thread
cschuchardt88 marked this conversation as resolved.
{
int length = 0;
for (int i = 0; i < buffers.Length; i++)
length += buffers[i].Length;
byte[] dst = new byte[length];
int p = 0;
foreach (byte[] src in buffers)
{
Buffer.BlockCopy(src, 0, dst, p, src.Length);
p += src.Length;
}
return dst;
}

/// <summary>
/// Concatenates two byte arrays.
/// </summary>
/// <param name="a">The first byte array to concatenate.</param>
/// <param name="b">The second byte array to concatenate.</param>
/// <returns>The concatenated byte array.</returns>
public static byte[] Concat(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
{
byte[] buffer = new byte[a.Length + b.Length];
a.CopyTo(buffer);
b.CopyTo(buffer.AsSpan(a.Length));
return buffer;
}

internal static void Remove<T>(this HashSet<T> set, ISet<T> other)
{
if (set.Count > other.Count)
Expand Down Expand Up @@ -158,46 +103,6 @@ internal static BigInteger NextBigInteger(this Random rand, int sizeInBits)
return new BigInteger(b);
}

/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this byte[] value)
{
StringBuilder sb = new();
foreach (byte b in value)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}

/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <param name="reverse">Indicates whether it should be converted in the reversed byte order.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this byte[] value, bool reverse = false)
{
StringBuilder sb = new();
for (int i = 0; i < value.Length; i++)
sb.AppendFormat("{0:x2}", value[reverse ? value.Length - i - 1 : i]);
return sb.ToString();
}

/// <summary>
/// Converts a byte array to hex <see cref="string"/>.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <returns>The converted hex <see cref="string"/>.</returns>
public static string ToHexString(this ReadOnlySpan<byte> value)
{
StringBuilder sb = new();
foreach (byte b in value)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}

/// <summary>
/// Converts a <see cref="DateTime"/> to timestamp.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Neo/SmartContract/ContractParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Json;
using System;
using System.Collections.Generic;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using System;
using System.Globalization;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using System;
using System.Globalization;
Expand Down
1 change: 1 addition & 0 deletions src/Plugins/DBFTPlugin/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Akka.Actor;
using Neo.Extensions;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P;
Expand Down
5 changes: 2 additions & 3 deletions src/Plugins/MPTTrie/Cryptography/MPTTrie/Trie.Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using System;
using System.Collections.Generic;
using static Neo.Helper;

namespace Neo.Cryptography.MPTTrie
{
Expand Down Expand Up @@ -57,7 +56,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
if (node.Next.Type == NodeType.ExtensionNode)
{
if (!full) cache.DeleteNode(node.Next.Hash);
node.Key = Concat(node.Key.Span, node.Next.Key.Span);
node.Key = new([.. node.Key.Span, .. node.Next.Key.Span]);
node.Next = node.Next.Next;
}
node.SetDirty();
Expand Down Expand Up @@ -107,7 +106,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
if (lastChild.Type == NodeType.ExtensionNode)
{
if (!full) cache.DeleteNode(lastChild.Hash);
lastChild.Key = Concat(childrenIndexes.ToArray(), lastChild.Key.Span);
lastChild.Key = new([.. childrenIndexes.ToArray(), .. lastChild.Key.Span]);
lastChild.SetDirty();
cache.PutNode(lastChild);
node = lastChild;
Expand Down
Loading