Skip to content

Commit 670faac

Browse files
committed
Better redirect page // specific client overrides
1 parent 2f3a108 commit 670faac

File tree

9 files changed

+74
-87
lines changed

9 files changed

+74
-87
lines changed

Thirdweb/Thirdweb.Client/ThirdwebClient.cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public class ThirdwebClient
2121
internal string BundleId { get; }
2222
internal ITimeoutOptions FetchTimeoutOptions { get; }
2323

24-
private ThirdwebClient(string clientId = null, string secretKey = null, string bundleId = null, ITimeoutOptions fetchTimeoutOptions = null, IThirdwebHttpClient httpClient = null)
24+
private ThirdwebClient(
25+
string clientId = null,
26+
string secretKey = null,
27+
string bundleId = null,
28+
ITimeoutOptions fetchTimeoutOptions = null,
29+
IThirdwebHttpClient httpClient = null,
30+
string sdkName = null,
31+
string sdkOs = null,
32+
string sdkPlatform = null,
33+
string sdkVersion = null
34+
)
2535
{
2636
if (string.IsNullOrEmpty(clientId) && string.IsNullOrEmpty(secretKey))
2737
{
@@ -42,30 +52,25 @@ private ThirdwebClient(string clientId = null, string secretKey = null, string b
4252

4353
this.FetchTimeoutOptions = fetchTimeoutOptions ?? new TimeoutOptions();
4454

45-
if (httpClient != null)
55+
var defaultHeaders = new Dictionary<string, string>
4656
{
47-
this.HttpClient = httpClient;
57+
{ "x-sdk-name", sdkName ?? "Thirdweb.NET" },
58+
{ "x-sdk-os", sdkOs ?? System.Runtime.InteropServices.RuntimeInformation.OSDescription },
59+
{ "x-sdk-platform", sdkPlatform ?? "dotnet" },
60+
{ "x-sdk-version", sdkVersion ?? Constants.VERSION },
61+
{ "x-client-id", this.ClientId },
62+
};
63+
if (!string.IsNullOrEmpty(this.BundleId))
64+
{
65+
defaultHeaders.Add("x-bundle-id", this.BundleId);
4866
}
49-
else
67+
if (!string.IsNullOrEmpty(this.SecretKey))
5068
{
51-
var defaultHeaders = new Dictionary<string, string>
52-
{
53-
{ "x-sdk-name", "Thirdweb.NET" },
54-
{ "x-sdk-os", System.Runtime.InteropServices.RuntimeInformation.OSDescription },
55-
{ "x-sdk-platform", "dotnet" },
56-
{ "x-sdk-version", Constants.VERSION },
57-
{ "x-client-id", this.ClientId },
58-
};
59-
if (!string.IsNullOrEmpty(this.SecretKey))
60-
{
61-
defaultHeaders.Add("x-secret-key", this.SecretKey);
62-
}
63-
if (!string.IsNullOrEmpty(this.BundleId))
64-
{
65-
defaultHeaders.Add("x-bundle-id", this.BundleId);
66-
}
67-
this.HttpClient = new ThirdwebHttpClient(defaultHeaders);
69+
defaultHeaders.Add("x-secret-key", this.SecretKey);
6870
}
71+
72+
this.HttpClient = httpClient ?? new ThirdwebHttpClient();
73+
this.HttpClient.SetHeaders(defaultHeaders);
6974
}
7075

7176
/// <summary>
@@ -76,9 +81,23 @@ private ThirdwebClient(string clientId = null, string secretKey = null, string b
7681
/// <param name="bundleId">The bundle ID (optional).</param>
7782
/// <param name="fetchTimeoutOptions">The fetch timeout options (optional).</param>
7883
/// <param name="httpClient">The HTTP client (optional).</param>
84+
/// <param name="sdkName">The SDK name (optional).</param>
85+
/// <param name="sdkOs">The SDK OS (optional).</param>
86+
/// <param name="sdkPlatform">The SDK platform (optional).</param>
87+
/// <param name="sdkVersion">The SDK version (optional).</param>
7988
/// <returns>A new instance of <see cref="ThirdwebClient"/>.</returns>
80-
public static ThirdwebClient Create(string clientId = null, string secretKey = null, string bundleId = null, ITimeoutOptions fetchTimeoutOptions = null, IThirdwebHttpClient httpClient = null)
89+
public static ThirdwebClient Create(
90+
string clientId = null,
91+
string secretKey = null,
92+
string bundleId = null,
93+
ITimeoutOptions fetchTimeoutOptions = null,
94+
IThirdwebHttpClient httpClient = null,
95+
string sdkName = null,
96+
string sdkOs = null,
97+
string sdkPlatform = null,
98+
string sdkVersion = null
99+
)
81100
{
82-
return new ThirdwebClient(clientId, secretKey, bundleId, fetchTimeoutOptions, httpClient);
101+
return new ThirdwebClient(clientId, secretKey, bundleId, fetchTimeoutOptions, httpClient, sdkName, sdkOs, sdkPlatform, sdkVersion);
83102
}
84103
}

Thirdweb/Thirdweb.Http/ThirdwebHttpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class ThirdwebHttpClient : IThirdwebHttpClient
1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="ThirdwebHttpClient"/> class.
1818
/// </summary>
19-
public ThirdwebHttpClient(Dictionary<string, string> defaultHeaders = null)
19+
public ThirdwebHttpClient()
2020
{
2121
this._httpClient = new HttpClient();
22-
this.Headers = defaultHeaders ?? new Dictionary<string, string>();
22+
this.Headers = new Dictionary<string, string>();
2323
}
2424

2525
/// <summary>

Thirdweb/Thirdweb.Utils/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public static class Constants
1212
public const string DEFAULT_FACTORY_ADDRESS_V06 = "0x85e23b94e7F5E9cC1fF78BCe78cfb15B81f0DF00";
1313
public const string DEFAULT_FACTORY_ADDRESS_V07 = "0x4bE0ddfebcA9A5A4a617dee4DeCe99E7c862dceb";
1414

15+
public const string REDIRECT_HTML =
16+
"<html lang=\"en\" style=\"background-color:#050505;color:#fff\"><body style=\"position:relative;display:flex;flex-direction:column;height:100%;width:100%;margin:0;justify-content:center;align-items:center;text-align:center;overflow:hidden\"><div style=\"position:fixed;top:0;left:50%;background-image:radial-gradient(ellipse at center,hsl(260deg 78% 35% / 40%),transparent 60%);width:2400px;height:1400px;transform:translate(-50%,-50%);z-index:-1\"></div><h1>Authentication Complete!</h1><h2>You may close this tab now and return to the game</h2></body></html>";
17+
1518
internal const string VERSION = "2.2.2";
1619
internal const int DEFAULT_FETCH_TIMEOUT = 120000;
1720
internal const string DUMMY_SIG = "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,14 @@ public static async Task<bool> IsDeployed(ThirdwebClient client, BigInteger chai
836836
var code = await rpc.SendRequestAsync<string>("eth_getCode", address, "latest");
837837
return code != "0x";
838838
}
839+
840+
public static IThirdwebHttpClient ReconstructHttpClient(IThirdwebHttpClient httpClient, Dictionary<string, string> defaultHeaders = null)
841+
{
842+
var reconstructedHttpClient = httpClient.GetType().GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
843+
if (defaultHeaders != null)
844+
{
845+
reconstructedHttpClient.SetHeaders(defaultHeaders ?? httpClient.Headers);
846+
}
847+
return reconstructedHttpClient;
848+
}
839849
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public static async Task<EcosystemWallet> Create(
9898
_ => throw new ArgumentException("Invalid AuthProvider"),
9999
};
100100

101-
var enclaveHttpClient = client.HttpClient.GetType().GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
102101
var headers = client.HttpClient.Headers.ToDictionary(entry => entry.Key, entry => entry.Value);
103102
var platform = client.HttpClient.Headers["x-sdk-platform"];
104103
var version = client.HttpClient.Headers["x-sdk-version"];
@@ -120,7 +119,7 @@ public static async Task<EcosystemWallet> Create(
120119
headers.Add("x-ecosystem-partner-id", ecosystemPartnerId);
121120
}
122121
}
123-
enclaveHttpClient.SetHeaders(headers);
122+
var enclaveHttpClient = Utils.ReconstructHttpClient(client.HttpClient, headers);
124123

125124
storageDirectoryPath ??= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Thirdweb", "EcosystemWallet");
126125
var embeddedWallet = new EmbeddedWallet(client, storageDirectoryPath, ecosystemId, ecosystemPartnerId);

Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet.Authentication/AWS.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ internal class AWS
1212
private static readonly string _recoverySharePasswordLambdaFunctionNameV2 = $"arn:aws:lambda:{AWS_REGION}:324457261097:function:lambda-thirdweb-auth-enc-key-prod-ThirdwebAuthEncKeyFunction";
1313
private static readonly string _migrationKeyId = $"arn:aws:kms:{AWS_REGION}:324457261097:key/ccfb9ecd-f45d-4f37-864a-25fe72dcb49e";
1414

15-
internal static async Task<MemoryStream> InvokeRecoverySharePasswordLambdaAsync(string identityId, string token, string invokePayload, Type thirdwebHttpClientType)
15+
internal static async Task<MemoryStream> InvokeRecoverySharePasswordLambdaAsync(string identityId, string token, string invokePayload, IThirdwebHttpClient httpClient)
1616
{
17-
var credentials = await GetTemporaryCredentialsAsync(identityId, token, thirdwebHttpClientType).ConfigureAwait(false);
18-
return await InvokeLambdaWithTemporaryCredentialsAsync(credentials, invokePayload, thirdwebHttpClientType, _recoverySharePasswordLambdaFunctionNameV2).ConfigureAwait(false);
17+
var credentials = await GetTemporaryCredentialsAsync(identityId, token, httpClient).ConfigureAwait(false);
18+
return await InvokeLambdaWithTemporaryCredentialsAsync(credentials, invokePayload, httpClient, _recoverySharePasswordLambdaFunctionNameV2).ConfigureAwait(false);
1919
}
2020

21-
internal static async Task<JToken> GenerateDataKey(string identityId, string token, Type thirdwebHttpClientType)
21+
internal static async Task<JToken> GenerateDataKey(string identityId, string token, IThirdwebHttpClient httpClient)
2222
{
23-
var credentials = await GetTemporaryCredentialsAsync(identityId, token, thirdwebHttpClientType).ConfigureAwait(false);
24-
return await GenerateDataKey(credentials, thirdwebHttpClientType).ConfigureAwait(false);
23+
var credentials = await GetTemporaryCredentialsAsync(identityId, token, httpClient).ConfigureAwait(false);
24+
return await GenerateDataKey(credentials, httpClient).ConfigureAwait(false);
2525
}
2626

27-
private static async Task<AwsCredentials> GetTemporaryCredentialsAsync(string identityId, string token, Type thirdwebHttpClientType)
27+
private static async Task<AwsCredentials> GetTemporaryCredentialsAsync(string identityId, string token, IThirdwebHttpClient httpClient)
2828
{
29-
var client = thirdwebHttpClientType.GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
29+
var client = Utils.ReconstructHttpClient(httpClient);
3030
var endpoint = $"https://cognito-identity.{AWS_REGION}.amazonaws.com/";
3131

3232
var payloadForGetCredentials = new { IdentityId = identityId, Logins = new Dictionary<string, string> { { "cognito-identity.amazonaws.com", token } } };
@@ -53,9 +53,9 @@ private static async Task<AwsCredentials> GetTemporaryCredentialsAsync(string id
5353
};
5454
}
5555

56-
private static async Task<JToken> GenerateDataKey(AwsCredentials credentials, Type thirdwebHttpClientType)
56+
private static async Task<JToken> GenerateDataKey(AwsCredentials credentials, IThirdwebHttpClient httpClient)
5757
{
58-
var client = thirdwebHttpClientType.GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
58+
var client = Utils.ReconstructHttpClient(httpClient);
5959
var endpoint = $"https://kms.{AWS_REGION}.amazonaws.com/";
6060

6161
var payloadForGenerateDataKey = new { KeyId = _migrationKeyId, KeySpec = "AES_256" };
@@ -119,12 +119,12 @@ private static async Task<JToken> GenerateDataKey(AwsCredentials credentials, Ty
119119
return responseObject;
120120
}
121121

122-
private static async Task<MemoryStream> InvokeLambdaWithTemporaryCredentialsAsync(AwsCredentials credentials, string invokePayload, Type thirdwebHttpClientType, string lambdaFunction)
122+
private static async Task<MemoryStream> InvokeLambdaWithTemporaryCredentialsAsync(AwsCredentials credentials, string invokePayload, IThirdwebHttpClient httpClient, string lambdaFunction)
123123
{
124124
var endpoint = $"https://lambda.{AWS_REGION}.amazonaws.com/2015-03-31/functions/{lambdaFunction}/invocations";
125125
var requestBody = new StringContent(invokePayload, Encoding.UTF8, "application/json");
126126

127-
var client = thirdwebHttpClientType.GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
127+
var client = Utils.ReconstructHttpClient(httpClient);
128128

129129
var dateTimeNow = DateTime.UtcNow;
130130
var dateStamp = dateTimeNow.ToString("yyyyMMdd");

Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet.Authentication/Server.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ internal partial class Server : ServerBase
4747

4848
private readonly string _clientId;
4949

50-
private static Type _thirdwebHttpClientType = typeof(ThirdwebHttpClient);
51-
5250
internal Server(ThirdwebClient client, IThirdwebHttpClient httpClient)
5351
{
5452
this._clientId = client.ClientId;
5553
this._httpClient = httpClient;
56-
57-
_thirdwebHttpClientType = httpClient.GetType();
5854
}
5955

6056
// account/connect
@@ -308,7 +304,7 @@ internal override async Task<VerifyResult> VerifyOAuthAsync(string authResultStr
308304
internal override async Task<JToken> GenerateEncryptedKeyResultAsync(string authToken)
309305
{
310306
var webExchangeResult = await this.FetchCognitoIdTokenAsync(authToken).ConfigureAwait(false);
311-
return await AWS.GenerateDataKey(webExchangeResult.IdentityId, webExchangeResult.Token, _thirdwebHttpClientType).ConfigureAwait(false);
307+
return await AWS.GenerateDataKey(webExchangeResult.IdentityId, webExchangeResult.Token, this._httpClient).ConfigureAwait(false);
312308
}
313309

314310
private async Task<VerifyResult> InvokeAuthResultLambdaAsync(AuthResultType authResult)
@@ -317,7 +313,7 @@ private async Task<VerifyResult> InvokeAuthResultLambdaAsync(AuthResultType auth
317313
var idTokenResponse = await this.FetchCognitoIdTokenAsync(authToken).ConfigureAwait(false);
318314

319315
var invokePayload = Serialize(new { token = idTokenResponse.LambdaToken });
320-
var responsePayload = await AWS.InvokeRecoverySharePasswordLambdaAsync(idTokenResponse.IdentityId, idTokenResponse.Token, invokePayload, _thirdwebHttpClientType).ConfigureAwait(false);
316+
var responsePayload = await AWS.InvokeRecoverySharePasswordLambdaAsync(idTokenResponse.IdentityId, idTokenResponse.Token, invokePayload, this._httpClient).ConfigureAwait(false);
321317

322318
var jsonSerializer = new JsonSerializer();
323319
var payload = jsonSerializer.Deserialize<RecoverySharePasswordResponse>(new JsonTextReader(new StreamReader(responsePayload)));

Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public EmbeddedWallet(ThirdwebClient client, string storageDirectoryPath = null,
2020
this._localStorage = new LocalStorage(client.ClientId, storageDirectoryPath);
2121

2222
// Create a new client of same type with extra needed headers for EWS
23-
var thirdwebHttpClientType = client.HttpClient.GetType();
24-
var ewsHttpClient = thirdwebHttpClientType.GetConstructor(Type.EmptyTypes).Invoke(null) as IThirdwebHttpClient;
2523
var headers = client.HttpClient.Headers.ToDictionary(entry => entry.Key, entry => entry.Value);
2624
var platform = client.HttpClient.Headers["x-sdk-platform"];
2725
var version = client.HttpClient.Headers["x-sdk-version"];
@@ -45,7 +43,7 @@ public EmbeddedWallet(ThirdwebClient client, string storageDirectoryPath = null,
4543
}
4644
}
4745

48-
ewsHttpClient.SetHeaders(headers);
46+
var ewsHttpClient = Utils.ReconstructHttpClient(client.HttpClient, headers);
4947

5048
this._server = new Server(client, ewsHttpClient);
5149

Thirdweb/Thirdweb.Wallets/InAppWallet/InAppWalletBrowser.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,6 @@ public class InAppWalletBrowser : IThirdwebBrowser
1010
private TaskCompletionSource<BrowserResult> _taskCompletionSource;
1111
private static readonly HttpListener _httpListener = new();
1212

13-
private readonly string _closePageResponse =
14-
@"
15-
<html>
16-
<head>
17-
<style>
18-
body {
19-
font-family: Arial, sans-serif;
20-
background-color: #2c2c2c;
21-
color: #ffffff;
22-
display: flex;
23-
justify-content: center;
24-
align-items: center;
25-
height: 100vh;
26-
flex-direction: column;
27-
}
28-
.container {
29-
background-color: #3c3c3c;
30-
padding: 20px;
31-
border-radius: 10px;
32-
box-shadow: 0 0 10px rgba(0,0,0,0.3);
33-
text-align: center;
34-
}
35-
.instruction {
36-
margin-top: 20px;
37-
font-size: 18px;
38-
}
39-
</style>
40-
</head>
41-
<body>
42-
<div class='container'>
43-
<b>DONE!</b>
44-
<div class='instruction'>
45-
You can close this tab/window now.
46-
</div>
47-
</div>
48-
</body>
49-
</html>";
50-
5113
/// <summary>
5214
/// Initiates a login process using the in-app browser.
5315
/// </summary>
@@ -124,7 +86,7 @@ private void IncomingHttpRequest(IAsyncResult result)
12486
var httpContext = httpListener.EndGetContext(result);
12587
var httpRequest = httpContext.Request;
12688
var httpResponse = httpContext.Response;
127-
var buffer = System.Text.Encoding.UTF8.GetBytes(this._closePageResponse);
89+
var buffer = System.Text.Encoding.UTF8.GetBytes(Constants.REDIRECT_HTML);
12890

12991
httpResponse.ContentLength64 = buffer.Length;
13092
var output = httpResponse.OutputStream;

0 commit comments

Comments
 (0)