Skip to content

Commit

Permalink
NBH3
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
bl4z committed Mar 10, 2020
0 parents commit d162160
Show file tree
Hide file tree
Showing 43 changed files with 5,736 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs
packages
NHB3/bin
NHB3/obj
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 NiceHash.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions NHB3.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHB3", "NHB3\NHB3.csproj", "{544DB7FC-334D-4C5D-A298-61508693EB31}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{544DB7FC-334D-4C5D-A298-61508693EB31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{544DB7FC-334D-4C5D-A298-61508693EB31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{544DB7FC-334D-4C5D-A298-61508693EB31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{544DB7FC-334D-4C5D-A298-61508693EB31}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ACE18268-A609-422C-8625-55B261915EF9}
EndGlobalSection
EndGlobal
194 changes: 194 additions & 0 deletions NHB3/Api.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHB3
{
class Api
{
private string urlRoot;
private string orgId;
private string apiKey;
private string apiSecret;

public string time;

public Api(string urlRoot, string orgId, string apiKey, string apiSecret)
{
this.urlRoot = urlRoot;
this.orgId = orgId;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}

private static string HashBySegments(string key, string apiKey, string time, string nonce, string orgId, string method, string encodedPath, string query, string bodyStr)
{
List<string> segments = new List<string>();
segments.Add(apiKey);
segments.Add(time);
segments.Add(nonce);
segments.Add(null);
segments.Add(orgId);
segments.Add(null);
segments.Add(method);
segments.Add(encodedPath == null ? null : encodedPath);
segments.Add(query == null ? null : query);

if (bodyStr != null && bodyStr.Length > 0)
{
segments.Add(bodyStr);
}
return Api.CalcHMACSHA256Hash(Api.JoinSegments(segments), key);
}
private static string getPath(string url)
{
var arrSplit = url.Split('?');
return arrSplit[0];
}
private static string getQuery(string url)
{
var arrSplit = url.Split('?');

if (arrSplit.Length == 1)
{
return null;
}
else
{
return arrSplit[1];
}
}

private static string JoinSegments(List<string> segments)
{
var sb = new System.Text.StringBuilder();
bool first = true;
foreach (var segment in segments)
{
if (!first)
{
sb.Append("\x00");
}
else
{
first = false;
}

if (segment != null)
{
sb.Append(segment);
}
}
Console.Out.WriteLine("req: [" + sb.ToString() + "]");
return sb.ToString();
}

private static string CalcHMACSHA256Hash(string plaintext, string salt)
{
string result = "";
var enc = Encoding.Default;
byte[]
baText2BeHashed = enc.GetBytes(plaintext),
baSalt = enc.GetBytes(salt);
System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(baSalt);
byte[] baHashedText = hasher.ComputeHash(baText2BeHashed);
result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());
return result;
}

private string srvrTime()
{
string timeResponse = this.get("/api/v2/time", false);
ServerTime serverTimeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerTime>(timeResponse);
time = serverTimeObject.serverTime;
return time;
}

public string get(string url, bool auth)
{
var client = new RestSharp.RestClient(this.urlRoot);
var request = new RestSharp.RestRequest(url);

if (auth)
{
string time = this.srvrTime();
string nonce = Guid.NewGuid().ToString();
string digest = Api.HashBySegments(this.apiSecret, this.apiKey, time, nonce, this.orgId, "GET", getPath(url), getQuery(url), null);

request.AddHeader("X-Time", time);
request.AddHeader("X-Nonce", nonce);
request.AddHeader("X-Auth", this.apiKey + ":" + digest);
request.AddHeader("X-Organization-Id", this.orgId);
}

var response = client.Execute(request, RestSharp.Method.GET);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
}

public string post(string url, string payload, bool requestId)
{
string time = this.srvrTime();
var client = new RestSharp.RestClient(this.urlRoot);
var request = new RestSharp.RestRequest(url);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-type", "application/json");

string nonce = Guid.NewGuid().ToString();
string digest = Api.HashBySegments(this.apiSecret, this.apiKey, time, nonce, this.orgId, "POST", getPath(url), getQuery(url), payload);

if (payload != null)
{
request.AddJsonBody(payload);
}

request.AddHeader("X-Time", time);
request.AddHeader("X-Nonce", nonce);
request.AddHeader("X-Auth", this.apiKey + ":" + digest);
request.AddHeader("X-Organization-Id", this.orgId);

if (requestId)
{
request.AddHeader("X-Request-Id", Guid.NewGuid().ToString());
}

var response = client.Execute(request, RestSharp.Method.POST);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
}

public string delete(string url, bool requestId)
{
string time = this.srvrTime();
var client = new RestSharp.RestClient(this.urlRoot);
var request = new RestSharp.RestRequest(url);

string nonce = Guid.NewGuid().ToString();
string digest = Api.HashBySegments(this.apiSecret, this.apiKey, time, nonce, this.orgId, "DELETE", getPath(url), getQuery(url), null);

request.AddHeader("X-Time", time);
request.AddHeader("X-Nonce", nonce);
request.AddHeader("X-Auth", this.apiKey + ":" + digest);
request.AddHeader("X-Organization-Id", this.orgId);

if (requestId)
{
request.AddHeader("X-Request-Id", Guid.NewGuid().ToString());
}

var response = client.Execute(request, RestSharp.Method.DELETE);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
}
}

public class ServerTime
{
public string serverTime { get; set; }
}
}
Loading

1 comment on commit d162160

@Princeprince559
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.