Skip to content

Commit 0a8eac4

Browse files
authored
Storage/AA api key support (#92)
1 parent e1a8725 commit 0a8eac4

23 files changed

+325407
-449
lines changed

Assets/Thirdweb/Core/Scripts/AccountAbstraction/Core/BundlerClient.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ private static async Task<RpcResponseMessage> BundlerRequest(string url, string
4848

4949
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url);
5050
httpRequestMessage.Content = new StringContent(requestMessageJson, System.Text.Encoding.UTF8, "application/json");
51-
httpRequestMessage.Headers.Add("x-api-key", apiKey);
51+
if (new Uri(url).Host.EndsWith(".thirdweb.com"))
52+
{
53+
httpRequestMessage.Headers.Add("x-client-id", ThirdwebManager.Instance.SDK.session.Options.clientId);
54+
if (!Utils.IsWebGLBuild())
55+
httpRequestMessage.Headers.Add("x-bundle-id", Utils.GetBundleId());
56+
}
5257

5358
var httpResponse = await client.SendAsync(httpRequestMessage);
5459

Assets/Thirdweb/Core/Scripts/AccountAbstraction/Core/SmartWallet.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public SmartWallet(Web3 personalWeb3, ThirdwebSDK.SmartWalletConfig config)
4646
Config = new ThirdwebSDK.SmartWalletConfig()
4747
{
4848
factoryAddress = config.factoryAddress,
49-
thirdwebApiKey = config.thirdwebApiKey,
5049
gasless = config.gasless,
5150
bundlerUrl = string.IsNullOrEmpty(config.bundlerUrl) ? $"https://{ThirdwebManager.Instance.SDK.session.CurrentChainData.chainName}.bundler.thirdweb.com" : config.bundlerUrl,
5251
paymasterUrl = string.IsNullOrEmpty(config.paymasterUrl) ? $"https://{ThirdwebManager.Instance.SDK.session.CurrentChainData.chainName}.bundler.thirdweb.com" : config.paymasterUrl,
@@ -123,6 +122,8 @@ internal async Task<RpcResponseMessage> Request(RpcRequestMessage requestMessage
123122

124123
private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage requestMessage)
125124
{
125+
string apiKey = ThirdwebManager.Instance.SDK.session.Options.clientId;
126+
126127
// Deserialize the transaction input from the request message
127128

128129
var paramList = JsonConvert.DeserializeObject<List<object>>(JsonConvert.SerializeObject(requestMessage.RawParameters));
@@ -165,7 +166,7 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
165166

166167
// Update paymaster data if any
167168

168-
partialUserOp.PaymasterAndData = await GetPaymasterAndData(requestMessage.Id, partialUserOpHexified);
169+
partialUserOp.PaymasterAndData = await GetPaymasterAndData(requestMessage.Id, partialUserOpHexified, apiKey);
169170

170171
// Hash, sign and encode the user operation
171172

@@ -176,15 +177,15 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
176177

177178
Debug.Log("Valid UserOp: " + JsonConvert.SerializeObject(partialUserOp));
178179
Debug.Log("Valid Encoded UserOp: " + JsonConvert.SerializeObject(partialUserOpHexified));
179-
var userOpHash = await BundlerClient.EthSendUserOperation(Config.bundlerUrl, Config.thirdwebApiKey, requestMessage.Id, partialUserOpHexified, Config.entryPointAddress);
180+
var userOpHash = await BundlerClient.EthSendUserOperation(Config.bundlerUrl, apiKey, requestMessage.Id, partialUserOpHexified, Config.entryPointAddress);
180181
Debug.Log("UserOp Hash: " + userOpHash);
181182

182183
// Wait for the transaction to be mined
183184

184185
string txHash = null;
185186
while (txHash == null && Application.isPlaying)
186187
{
187-
var getUserOpResponse = await BundlerClient.EthGetUserOperationByHash(Config.bundlerUrl, Config.thirdwebApiKey, requestMessage.Id, userOpHash);
188+
var getUserOpResponse = await BundlerClient.EthGetUserOperationByHash(Config.bundlerUrl, apiKey, requestMessage.Id, userOpHash);
188189
txHash = getUserOpResponse?.transactionHash;
189190
await new WaitForSecondsRealtime(5f);
190191
}
@@ -215,10 +216,10 @@ private async Task<BigInteger> GetNonce()
215216
return nonce.ReturnValue1;
216217
}
217218

218-
private async Task<byte[]> GetPaymasterAndData(object requestId, UserOperationHexified userOp)
219+
private async Task<byte[]> GetPaymasterAndData(object requestId, UserOperationHexified userOp, string apiKey)
219220
{
220221
return Config.gasless
221-
? (await BundlerClient.PMSponsorUserOperation(Config.paymasterUrl, Config.thirdwebApiKey, requestId, userOp, Config.entryPointAddress)).paymasterAndData.HexStringToByteArray()
222+
? (await BundlerClient.PMSponsorUserOperation(Config.paymasterUrl, apiKey, requestId, userOp, Config.entryPointAddress)).paymasterAndData.HexStringToByteArray()
222223
: new byte[] { };
223224
}
224225
}

Assets/Thirdweb/Core/Scripts/Storage.cs

Lines changed: 0 additions & 119 deletions
This file was deleted.

Assets/Thirdweb/Core/Scripts/Storage.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Threading.Tasks;
2+
using UnityEngine;
3+
4+
namespace Thirdweb
5+
{
6+
public interface IStorageDownloader
7+
{
8+
Task<T> DownloadText<T>(string textURI);
9+
Task<Sprite> DownloadImage(string imageURI);
10+
}
11+
}

Assets/Thirdweb/Core/Scripts/Storage/IStorageDownloader.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Thirdweb
4+
{
5+
public interface IStorageUploader
6+
{
7+
Task<IPFSUploadResult> UploadText(string text);
8+
Task<IPFSUploadResult> UploadFromPath(string path);
9+
}
10+
}

Assets/Thirdweb/Core/Scripts/Storage/IStorageUploader.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using UnityEngine;
2+
using System.Threading.Tasks;
3+
4+
namespace Thirdweb
5+
{
6+
public class Storage
7+
{
8+
public string IPFSGateway { get; private set; }
9+
public string ClientId { get; private set; }
10+
11+
private IStorageUploader uploader;
12+
private IStorageDownloader downloader;
13+
14+
private const string FALLBACK_IPFS_GATEWAY = "https://cloudflare-ipfs.com/ipfs/";
15+
16+
public Storage(ThirdwebSDK.StorageOptions? storageOptions, string clientId = null)
17+
{
18+
this.ClientId = clientId;
19+
20+
string thirdwebIpfsGateway = $"https://{ClientId}.ipfscdn.io/ipfs/";
21+
if (storageOptions == null)
22+
{
23+
this.IPFSGateway = ClientId != null ? thirdwebIpfsGateway : FALLBACK_IPFS_GATEWAY;
24+
this.uploader = new StorageUploader();
25+
this.downloader = new StorageDownloader();
26+
}
27+
else
28+
{
29+
this.IPFSGateway = string.IsNullOrEmpty(storageOptions?.ipfsGatewayUrl) ? (ClientId != null ? thirdwebIpfsGateway : FALLBACK_IPFS_GATEWAY) : storageOptions?.ipfsGatewayUrl;
30+
this.uploader = storageOptions.Value.uploaderOverride ?? new StorageUploader();
31+
this.downloader = storageOptions.Value.downloaderOverride ?? new StorageDownloader();
32+
}
33+
}
34+
35+
public async Task<IPFSUploadResult> UploadText(string text)
36+
{
37+
return await uploader.UploadText(text);
38+
}
39+
40+
public async Task<IPFSUploadResult> UploadFromPath(string path)
41+
{
42+
return await uploader.UploadFromPath(path);
43+
}
44+
45+
public async Task<T> DownloadText<T>(string textURI)
46+
{
47+
return await downloader.DownloadText<T>(textURI);
48+
}
49+
50+
public async Task<Sprite> DownloadImage(string imageURI)
51+
{
52+
return await downloader.DownloadImage(imageURI);
53+
}
54+
}
55+
}

Assets/Thirdweb/Core/Scripts/Storage.cs.meta renamed to Assets/Thirdweb/Core/Scripts/Storage/Storage.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)