Skip to content

Commit

Permalink
Add custom auth migration
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Nov 5, 2024
1 parent 92dc138 commit b6c60ca
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Thirdweb;

public partial class EcosystemWallet
{
public class EnclaveUserStatusResponse
public class UserStatusResponse
{
[JsonProperty("linkedAccounts")]
internal List<LinkedAccount> LinkedAccounts { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public partial class EcosystemWallet : IThirdwebWallet
internal readonly string Email;
internal readonly string PhoneNumber;
internal readonly string AuthProvider;
internal readonly string LegacyEncryptionKey;

internal string Address;

Expand All @@ -43,12 +44,14 @@ internal EcosystemWallet(
string email,
string phoneNumber,
string authProvider,
IThirdwebWallet siweSigner
IThirdwebWallet siweSigner,
string legacyEncryptionKey
)
{
this.Client = client;
this._ecosystemId = ecosystemId;
this._ecosystemPartnerId = ecosystemPartnerId;
this.LegacyEncryptionKey = legacyEncryptionKey;
this.EmbeddedWallet = embeddedWallet;
this.HttpClient = httpClient;
this.Email = email;
Expand All @@ -59,6 +62,20 @@ IThirdwebWallet siweSigner

#region Creation

/// <summary>
/// Creates a new instance of the <see cref="EcosystemWallet"/> class.
/// </summary>
/// <param name="ecosystemId">Your ecosystem ID (see thirdweb dashboard e.g. ecosystem.the-bonfire).</param>
/// <param name="ecosystemPartnerId">Your ecosystem partner ID (required if you are integrating someone else's ecosystem).</param>
/// <param name="client">The Thirdweb client instance.</param>
/// <param name="email">The email address for Email OTP authentication.</param>
/// <param name="phoneNumber">The phone number for Phone OTP authentication.</param>
/// <param name="authProvider">The authentication provider to use.</param>
/// <param name="storageDirectoryPath">The path to the storage directory.</param>
/// <param name="siweSigner">The SIWE signer wallet for SIWE authentication.</param>
/// <param name="legacyEncryptionKey">The encryption key that is no longer required but was used in the past. Only pass this if you had used custom auth before this was deprecated.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the created in-app wallet.</returns>
/// <exception cref="ArgumentException">Thrown when required parameters are not provided.</exception>
public static async Task<EcosystemWallet> Create(
ThirdwebClient client,
string ecosystemId,
Expand All @@ -67,7 +84,8 @@ public static async Task<EcosystemWallet> Create(
string phoneNumber = null,
AuthProvider authProvider = Thirdweb.AuthProvider.Default,
string storageDirectoryPath = null,
IThirdwebWallet siweSigner = null
IThirdwebWallet siweSigner = null,
string legacyEncryptionKey = null
)
{
if (client == null)
Expand Down Expand Up @@ -130,12 +148,18 @@ public static async Task<EcosystemWallet> Create(
try
{
var userAddress = await ResumeEnclaveSession(enclaveHttpClient, embeddedWallet, email, phoneNumber, authproviderStr).ConfigureAwait(false);
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { Address = userAddress };
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner, legacyEncryptionKey)
{
Address = userAddress
};
}
catch
{
enclaveHttpClient.RemoveHeader("Authorization");
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { Address = null };
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner, legacyEncryptionKey)
{
Address = null
};
}
}

Expand Down Expand Up @@ -175,13 +199,13 @@ private static void CreateEnclaveSession(EmbeddedWallet embeddedWallet, string a
embeddedWallet.UpdateSessionData(data);
}

private static async Task<EnclaveUserStatusResponse> GetUserStatus(IThirdwebHttpClient httpClient)
private static async Task<UserStatusResponse> GetUserStatus(IThirdwebHttpClient httpClient)
{
var url = $"{EMBEDDED_WALLET_PATH_2024}/accounts";
var response = await httpClient.GetAsync(url).ConfigureAwait(false);
_ = response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var userStatus = JsonConvert.DeserializeObject<EnclaveUserStatusResponse>(content);
var userStatus = JsonConvert.DeserializeObject<UserStatusResponse>(content);
return userStatus;
}

Expand Down Expand Up @@ -233,7 +257,9 @@ private async Task<string> PostAuth(Server.VerifyResult result)
private async Task<string> MigrateShardToEnclave(Server.VerifyResult authResult)
{
// TODO: For recovery code, allow old encryption keys as overrides to migrate sharded custom auth?
var (address, encryptedPrivateKeyB64, ivB64, kmsCiphertextB64) = await this.EmbeddedWallet.GenerateEncryptionDataAsync(authResult.AuthToken, authResult.RecoveryCode).ConfigureAwait(false);
var (address, encryptedPrivateKeyB64, ivB64, kmsCiphertextB64) = await this.EmbeddedWallet
.GenerateEncryptionDataAsync(authResult.AuthToken, this.LegacyEncryptionKey ?? authResult.RecoveryCode)
.ConfigureAwait(false);

var url = $"{ENCLAVE_PATH}/migrate";
var payload = new
Expand All @@ -260,7 +286,7 @@ private async Task<string> MigrateShardToEnclave(Server.VerifyResult authResult)
/// Gets the user details from the enclave wallet.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result contains the user details.</returns>
public async Task<EnclaveUserStatusResponse> GetUserDetails()
public async Task<UserStatusResponse> GetUserDetails()
{
return await GetUserStatus(this.HttpClient).ConfigureAwait(false);
}
Expand Down
14 changes: 9 additions & 5 deletions Thirdweb/Thirdweb.Wallets/InAppWallet/InAppWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ internal InAppWallet(
string phoneNumber,
string authProvider,
IThirdwebWallet siweSigner,
string address
string address,
string legacyEncryptionKey
)
: base(null, null, client, embeddedWallet, httpClient, email, phoneNumber, authProvider, siweSigner)
: base(null, null, client, embeddedWallet, httpClient, email, phoneNumber, authProvider, siweSigner, legacyEncryptionKey)
{
this.Address = address;
}
Expand All @@ -31,6 +32,7 @@ string address
/// <param name="authProvider">The authentication provider to use.</param>
/// <param name="storageDirectoryPath">The path to the storage directory.</param>
/// <param name="siweSigner">The SIWE signer wallet for SIWE authentication.</param>
/// <param name="legacyEncryptionKey">The encryption key that is no longer required but was used in the past. Only pass this if you had used custom auth before this was deprecated.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the created in-app wallet.</returns>
/// <exception cref="ArgumentException">Thrown when required parameters are not provided.</exception>
public static async Task<InAppWallet> Create(
Expand All @@ -39,11 +41,12 @@ public static async Task<InAppWallet> Create(
string phoneNumber = null,
AuthProvider authProvider = Thirdweb.AuthProvider.Default,
string storageDirectoryPath = null,
IThirdwebWallet siweSigner = null
IThirdwebWallet siweSigner = null,
string legacyEncryptionKey = null
)
{
storageDirectoryPath ??= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Thirdweb", "InAppWallet");
var ecoWallet = await Create(client, null, null, email, phoneNumber, authProvider, storageDirectoryPath, siweSigner);
var ecoWallet = await Create(client, null, null, email, phoneNumber, authProvider, storageDirectoryPath, siweSigner, legacyEncryptionKey);
return new InAppWallet(
ecoWallet.Client,
ecoWallet.EmbeddedWallet,
Expand All @@ -52,7 +55,8 @@ public static async Task<InAppWallet> Create(
ecoWallet.PhoneNumber,
ecoWallet.AuthProvider,
ecoWallet.SiweSigner,
ecoWallet.Address
ecoWallet.Address,
ecoWallet.LegacyEncryptionKey
);
}
}

0 comments on commit b6c60ca

Please sign in to comment.