/
Bytes32TypeDecoder.cs
58 lines (46 loc) · 1.72 KB
/
Bytes32TypeDecoder.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
using System;
using System.Text;
namespace Nethereum.ABI.Decoders
{
public class Bytes32TypeDecoder : TypeDecoder
{
private readonly BoolTypeDecoder _boolTypeDecoder;
private readonly IntTypeDecoder _intTypeDecoder;
public Bytes32TypeDecoder()
{
_intTypeDecoder = new IntTypeDecoder();
_boolTypeDecoder = new BoolTypeDecoder();
}
public override object Decode(byte[] encoded, Type type)
{
if (!IsSupportedType(type)) throw new NotSupportedException(type + " is not supported");
if (type == typeof(byte[]) || type == typeof(object))
return encoded;
if (type == typeof(string))
return DecodeString(encoded);
if (_intTypeDecoder.IsSupportedType(type))
return _intTypeDecoder.Decode(encoded, type);
if (_boolTypeDecoder.IsSupportedType(type))
return _boolTypeDecoder.Decode(encoded, type);
throw new NotSupportedException();
}
public override object DecodePacked(byte[] encoded, Type type)
{
//32 bytes already
return Decode(encoded, type);
}
public override Type GetDefaultDecodingType()
{
return typeof(byte[]);
}
public override bool IsSupportedType(Type type)
{
return type == typeof(byte[]) || type == typeof(string) || _intTypeDecoder.IsSupportedType(type)
|| type == typeof(bool) || type == typeof(object);
}
private string DecodeString(byte[] encoded)
{
return Encoding.UTF8.GetString(encoded, 0, encoded.Length).TrimEnd('\0');
}
}
}