Skip to content

Commit 69df32e

Browse files
committed
Integrate Ecosystem Signing Policies
Closes TOOL-4019
1 parent b6541d6 commit 69df32e

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

Thirdweb/Thirdweb.RPC/ThirdwebRPC.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ThirdwebRPC : IDisposable
2525
private readonly object _responseLock = new();
2626
private readonly object _cacheLock = new();
2727
private readonly CancellationTokenSource _cancellationTokenSource = new();
28+
private readonly JsonSerializerSettings _jsonSerializerSettings = new() { NullValueHandling = NullValueHandling.Ignore, };
2829

2930
private int _requestIdCounter = 1;
3031

@@ -167,7 +168,7 @@ private ThirdwebRPC(ThirdwebClient client, BigInteger chainId)
167168

168169
private async Task SendBatchAsync(List<RpcRequest> batch)
169170
{
170-
var batchJson = JsonConvert.SerializeObject(batch);
171+
var batchJson = JsonConvert.SerializeObject(batch, this._jsonSerializerSettings);
171172
var content = new StringContent(batchJson, Encoding.UTF8, "application/json");
172173

173174
try

Thirdweb/Thirdweb.Wallets/EngineWallet/EngineWallet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public Task<string> EthSign(string message)
188188
throw new NotImplementedException();
189189
}
190190

191-
public async Task<string> PersonalSign(byte[] rawMessage)
191+
public async Task<string> PersonalSign(byte[] rawMessage, object originalMessage = null, BigInteger? chainId = null)
192192
{
193193
if (rawMessage == null)
194194
{
@@ -207,7 +207,7 @@ public async Task<string> PersonalSign(byte[] rawMessage)
207207
return JObject.Parse(content)["result"].Value<string>();
208208
}
209209

210-
public async Task<string> PersonalSign(string message)
210+
public async Task<string> PersonalSign(string message, object originalMessage = null, BigInteger? chainId = null)
211211
{
212212
if (string.IsNullOrEmpty(message))
213213
{

Thirdweb/Thirdweb.Wallets/IThirdwebWallet.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ public interface IThirdwebWallet
5656
/// Signs a raw message using personal signing.
5757
/// </summary>
5858
/// <param name="rawMessage">The raw message to sign.</param>
59+
/// <param name="originalMessage">Used for Ecosystem signing polciies purposes only.</param>
60+
/// <param name="chainId">Used for Ecosystem signing polciies purposes only.</param>
5961
/// <returns>The signed message.</returns>
60-
public Task<string> PersonalSign(byte[] rawMessage);
62+
public Task<string> PersonalSign(byte[] rawMessage, object originalMessage = null, BigInteger? chainId = null);
6163

6264
/// <summary>
6365
/// Signs a message using personal signing.
6466
/// </summary>
6567
/// <param name="message">The message to sign.</param>
68+
/// <param name="originalMessage">Used for Ecosystem signing polciies purposes only.</param>
69+
/// <param name="chainId">Used for Ecosystem signing polciies purposes only.</param>
6670
/// <returns>The signed message.</returns>
67-
public Task<string> PersonalSign(string message);
71+
public Task<string> PersonalSign(string message, object originalMessage = null, BigInteger? chainId = null);
6872

6973
/// <summary>
7074
/// Recovers the address from a signed message using personal signing.

Thirdweb/Thirdweb.Wallets/InAppWallet/EcosystemWallet/EcosystemWallet.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public partial class EcosystemWallet : IThirdwebWallet
4848
private const string EMBEDDED_WALLET_PATH_V1 = $"{EMBEDDED_WALLET_BASE_PATH}/v1";
4949
private const string ENCLAVE_PATH = $"{EMBEDDED_WALLET_PATH_V1}/enclave-wallet";
5050

51+
private readonly JsonSerializerSettings _jsonSerializerSettings = new() { NullValueHandling = NullValueHandling.Ignore, };
52+
5153
internal EcosystemWallet(
5254
string ecosystemId,
5355
string ecosystemPartnerId,
@@ -1001,17 +1003,26 @@ public Task<string> EthSign(string message)
10011003
throw new NotImplementedException();
10021004
}
10031005

1004-
public async Task<string> PersonalSign(byte[] rawMessage)
1006+
public async Task<string> PersonalSign(byte[] rawMessage, object originalMessage = null, BigInteger? chainId = null)
10051007
{
10061008
if (rawMessage == null)
10071009
{
10081010
throw new ArgumentNullException(nameof(rawMessage), "Message to sign cannot be null.");
10091011
}
10101012

10111013
var url = $"{ENCLAVE_PATH}/sign-message";
1012-
var payload = new { messagePayload = new { message = rawMessage.BytesToHex(), isRaw = true } };
1014+
var payload = new
1015+
{
1016+
messagePayload = new
1017+
{
1018+
message = rawMessage.BytesToHex(),
1019+
isRaw = true,
1020+
originalMessage,
1021+
chainId
1022+
}
1023+
};
10131024

1014-
var requestContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
1025+
var requestContent = new StringContent(JsonConvert.SerializeObject(payload, this._jsonSerializerSettings), Encoding.UTF8, "application/json");
10151026

10161027
var response = await this.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false);
10171028
_ = response.EnsureSuccessStatusCode();
@@ -1021,17 +1032,26 @@ public async Task<string> PersonalSign(byte[] rawMessage)
10211032
return res.Signature;
10221033
}
10231034

1024-
public async Task<string> PersonalSign(string message)
1035+
public async Task<string> PersonalSign(string message, object originalMessage = null, BigInteger? chainId = null)
10251036
{
10261037
if (string.IsNullOrEmpty(message))
10271038
{
10281039
throw new ArgumentNullException(nameof(message), "Message to sign cannot be null.");
10291040
}
10301041

10311042
var url = $"{ENCLAVE_PATH}/sign-message";
1032-
var payload = new { messagePayload = new { message, isRaw = false } };
1043+
var payload = new
1044+
{
1045+
messagePayload = new
1046+
{
1047+
message,
1048+
isRaw = false,
1049+
originalMessage,
1050+
chainId
1051+
}
1052+
};
10331053

1034-
var requestContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
1054+
var requestContent = new StringContent(JsonConvert.SerializeObject(payload, this._jsonSerializerSettings), Encoding.UTF8, "application/json");
10351055

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

Thirdweb/Thirdweb.Wallets/PrivateKeyWallet/PrivateKeyWallet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public virtual Task<string> RecoverAddressFromEthSign(string message, string sig
209209
return Task.FromResult(address);
210210
}
211211

212-
public virtual Task<string> PersonalSign(byte[] rawMessage)
212+
public virtual Task<string> PersonalSign(byte[] rawMessage, object originalMessage = null, BigInteger? chainId = null)
213213
{
214214
if (rawMessage == null)
215215
{
@@ -221,7 +221,7 @@ public virtual Task<string> PersonalSign(byte[] rawMessage)
221221
return Task.FromResult(signature);
222222
}
223223

224-
public virtual Task<string> PersonalSign(string message)
224+
public virtual Task<string> PersonalSign(string message, object originalMessage = null, BigInteger? chainId = null)
225225
{
226226
if (string.IsNullOrEmpty(message))
227227
{

Thirdweb/Thirdweb.Wallets/SmartWallet/SmartWallet.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,12 @@ private async Task<PMSponsorOperationResponse> GetPaymasterAndData(object reques
873873

874874
private async Task<byte[]> HashAndSignUserOp(UserOperationV6 userOp, ThirdwebContract entryPointContract)
875875
{
876+
var hexified = EncodeUserOperation(userOp);
876877
var userOpHash = await ThirdwebContract.Read<byte[]>(entryPointContract, "getUserOpHash", userOp);
877878
var sig =
878879
this._personalAccount.AccountType == ThirdwebAccountType.ExternalAccount
879-
? await this._personalAccount.PersonalSign(userOpHash.BytesToHex()).ConfigureAwait(false)
880-
: await this._personalAccount.PersonalSign(userOpHash).ConfigureAwait(false);
880+
? await this._personalAccount.PersonalSign(userOpHash.BytesToHex(), hexified, this.ActiveChainId).ConfigureAwait(false)
881+
: await this._personalAccount.PersonalSign(userOpHash, hexified, this.ActiveChainId).ConfigureAwait(false);
881882
return sig.HexToBytes();
882883
}
883884

@@ -945,10 +946,11 @@ private async Task<byte[]> HashAndSignUserOp(UserOperationV7 userOp, ThirdwebCon
945946

946947
var userOpHash = await ThirdwebContract.Read<byte[]>(entryPointContract, "getUserOpHash", packedOp).ConfigureAwait(false);
947948

949+
var hexified = EncodeUserOperation(userOp);
948950
var sig =
949951
this._personalAccount.AccountType == ThirdwebAccountType.ExternalAccount
950-
? await this._personalAccount.PersonalSign(userOpHash.BytesToHex()).ConfigureAwait(false)
951-
: await this._personalAccount.PersonalSign(userOpHash).ConfigureAwait(false);
952+
? await this._personalAccount.PersonalSign(userOpHash.BytesToHex(), hexified, this.ActiveChainId).ConfigureAwait(false)
953+
: await this._personalAccount.PersonalSign(userOpHash, hexified, this.ActiveChainId).ConfigureAwait(false);
952954

953955
return sig.HexToBytes();
954956
}
@@ -1081,17 +1083,15 @@ public Task<string> RecoverAddressFromEthSign(string message, string signature)
10811083
throw new NotImplementedException();
10821084
}
10831085

1084-
public Task<string> PersonalSign(byte[] rawMessage)
1086+
public Task<string> PersonalSign(byte[] rawMessage, object originalMessage = null, BigInteger? chainId = null)
10851087
{
10861088
throw new NotImplementedException();
10871089
}
10881090

10891091
/// <summary>
10901092
/// Signs a message with the personal account. The message will be verified using EIPs 1271 and 6492 if applicable.
10911093
/// </summary>
1092-
/// <param name="message">The message to sign.</param>
1093-
/// <returns>The signature.</returns>
1094-
public async Task<string> PersonalSign(string message)
1094+
public async Task<string> PersonalSign(string message, object originalMessage = null, BigInteger? chainId = null)
10951095
{
10961096
if (await Utils.IsZkSync(this.Client, this.ActiveChainId).ConfigureAwait(false))
10971097
{

Thirdweb/Thirdweb.Wallets/SmartWallet/Thirdweb.AccountAbstraction/AATypes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public class PackedUserOperation
119119
public virtual byte[] Signature { get; set; }
120120
}
121121

122+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
122123
public class UserOperationHexifiedV6
123124
{
124125
[JsonProperty("sender")]
@@ -155,6 +156,7 @@ public class UserOperationHexifiedV6
155156
public string Signature { get; set; }
156157
}
157158

159+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
158160
public class UserOperationHexifiedV7
159161
{
160162
[JsonProperty("sender")]

0 commit comments

Comments
 (0)