Add files via upload

This commit is contained in:
Dima YaFlay
2023-10-16 18:24:17 +03:00
committed by GitHub
parent b6655c1b9e
commit a48ccdf95b
4 changed files with 177 additions and 89 deletions

9
src/Types/PaymentData.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace spworlds.Types;
public class PaymentData
{
public int Amount;
public string RedirectUrl;
public string WebHookUrl;
public string Data;
}

13
src/Types/SkinPart.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace spworlds.Types;
public enum SkinPart
{
face;
front;
front_full;
head;
bust;
full;
skin;
}

23
src/Types/User.cs Normal file
View File

@@ -0,0 +1,23 @@
using spworlds;
using System.Text.Json.Nodes;
namespace spworlds.Types;
public class User
{
private string Name { get; }
private HttpClient client = new();
private string nonSerializedUuid = await client.GetStringAsync($"https://api.mojang.com/users/profiles/minecraft/{Name}");
private JsonNode uuid = JsonNode.Parse(nonSerializedUuid);
private string Uuid { get; } = uuid["id"]
string nonSerializedProfileId = await client.GetStringAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{Uuid}");
private JsonNode profile = JsonNode.Parse(nonSerializedProfileId);
public async Task<string> GetSkinPart(SkinPart skinPart, string size)
{
return (string)$"https://visage.surgeplay.com/{skinPart}/{size}/{this.profile["profileId"]}"
}
public string GetName() => this.Name;
public string GetUuid() => this.Uuid;
public JsonNode GetProfile() => return this.profile;
public bool IsPlayer() => this.Name != null : false;
}

View File

@@ -2,29 +2,56 @@
using System.Text.Json;
using System.Text;
using System.Text.Json.Nodes;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using spworlds.Types;
namespace spworlds;
public class SPWorlds
{
private readonly HttpClient client;
private string token;
public SPWorlds(string id, string token)
{
client = new HttpClient();
var BearerToken = $"{id}:{token}";
this.token = token;
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
}
// Полностью бесполезная функция, вебхук, возвращающийся от сайта по факту невозможно валидировать.
private async Task<bool> ValidateWebHook(string webHook, string bodyHash)
{
// Если я правильно все понял, то вот
// Конвертим из string в bytes body_hash
byte[] body = Encoding.UTF8.GetBytes(bodyHash);
// потом конвертим вебхук
byte[] webhook = Encoding.UTF8.GetBytes(webHook);
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
// Переводим в Base64
string webhook64 = Convert.ToBase64String(key.ComputeHash(webhook));
return webhook64.Equals(body);
/**
* Тот же код, но на Python:
hmacData = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
base64Data = b64encode(hmacData)
return hmac.compare_digest(base64Data, bodyHash.encode('utf-8'))
**/
}
private async Task<string> SendRequest(string endpoint, Boolean getResult = true, Dictionary<string, object>? body = null)
{
string respond;
string jsonBody;
if(body == null)
if (body == null)
{
return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;
}
@@ -33,7 +60,7 @@ public class SPWorlds
jsonBody = JsonSerializer.Serialize(body);
var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json");
if(getResult)
if (getResult)
return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
else
await client.PostAsync(endpoint, payload);
@@ -52,7 +79,7 @@ public class SPWorlds
return (int)balance;
}
public async Task CreatTransaction(string receiver, int amount, string comment)
public async Task CreateTransaction(string receiver, int amount, string comment)
{
var transitionInfo = new Dictionary<string, object>
{
@@ -64,12 +91,12 @@ public class SPWorlds
await SendRequest(endpoint: "transactions", body: transitionInfo);
}
public async Task<string> GetUser(string discordId)
public async Task<User> GetUser(string discordId)
{
var user = JsonObject.Parse(await SendRequest($"users/{discordId}"));
var userName = user["username"];
return (string)userName;
var userResponse = JsonObject.Parse(await SendRequest($"users/{discordId}"));
var userName = userResponse["username"];
User user = new() { Name = userName}
return (User)user;
}
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
@@ -82,7 +109,23 @@ public class SPWorlds
{ "data", data }
};
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment",body: paymentInfo));
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment", body: paymentInfo));
var url = payment["url"];
return (string)url;
}
public async Task<string> InitPayment(PaymentData paymentData)
{
var paymentInfo = new Dictionary<string, object>
{
{ "amount", paymentData.Amount },
{ "redirectUrl", paymentData.RedirectUrl },
{ "webhookUrl", paymentData.WebHookUrl },
{ "data", paymentData.Data }
};
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment", body: paymentInfo));
var url = payment["url"];
return (string)url;