-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathContractManifest.cs
182 lines (166 loc) · 7.83 KB
/
ContractManifest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2015-2022 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project 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.IO;
using Neo.Json;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Linq;
using Array = Neo.VM.Types.Array;
namespace Neo.SmartContract.Manifest
{
/// <summary>
/// Represents the manifest of a smart contract.
/// When a smart contract is deployed, it must explicitly declare the features and permissions it will use.
/// When it is running, it will be limited by its declared list of features and permissions, and cannot make any behavior beyond the scope of the list.
/// </summary>
/// <remarks>For more details, see NEP-15.</remarks>
public class ContractManifest : IInteroperable
{
/// <summary>
/// The maximum length of a manifest.
/// </summary>
public const int MaxLength = ushort.MaxValue;
/// <summary>
/// The name of the contract.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The groups of the contract.
/// </summary>
public ContractGroup[] Groups { get; set; }
/// <summary>
/// Indicates which standards the contract supports. It can be a list of NEPs.
/// </summary>
public string[] SupportedStandards { get; set; }
/// <summary>
/// The ABI of the contract.
/// </summary>
public ContractAbi Abi { get; set; }
/// <summary>
/// The permissions of the contract.
/// </summary>
public ContractPermission[] Permissions { get; set; }
/// <summary>
/// The trusted contracts and groups of the contract.
/// If a contract is trusted, the user interface will not give any warnings when called by the contract.
/// </summary>
public WildcardContainer<ContractPermissionDescriptor> Trusts { get; set; }
/// <summary>
/// Custom user data.
/// </summary>
public JObject Extra { get; set; }
void IInteroperable.FromStackItem(StackItem stackItem)
{
Struct @struct = (Struct)stackItem;
Name = @struct[0].GetString();
Groups = ((Array)@struct[1]).Select(p => p.ToInteroperable<ContractGroup>()).ToArray();
if (((Map)@struct[2]).Count != 0)
throw new ArgumentException(null, nameof(stackItem));
SupportedStandards = ((Array)@struct[3]).Select(p => p.GetString()).ToArray();
Abi = @struct[4].ToInteroperable<ContractAbi>();
Permissions = ((Array)@struct[5]).Select(p => p.ToInteroperable<ContractPermission>()).ToArray();
Trusts = @struct[6] switch
{
Null _ => WildcardContainer<ContractPermissionDescriptor>.CreateWildcard(),
// Array array when array.Any(p => ((ByteString)p).Size == 0) => WildcardContainer<ContractPermissionDescriptor>.CreateWildcard(),
Array array => WildcardContainer<ContractPermissionDescriptor>.Create(array.Select(p => new ContractPermissionDescriptor(p.GetSpan())).ToArray()),
_ => throw new ArgumentException(null, nameof(stackItem))
};
Extra = (JObject)JToken.Parse(@struct[7].GetSpan());
}
public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Struct(referenceCounter)
{
Name,
new Array(referenceCounter, Groups.Select(p => p.ToStackItem(referenceCounter))),
new Map(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 => p.ToArray()?? StackItem.Null)),
Extra is null ? "null" : Extra.ToByteArray(false)
};
}
/// <summary>
/// Converts the manifest from a JSON object.
/// </summary>
/// <param name="json">The manifest represented by a JSON object.</param>
/// <returns>The converted manifest.</returns>
public static ContractManifest FromJson(JObject json)
{
ContractManifest manifest = new()
{
Name = json["name"].GetString(),
Groups = ((JArray)json["groups"]).Select(u => ContractGroup.FromJson((JObject)u)).ToArray(),
SupportedStandards = ((JArray)json["supportedstandards"]).Select(u => u.GetString()).ToArray(),
Abi = ContractAbi.FromJson((JObject)json["abi"]),
Permissions = ((JArray)json["permissions"]).Select(u => ContractPermission.FromJson((JObject)u)).ToArray(),
Trusts = WildcardContainer<ContractPermissionDescriptor>.FromJson(json["trusts"], u => ContractPermissionDescriptor.FromJson((JString)u)),
Extra = (JObject)json["extra"]
};
if (string.IsNullOrEmpty(manifest.Name))
throw new FormatException();
_ = manifest.Groups.ToDictionary(p => p.PubKey);
if (json["features"] is not JObject features || features.Count != 0)
throw new FormatException();
if (manifest.SupportedStandards.Any(p => string.IsNullOrEmpty(p)))
throw new FormatException();
_ = manifest.SupportedStandards.ToDictionary(p => p);
_ = manifest.Permissions.ToDictionary(p => p.Contract);
_ = manifest.Trusts.ToDictionary(p => p);
return manifest;
}
/// <summary>
/// Parse the manifest from a byte array containing JSON data.
/// </summary>
/// <param name="json">The byte array containing JSON data.</param>
/// <returns>The parsed manifest.</returns>
public static ContractManifest Parse(ReadOnlySpan<byte> json)
{
if (json.Length > MaxLength) throw new ArgumentException(null, nameof(json));
return FromJson((JObject)JToken.Parse(json));
}
/// <summary>
/// Parse the manifest from a JSON <see cref="string"/>.
/// </summary>
/// <param name="json">The JSON <see cref="string"/>.</param>
/// <returns>The parsed manifest.</returns>
public static ContractManifest Parse(string json) => Parse(Utility.StrictUTF8.GetBytes(json));
/// <summary>
/// Converts the manifest to a JSON object.
/// </summary>
/// <returns>The manifest represented by a JSON object.</returns>
public JObject ToJson()
{
return new JObject
{
["name"] = Name,
["groups"] = Groups.Select(u => u.ToJson()).ToArray(),
["features"] = new JObject(),
["supportedstandards"] = SupportedStandards.Select(u => new JString(u)).ToArray(),
["abi"] = Abi.ToJson(),
["permissions"] = Permissions.Select(p => p.ToJson()).ToArray(),
["trusts"] = Trusts.ToJson(p => p.ToJson()),
["extra"] = Extra
};
}
/// <summary>
/// Determines whether the manifest is valid.
/// </summary>
/// <param name="hash">The hash of the contract.</param>
/// <returns><see langword="true"/> if the manifest is valid; otherwise, <see langword="false"/>.</returns>
public bool IsValid(UInt160 hash)
{
return Groups.All(u => u.IsValid(hash));
}
}
}