mirror of
https://github.com/yawaflua/spworlds-csharp-library.git
synced 2025-12-10 12:29:30 +02:00
Add files via upload
This commit is contained in:
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;
|
||||||
|
|
||||||
|
}
|
||||||
23
src/Types/User.cs
Normal file
23
src/Types/User.cs
Normal 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;
|
||||||
|
}
|
||||||
221
src/spworlds.cs
221
src/spworlds.cs
@@ -1,90 +1,133 @@
|
|||||||
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;
|
||||||
namespace spworlds;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
public class SPWorlds
|
using spworlds.Types;
|
||||||
{
|
namespace spworlds;
|
||||||
private readonly HttpClient client;
|
|
||||||
|
public class SPWorlds
|
||||||
public SPWorlds(string id, string token)
|
{
|
||||||
{
|
private readonly HttpClient client;
|
||||||
client = new HttpClient();
|
private string token;
|
||||||
var BearerToken = $"{id}:{token}";
|
|
||||||
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
public SPWorlds(string id, string token)
|
||||||
|
{
|
||||||
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
client = new HttpClient();
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
var BearerToken = $"{id}:{token}";
|
||||||
}
|
this.token = token;
|
||||||
|
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
||||||
private async Task<string> SendRequest(string endpoint, Boolean getResult = true, Dictionary<string, object>? body = null)
|
|
||||||
{
|
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
||||||
string respond;
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
||||||
string jsonBody;
|
}
|
||||||
|
|
||||||
if(body == null)
|
|
||||||
{
|
// Полностью бесполезная функция, вебхук, возвращающийся от сайта по факту невозможно валидировать.
|
||||||
return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;
|
private async Task<bool> ValidateWebHook(string webHook, string bodyHash)
|
||||||
}
|
{
|
||||||
else
|
// Если я правильно все понял, то вот
|
||||||
{
|
// Конвертим из string в bytes body_hash
|
||||||
jsonBody = JsonSerializer.Serialize(body);
|
byte[] body = Encoding.UTF8.GetBytes(bodyHash);
|
||||||
var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
// потом конвертим вебхук
|
||||||
|
byte[] webhook = Encoding.UTF8.GetBytes(webHook);
|
||||||
if(getResult)
|
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
|
||||||
return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
|
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
|
||||||
else
|
// Переводим в Base64
|
||||||
await client.PostAsync(endpoint, payload);
|
string webhook64 = Convert.ToBase64String(key.ComputeHash(webhook));
|
||||||
}
|
return webhook64.Equals(body);
|
||||||
|
/**
|
||||||
return null;
|
* Тот же код, но на Python:
|
||||||
}
|
hmacData = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
|
||||||
|
base64Data = b64encode(hmacData)
|
||||||
public async Task<int> GetBalance()
|
return hmac.compare_digest(base64Data, bodyHash.encode('utf-8'))
|
||||||
{
|
**/
|
||||||
string respond = await SendRequest("card");
|
}
|
||||||
|
|
||||||
var card = JsonObject.Parse(respond);
|
private async Task<string> SendRequest(string endpoint, Boolean getResult = true, Dictionary<string, object>? body = null)
|
||||||
var balance = card["balance"];
|
{
|
||||||
|
string respond;
|
||||||
return (int)balance;
|
string jsonBody;
|
||||||
}
|
|
||||||
|
if (body == null)
|
||||||
public async Task CreatTransaction(string receiver, int amount, string comment)
|
{
|
||||||
{
|
return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;
|
||||||
var transitionInfo = new Dictionary<string, object>
|
}
|
||||||
{
|
else
|
||||||
{ "receiver", receiver },
|
{
|
||||||
{ "amount", amount },
|
jsonBody = JsonSerializer.Serialize(body);
|
||||||
{ "comment", comment }
|
var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||||
};
|
|
||||||
|
if (getResult)
|
||||||
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
|
||||||
}
|
else
|
||||||
|
await client.PostAsync(endpoint, payload);
|
||||||
public async Task<string> GetUser(string discordId)
|
}
|
||||||
{
|
|
||||||
var user = JsonObject.Parse(await SendRequest($"users/{discordId}"));
|
return null;
|
||||||
var userName = user["username"];
|
}
|
||||||
|
|
||||||
return (string)userName;
|
public async Task<int> GetBalance()
|
||||||
}
|
{
|
||||||
|
string respond = await SendRequest("card");
|
||||||
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
|
|
||||||
{
|
var card = JsonObject.Parse(respond);
|
||||||
var paymentInfo = new Dictionary<string, object>
|
var balance = card["balance"];
|
||||||
{
|
|
||||||
{ "amount", amount },
|
return (int)balance;
|
||||||
{ "redirectUrl", redirectUrl },
|
}
|
||||||
{ "webhookUrl", webhookUrl },
|
|
||||||
{ "data", data }
|
public async Task CreateTransaction(string receiver, int amount, string comment)
|
||||||
};
|
{
|
||||||
|
var transitionInfo = new Dictionary<string, object>
|
||||||
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment",body: paymentInfo));
|
{
|
||||||
var url = payment["url"];
|
{ "receiver", receiver },
|
||||||
|
{ "amount", amount },
|
||||||
return (string)url;
|
{ "comment", comment }
|
||||||
}
|
};
|
||||||
|
|
||||||
|
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User> GetUser(string discordId)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var paymentInfo = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "amount", amount },
|
||||||
|
{ "redirectUrl", redirectUrl },
|
||||||
|
{ "webhookUrl", webhookUrl },
|
||||||
|
{ "data", data }
|
||||||
|
};
|
||||||
|
|
||||||
|
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