Skip to content

Commit b6c60ca

Browse files
committed
Add custom auth migration
1 parent 92dc138 commit b6c60ca

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Thirdweb;
55

66
public partial class EcosystemWallet
77
{
8-
public class EnclaveUserStatusResponse
8+
public class UserStatusResponse
99
{
1010
[JsonProperty("linkedAccounts")]
1111
internal List<LinkedAccount> LinkedAccounts { get; set; }

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public partial class EcosystemWallet : IThirdwebWallet
2323
internal readonly string Email;
2424
internal readonly string PhoneNumber;
2525
internal readonly string AuthProvider;
26+
internal readonly string LegacyEncryptionKey;
2627

2728
internal string Address;
2829

@@ -43,12 +44,14 @@ internal EcosystemWallet(
4344
string email,
4445
string phoneNumber,
4546
string authProvider,
46-
IThirdwebWallet siweSigner
47+
IThirdwebWallet siweSigner,
48+
string legacyEncryptionKey
4749
)
4850
{
4951
this.Client = client;
5052
this._ecosystemId = ecosystemId;
5153
this._ecosystemPartnerId = ecosystemPartnerId;
54+
this.LegacyEncryptionKey = legacyEncryptionKey;
5255
this.EmbeddedWallet = embeddedWallet;
5356
this.HttpClient = httpClient;
5457
this.Email = email;
@@ -59,6 +62,20 @@ IThirdwebWallet siweSigner
5962

6063
#region Creation
6164

65+
/// <summary>
66+
/// Creates a new instance of the <see cref="EcosystemWallet"/> class.
67+
/// </summary>
68+
/// <param name="ecosystemId">Your ecosystem ID (see thirdweb dashboard e.g. ecosystem.the-bonfire).</param>
69+
/// <param name="ecosystemPartnerId">Your ecosystem partner ID (required if you are integrating someone else's ecosystem).</param>
70+
/// <param name="client">The Thirdweb client instance.</param>
71+
/// <param name="email">The email address for Email OTP authentication.</param>
72+
/// <param name="phoneNumber">The phone number for Phone OTP authentication.</param>
73+
/// <param name="authProvider">The authentication provider to use.</param>
74+
/// <param name="storageDirectoryPath">The path to the storage directory.</param>
75+
/// <param name="siweSigner">The SIWE signer wallet for SIWE authentication.</param>
76+
/// <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>
77+
/// <returns>A task that represents the asynchronous operation. The task result contains the created in-app wallet.</returns>
78+
/// <exception cref="ArgumentException">Thrown when required parameters are not provided.</exception>
6279
public static async Task<EcosystemWallet> Create(
6380
ThirdwebClient client,
6481
string ecosystemId,
@@ -67,7 +84,8 @@ public static async Task<EcosystemWallet> Create(
6784
string phoneNumber = null,
6885
AuthProvider authProvider = Thirdweb.AuthProvider.Default,
6986
string storageDirectoryPath = null,
70-
IThirdwebWallet siweSigner = null
87+
IThirdwebWallet siweSigner = null,
88+
string legacyEncryptionKey = null
7189
)
7290
{
7391
if (client == null)
@@ -130,12 +148,18 @@ public static async Task<EcosystemWallet> Create(
130148
try
131149
{
132150
var userAddress = await ResumeEnclaveSession(enclaveHttpClient, embeddedWallet, email, phoneNumber, authproviderStr).ConfigureAwait(false);
133-
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { Address = userAddress };
151+
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner, legacyEncryptionKey)
152+
{
153+
Address = userAddress
154+
};
134155
}
135156
catch
136157
{
137158
enclaveHttpClient.RemoveHeader("Authorization");
138-
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { Address = null };
159+
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner, legacyEncryptionKey)
160+
{
161+
Address = null
162+
};
139163
}
140164
}
141165

@@ -175,13 +199,13 @@ private static void CreateEnclaveSession(EmbeddedWallet embeddedWallet, string a
175199
embeddedWallet.UpdateSessionData(data);
176200
}
177201

178-
private static async Task<EnclaveUserStatusResponse> GetUserStatus(IThirdwebHttpClient httpClient)
202+
private static async Task<UserStatusResponse> GetUserStatus(IThirdwebHttpClient httpClient)
179203
{
180204
var url = $"{EMBEDDED_WALLET_PATH_2024}/accounts";
181205
var response = await httpClient.GetAsync(url).ConfigureAwait(false);
182206
_ = response.EnsureSuccessStatusCode();
183207
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
184-
var userStatus = JsonConvert.DeserializeObject<EnclaveUserStatusResponse>(content);
208+
var userStatus = JsonConvert.DeserializeObject<UserStatusResponse>(content);
185209
return userStatus;
186210
}
187211

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

238264
var url = $"{ENCLAVE_PATH}/migrate";
239265
var payload = new
@@ -260,7 +286,7 @@ private async Task<string> MigrateShardToEnclave(Server.VerifyResult authResult)
260286
/// Gets the user details from the enclave wallet.
261287
/// </summary>
262288
/// <returns>A task that represents the asynchronous operation. The task result contains the user details.</returns>
263-
public async Task<EnclaveUserStatusResponse> GetUserDetails()
289+
public async Task<UserStatusResponse> GetUserDetails()
264290
{
265291
return await GetUserStatus(this.HttpClient).ConfigureAwait(false);
266292
}

Thirdweb/Thirdweb.Wallets/InAppWallet/InAppWallet.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ internal InAppWallet(
1515
string phoneNumber,
1616
string authProvider,
1717
IThirdwebWallet siweSigner,
18-
string address
18+
string address,
19+
string legacyEncryptionKey
1920
)
20-
: base(null, null, client, embeddedWallet, httpClient, email, phoneNumber, authProvider, siweSigner)
21+
: base(null, null, client, embeddedWallet, httpClient, email, phoneNumber, authProvider, siweSigner, legacyEncryptionKey)
2122
{
2223
this.Address = address;
2324
}
@@ -31,6 +32,7 @@ string address
3132
/// <param name="authProvider">The authentication provider to use.</param>
3233
/// <param name="storageDirectoryPath">The path to the storage directory.</param>
3334
/// <param name="siweSigner">The SIWE signer wallet for SIWE authentication.</param>
35+
/// <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>
3436
/// <returns>A task that represents the asynchronous operation. The task result contains the created in-app wallet.</returns>
3537
/// <exception cref="ArgumentException">Thrown when required parameters are not provided.</exception>
3638
public static async Task<InAppWallet> Create(
@@ -39,11 +41,12 @@ public static async Task<InAppWallet> Create(
3941
string phoneNumber = null,
4042
AuthProvider authProvider = Thirdweb.AuthProvider.Default,
4143
string storageDirectoryPath = null,
42-
IThirdwebWallet siweSigner = null
44+
IThirdwebWallet siweSigner = null,
45+
string legacyEncryptionKey = null
4346
)
4447
{
4548
storageDirectoryPath ??= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Thirdweb", "InAppWallet");
46-
var ecoWallet = await Create(client, null, null, email, phoneNumber, authProvider, storageDirectoryPath, siweSigner);
49+
var ecoWallet = await Create(client, null, null, email, phoneNumber, authProvider, storageDirectoryPath, siweSigner, legacyEncryptionKey);
4750
return new InAppWallet(
4851
ecoWallet.Client,
4952
ecoWallet.EmbeddedWallet,
@@ -52,7 +55,8 @@ public static async Task<InAppWallet> Create(
5255
ecoWallet.PhoneNumber,
5356
ecoWallet.AuthProvider,
5457
ecoWallet.SiweSigner,
55-
ecoWallet.Address
58+
ecoWallet.Address,
59+
ecoWallet.LegacyEncryptionKey
5660
);
5761
}
5862
}

0 commit comments

Comments
 (0)