Skip to content

Commit cac4184

Browse files
authored
Add explicit PrivateKeyWallet save/load/export (#62)
1 parent ef28f85 commit cac4184

File tree

3 files changed

+181
-112
lines changed

3 files changed

+181
-112
lines changed

Thirdweb.Tests/Thirdweb.Wallets/Thirdweb.PrivateKeyWallet.Tests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,70 @@ public async Task ExecuteTransaction_InvalidOperation()
364364
};
365365
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => account.ExecuteTransaction(transaction));
366366
}
367+
368+
[Fact(Timeout = 120000)]
369+
public async Task LoadOrGenerate_LoadsExistingWallet()
370+
{
371+
// Generate and save a wallet to simulate an existing wallet file
372+
var wallet = await PrivateKeyWallet.Generate(this.Client);
373+
await wallet.Save();
374+
375+
var loadedWallet = await PrivateKeyWallet.LoadOrGenerate(this.Client);
376+
377+
Assert.NotNull(loadedWallet);
378+
Assert.Equal(await wallet.Export(), await loadedWallet.Export());
379+
Assert.Equal(await wallet.GetAddress(), await loadedWallet.GetAddress());
380+
381+
// Clean up
382+
var path = PrivateKeyWallet.GetSavePath();
383+
if (File.Exists(path))
384+
{
385+
File.Delete(path);
386+
}
387+
}
388+
389+
[Fact(Timeout = 120000)]
390+
public async Task LoadOrGenerate_GeneratesNewWalletIfNoExistingWallet()
391+
{
392+
var path = PrivateKeyWallet.GetSavePath();
393+
394+
if (File.Exists(path))
395+
{
396+
File.Delete(path);
397+
}
398+
399+
var wallet = await PrivateKeyWallet.LoadOrGenerate(this.Client);
400+
401+
Assert.NotNull(wallet);
402+
Assert.NotNull(await wallet.Export());
403+
Assert.False(File.Exists(path));
404+
}
405+
406+
[Fact(Timeout = 120000)]
407+
public async Task Save_SavesPrivateKeyToFile()
408+
{
409+
var wallet = await PrivateKeyWallet.Generate(this.Client);
410+
411+
await wallet.Save();
412+
413+
var path = PrivateKeyWallet.GetSavePath();
414+
Assert.True(File.Exists(path));
415+
416+
var savedPrivateKey = await File.ReadAllTextAsync(path);
417+
Assert.Equal(await wallet.Export(), savedPrivateKey);
418+
419+
// Clean up
420+
File.Delete(path);
421+
}
422+
423+
[Fact(Timeout = 120000)]
424+
public async Task Export_ReturnsPrivateKey()
425+
{
426+
var wallet = await PrivateKeyWallet.Generate(this.Client);
427+
428+
var privateKey = await wallet.Export();
429+
430+
Assert.NotNull(privateKey);
431+
Assert.Equal(privateKey, await wallet.Export());
432+
}
367433
}

Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet.Storage/LocalStorage.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@ internal abstract class LocalStorageBase
1212

1313
internal partial class LocalStorage : LocalStorageBase
1414
{
15-
internal override DataStorage Data => this.storage.Data;
16-
private readonly Storage storage;
17-
private readonly string filePath;
15+
internal override DataStorage Data => this._storage.Data;
16+
private readonly Storage _storage;
17+
private readonly string _filePath;
1818

1919
internal LocalStorage(string clientId, string storageDirectoryPath = null)
2020
{
2121
string directory;
2222
directory = storageDirectoryPath ?? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
2323
directory = Path.Combine(directory, "Thirdweb", "InAppWallet");
2424
_ = Directory.CreateDirectory(directory);
25-
this.filePath = Path.Combine(directory, $"{clientId}.txt");
25+
this._filePath = Path.Combine(directory, $"{clientId}.txt");
2626
try
2727
{
28-
var json = File.ReadAllBytes(this.filePath);
28+
var json = File.ReadAllBytes(this._filePath);
2929
DataContractJsonSerializer serializer = new(typeof(Storage));
3030
MemoryStream fin = new(json);
31-
this.storage = (Storage)serializer.ReadObject(fin);
31+
this._storage = (Storage)serializer.ReadObject(fin);
3232
}
3333
catch (Exception)
3434
{
35-
this.storage = new Storage();
35+
this._storage = new Storage();
3636
}
3737
}
3838

3939
internal override Task RemoveAuthTokenAsync()
4040
{
4141
return this.UpdateDataAsync(() =>
4242
{
43-
if (this.storage.Data?.AuthToken != null)
43+
if (this._storage.Data?.AuthToken != null)
4444
{
45-
this.storage.Data.ClearAuthToken();
45+
this._storage.Data.ClearAuthToken();
4646
return true;
4747
}
4848
return false;
@@ -55,8 +55,8 @@ private async Task<bool> UpdateDataAsync(Func<bool> fn)
5555
{
5656
DataContractJsonSerializer serializer = new(typeof(Storage));
5757
MemoryStream fout = new();
58-
serializer.WriteObject(fout, this.storage);
59-
await File.WriteAllBytesAsync(this.filePath, fout.ToArray()).ConfigureAwait(false);
58+
serializer.WriteObject(fout, this._storage);
59+
await File.WriteAllBytesAsync(this._filePath, fout.ToArray()).ConfigureAwait(false);
6060
return true;
6161
}
6262
return false;
@@ -66,7 +66,7 @@ internal override Task SaveDataAsync(DataStorage data)
6666
{
6767
return this.UpdateDataAsync(() =>
6868
{
69-
this.storage.Data = data;
69+
this._storage.Data = data;
7070
return true;
7171
});
7272
}

0 commit comments

Comments
 (0)