Skip to content

Commit

Permalink
Fix ID related things (#1350)
Browse files Browse the repository at this point in the history
* Fix ID fields to be null when null

* Don't hash listing or retainer IDs
  • Loading branch information
karashiiro authored Jul 26, 2024
1 parent 29d0df6 commit c9c20f7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
23 changes: 7 additions & 16 deletions src/Universalis.Application/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static ListingView ListingToView(Listing l)
var ppu = l.PricePerUnit;
var listingView = new ListingView
{
ListingIdHash = l.ListingId,
RetainerIdHash = l.RetainerId,
Hq = l.Hq,
OnMannequin = l.OnMannequin,
Materia = l.Materia?
Expand All @@ -67,27 +69,14 @@ public static ListingView ListingToView(Listing l)
Total = ppu * l.Quantity,
DyeId = l.DyeId,
CreatorName = l.CreatorName ?? "",
CreatorIdHash = null,
IsCrafted = !string.IsNullOrEmpty(l.CreatorId),
SellerIdHash = null,
LastReviewTimeUnixSeconds = new DateTimeOffset(l.LastReviewTime).ToUnixTimeSeconds(),
RetainerName = l.RetainerName,
RetainerCityId = l.RetainerCityId,
};

using var sha256 = SHA256.Create();

if (!string.IsNullOrEmpty(l.CreatorId))
{
listingView.CreatorIdHash = Hash(sha256, l.CreatorId);
}

if (!string.IsNullOrEmpty(l.ListingId))
{
listingView.ListingIdHash = Hash(sha256, l.ListingId);
}

listingView.SellerIdHash = Hash(sha256, l.SellerId);
listingView.RetainerIdHash = Hash(sha256, l.RetainerId);

return listingView;
}

Expand All @@ -99,8 +88,10 @@ public static ListingView ListingToView(Listing l)
/// <returns>A hash representing the input string.</returns>
public static string Hash(HashAlgorithm hasher, string input)
{
if (input is null)
return null;
Span<byte> hash = stackalloc byte[hasher.HashSize / 8];
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input ?? "");
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input);
if (hasher.TryComputeHash(bytes, hash, out _)) // Since we stackalloc the hash buffer, written is not needed
return Convert.ToHexString(hash).ToLowerInvariant(); // https://github.com/dotnet/runtime/issues/60393
throw new InvalidOperationException("Destination buffer was too small, this should never occur");
Expand Down
4 changes: 2 additions & 2 deletions src/Universalis.Application/Views/V1/ListingView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class ListingView : PartiallySerializable, IPriceable, ICopyable
public bool IsCrafted { get; init; }

/// <summary>
/// A SHA256 hash of the ID of this listing. Due to some current client-side bugs, this will almost always be null.
/// The ID of this listing.
/// </summary>
[BsonElement("listingID")]
[JsonPropertyName("listingID")]
Expand Down Expand Up @@ -122,7 +122,7 @@ public class ListingView : PartiallySerializable, IPriceable, ICopyable
public int RetainerCityId { get; init; }

/// <summary>
/// A SHA256 hash of the retainer's ID.
/// The retainer's ID.
/// </summary>
[BsonElement("retainerID")]
[JsonPropertyName("retainerID")]
Expand Down

0 comments on commit c9c20f7

Please sign in to comment.