You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The root hash must be computed before serialization:
70
+
```csharp
71
+
// This will throw InvalidRootException if root hasn't been computed
72
+
tree.ToJson();
73
+
74
+
// Always compute the root first
75
+
tree.RecomputeSha256Root();
76
+
tree.ToJson(); // Now works
77
+
```
78
+
79
+
#### Error Handling
80
+
The library provides clear error messages for common issues:
81
+
```csharp
82
+
try {
83
+
tree.VerifyRoot();
84
+
} catch (NotSupportedExceptionex) {
85
+
// Error message explains how to use custom hash functions
86
+
Console.WriteLine(ex.Message);
87
+
}
88
+
```
89
+
90
+
#### Custom Hash Functions
91
+
You can implement custom hash functions and select them based on the tree's metadata:
92
+
```csharp
93
+
// Define a custom hash function
94
+
HexComputeReverseSha256Hash(byte[] data)
95
+
{
96
+
// Reverse the input bytes
97
+
byte[] reversed=newbyte[data.Length];
98
+
Array.Copy(data, reversed, data.Length);
99
+
Array.Reverse(reversed);
100
+
101
+
// Hash the reversed bytes
102
+
usingvarsha256=SHA256.Create();
103
+
returnnewHex(sha256.ComputeHash(reversed));
104
+
}
105
+
106
+
// Create a hash function selector
107
+
HashFunctionSelectHashFunction(MerkleTreetree)
108
+
{
109
+
returntree.Metadata.HashAlgorithmswitch
110
+
{
111
+
"sha256"=>MerkleTree.ComputeSha256Hash,
112
+
"sha256-reverse"=>ComputeReverseSha256Hash,
113
+
_=>thrownewNotSupportedException(
114
+
$"Hash algorithm '{tree.Metadata.HashAlgorithm}' is not supported. "+
115
+
"Please implement a custom hash function for this algorithm.")
116
+
};
117
+
}
118
+
119
+
// Use the selector to get the right hash function
120
+
varhashFunction=SelectHashFunction(tree);
121
+
boolisValid=tree.VerifyRoot(hashFunction);
122
+
```
123
+
124
+
#### Version Support
125
+
The parser automatically detects the version format of the JSON. The library currently defaults to v1.0 format, but also supports the newer v2.0 format which uses JWT-style headers:
126
+
127
+
```csharp
128
+
// v1.0 format (current default, uses "metadata" property)
0 commit comments