|
13 | 13 | using Neo.VM;
|
14 | 14 | using Neo.VM.Types;
|
15 | 15 | using System;
|
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Text.RegularExpressions; |
16 | 19 |
|
17 | 20 | namespace Neo.SmartContract.Manifest
|
18 | 21 | {
|
19 | 22 | #nullable enable
|
20 | 23 |
|
21 | 24 | public class ExtendedType : IInteroperable, IEquatable<ExtendedType>
|
22 | 25 | {
|
| 26 | + private static readonly Regex NamedTypePattern = new("^[A-Za-z][A-Za-z0-9.]{0,63}$", RegexOptions.Compiled); |
| 27 | + |
| 28 | + private static readonly HashSet<ContractParameterType> LengthAllowedTypes = new() |
| 29 | + { |
| 30 | + ContractParameterType.Integer, |
| 31 | + ContractParameterType.ByteArray, |
| 32 | + ContractParameterType.String, |
| 33 | + ContractParameterType.Array |
| 34 | + }; |
| 35 | + |
| 36 | + private static readonly HashSet<ContractParameterType> ForbidNullAllowedTypes = new() |
| 37 | + { |
| 38 | + ContractParameterType.Hash160, |
| 39 | + ContractParameterType.Hash256, |
| 40 | + ContractParameterType.ByteArray, |
| 41 | + ContractParameterType.String, |
| 42 | + ContractParameterType.Array, |
| 43 | + ContractParameterType.Map, |
| 44 | + ContractParameterType.InteropInterface |
| 45 | + }; |
| 46 | + |
| 47 | + private static readonly HashSet<ContractParameterType> MapKeyAllowedTypes = new() |
| 48 | + { |
| 49 | + ContractParameterType.Signature, |
| 50 | + ContractParameterType.Boolean, |
| 51 | + ContractParameterType.Integer, |
| 52 | + ContractParameterType.Hash160, |
| 53 | + ContractParameterType.Hash256, |
| 54 | + ContractParameterType.ByteArray, |
| 55 | + ContractParameterType.PublicKey, |
| 56 | + ContractParameterType.String |
| 57 | + }; |
| 58 | + |
| 59 | + private static FormatException Nep25Error(string message) => new($"Invalid NEP-25 extended type: {message}"); |
| 60 | + |
| 61 | + internal static bool IsValidNamedTypeIdentifier(string name) |
| 62 | + { |
| 63 | + return !string.IsNullOrEmpty(name) && NamedTypePattern.IsMatch(name); |
| 64 | + } |
| 65 | + |
| 66 | + internal static void EnsureValidNamedTypeIdentifier(string name) |
| 67 | + { |
| 68 | + if (!IsValidNamedTypeIdentifier(name)) |
| 69 | + throw Nep25Error($"Named type '{name}' must start with a letter, contain only alphanumeric characters or dots, and be at most 64 characters long."); |
| 70 | + } |
| 71 | + |
23 | 72 | /// <summary>
|
24 | 73 | /// The type of the parameter. It can be any value of <see cref="ContractParameterType"/> except <see cref="ContractParameterType.Void"/>.
|
25 | 74 | /// </summary>
|
@@ -214,6 +263,7 @@ public static ExtendedType FromJson(JObject json)
|
214 | 263 | NamedType = json["namedtype"]?.GetString(),
|
215 | 264 | };
|
216 | 265 | if (!Enum.IsDefined(typeof(ContractParameterType), type.Type)) throw new FormatException();
|
| 266 | + if (type.Type == ContractParameterType.Void) throw Nep25Error("Void type is not allowed."); |
217 | 267 | if (json["length"] != null)
|
218 | 268 | {
|
219 | 269 | type.Length = json["length"]!.GetInt32();
|
@@ -329,7 +379,120 @@ public bool Equals(ExtendedType? other)
|
329 | 379 |
|
330 | 380 | return true;
|
331 | 381 | }
|
| 382 | + |
| 383 | + internal void ValidateForParameterOrReturn(ContractParameterType expectedType, ISet<string>? knownNamedTypes) |
| 384 | + { |
| 385 | + ValidateCore(expectedType, allowFields: false, knownNamedTypes, allowNamedTypeReference: true); |
| 386 | + } |
| 387 | + |
| 388 | + internal void ValidateForNamedTypeDefinition(ISet<string>? knownNamedTypes) |
| 389 | + { |
| 390 | + ValidateCore(expectedType: null, allowFields: true, knownNamedTypes, allowNamedTypeReference: true); |
| 391 | + } |
| 392 | + |
| 393 | + private void ValidateCore(ContractParameterType? expectedType, bool allowFields, ISet<string>? knownNamedTypes, bool allowNamedTypeReference) |
| 394 | + { |
| 395 | + if (expectedType.HasValue && Type != expectedType.Value) |
| 396 | + throw Nep25Error($"Type mismatch. Expected '{expectedType.Value}', got '{Type}'."); |
| 397 | + |
| 398 | + if (!Enum.IsDefined(typeof(ContractParameterType), Type) || Type == ContractParameterType.Void) |
| 399 | + throw Nep25Error($"Unsupported type '{Type}'."); |
| 400 | + |
| 401 | + if (Length.HasValue && !LengthAllowedTypes.Contains(Type)) |
| 402 | + throw Nep25Error($"length cannot be specified for type '{Type}'."); |
| 403 | + |
| 404 | + if (ForbidNull.HasValue && !ForbidNullAllowedTypes.Contains(Type)) |
| 405 | + throw Nep25Error($"forbidnull cannot be specified for type '{Type}'."); |
| 406 | + |
| 407 | + if (Interface.HasValue && Type != ContractParameterType.InteropInterface) |
| 408 | + throw Nep25Error($"interface can only be used with InteropInterface type."); |
| 409 | + |
| 410 | + if (Type == ContractParameterType.InteropInterface && !Interface.HasValue) |
| 411 | + throw Nep25Error("interface is required for InteropInterface type."); |
| 412 | + |
| 413 | + if (Key.HasValue && Type != ContractParameterType.Map) |
| 414 | + throw Nep25Error($"key cannot be used with type '{Type}'."); |
| 415 | + |
| 416 | + if (Key.HasValue && !MapKeyAllowedTypes.Contains(Key.Value)) |
| 417 | + throw Nep25Error($"key '{Key.Value}' is not allowed for map definitions."); |
| 418 | + |
| 419 | + if (Type == ContractParameterType.Map && !Key.HasValue) |
| 420 | + throw Nep25Error("key is required for Map type."); |
| 421 | + |
| 422 | + if (NamedType != null) |
| 423 | + { |
| 424 | + if (!allowNamedTypeReference) |
| 425 | + throw Nep25Error("namedtype is not allowed in this context."); |
| 426 | + |
| 427 | + if (Type != ContractParameterType.Array) |
| 428 | + throw Nep25Error("namedtype can only be used with Array type."); |
| 429 | + |
| 430 | + EnsureValidNamedTypeIdentifier(NamedType); |
| 431 | + |
| 432 | + if (Length.HasValue || ForbidNull.HasValue || Interface.HasValue || Key.HasValue || Value is not null || (Fields is not null && Fields.Length > 0)) |
| 433 | + throw Nep25Error("namedtype cannot be combined with other modifiers."); |
| 434 | + |
| 435 | + if (knownNamedTypes != null && !knownNamedTypes.Contains(NamedType)) |
| 436 | + throw Nep25Error($"namedtype '{NamedType}' is not defined in the manifest."); |
| 437 | + } |
| 438 | + |
| 439 | + if (Value is not null) |
| 440 | + { |
| 441 | + if (Type != ContractParameterType.Array && Type != ContractParameterType.InteropInterface && Type != ContractParameterType.Map) |
| 442 | + throw Nep25Error("value can only be specified for Array, Map or InteropInterface types."); |
| 443 | + |
| 444 | + if (Fields is not null && Fields.Length > 0) |
| 445 | + throw Nep25Error("value and fields cannot be used together."); |
| 446 | + |
| 447 | + if (Type == ContractParameterType.InteropInterface && !Interface.HasValue) |
| 448 | + throw Nep25Error("interface must be provided when value is specified for InteropInterface type."); |
| 449 | + |
| 450 | + if (Type == ContractParameterType.Map && !Key.HasValue) |
| 451 | + throw Nep25Error("key must be provided when value is specified for Map type."); |
| 452 | + |
| 453 | + Value.ValidateCore(expectedType: null, allowFields, knownNamedTypes, allowNamedTypeReference); |
| 454 | + } |
| 455 | + else |
| 456 | + { |
| 457 | + if (Type == ContractParameterType.Map) |
| 458 | + throw Nep25Error("value is required for Map type."); |
| 459 | + |
| 460 | + if (Type == ContractParameterType.InteropInterface) |
| 461 | + throw Nep25Error("value is required for InteropInterface type."); |
| 462 | + |
| 463 | + if (Type == ContractParameterType.Array && NamedType is null && (Fields is null || Fields.Length == 0)) |
| 464 | + throw Nep25Error("value, namedtype or fields must be provided for Array type to describe element type."); |
| 465 | + } |
| 466 | + |
| 467 | + if (Fields is not null && Fields.Length > 0) |
| 468 | + { |
| 469 | + if (!allowFields) |
| 470 | + throw Nep25Error("fields cannot be used in method parameters or return values."); |
| 471 | + |
| 472 | + if (Type != ContractParameterType.Array) |
| 473 | + throw Nep25Error("fields can only be used with Array type."); |
| 474 | + |
| 475 | + if (Value is not null) |
| 476 | + throw Nep25Error("fields and value cannot be used together."); |
| 477 | + |
| 478 | + if (NamedType != null) |
| 479 | + throw Nep25Error("fields cannot be combined with namedtype."); |
| 480 | + |
| 481 | + foreach (var field in Fields) |
| 482 | + { |
| 483 | + field.ExtendedType?.ValidateCore(field.Type, allowFields: true, knownNamedTypes, allowNamedTypeReference); |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + if (!allowFields) |
| 488 | + { |
| 489 | + if (Fields is not null && Fields.Length > 0) |
| 490 | + throw Nep25Error("fields cannot be used in method parameters or return values."); |
| 491 | + |
| 492 | + if (Value?.Fields is { Length: > 0 }) |
| 493 | + throw Nep25Error("fields cannot be used in method parameters or return values."); |
| 494 | + } |
| 495 | + } |
332 | 496 | }
|
333 | 497 | #nullable disable
|
334 | 498 | }
|
335 |
| - |
|
0 commit comments