Skip to content

Commit

Permalink
Preprocess Typed Data JSON w/ >uint32 fields
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Nov 15, 2024
1 parent 1fef151 commit 167d871
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
50 changes: 50 additions & 0 deletions Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,54 @@ public static async Task<SocialProfiles> GetSocialProfiles(ThirdwebClient client

return new SocialProfiles(deserializedResponse.Data);
}

/// <summary>
/// Preprocesses the typed data JSON to stringify large numbers.
/// </summary>
/// <param name="json">The typed data JSON.</param>
/// <returns>The preprocessed typed data JSON.</returns>
public static string PreprocessTypedDataJson(string json)
{
var jObject = JObject.Parse(json);

static void StringifyLargeNumbers(JToken token)
{
if (token is JObject obj)
{
foreach (var property in obj.Properties())
{
StringifyLargeNumbers(property.Value);
}
}
else if (token is JArray array)
{
foreach (var item in array)
{
StringifyLargeNumbers(item);
}
}
else if (token is JValue value)
{
if (value.Type == JTokenType.Integer)
{
try
{
var bigInt = BigInteger.Parse(value.ToString());
if (bigInt > new BigInteger(uint.MaxValue) || bigInt < BigInteger.Zero)
{
value.Replace(bigInt.ToString());
}
}
catch (FormatException)
{
// Skip if the value isn't properly formatted as an integer
}
}
}
}

StringifyLargeNumbers(jObject);

return jObject.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,11 @@ public async Task<string> SignTypedDataV4(string json)
throw new ArgumentNullException(nameof(json), "Json to sign cannot be null.");
}

var processedJson = Utils.PreprocessTypedDataJson(json);

var url = $"{ENCLAVE_PATH}/sign-typed-data";

var requestContent = new StringContent(json, Encoding.UTF8, "application/json");
var requestContent = new StringContent(processedJson, Encoding.UTF8, "application/json");

var response = await this.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false);
_ = response.EnsureSuccessStatusCode();
Expand Down

0 comments on commit 167d871

Please sign in to comment.