Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #234 from fracek/timebound-improvements
Browse files Browse the repository at this point in the history
Add TimeSpan overloads to TimeBounds ctor
  • Loading branch information
elucidsoft committed Feb 27, 2020
2 parents fb49846 + 7b94914 commit fd32992
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
23 changes: 23 additions & 0 deletions stellar-dotnet-sdk-test/TimeBoundsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,28 @@ public void TestTimeBoundsWithDateTimeThrowsIfMaxTimeGreaterThanMinTime()

Assert.ThrowsException<ArgumentException>(() => new TimeBounds(now, yesterday));
}

[TestMethod]
public void TestTimeBoundsWithDuration()
{
var now = new DateTime(2018, 12, 01, 17, 30, 30);
var duration = TimeSpan.FromDays(2.0);
var timeBounds = new TimeBounds(now, duration);

Assert.AreEqual(1543685430, timeBounds.MinTime);
Assert.AreEqual(1543858230, timeBounds.MaxTime);
}

[TestMethod]
public void TestTimeBoundsWithDurationFromUtcNow()
{
var now = DateTimeOffset.UtcNow;
var duration = TimeSpan.FromDays(2.0);
var timeBounds = new TimeBounds(duration);
var maxDateTime = DateTimeOffset.FromUnixTimeSeconds(timeBounds.MaxTime);

Assert.AreNotEqual(0, timeBounds.MinTime);
Assert.IsTrue(maxDateTime > now);
}
}
}
17 changes: 17 additions & 0 deletions stellar-dotnet-sdk/TimeBounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ public TimeBounds(DateTimeOffset? minTime = null, DateTimeOffset? maxTime = null
_maxTime = (ulong) maxEpoch;
}

/// <summary>
/// Timebounds constructor.
/// </summary>
/// <param name="minTime">earliest time the transaction is valid from</param>
/// <param name="duration">duration window the transaction is valid for</param>
public TimeBounds(DateTimeOffset minTime, TimeSpan duration) : this(minTime, minTime.Add(duration))
{
}

/// <summary>
/// Timebounds constructor.
/// </summary>
/// <param name="duration">duration window the transaction is valid for from now</param>
public TimeBounds(TimeSpan duration) : this(DateTimeOffset.UtcNow, duration)
{
}

public static TimeBounds FromXdr(xdr.TimeBounds timeBounds)
{
if (timeBounds == null)
Expand Down
4 changes: 2 additions & 2 deletions stellar-dotnet-sdk/WebAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static class WebAuthentication

network = network ?? Network.Current;
var validFrom = now ?? DateTimeOffset.Now;
var validTo = validFrom.Add(timeout ?? TimeSpan.FromMinutes(5.0));
var validFor = timeout ?? TimeSpan.FromMinutes(5.0);

var sourceAccountKeypair = KeyPair.FromAccountId(clientAccountId);

Expand All @@ -56,7 +56,7 @@ public static class WebAuthentication
var manageDataKey = $"{anchorName} auth";
var manageDataValue = Encoding.UTF8.GetBytes(Convert.ToBase64String(nonce));

var timeBounds = new TimeBounds(validFrom, validTo);
var timeBounds = new TimeBounds(validFrom, validFor);

var operation = new ManageDataOperation.Builder(manageDataKey, manageDataValue)
.SetSourceAccount(sourceAccountKeypair)
Expand Down

0 comments on commit fd32992

Please sign in to comment.