mirror of
https://github.com/yawaflua/spworlds-csharp-library.git
synced 2025-12-10 04:29:25 +02:00
9
src/Types/PaymentData.cs
Normal file
9
src/Types/PaymentData.cs
Normal 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
13
src/Types/SkinPart.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace spworlds.Types;
|
||||||
|
|
||||||
|
public enum SkinPart
|
||||||
|
{
|
||||||
|
face;
|
||||||
|
front;
|
||||||
|
front_full;
|
||||||
|
head;
|
||||||
|
bust;
|
||||||
|
full;
|
||||||
|
skin;
|
||||||
|
|
||||||
|
}
|
||||||
25
src/Types/User.cs
Normal file
25
src/Types/User.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using spworlds;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
namespace spworlds.Types;
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public readonly string Name
|
||||||
|
public readonly string Uuid
|
||||||
|
public readonly JsonNode profile
|
||||||
|
|
||||||
|
private HttpClient client = new();
|
||||||
|
|
||||||
|
public bool IsPlayer() => Name != null ? true : false;
|
||||||
|
|
||||||
|
public User(string name)
|
||||||
|
{
|
||||||
|
Uuid = JsonNode.Parse(client.GetStringAsync($"https://api.mojang.com/users/profiles/minecraft/{name}"))["id"];
|
||||||
|
profile = JsonNode.Parse(client.GetStringAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{Uuid}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetSkinPart(SkinPart skinPart, string size = "64")
|
||||||
|
{
|
||||||
|
return (string)$"https://visage.surgeplay.com/{skinPart}/{size}/{this.profile["profileId"]}"
|
||||||
|
}
|
||||||
|
}
|
||||||
247
src/spworlds.cs
247
src/spworlds.cs
@@ -1,113 +1,134 @@
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace spworlds;
|
using spworlds.Types;
|
||||||
|
namespace spworlds;
|
||||||
public class SPWorlds
|
|
||||||
{
|
public class SPWorlds
|
||||||
private readonly HttpClient client;
|
{
|
||||||
|
private readonly HttpClient client;
|
||||||
public SPWorlds(string id, string token)
|
private string token;
|
||||||
{
|
|
||||||
client = new HttpClient();
|
public SPWorlds(string id, string token)
|
||||||
var BearerToken = $"{id}:{token}";
|
{
|
||||||
var token = token
|
client = new HttpClient();
|
||||||
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
var BearerToken = $"{id}:{token}";
|
||||||
|
this.token = token;
|
||||||
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
|
||||||
}
|
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
||||||
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
||||||
private async Task<bool> ValidateWebhook(string webhook, string body_hash)
|
}
|
||||||
{
|
|
||||||
// Если я правильно все понял, то вот
|
|
||||||
// Конвертим из string в bytes body_hash
|
// Полностью бесполезная функция, вебхук, возвращающийся от сайта по факту невозможно валидировать.
|
||||||
byte[] body = Encoding.UTF8.GetBytes(body_hash);
|
private async Task<bool> ValidateWebHook(string webHook, string bodyHash)
|
||||||
// потом конвертим вебхук
|
{
|
||||||
byte[] webhook = Encoding.UTF8.GetBytes(webhook);
|
// Если я правильно все понял, то вот
|
||||||
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
|
// Конвертим из string в bytes body_hash
|
||||||
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
|
byte[] body = Encoding.UTF8.GetBytes(bodyHash);
|
||||||
// Переводим в Base64
|
// потом конвертим вебхук
|
||||||
string webhook_64 = Convert.ToBase64String(key.ComputeHash(webhook));
|
byte[] webhook = Encoding.UTF8.GetBytes(webHook);
|
||||||
return webhook_64.Equals(body);
|
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
|
||||||
/**
|
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
|
||||||
* Тот же код, но на Python:
|
// Переводим в Base64
|
||||||
hmac_data = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
|
string webhook64 = Convert.ToBase64String(key.ComputeHash(webhook));
|
||||||
base64_data = b64encode(hmac_data)
|
return webhook64.Equals(body);
|
||||||
return hmac.compare_digest(base64_data, body_hash.encode('utf-8'))
|
/**
|
||||||
**/
|
* Тот же код, но на Python:
|
||||||
}
|
hmacData = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
|
||||||
|
base64Data = b64encode(hmacData)
|
||||||
private async Task<string> SendRequest(string endpoint, Boolean getResult = true, Dictionary<string, object>? body = null)
|
return hmac.compare_digest(base64Data, bodyHash.encode('utf-8'))
|
||||||
{
|
**/
|
||||||
string respond;
|
}
|
||||||
string jsonBody;
|
|
||||||
|
private async Task<string> SendRequest(string endpoint, Boolean getResult = true, Dictionary<string, object>? body = null)
|
||||||
if (body == null)
|
{
|
||||||
{
|
string respond;
|
||||||
return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;
|
string jsonBody;
|
||||||
}
|
|
||||||
else
|
if (body == null)
|
||||||
{
|
{
|
||||||
jsonBody = JsonSerializer.Serialize(body);
|
return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;
|
||||||
var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
}
|
||||||
|
else
|
||||||
if (getResult)
|
{
|
||||||
return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
|
jsonBody = JsonSerializer.Serialize(body);
|
||||||
else
|
var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||||
await client.PostAsync(endpoint, payload);
|
|
||||||
}
|
if (getResult)
|
||||||
|
return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
|
||||||
return null;
|
else
|
||||||
}
|
await client.PostAsync(endpoint, payload);
|
||||||
|
}
|
||||||
public async Task<int> GetBalance()
|
|
||||||
{
|
return null;
|
||||||
string respond = await SendRequest("card");
|
}
|
||||||
|
|
||||||
var card = JsonObject.Parse(respond);
|
public async Task<int> GetBalance()
|
||||||
var balance = card["balance"];
|
{
|
||||||
|
string respond = await SendRequest("card");
|
||||||
return (int)balance;
|
|
||||||
}
|
var card = JsonObject.Parse(respond);
|
||||||
|
var balance = card["balance"];
|
||||||
public async Task CreateTransaction(string receiver, int amount, string comment)
|
|
||||||
{
|
return (int)balance;
|
||||||
var transitionInfo = new Dictionary<string, object>
|
}
|
||||||
{
|
|
||||||
{ "receiver", receiver },
|
public async Task CreateTransaction(string receiver, int amount, string comment)
|
||||||
{ "amount", amount },
|
{
|
||||||
{ "comment", comment }
|
var transitionInfo = new Dictionary<string, object>
|
||||||
};
|
{
|
||||||
|
{ "receiver", receiver },
|
||||||
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
{ "amount", amount },
|
||||||
}
|
{ "comment", comment }
|
||||||
|
};
|
||||||
public async Task<string> GetUser(string discordId)
|
|
||||||
{
|
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
||||||
var user = JsonObject.Parse(await SendRequest($"users/{discordId}"));
|
}
|
||||||
var userName = user["username"];
|
|
||||||
|
public async Task<User> GetUser(string discordId)
|
||||||
return (string)userName;
|
{
|
||||||
}
|
var userResponse = JsonObject.Parse(await SendRequest($"users/{discordId}"));
|
||||||
|
var userName = userResponse["username"];
|
||||||
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
|
User user = new() { Name = userName}
|
||||||
{
|
return (User)user;
|
||||||
var paymentInfo = new Dictionary<string, object>
|
}
|
||||||
{
|
|
||||||
{ "amount", amount },
|
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
|
||||||
{ "redirectUrl", redirectUrl },
|
{
|
||||||
{ "webhookUrl", webhookUrl },
|
var paymentInfo = new Dictionary<string, object>
|
||||||
{ "data", data }
|
{
|
||||||
};
|
{ "amount", amount },
|
||||||
|
{ "redirectUrl", redirectUrl },
|
||||||
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment", body: paymentInfo));
|
{ "webhookUrl", webhookUrl },
|
||||||
var url = payment["url"];
|
{ "data", data }
|
||||||
|
};
|
||||||
return (string)url;
|
|
||||||
}
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user