Skip to content

Commit

Permalink
Update to match new rotating secret key model
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Feb 12, 2025
1 parent 6b4afc1 commit abe39c0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
10 changes: 4 additions & 6 deletions Thirdweb.Tests/Thirdweb.Client/Thirdweb.Client.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public void NoSecretKeyNoClientId()
public void SecretKeyInitialization()
{
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
Assert.NotNull(client.ClientId);
Assert.NotNull(client.SecretKey);
Assert.Null(client.BundleId);
Assert.Equal(client.ClientId, Utils.ComputeClientIdFromSecretKey(client.SecretKey));
Assert.Null(client.ClientId);
Assert.Equal(client.SecretKey, this.SecretKey);
}

Expand Down Expand Up @@ -49,8 +48,7 @@ public void ClientIdAndSecretKeyInitialization()
Assert.NotNull(client.ClientId);
Assert.NotNull(client.SecretKey);
Assert.Null(client.BundleId);
Assert.NotEqual(client.ClientId, clientId);
Assert.Equal(client.ClientId, Utils.ComputeClientIdFromSecretKey(client.SecretKey));
Assert.Equal(client.ClientId, clientId);
Assert.Equal(client.SecretKey, this.SecretKey);
}

Expand All @@ -74,10 +72,10 @@ public void SecretKeyAndBundleIdInitialization()
var client = ThirdwebClient.Create(secretKey: this.SecretKey, bundleId: bundleId);
Assert.NotNull(client.SecretKey);
Assert.NotNull(client.BundleId);
Assert.NotNull(client.ClientId);
Assert.Null(client.ClientId);
Assert.Equal(client.SecretKey, this.SecretKey);
Assert.Equal(client.BundleId, bundleId);
Assert.Equal(client.ClientId, Utils.ComputeClientIdFromSecretKey(client.SecretKey));
Assert.Null(client.ClientId);
}

[Fact(Timeout = 120000)]
Expand Down
6 changes: 0 additions & 6 deletions Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ public class UtilsTests : BaseTests
public UtilsTests(ITestOutputHelper output)
: base(output) { }

[Fact(Timeout = 120000)]
public void ComputeClientIdFromSecretKey()
{
Assert.True(Utils.ComputeClientIdFromSecretKey(this.SecretKey).Length == 32);
}

[Fact(Timeout = 120000)]
public void HexConcat()
{
Expand Down
11 changes: 7 additions & 4 deletions Thirdweb/Thirdweb.Client/ThirdwebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ private ThirdwebClient(

if (!string.IsNullOrEmpty(secretKey))
{
this.ClientId = Utils.ComputeClientIdFromSecretKey(secretKey);
this.SecretKey = secretKey;
}
else

if (!string.IsNullOrEmpty(clientId))
{
this.ClientId = clientId;
}
Expand All @@ -61,9 +61,12 @@ private ThirdwebClient(
{ "x-sdk-name", sdkName ?? "Thirdweb.NET" },
{ "x-sdk-os", sdkOs ?? System.Runtime.InteropServices.RuntimeInformation.OSDescription },
{ "x-sdk-platform", sdkPlatform ?? "dotnet" },
{ "x-sdk-version", sdkVersion ?? Constants.VERSION },
{ "x-client-id", this.ClientId },
{ "x-sdk-version", sdkVersion ?? Constants.VERSION }
};
if (!string.IsNullOrEmpty(this.ClientId))
{
defaultHeaders.Add("x-client-id", this.ClientId);
}
if (!string.IsNullOrEmpty(this.BundleId))
{
defaultHeaders.Add("x-bundle-id", this.BundleId);
Expand Down
18 changes: 0 additions & 18 deletions Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using ADRaffy.ENSNormalize;
Expand Down Expand Up @@ -31,23 +30,6 @@ public static partial class Utils
private static readonly Dictionary<string, string> _ensCache = new();
private static readonly List<string[]> _errorSubstringsComposite = new() { new string[] { "account", "not found!" }, new[] { "wrong", "chainid" } };

/// <summary>
/// Computes the client ID from the given secret key.
/// </summary>
/// <param name="secretKey">The secret key.</param>
/// <returns>The computed client ID.</returns>
public static string ComputeClientIdFromSecretKey(string secretKey)
{
#if NETSTANDARD
using var sha256 = SHA256.Create();
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
return BitConverter.ToString(hash).Replace("-", "").ToLower()[..32];
#else
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(secretKey));
return BitConverter.ToString(hash).Replace("-", "").ToLower()[..32];
#endif
}

// public static byte[] StringToSha256(string bytes)
// {
// #if NETSTANDARD
Expand Down

0 comments on commit abe39c0

Please sign in to comment.