16.2 C-Sharp

using System.Text;
using System.Security.Cryptography;
using System.Text.Json;

namespace GalaGames
{
    public class GalaGameAPI
    {
        private Uri baseAddress;
        private string gameId;
        private HMACSHA256 hmac;
        private SHA256 sha256;
        private HttpClient client = new HttpClient();
        private string host;

        public GalaGameAPI(string _url, string _gameId, string _hmacSecret)
        {
            baseAddress = new Uri(_url);
            host = baseAddress.Host.Trim();
            gameId = _gameId;
            hmac = new HMACSHA256(Encoding.UTF8.GetBytes(_hmacSecret));
            sha256 = SHA256.Create();
            client.Timeout = TimeSpan.FromSeconds(1000);
        }

        public async Task<HttpResponseMessage> GetUserProfile(string userId)
        {
            HttpResponseMessage response = await GetAsync($"/api/v1/users/{userId}").ConfigureAwait(false);
            return response;
        }

        // This is NOT sorted by collection, but rather gives you EVERYTHING back.
        public async Task<HttpResponseMessage> GetUserItemsV1Now(string userId)
        {
            HttpResponseMessage response = await GetAsync($"/api/v1/item/{userId}").ConfigureAwait(false);
            return response;
        }

        // This is limited to only your game's NFTs
        public async Task<HttpResponseMessage> GetUserItemsV2Now(string userId)
        {
            HttpResponseMessage response = await GetAsync($"/api/v2/items/{userId}").ConfigureAwait(false);
            return response;
        }

        // This is limited to only your game's NFTs
        public async Task<HttpResponseMessage> GetUserItemsV3Now(string userId)
        {
            HttpResponseMessage response = await GetAsync($"/api/v3/items/{userId}").ConfigureAwait(false);
            return response;
        }

        public async Task<HttpResponseMessage> PostAwardingGamePoints(string id, string token, int amount)
        {
            var payload = new
            {
                userId = id,
                gameToken = token,
                quantity = amount
            };

            HttpResponseMessage response = await PostAsync("/api/v2/game-points", JsonSerializer.Serialize(
                payload
            ));

            return response;
        }

        // Contact the Gala Game API with a request.
        private async Task<HttpResponseMessage> GetAsync(string path)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(baseAddress, path));

            // Date and indicate headers included in signature
            request.Headers.Add("x-auth-signedheaders", "date;host");
            request.Headers.Date = DateTime.UtcNow;

            // Format headers for inclusion in signature
            string date = new List<string>(request.Headers.GetValues("Date"))[0].Trim();

            // This is the hash of the request body (which is always "")
            string bodyHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

            // Format entire request in string format for signing
            StringBuilder crb = new StringBuilder(300);
            crb.AppendLine("GET");
            crb.AppendLine(path);
            crb.AppendLine();
            crb.Append("date:");
            crb.AppendLine(date);
            crb.Append("host:");
            crb.AppendLine(host);
            crb.AppendLine();
            crb.AppendLine("date;host");
            crb.Append(bodyHash);
            string canonicalRequest = crb.ToString().Replace("\r", "");

            // Calculate signature using our secret key
            string signature = ToHex(hmac.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest)));

            // Attach signature to request
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("HMAC", $"{gameId}:{signature}");


            HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
            return response;
        }

        // Contact the Gala Game API with a request.
        private async Task<HttpResponseMessage> PostAsync(string path, string body)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(baseAddress, path));

            // Date and indicate headers included in signature
            request.Headers.Add("x-auth-signedheaders", "content-type;date;host");
            request.Headers.Date = DateTime.UtcNow;
            request.Content = new StringContent(body, Encoding.UTF8, "application/json");

            // Format headers for inclusion in signature
            string date = new List<string>(request.Headers.GetValues("Date"))[0].Trim();

            // Hash the body of the request
            byte[] bodyBytes = request.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
            byte[] bodyHash = sha256.ComputeHash(bodyBytes);

            // Format entire request in string format for signing
            StringBuilder crb = new StringBuilder(300);
            crb.AppendLine("POST");
            crb.AppendLine(path);
            crb.AppendLine();
			crb.AppendLine("content-type:application/json; charset=utf-8");
            crb.Append("date:");
            crb.AppendLine(date);
            crb.Append("host:");
            crb.AppendLine(host);
            crb.AppendLine();
            crb.AppendLine("content-type;date;host");
            crb.Append(ToHex(bodyHash));
            string canonicalRequest = crb.ToString().Replace("\r", "");

            // Calculate signature using our secret key
            string signature = ToHex(hmac.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest)));

            // Attach signature to request
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("HMAC", $"{gameId}:{signature}");

            HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);


            return response;
        }

        // Table-driven byte[] to lowercase hex
        static private string[] sHexTable = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff" };
        private string ToHex(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 2);
            foreach (byte b in data)
                sb.Append(sHexTable[b]);
            return sb.ToString();
        }
    }


    class Program
    {
        static async Task Test()
        {
            // Stage URI: https://game-server-api-stage0.gala.games/
            // Production URI: https://game-server-api.gala.games/

            var api = new GalaGameAPI("stge_or_prod_uri", "your_game_id", "your_key");

            try
            {
                Console.WriteLine(await api.GetUserItemsV3Now("user_id_here"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


        }


        static void Main(string[] args)
        {
            Test().Wait();
        }

    }
}