Skip to content

Commit

Permalink
Apply a timeshift from the server when initializing IntentSender to b…
Browse files Browse the repository at this point in the history
…etter resolve time out of sync error (#219)
  • Loading branch information
BellringerQuinn authored Dec 17, 2024
1 parent 31eb2ac commit d882f9c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Assets/SequenceSDK/WaaS/Tests/IntentSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public async Task<T2> SendRequest<T, T2>(string path, T args, Dictionary<string,
string responseJson = JsonConvert.SerializeObject(response);
return JsonConvert.DeserializeObject<T2>(responseJson);
}

public async Task<TimeSpan> GetTimeShift()
{
return TimeSpan.Zero;
}
}

private class MockHttpClientReturnsUnknownCode : IHttpClient
Expand All @@ -177,6 +182,11 @@ public async Task<T2> SendRequest<T, T2>(string path, T args, Dictionary<string,
string responseJson = JsonConvert.SerializeObject(response);
return JsonConvert.DeserializeObject<T2>(responseJson);
}

public async Task<TimeSpan> GetTimeShift()
{
return TimeSpan.Zero;
}
}

private class MockHttpClientReturnsSuccessfulTransaction : IHttpClient
Expand All @@ -190,6 +200,11 @@ public async Task<T2> SendRequest<T, T2>(string path, T args, Dictionary<string,
string responseJson = JsonConvert.SerializeObject(response);
return JsonConvert.DeserializeObject<T2>(responseJson);
}

public async Task<TimeSpan> GetTimeShift()
{
return TimeSpan.Zero;
}
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace Sequence.EmbeddedWallet
public class IntentPayload
{
public JObject data;
public uint expiresAt;
public uint issuedAt;
public ulong expiresAt;
public ulong issuedAt;
public string name;
public Signature[] signatures;
public string version;

[Preserve]
[JsonConstructor]
public IntentPayload(string version, string name, uint expiresAt, uint issuedAt, JObject data, Signature[] signatures)
public IntentPayload(string version, string name, ulong expiresAt, ulong issuedAt, JObject data, Signature[] signatures)
{
this.version = version;
this.name = name;
Expand All @@ -29,7 +29,7 @@ public IntentPayload(string version, string name, uint expiresAt, uint issuedAt,
this.signatures = signatures;
}

public IntentPayload(string version, IntentType name, uint expiresAt, uint issuedAt, JObject data, Signature[] signatures)
public IntentPayload(string version, IntentType name, ulong expiresAt, ulong issuedAt, JObject data, Signature[] signatures)
{
this.version = version;
this.name = IntentNames[name];
Expand All @@ -39,7 +39,7 @@ public IntentPayload(string version, IntentType name, uint expiresAt, uint issue
this.signatures = signatures;
}

public IntentPayload(string version, IntentType type, JObject data, Signature[] signatures, uint timeBeforeExpiryInSeconds = 30, uint currentTime = 0)
public IntentPayload(string version, IntentType type, JObject data, Signature[] signatures, uint timeBeforeExpiryInSeconds = 30, ulong currentTime = 0)
{
this.version = version;
this.name = IntentNames[type];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ public async Task<T2> SendRequest<T, T2>(string path, T args, [CanBeNull] Dictio
}
}

public async Task<TimeSpan> GetTimeShift()
{
UnityWebRequest request = UnityWebRequest.Get(_waasUrl.AppendTrailingSlashIfNeeded() + "status");
request.method = UnityWebRequest.kHttpVerbGET;

try
{
await request.SendWebRequest();
DateTime serverTime = DateTime.Parse(request.GetResponseHeader("date")).ToUniversalTime();
DateTime localTime = DateTime.UtcNow;
TimeSpan timeShift = serverTime - localTime;
return timeShift;
}
catch (Exception e)
{
Debug.LogError("Error getting time shift: " + e.Message);
return TimeSpan.Zero;
}
finally
{
request.Dispose();
}
}

private string GetRequestErrorIfAvailable(UnityWebRequest request)
{
if (request.downloadHandler != null && request.downloadHandler.data != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand All @@ -8,5 +9,7 @@ public interface IHttpClient
{
public Task<T2> SendRequest<T, T2>(string path, T args,
[CanBeNull] Dictionary<string, string> headers = null, string overrideUrl = null);

public Task<TimeSpan> GetTimeShift();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class IntentSender : IIntentSender
private int _waasProjectId;
private string _waasVersion;
private string _sessionId;
private TimeSpan _timeshift;
private bool _ready = false;

private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
Expand All @@ -33,6 +35,13 @@ public IntentSender(IHttpClient httpClient, Sequence.Wallet.IWallet sessionWalle
_waasProjectId = waasProjectId;
_waasVersion = waasVersion;
_sessionId = IntentDataOpenSession.CreateSessionId(_sessionWallet.GetAddress());
GetTimeShift().ConfigureAwait(false);
}

private async Task GetTimeShift()
{
_timeshift = await _httpClient.GetTimeShift();
_ready = true;
}

public async Task<T> SendIntent<T, T2>(T2 args, IntentType type, uint timeBeforeExpiryInSeconds = 30, uint currentTime = 0)
Expand Down Expand Up @@ -68,11 +77,11 @@ public async Task<T> SendIntent<T, T2>(T2 args, IntentType type, uint timeBefore
long currentTimeAccordingToIntent = 0;
if (intentPayload is IntentPayload intent)
{
currentTimeAccordingToIntent = intent.issuedAt;
currentTimeAccordingToIntent = (long)intent.issuedAt;
}
else if (intentPayload is RegisterSessionIntent registerSessionIntent)
{
currentTimeAccordingToIntent = registerSessionIntent.intent.issuedAt;
currentTimeAccordingToIntent = (long)registerSessionIntent.intent.issuedAt;
}
else
{
Expand Down Expand Up @@ -116,8 +125,17 @@ private string AssemblePayloadJson<T>(T args)
return JsonConvert.SerializeObject(args, serializerSettings);
}

private async Task<object> AssembleIntentPayload(string payload, IntentType type, uint timeToLiveInSeconds, uint currentTime)
private async Task<object> AssembleIntentPayload(string payload, IntentType type, uint timeToLiveInSeconds, ulong currentTime)
{
while (!_ready)
{
await Task.Yield();
}

if (currentTime == 0)
{
currentTime = (ulong)DateTimeOffset.UtcNow.Add(_timeshift).ToUnixTimeSeconds();
}
JObject packet = JsonConvert.DeserializeObject<JObject>(payload);
IntentPayload toSign = new IntentPayload(_waasVersion, type, packet, null, timeToLiveInSeconds, currentTime);
string toSignJson = JsonConvert.SerializeObject(toSign, serializerSettings);
Expand Down

0 comments on commit d882f9c

Please sign in to comment.