-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Move]
Part-7 Classes into Different Library - Neo.Extensions
#3446
Merged
NGDAdmin
merged 39 commits into
neo-project:master
from
cschuchardt88:rebuild/the-split-7
Nov 29, 2024
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
bf93412
Part-1 `Neo.IO` - move
cschuchardt88 4d08154
Part-2
cschuchardt88 597c9ca
Merge branch 'master' into rebuild/the-split-2
cschuchardt88 9881579
Added `BigInteger` to `Neo.Extensions`
cschuchardt88 8779151
Found more `BigInteger`
cschuchardt88 ed34707
Added `ByteArray` to `Neo.Extensions`
cschuchardt88 5b12304
Added `DateTime` Extensions to `Neo.Extensions`
cschuchardt88 0f6627a
Added `HashSetExtensions`, `HashSetExtensions2`, `IpAddressExtensions…
cschuchardt88 95f8a2d
Merge branch 'master' into rebuild/the-split-4
cschuchardt88 721ce58
Added `ICollection`, `Memory`, `String`, `Unsafe` extensions
cschuchardt88 129a32f
Adding `using`
cschuchardt88 397cc1f
dotnet format
cschuchardt88 a127386
Added more Extensions
cschuchardt88 9a84436
Move some methods
cschuchardt88 dbbf5b3
Merge branch 'master' into rebuild/the-split-2
shargon b64435b
Added Tests
cschuchardt88 583e610
Merge branch 'rebuild/the-split-2' of https://github.com/cschuchardt8…
cschuchardt88 6a50385
Merge branch 'rebuild/the-split-2' into rebuild/the-split-3
cschuchardt88 f381b89
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 6261f70
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 4a0fb7a
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 58f7fee
Added `tests` from `Part-2`
cschuchardt88 7f74300
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 c790166
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 26d0515
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 e644bd3
Added `tests` for `PART-4`
cschuchardt88 79a3578
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 00ce5ae
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 6fa0fda
Added `tests` for `PART-5`
cschuchardt88 708ce84
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 bb9992e
Merge Master
cschuchardt88 ff3c158
Added the `Part` 7
cschuchardt88 d761585
Merge Master into `the-split-7`
cschuchardt88 9ecd77f
revert `OrCondition.cs`
cschuchardt88 15c092d
revert `OrCondition.cs`
cschuchardt88 b333474
Merge branch 'master' into rebuild/the-split-7
shargon 0b24c62
Update tests/Neo.Extensions.Tests/UT_BigIntegerExtensions.cs
shargon b241941
Merge branch 'master' into rebuild/the-split-7
shargon 8e1ff35
Merge branch 'master' into rebuild/the-split-7
Jim8y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// ScriptBuilderExtensions.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 Neo.Cryptography.ECC; | ||
using Neo.IO; | ||
using Neo.SmartContract; | ||
using Neo.VM; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
|
||
namespace Neo.Extensions | ||
{ | ||
public static class ScriptBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Emits the opcodes for creating an array. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements of the array.</typeparam> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="list">The elements of the array.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder CreateArray<T>(this ScriptBuilder builder, IReadOnlyList<T> list = null) | ||
{ | ||
if (list is null || list.Count == 0) | ||
return builder.Emit(OpCode.NEWARRAY0); | ||
for (var i = list.Count - 1; i >= 0; i--) | ||
builder.EmitPush(list[i]); | ||
builder.EmitPush(list.Count); | ||
return builder.Emit(OpCode.PACK); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for creating a map. | ||
/// </summary> | ||
/// <typeparam name="TKey">The type of the key of the map.</typeparam> | ||
/// <typeparam name="TValue">The type of the value of the map.</typeparam> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="map">The key/value pairs of the map.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IEnumerable<KeyValuePair<TKey, TValue>> map) | ||
where TKey : notnull | ||
where TValue : notnull | ||
{ | ||
var count = map.Count(); | ||
|
||
if (count == 0) | ||
return builder.Emit(OpCode.NEWMAP); | ||
|
||
foreach (var (key, value) in map.Reverse()) | ||
{ | ||
builder.EmitPush(value); | ||
builder.EmitPush(key); | ||
} | ||
builder.EmitPush(count); | ||
return builder.Emit(OpCode.PACKMAP); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for creating a map. | ||
/// </summary> | ||
/// <typeparam name="TKey">The type of the key of the map.</typeparam> | ||
/// <typeparam name="TValue">The type of the value of the map.</typeparam> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="map">The key/value pairs of the map.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IReadOnlyDictionary<TKey, TValue> map) | ||
where TKey : notnull | ||
where TValue : notnull | ||
{ | ||
if (map.Count == 0) | ||
return builder.Emit(OpCode.NEWMAP); | ||
|
||
foreach (var (key, value) in map.Reverse()) | ||
{ | ||
builder.EmitPush(value); | ||
builder.EmitPush(key); | ||
} | ||
builder.EmitPush(map.Count); | ||
return builder.Emit(OpCode.PACKMAP); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for creating a struct. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the property.</typeparam> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="array">The list of properties.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder CreateStruct<T>(this ScriptBuilder builder, IReadOnlyList<T> array) | ||
where T : notnull | ||
{ | ||
if (array.Count == 0) | ||
return builder.Emit(OpCode.NEWSTRUCT0); | ||
for (var i = array.Count - 1; i >= 0; i--) | ||
builder.EmitPush(array[i]); | ||
builder.EmitPush(array.Count); | ||
return builder.Emit(OpCode.PACKSTRUCT); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the specified opcodes. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="ops">The opcodes to emit.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder Emit(this ScriptBuilder builder, params OpCode[] ops) | ||
{ | ||
foreach (var op in ops) | ||
builder.Emit(op); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for calling a contract dynamically. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="scriptHash">The hash of the contract to be called.</param> | ||
/// <param name="method">The method to be called in the contract.</param> | ||
/// <param name="args">The arguments for calling the contract.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitDynamicCall(this ScriptBuilder builder, UInt160 scriptHash, string method, params object[] args) | ||
{ | ||
return EmitDynamicCall(builder, scriptHash, method, CallFlags.All, args); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for calling a contract dynamically. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="scriptHash">The hash of the contract to be called.</param> | ||
/// <param name="method">The method to be called in the contract.</param> | ||
/// <param name="flags">The <see cref="CallFlags"/> for calling the contract.</param> | ||
/// <param name="args">The arguments for calling the contract.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitDynamicCall(this ScriptBuilder builder, UInt160 scriptHash, string method, CallFlags flags, params object[] args) | ||
{ | ||
builder.CreateArray(args); | ||
builder.EmitPush(flags); | ||
builder.EmitPush(method); | ||
builder.EmitPush(scriptHash); | ||
builder.EmitSysCall(ApplicationEngine.System_Contract_Call); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for pushing the specified data onto the stack. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="data">The data to be pushed.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitPush(this ScriptBuilder builder, ISerializable data) | ||
{ | ||
return builder.EmitPush(data.ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for pushing the specified data onto the stack. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="parameter">The data to be pushed.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitPush(this ScriptBuilder builder, ContractParameter parameter) | ||
{ | ||
if (parameter.Value is null) | ||
builder.Emit(OpCode.PUSHNULL); | ||
else | ||
switch (parameter.Type) | ||
{ | ||
case ContractParameterType.Signature: | ||
case ContractParameterType.ByteArray: | ||
builder.EmitPush((byte[])parameter.Value); | ||
break; | ||
case ContractParameterType.Boolean: | ||
builder.EmitPush((bool)parameter.Value); | ||
break; | ||
case ContractParameterType.Integer: | ||
if (parameter.Value is BigInteger bi) | ||
builder.EmitPush(bi); | ||
else | ||
builder.EmitPush((BigInteger)typeof(BigInteger).GetConstructor([parameter.Value.GetType()]).Invoke([parameter.Value])); | ||
break; | ||
case ContractParameterType.Hash160: | ||
builder.EmitPush((UInt160)parameter.Value); | ||
break; | ||
case ContractParameterType.Hash256: | ||
builder.EmitPush((UInt256)parameter.Value); | ||
break; | ||
case ContractParameterType.PublicKey: | ||
builder.EmitPush((ECPoint)parameter.Value); | ||
break; | ||
case ContractParameterType.String: | ||
builder.EmitPush((string)parameter.Value); | ||
break; | ||
case ContractParameterType.Array: | ||
{ | ||
var parameters = (IList<ContractParameter>)parameter.Value; | ||
for (var i = parameters.Count - 1; i >= 0; i--) | ||
builder.EmitPush(parameters[i]); | ||
builder.EmitPush(parameters.Count); | ||
builder.Emit(OpCode.PACK); | ||
} | ||
break; | ||
case ContractParameterType.Map: | ||
{ | ||
var pairs = (IList<KeyValuePair<ContractParameter, ContractParameter>>)parameter.Value; | ||
builder.CreateMap(pairs); | ||
} | ||
break; | ||
default: | ||
throw new ArgumentException(null, nameof(parameter)); | ||
} | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for pushing the specified data onto the stack. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="obj">The data to be pushed.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitPush(this ScriptBuilder builder, object obj) | ||
{ | ||
switch (obj) | ||
{ | ||
case bool data: | ||
builder.EmitPush(data); | ||
break; | ||
case byte[] data: | ||
builder.EmitPush(data); | ||
break; | ||
case string data: | ||
builder.EmitPush(data); | ||
break; | ||
case BigInteger data: | ||
builder.EmitPush(data); | ||
break; | ||
case ISerializable data: | ||
builder.EmitPush(data); | ||
break; | ||
case sbyte data: | ||
builder.EmitPush(data); | ||
break; | ||
case byte data: | ||
builder.EmitPush(data); | ||
break; | ||
case short data: | ||
builder.EmitPush(data); | ||
break; | ||
case char data: | ||
builder.EmitPush(data); | ||
break; | ||
case ushort data: | ||
builder.EmitPush(data); | ||
break; | ||
case int data: | ||
builder.EmitPush(data); | ||
break; | ||
case uint data: | ||
builder.EmitPush(data); | ||
break; | ||
case long data: | ||
builder.EmitPush(data); | ||
break; | ||
case ulong data: | ||
builder.EmitPush(data); | ||
break; | ||
case Enum data: | ||
builder.EmitPush(BigInteger.Parse(data.ToString("d"))); | ||
break; | ||
case ContractParameter data: | ||
builder.EmitPush(data); | ||
break; | ||
case null: | ||
builder.Emit(OpCode.PUSHNULL); | ||
break; | ||
default: | ||
throw new ArgumentException(null, nameof(obj)); | ||
} | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Emits the opcodes for invoking an interoperable service. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param> | ||
/// <param name="method">The hash of the interoperable service.</param> | ||
/// <param name="args">The arguments for calling the interoperable service.</param> | ||
/// <returns>The same instance as <paramref name="builder"/>.</returns> | ||
public static ScriptBuilder EmitSysCall(this ScriptBuilder builder, uint method, params object[] args) | ||
{ | ||
for (var i = args.Length - 1; i >= 0; i--) | ||
EmitPush(builder, args[i]); | ||
return builder.EmitSysCall(method); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this ugly old code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rather leave for another pr.