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

Implement NEP-25 #3272

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all 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
28 changes: 26 additions & 2 deletions src/Neo/SmartContract/Manifest/ContractMethodDescriptor.cs
Original file line number Diff line number Diff line change
@@ -28,6 +28,11 @@ public class ContractMethodDescriptor : ContractEventDescriptor, IEquatable<Cont
/// </summary>
public ContractParameterType ReturnType { get; set; }

/// <summary>
/// NEP-25 extended return type
/// </summary>
public ExtendedType ExtendedReturnType { get; set; }

/// <summary>
/// The position of the method in the contract script.
/// </summary>
@@ -46,14 +51,28 @@ public override void FromStackItem(StackItem stackItem)
ReturnType = (ContractParameterType)(byte)@struct[2].GetInteger();
Offset = (int)@struct[3].GetInteger();
Safe = @struct[4].GetBoolean();
if (@struct.Count >= 6)
{
ExtendedReturnType = new ExtendedType();
ExtendedReturnType.FromStackItem((VM.Types.Array)@struct[5], 0);
}
else
{
ExtendedReturnType = null;
}
}

public override StackItem ToStackItem(IReferenceCounter referenceCounter)
{
Struct @struct = (Struct)base.ToStackItem(referenceCounter);
var @struct = (Struct)base.ToStackItem(referenceCounter);
@struct.Add((byte)ReturnType);
@struct.Add(Offset);
@struct.Add(Safe);
if (ExtendedReturnType != null)
{
var structExtended = new Struct(referenceCounter);
@struct.Add(ExtendedReturnType.ToStackItem(referenceCounter, structExtended));
}
return @struct;
}

@@ -70,7 +89,8 @@ public override StackItem ToStackItem(IReferenceCounter referenceCounter)
Parameters = ((JArray)json["parameters"]).Select(u => ContractParameterDefinition.FromJson((JObject)u)).ToArray(),
ReturnType = Enum.Parse<ContractParameterType>(json["returntype"].GetString()),
Offset = json["offset"].GetInt32(),
Safe = json["safe"].GetBoolean()
Safe = json["safe"].GetBoolean(),
ExtendedReturnType = json["extendedreturntype"] != null ? ExtendedType.FromJson((JObject)json["extendedreturntype"]) : null
};
if (string.IsNullOrEmpty(descriptor.Name)) throw new FormatException();
_ = descriptor.Parameters.ToDictionary(p => p.Name);
@@ -89,6 +109,10 @@ public override JObject ToJson()
json["returntype"] = ReturnType.ToString();
json["offset"] = Offset;
json["safe"] = Safe;
if (ExtendedReturnType != null)
{
json["extendedreturntype"] = ExtendedReturnType.ToJson();
}
return json;
}

36 changes: 32 additions & 4 deletions src/Neo/SmartContract/Manifest/ContractParameterDefinition.cs
Original file line number Diff line number Diff line change
@@ -32,16 +32,39 @@ public class ContractParameterDefinition : IInteroperable, IEquatable<ContractPa
/// </summary>
public ContractParameterType Type { get; set; }

/// <summary>
/// NEP-25 extended type
/// </summary>
public ExtendedType ExtendedType { get; set; }

void IInteroperable.FromStackItem(StackItem stackItem)
{
Struct @struct = (Struct)stackItem;
var @struct = (Struct)stackItem;
Name = @struct[0].GetString();
Type = (ContractParameterType)(byte)@struct[1].GetInteger();

if (@struct.Count >= 3)
{
ExtendedType = new ExtendedType();
ExtendedType.FromStackItem((VM.Types.Array)@struct[5], 0);
}
else
{
ExtendedType = null;
}
}

public StackItem ToStackItem(IReferenceCounter referenceCounter)
{
return new Struct(referenceCounter) { Name, (byte)Type };
var @struct = new Struct(referenceCounter) { Name, (byte)Type };

if (ExtendedType != null)
{
var structExtended = new Struct(referenceCounter);
@struct.Add(ExtendedType.ToStackItem(referenceCounter, structExtended));
}

return @struct;
}

/// <summary>
@@ -51,10 +74,11 @@ public StackItem ToStackItem(IReferenceCounter referenceCounter)
/// <returns>The converted parameter.</returns>
public static ContractParameterDefinition FromJson(JObject json)
{
ContractParameterDefinition parameter = new()
var parameter = new ContractParameterDefinition()
{
Name = json["name"].GetString(),
Type = Enum.Parse<ContractParameterType>(json["type"].GetString())
Type = Enum.Parse<ContractParameterType>(json["type"].GetString()),
ExtendedType = json["extendedtype"] != null ? ExtendedType.FromJson((JObject)json["extendedtype"]) : null,
};
if (string.IsNullOrEmpty(parameter.Name))
throw new FormatException();
@@ -72,6 +96,10 @@ public JObject ToJson()
var json = new JObject();
json["name"] = Name;
json["type"] = Type.ToString();
if (ExtendedType != null)
{
json["extendedtype"] = ExtendedType.ToJson();
}
return json;
}

94 changes: 94 additions & 0 deletions src/Neo/SmartContract/Manifest/ExtendedType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2015-2025 The Neo Project.
//
// ExtendedType.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.

#nullable enable

using Neo.Json;
using Neo.VM;
using Neo.VM.Types;
using System;

namespace Neo.SmartContract.Manifest
{
public class ExtendedType : IInteroperable
{
/// <summary>
/// The type of the parameter. It can be any value of <see cref="ContractParameterType"/> except <see cref="ContractParameterType.Void"/>.
/// </summary>
public ContractParameterType Type { get; set; }

/// <summary>
/// NamedType is used to refer to one of the types defined in the namedtypes object of Contract,
/// so namedtypes MUST contain a field named name.
/// This field is only used for structures (ordered set of named values of diffent types),
/// if used other fields MUST NOT be set, except for the type which MUST be an Array.
/// Value string MUST start with a letter and can contain alphanumeric characters and dots.
/// It MUST NOT be longer than 64 characters.
/// </summary>
public string? NamedType { get; set; }

void IInteroperable.FromStackItem(StackItem stackItem)
{
FromStackItem((VM.Types.Array)stackItem, 0);
}

internal void FromStackItem(VM.Types.Array @struct, int startIndex)
{
Type = (ContractParameterType)(byte)@struct[startIndex].GetInteger();
if (!Enum.IsDefined(typeof(ContractParameterType), Type)) throw new FormatException();
NamedType = @struct[startIndex + 1].GetString();
}

internal StackItem ToStackItem(IReferenceCounter referenceCounter, Struct @struct)
{
@struct.Add((byte)Type);
@struct.Add(NamedType ?? StackItem.Null);
return @struct;
}

StackItem IInteroperable.ToStackItem(IReferenceCounter referenceCounter)
{
var @struct = new Struct(referenceCounter);
ToStackItem(referenceCounter, @struct);
return @struct;
}

/// <summary>
/// Converts the type from a JSON object.
/// </summary>
/// <param name="json">The method represented by a JSON object.</param>
/// <returns>The extended type.</returns>
public static ExtendedType FromJson(JObject json)
{
ExtendedType type = new()
{
Type = Enum.Parse<ContractParameterType>(json["type"]?.GetString() ?? throw new FormatException()),
NamedType = json["namedtype"]?.GetString(),
};
if (!Enum.IsDefined(typeof(ContractParameterType), type.Type)) throw new FormatException();
return type;
}

/// <summary>
/// Converts the parameter to a JSON object.
/// </summary>
/// <returns>The parameter represented by a JSON object.</returns>
public virtual JObject ToJson()
{
var json = new JObject();
json["type"] = Type.ToString();
json["namedtype"] = NamedType;
return json;
}
}
}

#nullable disable