neo-decompiler 0.11.0

Neo N3 NEF decompiler: parse, disassemble, lift bytecode to high-level pseudocode and C# skeletons, with a CLI, JSON reports, and optional WebAssembly bindings.
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright (C) 2015-2026 The Neo Project.
//
// NativeContract.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.IO;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Neo.SmartContract.Native;

/// <summary>
/// The base class of all native contracts.
/// </summary>
public abstract class NativeContract
{
    private class NativeContractsCache
    {
        public record CacheEntry(Dictionary<int, ContractMethodMetadata> Methods, byte[] Script);

        internal Dictionary<int, CacheEntry> NativeContracts { get; set; } = new();

        public CacheEntry GetAllowedMethods(NativeContract native, ApplicationEngine engine)
        {
            if (NativeContracts.TryGetValue(native.Id, out var value)) return value;

            uint index = engine.PersistingBlock is null ? Ledger.CurrentIndex(engine.SnapshotCache) : engine.PersistingBlock.Index;
            CacheEntry methods = native.GetAllowedMethods(engine.ProtocolSettings.IsHardforkEnabled, index);
            NativeContracts[native.Id] = methods;
            return methods;
        }
    }

    public delegate bool IsHardforkEnabledDelegate(Hardfork hf, uint blockHeight);
    private static readonly List<NativeContract> s_contractsList = [];
    private static readonly Dictionary<UInt160, NativeContract> s_contractsDictionary = new();
    private readonly ImmutableHashSet<Hardfork> _usedHardforks;
    private readonly ReadOnlyCollection<ContractMethodMetadata> _methodDescriptors;
    private readonly ReadOnlyCollection<ContractEventAttribute> _eventsDescriptors;

    #region Named Native Contracts

    /// <summary>
    /// Gets the instance of the <see cref="Native.ContractManagement"/> class.
    /// </summary>
    public static ContractManagement ContractManagement { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="Native.StdLib"/> class.
    /// </summary>
    public static StdLib StdLib { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="Native.CryptoLib"/> class.
    /// </summary>
    public static CryptoLib CryptoLib { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="LedgerContract"/> class.
    /// </summary>
    public static LedgerContract Ledger { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="PolicyContract"/> class.
    /// </summary>
    public static PolicyContract Policy { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="Native.RoleManagement"/> class.
    /// </summary>
    public static RoleManagement RoleManagement { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="OracleContract"/> class.
    /// </summary>
    public static OracleContract Oracle { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="Notary"/> class.
    /// </summary>
    public static Notary Notary { get; } = new();

    /// <summary>
    /// Gets the instance of the <see cref="Treasury"/> class.
    /// </summary>
    public static Treasury Treasury { get; } = new();

    public static TokenManagement TokenManagement { get; } = new();

    public static Governance Governance { get; } = new();

    #endregion

    /// <summary>
    /// Gets all native contracts.
    /// </summary>
    public static IReadOnlyCollection<NativeContract> Contracts { get; } = s_contractsList;

    /// <summary>
    /// The name of the native contract.
    /// </summary>
    public string Name => GetType().Name;

    /// <summary>
    /// Since Hardfork has to start having access to the native contract.
    /// </summary>
    public virtual Hardfork? ActiveIn { get; } = null;

    /// <summary>
    /// The hash of the native contract.
    /// </summary>
    public UInt160 Hash { get; }

    /// <summary>
    /// The id of the native contract.
    /// </summary>
    public int Id { get; }

    /// <summary>
    /// Initializes a new instance of the <see cref="NativeContract"/> class.
    /// </summary>
    protected NativeContract(int id)
    {
        Id = id;
        Hash = Helper.GetContractHash(UInt160.Zero, 0, Name);

        // Reflection to get the methods

        List<ContractMethodMetadata> listMethods = [];
        foreach (var member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
        {
            foreach (var attribute in member.GetCustomAttributes<ContractMethodAttribute>())
            {
                listMethods.Add(new ContractMethodMetadata(member, attribute));
            }
        }
        _methodDescriptors = listMethods.OrderBy(p => p.Name, StringComparer.Ordinal).ThenBy(p => p.Parameters.Length).ToList().AsReadOnly();

        // Reflection to get the events
        _eventsDescriptors = GetType()
            .GetCustomAttributes<ContractEventAttribute>(true)
            .OrderBy(p => p.Order)
            .ToList()
            .AsReadOnly();

        // Calculate the initializations forks
        _usedHardforks =
            _methodDescriptors.Select(u => u.ActiveIn)
                .Concat(_methodDescriptors.Select(u => u.DeprecatedIn))
                .Concat(_eventsDescriptors.Select(u => u.DeprecatedIn))
                .Concat(_eventsDescriptors.Select(u => u.ActiveIn))
                .Concat([ActiveIn])
                .Where(u => u.HasValue)
                .Select(u => u!.Value)
                .OrderBy(u => (byte)u)
                .Cast<Hardfork>().ToImmutableHashSet();
        s_contractsList.Add(this);
        s_contractsDictionary.Add(Hash, this);
    }

    /// <summary>
    /// The allowed methods and his offsets.
    /// </summary>
    /// <param name="hfChecker">Hardfork checker</param>
    /// <param name="blockHeight">Block height. Used to check the hardforks and active methods.</param>
    /// <returns>The <see cref="NativeContractsCache"/>.</returns>
    private NativeContractsCache.CacheEntry GetAllowedMethods(IsHardforkEnabledDelegate hfChecker, uint blockHeight)
    {
        Dictionary<int, ContractMethodMetadata> methods = new();

        // Reflection to get the ContractMethods
        byte[] script;
        using (ScriptBuilder sb = new())
        {
            foreach (ContractMethodMetadata method in _methodDescriptors.Where(u => IsActive(u, hfChecker, blockHeight)))
            {
                method.Descriptor.Offset = sb.Length;
                sb.EmitPush(0); //version
                methods.Add(sb.Length, method);
                sb.EmitSysCall(ApplicationEngine.System_Contract_CallNative);
                sb.Emit(OpCode.RET);
            }
            script = sb.ToArray();
        }

        return new NativeContractsCache.CacheEntry(methods, script);
    }

    /// <summary>
    /// The <see cref="ContractState"/> of the native contract.
    /// </summary>
    /// <param name="settings">The <see cref="ProtocolSettings"/> where the HardForks are configured.</param>
    /// <param name="blockHeight">Block index</param>
    /// <returns>The <see cref="ContractState"/>.</returns>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ContractState GetContractState(ProtocolSettings settings, uint blockHeight) => GetContractState(settings.IsHardforkEnabled, blockHeight);

    internal static bool IsActive(IHardforkActivable u, IsHardforkEnabledDelegate hfChecker, uint blockHeight)
    {
        return  // no hardfork is involved
                u.ActiveIn is null && u.DeprecatedIn is null ||
                // deprecated method hardfork is involved
                u.DeprecatedIn is not null && hfChecker(u.DeprecatedIn.Value, blockHeight) == false ||
                // active method hardfork is involved
                u.ActiveIn is not null && hfChecker(u.ActiveIn.Value, blockHeight);
    }

    /// <summary>
    /// The <see cref="ContractState"/> of the native contract.
    /// </summary>
    /// <param name="hfChecker">Hardfork checker</param>
    /// <param name="blockHeight">Block height. Used to check hardforks and active methods.</param>
    /// <returns>The <see cref="ContractState"/>.</returns>
    public ContractState GetContractState(IsHardforkEnabledDelegate hfChecker, uint blockHeight)
    {
        // Get allowed methods and nef script
        var allowedMethods = GetAllowedMethods(hfChecker, blockHeight);

        // Compose nef file
        var nef = new NefFile()
        {
            Compiler = "neo-core-v3.0",
            Source = string.Empty,
            Tokens = [],
            Script = allowedMethods.Script
        };
        nef.CheckSum = NefFile.ComputeChecksum(nef);

        // Compose manifest
        var manifest = new ContractManifest()
        {
            Name = Name,
            Groups = [],
            SupportedStandards = [],
            Abi = new ContractAbi
            {
                Events = _eventsDescriptors
                    .Where(u => IsActive(u, hfChecker, blockHeight))
                    .Select(p => p.Descriptor).ToArray(),
                Methods = allowedMethods.Methods.Values
                    .Select(p => p.Descriptor).ToArray()
            },
            Permissions = [ContractPermission.DefaultPermission],
            Trusts = WildcardContainer<ContractPermissionDescriptor>.Create(),
            Extra = null
        };

        OnManifestCompose(hfChecker, blockHeight, manifest);

        // Return ContractState
        return new ContractState
        {
            Id = Id,
            Nef = nef,
            Hash = Hash,
            Manifest = manifest
        };
    }

    protected virtual void OnManifestCompose(IsHardforkEnabledDelegate hfChecker, uint blockHeight, ContractManifest manifest) { }

    /// <summary>
    /// It is the initialize block
    /// </summary>
    /// <param name="settings">The <see cref="ProtocolSettings"/> where the HardForks are configured.</param>
    /// <param name="index">Block index</param>
    /// <param name="hardforks">Active hardforks</param>
    /// <returns>True if the native contract must be initialized</returns>
    internal bool IsInitializeBlock(ProtocolSettings settings, uint index, [NotNullWhen(true)] out Hardfork[]? hardforks)
    {
        var hfs = new List<Hardfork>();

        // If is in the hardfork height, add them to return array
        foreach (var hf in _usedHardforks)
        {
            if (!settings.Hardforks.TryGetValue(hf, out var activeIn))
            {
                // If hf is not set in the configuration (with EnsureOmmitedHardforks applied over it), it is treated as disabled.
                continue;
            }

            if (activeIn == index)
            {
                hfs.Add(hf);
            }
        }

        // Return all initialize hardforks
        if (hfs.Count > 0)
        {
            hardforks = hfs.ToArray();
            return true;
        }

        // If is not configured, the Genesis is an initialization block.
        if (index == 0 && ActiveIn is null)
        {
            hardforks = hfs.ToArray();
            return true;
        }

        // Initialized not required
        hardforks = null;
        return false;
    }

    /// <summary>
    /// Is the native contract active
    /// </summary>
    /// <param name="settings">The <see cref="ProtocolSettings"/> where the HardForks are configured.</param>
    /// <param name="blockHeight">Block height</param>
    /// <returns>True if the native contract is active</returns>
    public bool IsActive(ProtocolSettings settings, uint blockHeight)
    {
        if (ActiveIn is null) return true;

        if (!settings.Hardforks.TryGetValue(ActiveIn.Value, out var activeIn))
        {
            // If is not set in the configuration is treated as enabled from the genesis
            activeIn = 0;
        }

        return activeIn <= blockHeight;
    }

    /// <summary>
    /// Checks whether the committee has witnessed the current transaction.
    /// </summary>
    /// <param name="engine">The <see cref="ApplicationEngine"/> that is executing the contract.</param>
    /// <returns><see langword="true"/> if the committee has witnessed the current transaction; otherwise, <see langword="false"/>.</returns>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected static bool CheckCommittee(ApplicationEngine engine)
    {
        var committeeMultiSigAddr = Governance.GetCommitteeAddress(engine.SnapshotCache);
        return engine.CheckWitnessInternal(committeeMultiSigAddr);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected static void AssertCommittee(ApplicationEngine engine)
    {
        if (!CheckCommittee(engine))
            throw new InvalidOperationException("Invalid committee signature. It should be a multisig(len(committee) - (len(committee) - 1) / 2)).");
    }

    protected void Notify(ApplicationEngine engine, string eventName, params object?[] args)
    {
        engine.SendNotification(Hash, eventName, new(engine.ReferenceCounter, args.Select(engine.Convert)));
    }

    #region Storage keys

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey(byte prefix) => new KeyBuilder(Id, prefix);

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey(byte prefix, byte data) => new KeyBuilder(Id, prefix) { data };

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey<T>(byte prefix, T bigEndianKey) where T : unmanaged => new KeyBuilder(Id, prefix) { bigEndianKey };

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey(byte prefix, ReadOnlySpan<byte> content) => new KeyBuilder(Id, prefix) { content };

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey(byte prefix, params IEnumerable<ISerializableSpan> serializables)
    {
        var builder = new KeyBuilder(Id, prefix);
        foreach (var serializable in serializables)
            builder.Add(serializable);
        return builder;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private protected StorageKey CreateStorageKey(byte prefix, UInt160 hash, int bigEndianKey)
        => new KeyBuilder(Id, prefix) { hash, bigEndianKey };

    #endregion

    /// <summary>
    /// Gets the native contract with the specified hash.
    /// </summary>
    /// <param name="hash">The hash of the native contract.</param>
    /// <returns>The native contract with the specified hash.</returns>
    public static NativeContract? GetContract(UInt160 hash)
    {
        s_contractsDictionary.TryGetValue(hash, out var contract);
        return contract;
    }

    internal Dictionary<int, ContractMethodMetadata> GetContractMethods(ApplicationEngine engine)
    {
        var nativeContracts = engine.GetState(() => new NativeContractsCache());
        var currentAllowedMethods = nativeContracts.GetAllowedMethods(this, engine);
        return currentAllowedMethods.Methods;
    }

    internal async void Invoke(ApplicationEngine engine, byte version)
    {
        try
        {
            if (version != 0)
                throw new InvalidOperationException($"The native contract of version {version} is not active.");
            // Get native contracts invocation cache
            var currentAllowedMethods = GetContractMethods(engine);
            // Check if the method is allowed
            var context = engine.CurrentContext!;
            var method = currentAllowedMethods[context.InstructionPointer];
            if (method.ActiveIn is not null && !engine.IsHardforkEnabled(method.ActiveIn.Value))
                throw new InvalidOperationException($"Cannot call this method before hardfork {method.ActiveIn}.");
            if (method.DeprecatedIn is not null && engine.IsHardforkEnabled(method.DeprecatedIn.Value))
                throw new InvalidOperationException($"Cannot call this method after hardfork {method.DeprecatedIn}.");
            var state = context.GetState<ExecutionContextState>();
            if (!state.CallFlags.HasFlag(method.RequiredCallFlags))
                throw new InvalidOperationException($"Cannot call this method with the flag {state.CallFlags}.");
            // Check native-whitelist
            if (!Policy.IsWhitelistFeeContract(engine.SnapshotCache, Hash, method.Descriptor, out var fixedFee))
            {
                // In the unit of datoshi, 1 datoshi = 1e-8 GAS
                engine.AddFee(method.CpuFee * engine.ExecFeeFactor + method.StorageFee * engine.StoragePrice);
            }
            List<object?> parameters = new();
            if (method.NeedApplicationEngine) parameters.Add(engine);
            if (method.NeedSnapshot) parameters.Add(engine.SnapshotCache);
            for (int i = 0; i < method.Parameters.Length; i++)
                parameters.Add(engine.Convert(context.EvaluationStack.Peek(i), method.Parameters[i]));
            object? returnValue = method.Handler.Invoke(this, parameters.ToArray());
            if (returnValue is ContractTask task)
            {
                await task;
                returnValue = task.GetResult();
            }
            for (int i = 0; i < method.Parameters.Length; i++)
            {
                context.EvaluationStack.Pop();
            }
            if (method.Handler.ReturnType != typeof(void) && method.Handler.ReturnType != typeof(ContractTask))
            {
                context.EvaluationStack.Push(engine.Convert(returnValue));
            }
        }
        catch (Exception ex)
        {
            engine.Throw(ex);
        }
    }

    /// <summary>
    /// Determine whether the specified contract is a native contract.
    /// </summary>
    /// <param name="hash">The hash of the contract.</param>
    /// <returns><see langword="true"/> if the contract is native; otherwise, <see langword="false"/>.</returns>
    public static bool IsNative(UInt160 hash)
    {
        return s_contractsDictionary.ContainsKey(hash);
    }

    internal virtual ContractTask InitializeAsync(ApplicationEngine engine, Hardfork? hardFork)
    {
        return ContractTask.CompletedTask;
    }

    internal virtual ContractTask OnPersistAsync(ApplicationEngine engine)
    {
        return ContractTask.CompletedTask;
    }

    internal virtual ContractTask PostPersistAsync(ApplicationEngine engine)
    {
        return ContractTask.CompletedTask;
    }
}