Skip to content

Commit

Permalink
OF-264 Add ConstructWebhookEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviatkov committed Oct 20, 2023
1 parent d2a2ddb commit a2b119d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Openfort.SDK/Extensions/BitArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Openfort.SDK.Extensions
{
public static class BitArrayExtensions
{
public static string ToHex(this byte[] value)
{
return BitConverter.ToString(value).Replace("-", string.Empty); ;
}
}
}

15 changes: 15 additions & 0 deletions Openfort.SDK/Model/WebHookEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Openfort.SDK.Model
{
public class WebHookEvent
{
public WebHookEvent(TransactionIntent data, int date)
{
Data = data;
Date = date;
}

public TransactionIntent Data { get; set; }

public int Date { get; set; }
}
}
2 changes: 2 additions & 0 deletions Openfort.SDK/Openfort.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
<None Remove="Generated\" />
<None Remove="Model\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Remove="Extensions\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Wrapper\" />
<Folder Include="Generated\" />
<Folder Include="Model\" />
<Folder Include="Extensions\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
41 changes: 41 additions & 0 deletions Openfort.SDK/OpenfortClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using Openfort.SDK.Wrapper;
using Openfort.SDK.Model;
using Openfort.SDK.Extensions;
using System.Security.Cryptography;
using Newtonsoft.Json;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

namespace Openfort.SDK
{
Expand Down Expand Up @@ -116,5 +122,40 @@ public TransactionIntentsApiWrapper TransactionIntents
return transactionIntents;
}
}

public WebHookEvent? ConstructWebhookEvent(string body, string signature) {
var signedPayload = Sign(body);
if (!string.Equals(signedPayload, signature, StringComparison.OrdinalIgnoreCase)) {
throw new Exception("Invalid signature");
}
return JsonConvert.DeserializeObject<WebHookEvent>(body);
}

private byte[] signingKey;
private byte[] SigningKey
{
get
{
if (signingKey == null)
{
var splittedSecret = apiKey.Split("_");
if (splittedSecret.Length != 3)
{
throw new Exception("Invalid secret key");
}
var bareKey = splittedSecret[2].Replace("-", String.Empty);
signingKey = Encoding.UTF8.GetBytes(bareKey);
}
return signingKey;
}
}

private string Sign(string message)
{
var bytesArray = Encoding.UTF8.GetBytes(message);
var hmac = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, SigningKey);
hmac.AppendData(bytesArray);
return hmac.GetCurrentHash().ToHex();
}
}
}

0 comments on commit a2b119d

Please sign in to comment.