diff --git a/src/Neo/SmartContract/Manifest/ContractManifest.cs b/src/Neo/SmartContract/Manifest/ContractManifest.cs index 7a067409f2..e68c764800 100644 --- a/src/Neo/SmartContract/Manifest/ContractManifest.cs +++ b/src/Neo/SmartContract/Manifest/ContractManifest.cs @@ -79,7 +79,8 @@ void IInteroperable.FromStackItem(StackItem stackItem) Permissions = ((Array)@struct[5]).Select(p => p.ToInteroperable()).ToArray(); Trusts = @struct[6] switch { - Null => WildcardContainer.CreateWildcard(), + Null _ => WildcardContainer.CreateWildcard(), + // Array array when array.Any(p => ((ByteString)p).Size == 0) => WildcardContainer.CreateWildcard(), Array array => WildcardContainer.Create(array.Select(p => new ContractPermissionDescriptor(p.GetSpan())).ToArray()), _ => throw new ArgumentException(null, nameof(stackItem)) }; @@ -96,7 +97,7 @@ public StackItem ToStackItem(ReferenceCounter referenceCounter) new Array(referenceCounter, SupportedStandards.Select(p => (StackItem)p)), Abi.ToStackItem(referenceCounter), new Array(referenceCounter, Permissions.Select(p => p.ToStackItem(referenceCounter))), - Trusts.IsWildcard ? StackItem.Null : new Array(referenceCounter, Trusts.Select(p => (StackItem)p.ToArray())), + Trusts.IsWildcard ? StackItem.Null : new Array(referenceCounter, Trusts.Select(p => p.ToArray()?? StackItem.Null)), Extra is null ? "null" : Extra.ToByteArray(false) }; } diff --git a/tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs b/tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs index 7d895a6e3b..986716e694 100644 --- a/tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs +++ b/tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs @@ -1,8 +1,11 @@ +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Cryptography.ECC; +using Neo.IO; using Neo.Json; using Neo.SmartContract; using Neo.SmartContract.Manifest; +using Neo.VM; namespace Neo.UnitTests.SmartContract.Manifest { @@ -53,12 +56,12 @@ public void ParseFromJson_SafeMethods() [TestMethod] public void ParseFromJson_Trust() { - var json = @"{""name"":""testManifest"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""extra"":null}"; + ReferenceCounter referenceCounter = new ReferenceCounter(); + var json = @"{""name"":""testManifest"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001"",""*""],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToJson().ToString(), json); - var check = TestUtils.CreateDefaultManifest(); - check.Trusts = WildcardContainer.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001"))); + check.Trusts = WildcardContainer.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")), ContractPermissionDescriptor.CreateWildcard()); Assert.AreEqual(manifest.ToJson().ToString(), check.ToJson().ToString()); } @@ -103,6 +106,21 @@ public void TestDeserializeAndSerialize() Assert.AreEqual(expected.ToString(), clone.ToString()); } + [TestMethod] + public void TestSerializeTrusts() + { + var check = TestUtils.CreateDefaultManifest(); + check.Trusts = WildcardContainer.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")), ContractPermissionDescriptor.CreateWildcard()); + var si = check.ToStackItem(null); + + var actualTrusts = ((VM.Types.Array)si)[6]; + + Assert.AreEqual(((VM.Types.Array)actualTrusts).Count, 2); + Assert.AreEqual(((VM.Types.Array)actualTrusts)[0], new VM.Types.ByteString(UInt160.Parse("0x0000000000000000000000000000000000000001").ToArray())); + // Wildcard trust should be represented as Null stackitem (not as zero-length ByteString): + Assert.AreEqual(((VM.Types.Array)actualTrusts)[1], VM.Types.StackItem.Null); + } + [TestMethod] public void TestGenerator() {