Skip to content

Commit 4783361

Browse files
committed
Fix byte array arguments not working in WebGL
1 parent cda06d2 commit 4783361

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using WalletConnectSharp.Common.Utils;
4+
5+
namespace WalletConnect.Web3Modal.Utils
6+
{
7+
/// <summary>
8+
/// Converts byte array to hex string and vice versa.
9+
/// </summary>
10+
/// <remarks>
11+
/// The default behavior of Newtonsoft.Json is to convert byte arrays to base64 strings.
12+
/// </remarks>
13+
public class ByteArrayJsonConverter : JsonConverter
14+
{
15+
public override bool CanConvert(Type objectType)
16+
{
17+
return objectType == typeof(byte[]);
18+
}
19+
20+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
21+
{
22+
if (reader.TokenType == JsonToken.Null)
23+
return null;
24+
25+
if (reader.TokenType == JsonToken.String)
26+
{
27+
var hexString = (string)reader.Value;
28+
return hexString.HexToByteArray();
29+
}
30+
31+
throw new JsonSerializationException("Expected byte array object value");
32+
}
33+
34+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
35+
{
36+
if (value == null)
37+
{
38+
writer.WriteNull();
39+
return;
40+
}
41+
42+
var byteArray = (byte[])value;
43+
var hexString = byteArray.ToHex(true);
44+
45+
writer.WriteValue(hexString);
46+
}
47+
}
48+
}

Packages/com.walletconnect.web3modal/Runtime/Utils/ByteArrayJsonConverter.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.walletconnect.web3modal/Runtime/WebGL/InteropService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AOT;
77
using Newtonsoft.Json;
88
using UnityEngine;
9+
using WalletConnect.Web3Modal.Utils;
910

1011
namespace WalletConnect.Web3Modal.WebGl
1112
{
@@ -15,6 +16,11 @@ public class InteropService
1516

1617
private readonly ExternalMethod _externalMethod;
1718

19+
private readonly JsonConverter[] _jsonConverts =
20+
{
21+
new ByteArrayJsonConverter()
22+
};
23+
1824
public InteropService(ExternalMethod externalMethod)
1925
{
2026
_externalMethod = externalMethod;
@@ -46,9 +52,13 @@ public async Task<TRes> InteropCallAsync<TReq, TRes>(string methodName, TReq req
4652
if (!Equals(requestParameter, default(TReq)))
4753
{
4854
if (typeof(TReq) == typeof(string))
55+
{
4956
paramStr = requestParameter as string;
57+
}
5058
else
51-
paramStr = JsonConvert.SerializeObject(requestParameter);
59+
{
60+
paramStr = JsonConvert.SerializeObject(requestParameter, _jsonConverts);
61+
}
5262
}
5363

5464
_externalMethod(id, methodName, paramStr, TcsCallback);

0 commit comments

Comments
 (0)