Skip to content

Commit 18b6df8

Browse files
committed
iterate over copy
1 parent 167d871 commit 18b6df8

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Numerics;
2+
using Newtonsoft.Json.Linq;
23

34
namespace Thirdweb.Tests.Utilities;
45

@@ -763,4 +764,104 @@ public async Task FetchGasFees_Celo()
763764
Assert.True(maxPrio > 0);
764765
Assert.Equal(maxFee, maxPrio);
765766
}
767+
768+
[Fact]
769+
public void PreprocessTypedDataJson_FormatsBigIntegers()
770+
{
771+
// Arrange
772+
var inputJson =
773+
/*lang=json,strict*/
774+
@"
775+
{
776+
""from"": 973250616940336452028326648501327235277017847475,
777+
""data"": ""0x"",
778+
""nested"": {
779+
""value"": 4294967295
780+
},
781+
""array"": [
782+
123,
783+
973250616940336452028326648501327235277017847475
784+
]
785+
}";
786+
787+
var expectedJson =
788+
/*lang=json,strict*/
789+
@"
790+
{
791+
""from"": ""973250616940336452028326648501327235277017847475"",
792+
""data"": ""0x"",
793+
""nested"": {
794+
""value"": 4294967295
795+
},
796+
""array"": [
797+
123,
798+
""973250616940336452028326648501327235277017847475""
799+
]
800+
}";
801+
802+
// Act
803+
var processedJson = Utils.PreprocessTypedDataJson(inputJson);
804+
805+
// Assert
806+
var expectedJObject = JObject.Parse(expectedJson);
807+
var processedJObject = JObject.Parse(processedJson);
808+
809+
Assert.Equal(expectedJObject, processedJObject);
810+
}
811+
812+
[Fact]
813+
public void PreprocessTypedDataJson_NoLargeNumbers_NoChange()
814+
{
815+
// Arrange
816+
var inputJson =
817+
/*lang=json,strict*/
818+
@"
819+
{
820+
""value"": 123,
821+
""nested"": {
822+
""value"": 456
823+
},
824+
""array"": [1, 2, 3]
825+
}";
826+
827+
// Act
828+
var processedJson = Utils.PreprocessTypedDataJson(inputJson);
829+
830+
// Assert
831+
Assert.Equal(inputJson.Replace("\r\n", "").Replace(" ", ""), processedJson.Replace("\r\n", "").Replace(" ", ""));
832+
}
833+
834+
[Fact]
835+
public void PreprocessTypedDataJson_NestedLargeNumbers()
836+
{
837+
// Arrange
838+
var inputJson =
839+
/*lang=json,strict*/
840+
@"
841+
{
842+
""nested"": {
843+
""value"": 973250616940336452028326648501327235277017847475
844+
},
845+
""array"": [123, 973250616940336452028326648501327235277017847475]
846+
}";
847+
848+
var expectedJson =
849+
/*lang=json,strict*/
850+
@"
851+
{
852+
""nested"": {
853+
""value"": ""973250616940336452028326648501327235277017847475""
854+
},
855+
""array"": [123, ""973250616940336452028326648501327235277017847475""]
856+
}";
857+
858+
// Act
859+
var processedJson = Utils.PreprocessTypedDataJson(inputJson);
860+
861+
// Assert
862+
var expectedJObject = JObject.Parse(expectedJson);
863+
var processedJObject = JObject.Parse(processedJson);
864+
865+
Assert.Equal(expectedJObject, processedJObject);
866+
}
766867
}

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,14 +967,14 @@ static void StringifyLargeNumbers(JToken token)
967967
{
968968
if (token is JObject obj)
969969
{
970-
foreach (var property in obj.Properties())
970+
foreach (var property in obj.Properties().ToList())
971971
{
972972
StringifyLargeNumbers(property.Value);
973973
}
974974
}
975975
else if (token is JArray array)
976976
{
977-
foreach (var item in array)
977+
foreach (var item in array.ToList())
978978
{
979979
StringifyLargeNumbers(item);
980980
}

0 commit comments

Comments
 (0)