-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathNefFile.cs
150 lines (135 loc) · 6.56 KB
/
NefFile.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
// 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.Cryptography;
using Neo.IO;
using Neo.Json;
using System;
using System.Buffers.Binary;
using System.IO;
using System.Linq;
namespace Neo.SmartContract
{
/*
┌───────────────────────────────────────────────────────────────────────┐
│ NEO Executable Format 3 (NEF3) │
├──────────┬───────────────┬────────────────────────────────────────────┤
│ Field │ Type │ Comment │
├──────────┼───────────────┼────────────────────────────────────────────┤
│ Magic │ uint32 │ Magic header │
│ Compiler │ byte[64] │ Compiler name and version │
├──────────┼───────────────┼────────────────────────────────────────────┤
│ Source │ byte[] │ The url of the source files │
│ Reserve │ byte │ Reserved for future extensions. Must be 0. │
│ Tokens │ MethodToken[] │ Method tokens. │
│ Reserve │ byte[2] │ Reserved for future extensions. Must be 0. │
│ Script │ byte[] │ Var bytes for the payload │
├──────────┼───────────────┼────────────────────────────────────────────┤
│ Checksum │ uint32 │ First four bytes of double SHA256 hash │
└──────────┴───────────────┴────────────────────────────────────────────┘
*/
/// <summary>
/// Represents the structure of NEO Executable Format.
/// </summary>
public class NefFile : ISerializable
{
/// <summary>
/// NEO Executable Format 3 (NEF3)
/// </summary>
private const uint Magic = 0x3346454E;
/// <summary>
/// The name and version of the compiler that generated this nef file.
/// </summary>
public string Compiler { get; set; }
/// <summary>
/// The url of the source files.
/// </summary>
public string Source { get; set; }
/// <summary>
/// The methods that to be called statically.
/// </summary>
public MethodToken[] Tokens { get; set; }
/// <summary>
/// The script of the contract.
/// </summary>
public ReadOnlyMemory<byte> Script { get; set; }
/// <summary>
/// The checksum of the nef file.
/// </summary>
public uint CheckSum { get; set; }
/// <summary>
/// The maximum length of the script.
/// </summary>
public const int MaxScriptLength = 512 * 1024;
private const int HeaderSize =
sizeof(uint) + // Magic
64; // Compiler
public int Size =>
HeaderSize + // Header
Source.GetVarSize() + // Source
1 + // Reserve
Tokens.GetVarSize() + // Tokens
2 + // Reserve
Script.GetVarSize() + // Script
sizeof(uint); // Checksum
public void Serialize(BinaryWriter writer)
{
SerializeHeader(writer);
writer.WriteVarString(Source);
writer.Write((byte)0);
writer.Write(Tokens);
writer.Write((short)0);
writer.WriteVarBytes(Script.Span);
writer.Write(CheckSum);
}
private void SerializeHeader(BinaryWriter writer)
{
writer.Write(Magic);
writer.WriteFixedString(Compiler, 64);
}
public void Deserialize(ref MemoryReader reader)
{
if (reader.ReadUInt32() != Magic) throw new FormatException("Wrong magic");
Compiler = reader.ReadFixedString(64);
Source = reader.ReadVarString(256);
if (reader.ReadByte() != 0) throw new FormatException("Reserved bytes must be 0");
Tokens = reader.ReadSerializableArray<MethodToken>(128);
if (reader.ReadUInt16() != 0) throw new FormatException("Reserved bytes must be 0");
Script = reader.ReadVarMemory(MaxScriptLength);
if (Script.Length == 0) throw new ArgumentException($"Script can't be empty");
CheckSum = reader.ReadUInt32();
if (CheckSum != ComputeChecksum(this)) throw new FormatException("CRC verification fail");
}
/// <summary>
/// Computes the checksum for the specified nef file.
/// </summary>
/// <param name="file">The specified nef file.</param>
/// <returns>The checksum of the nef file.</returns>
public static uint ComputeChecksum(NefFile file)
{
return BinaryPrimitives.ReadUInt32LittleEndian(Crypto.Hash256(file.ToArray().AsSpan(..^sizeof(uint))));
}
/// <summary>
/// Converts the nef file to a JSON object.
/// </summary>
/// <returns>The nef file represented by a JSON object.</returns>
public JObject ToJson()
{
return new JObject
{
["magic"] = Magic,
["compiler"] = Compiler,
["source"] = Source,
["tokens"] = new JArray(Tokens.Select(p => p.ToJson())),
["script"] = Convert.ToBase64String(Script.Span),
["checksum"] = CheckSum
};
}
}
}