Skip to content

Commit 8dbee26

Browse files
authored
Merge pull request #268 from 0xsequence/Fix/fetchTimeShiftFromAPI
Fetch timeshift from Sequence API when creating the RPC manager. Use …
2 parents 1643ffc + daa7433 commit 8dbee26

2 files changed

Lines changed: 69 additions & 6 deletions

File tree

Plugins/SequencePlugin/Source/SequencePlugin/Private/SequenceRPCManager.cpp

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "SequenceAuthenticator.h"
66
#include "RequestHandler.h"
77
#include "ConfigFetcher.h"
8+
#include "HttpModule.h"
89
#include "Interfaces/IHttpResponse.h"
910
#include "Types/BinaryData.h"
1011
#include "Misc/Base64.h"
@@ -15,7 +16,7 @@
1516

1617
template<typename T> FString USequenceRPCManager::GenerateIntent(T Data, TOptional<int64> CurrentTime) const
1718
{
18-
const int64 Issued = CurrentTime.IsSet() ? CurrentTime.GetValue() : FDateTime::UtcNow().ToUnixTimestamp() - 30;
19+
const int64 Issued = CurrentTime.IsSet() ? CurrentTime.GetValue() : (FDateTime::UtcNow() + TimeShift).ToUnixTimestamp() - 30;
1920
const int64 Expires = Issued + 86400;
2021
FGenericData * LocalDataPtr = &Data;
2122
const FString Operation = LocalDataPtr->Operation;
@@ -269,26 +270,36 @@ void USequenceRPCManager::UpdateWithStoredSessionWallet()
269270

270271
USequenceRPCManager* USequenceRPCManager::Make(const bool UseStoredSessionId)
271272
{
273+
USequenceRPCManager* Manager = nullptr;
274+
272275
if (UseStoredSessionId)
273276
{
274-
const USequenceAuthenticator * Authenticator = NewObject<USequenceAuthenticator>();
277+
const USequenceAuthenticator* Authenticator = NewObject<USequenceAuthenticator>();
275278
if (FStoredCredentials_BE StoredCredentials = Authenticator->GetStoredCredentials(); StoredCredentials.GetValid())
276279
{
277-
return Make(StoredCredentials.GetCredentials().GetSessionWallet());
280+
Manager = Make(StoredCredentials.GetCredentials().GetSessionWallet());
278281
}
279282
}
280-
return Make(UCryptoWallet::Make());
283+
284+
if (!Manager)
285+
{
286+
Manager = Make(UCryptoWallet::Make());
287+
}
288+
289+
return Manager;
281290
}
282291

283292
USequenceRPCManager* USequenceRPCManager::Make(UCryptoWallet* SessionWalletIn)
284293
{
285-
USequenceRPCManager * SequenceRPCManager = NewObject<USequenceRPCManager>();
294+
USequenceRPCManager* SequenceRPCManager = NewObject<USequenceRPCManager>();
286295
SequenceRPCManager->SessionWallet = SessionWalletIn;
287296
SequenceRPCManager->Validator = NewObject<UResponseSignatureValidator>();
288297
FString ParsedJwt;
289-
FBase64::Decode(UConfigFetcher::GetConfigVar(UConfigFetcher::WaaSConfigKey),ParsedJwt);
298+
FBase64::Decode(UConfigFetcher::GetConfigVar(UConfigFetcher::WaaSConfigKey), ParsedJwt);
290299
SequenceRPCManager->WaaSSettings = USequenceSupport::JSONStringToStruct<FWaasJWT>(ParsedJwt);
291300
SequenceRPCManager->Cached_ProjectAccessKey = UConfigFetcher::GetConfigVar(UConfigFetcher::ProjectAccessKey);
301+
302+
SequenceRPCManager->InitializeTimeShift();
292303
return SequenceRPCManager;
293304
}
294305

@@ -1088,3 +1099,50 @@ void USequenceRPCManager::FederateSessionInUse(const FString& WalletIn, const TF
10881099
return this->BuildFederateAccountIntent(FederateAccountData, CurrentTime);
10891100
}, OnFederateResponse, OnFailure);
10901101
}
1102+
1103+
void USequenceRPCManager::InitializeTimeShift()
1104+
{
1105+
const FString WaasUrl = this->WaaSSettings.GetRPCServer();
1106+
const FString StatusUrl = WaasUrl.EndsWith(TEXT("/")) ? WaasUrl + TEXT("status") : WaasUrl + TEXT("/status");
1107+
1108+
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
1109+
HttpRequest->SetVerb(TEXT("GET"));
1110+
HttpRequest->SetURL(StatusUrl);
1111+
1112+
HttpRequest->OnProcessRequestComplete().BindLambda([this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess)
1113+
{
1114+
if (bSuccess && Response.IsValid())
1115+
{
1116+
const FString DateHeader = Response->GetHeader(TEXT("date"));
1117+
if (!DateHeader.IsEmpty())
1118+
{
1119+
TimeShift = GetTimeShiftFromResponse(DateHeader);
1120+
}
1121+
else
1122+
{
1123+
UE_LOG(LogTemp, Warning, TEXT("No date header in response from status endpoint"));
1124+
TimeShift = FTimespan::Zero();
1125+
}
1126+
}
1127+
else
1128+
{
1129+
UE_LOG(LogTemp, Warning, TEXT("Failed to get server time for time shift calculation"));
1130+
TimeShift = FTimespan::Zero();
1131+
}
1132+
});
1133+
1134+
HttpRequest->ProcessRequest();
1135+
}
1136+
1137+
FTimespan USequenceRPCManager::GetTimeShiftFromResponse(const FString& DateHeader)
1138+
{
1139+
FDateTime ServerTime;
1140+
if (FDateTime::ParseHttpDate(DateHeader, ServerTime))
1141+
{
1142+
const FDateTime LocalTime = FDateTime::UtcNow();
1143+
return ServerTime - LocalTime;
1144+
}
1145+
1146+
UE_LOG(LogTemp, Warning, TEXT("Failed to parse server time from date header: %s"), *DateHeader);
1147+
return FTimespan::Zero();
1148+
}

Plugins/SequencePlugin/Source/SequencePlugin/Private/SequenceRPCManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ class SEQUENCEPLUGIN_API USequenceRPCManager : public UObject
119119
*/
120120
void UpdateWithStoredSessionWallet();
121121

122+
FTimespan TimeShift;
123+
124+
void InitializeTimeShift();
125+
static FTimespan GetTimeShiftFromResponse(const FString& DateHeader);
126+
122127
public:
123128

124129
UResponseSignatureValidator* Validator;

0 commit comments

Comments
 (0)