Skip to content

Commit

Permalink
replace references to deprecated fields on user object (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian authored Oct 25, 2024
1 parent 386aa2e commit 3e91425
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Assets/Treasure/TDK/Runtime/API/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct LogInResponse
[Serializable]
public struct User
{
public struct Signer
public struct Session
{
public bool isAdmin;
public string signer;
Expand All @@ -67,9 +67,9 @@ public struct Signer
}

public string id;
public string smartAccountAddress;
public string address;
public string email;
public List<Signer> allActiveSigners;
public List<Session> sessions;
}

public partial class API
Expand Down
36 changes: 18 additions & 18 deletions Assets/Treasure/TDK/Runtime/Identity/TDK.Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ await TDKServiceLocator.GetService<TDKThirdwebService>().ActiveWallet.CreateSess
);
}

private async Task<List<User.Signer>> GetActiveSigners()
private async Task<List<User.Session>> GetUserSessions()
{
var thirdwebService = TDKServiceLocator.GetService<TDKThirdwebService>();
var activeSignersTask = thirdwebService.ActiveWallet.GetAllActiveSigners();
var allAdminsTask = thirdwebService.ActiveWallet.GetAllAdmins();
await Task.WhenAll(activeSignersTask, allAdminsTask);
var activeSigners = activeSignersTask.Result;
var allAdmins = allAdminsTask.Result;
return activeSigners.Select(activeSigner => new User.Signer
return activeSigners.Select(activeSigner => new User.Session
{
isAdmin = allAdmins.Contains(activeSigner.Signer),
signer = activeSigner.Signer,
Expand All @@ -117,29 +117,29 @@ await TDKServiceLocator.GetService<TDKThirdwebService>().ActiveWallet.CreateSess
}).ToList();
}

private bool ValidateActiveSigner(string backendWallet, List<string> callTargets, BigInteger nativeTokenLimitPerTransaction, User.Signer signer)
private bool ValidateSession(string backendWallet, List<string> callTargets, BigInteger nativeTokenLimitPerTransaction, User.Session session)
{
var requestedCallTargets = callTargets.Select(callTarget => callTarget.ToLowerInvariant());
var signerApprovedTargets = signer.approvedTargets.Select(approvedTarget => approvedTarget.ToLowerInvariant());
var signerApprovedTargets = session.approvedTargets.Select(approvedTarget => approvedTarget.ToLowerInvariant());
var nowDate = Utils.GetUnixTimeStampNow();
var minEndDate = Utils.GetUnixTimeStampNow() + TDK.AppConfig.SessionMinDurationLeftSec;
var maxEndDate = Utils.GetUnixTimeStampIn10Years();
return
// Expected backend wallet is signer
signer.signer.ToLowerInvariant() == backendWallet.ToLowerInvariant() &&
session.signer.ToLowerInvariant() == backendWallet.ToLowerInvariant() &&
// If this signer is an admin, they always have the required permissions
(signer.isAdmin || (
(session.isAdmin || (
// Start date has passed
signer.startTimestamp < nowDate &&
session.startTimestamp < nowDate &&
// Expiration date meets minimum time requirements
signer.endTimestamp >= minEndDate &&
session.endTimestamp >= minEndDate &&
// Expiration date is not too far in the future (10 years because Thirdweb uses this for admins)
// This check is to prevent sessions from being created with timestamps in milliseconds
signer.endTimestamp <= maxEndDate &&
session.endTimestamp <= maxEndDate &&
// All requested targets are approved
requestedCallTargets.All(callTarget => signerApprovedTargets.Contains(callTarget)) &&
// Native token limit per transaction is approved
signer.nativeTokenLimitPerTransaction >= nativeTokenLimitPerTransaction
session.nativeTokenLimitPerTransaction >= nativeTokenLimitPerTransaction
));
}

Expand Down Expand Up @@ -186,9 +186,9 @@ private async Task StartLauncherSessionRequest()
var nativeTokenLimitPerTransaction = TDK.AppConfig.GetNativeTokenLimitPerTransaction();

// Check if any active signers match the call targets
var hasActiveSession = user.allActiveSigners.Any((signer) =>
var hasActiveSession = user.sessions.Any((signer) =>
{
return ValidateActiveSigner(
return ValidateSession(
backendWallet,
callTargets,
nativeTokenLimitPerTransaction,
Expand All @@ -203,7 +203,7 @@ private async Task StartLauncherSessionRequest()
}
}

_address = user.smartAccountAddress;
_address = user.address;
_authToken = authToken;

TDKLogger.Log("Existing user session is valid");
Expand Down Expand Up @@ -280,10 +280,10 @@ public async Task<string> StartUserSession(ChainId sessionChainId = ChainId.Unkn
if (!didCreateSession && requiresSession)
{
var hasActiveSession = false;
List<User.Signer> activeSigners = null;
List<User.Session> sessions = null;
try
{
activeSigners = await GetActiveSigners();
sessions = await GetUserSessions();
}
catch (Exception e)
{
Expand All @@ -292,12 +292,12 @@ public async Task<string> StartUserSession(ChainId sessionChainId = ChainId.Unkn
TDKLogger.LogError($"Error fetching active signers: {e}");
}

if (activeSigners != null && activeSigners.Count > 0)
if (sessions != null && sessions.Count > 0)
{
// Check if any active signers match the call targets
hasActiveSession = activeSigners.Any((signer) =>
hasActiveSession = sessions.Any((signer) =>
{
return ValidateActiveSigner(
return ValidateSession(
backendWallet,
callTargets,
nativeTokenLimitPerTransaction,
Expand Down
4 changes: 2 additions & 2 deletions Assets/Treasure/TDK/Runtime/Utils/TreasureLauncherUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Treasure
public class TreasureLauncherUtils
{
private static bool hasParsedCommandLineArgs = false;

private static string tdkAuthToken = null;

public static string GetLauncherAuthToken()
Expand All @@ -21,7 +21,7 @@ public static string GetWalletAddressFromJwt()
// TODO check token expiration date?
var content = GetLauncherAuthToken().Split('.')[1];
var jsonPayload = Decode(content);
return JsonConvert.DeserializeObject<JToken>(jsonPayload)["ctx"]["smartAccountAddress"].ToString();
return JsonConvert.DeserializeObject<JToken>(jsonPayload)["ctx"]["address"].ToString();
}

private static void ParseArgs()
Expand Down

0 comments on commit 3e91425

Please sign in to comment.