forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockHashOrIndex.cs
61 lines (52 loc) · 1.65 KB
/
BlockHashOrIndex.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
// Copyright (C) 2015-2024 The Neo Project.
//
// BlockHashOrIndex.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 System.Diagnostics.CodeAnalysis;
namespace Neo.Plugins.RpcServer.Model;
public class BlockHashOrIndex
{
private readonly object _value;
public BlockHashOrIndex(uint index)
{
_value = index;
}
public BlockHashOrIndex(UInt256 hash)
{
_value = hash;
}
public bool IsIndex => _value is uint;
public static bool TryParse(string value, [NotNullWhen(true)] out BlockHashOrIndex? blockHashOrIndex)
{
if (uint.TryParse(value, out var index))
{
blockHashOrIndex = new BlockHashOrIndex(index);
return true;
}
if (UInt256.TryParse(value, out var hash))
{
blockHashOrIndex = new BlockHashOrIndex(hash);
return true;
}
blockHashOrIndex = null;
return false;
}
public uint AsIndex()
{
if (_value is uint intValue)
return intValue;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid block index"));
}
public UInt256 AsHash()
{
if (_value is UInt256 hash)
return hash;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid block hash"));
}
}