Skip to content

Commit

Permalink
Merge pull request #7 from LeMoussel/master
Browse files Browse the repository at this point in the history
C# library for accessing the Bismuth Native API access.
  • Loading branch information
EggPool authored Nov 24, 2018
2 parents f440aed + e9c3736 commit 31ba6bf
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ ENV/
# mypy
.mypy_cache/

# Distribution / packaging VSCode
.vscode/
C++/analyseValgrind.sh
C++/analyseValgrind.txt
C#/obj
6 changes: 6 additions & 0 deletions C#/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
107 changes: 107 additions & 0 deletions C#/BismuthAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Linq;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text;

public class BismuthAPI
{
private string server;
private int port;
private TcpClient tcpClientAPI = null;
private NetworkStream tcpStream;

public BismuthAPI(string nodeServer, int nodePort)
{
this.server = nodeServer;
this.port = nodePort;
this.cnxCheck();
}

~BismuthAPI()
{
tcpStream.Close();
tcpClientAPI.Close();
}

public string Command(params object[] cmds)
{
for (int i = 0; i < cmds.Length; i++) this.Send(cmds[i]);
return this.Receive();
}

public string Receive()
{
Byte[] lengthSocketMessage = new Byte[10];
String responseData = String.Empty;

Int32 nBytes = tcpStream.Read(lengthSocketMessage, 0, lengthSocketMessage.Length);
responseData = System.Text.Encoding.ASCII.GetString(lengthSocketMessage, 0, nBytes);
Int32 socketLenReceiveMessage = Int32.Parse(responseData);
int socketTotalReceiveBytes = 0;

string strJson;
using (MemoryStream ms = new MemoryStream())
{
Byte[] data = new Byte[socketLenReceiveMessage];

int numBytesRead;
while ((numBytesRead = tcpStream.Read(data, 0, data.Length)) > 0)
{
Console.WriteLine("numBytesRead: "+ numBytesRead);
ms.Write(data, 0, numBytesRead);
socketTotalReceiveBytes += numBytesRead;
if (socketTotalReceiveBytes == socketLenReceiveMessage) break;
}
strJson = Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
}

return strJson;
}

public void Send(object cmd)
{
Byte[] data;

string socketMessage = JsonConvert.SerializeObject(cmd);
socketMessage = string.Format("{0:D10}", socketMessage.Length) + socketMessage;
data = System.Text.Encoding.ASCII.GetBytes(socketMessage);
tcpStream.Write(data, 0, data.Length);
}

static public string JsonDump(string jsonData)
{
JToken jt = JToken.Parse(jsonData);
return jt.ToString();
}

private void cnxCheck()
{
if (this.tcpClientAPI == null)
{
try
{
this.tcpClientAPI = new TcpClient(this.server, this.port);
this.tcpStream = this.tcpClientAPI.GetStream();
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
}

private TcpState getTCPState()
{
var foo = IPGlobalProperties.GetIPGlobalProperties()
.GetActiveTcpConnections()
.SingleOrDefault(x => x.LocalEndPoint.Equals(this.tcpClientAPI.Client.LocalEndPoint)
&& x.RemoteEndPoint.Equals(this.tcpClientAPI.Client.RemoteEndPoint)
);

return foo != null ? foo.State : TcpState.Unknown;
}
}
70 changes: 70 additions & 0 deletions C#/BismuthAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B49F850E-3516-451F-9B26-550E24C9FB0B}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BismuthAPI</RootNamespace>
<AssemblyName>BismuthAPI</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Demo_getaddress_since</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BismuthAPI.cs" />
<Compile Include="Demo.cs" />
<Compile Include="Demo_getaddress_since.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
22 changes: 22 additions & 0 deletions C#/BismuthAPI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BismuthAPI", "BismuthAPI.csproj", "{B49F850E-3516-451F-9B26-550E24C9FB0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B49F850E-3516-451F-9B26-550E24C9FB0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B49F850E-3516-451F-9B26-550E24C9FB0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B49F850E-3516-451F-9B26-550E24C9FB0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B49F850E-3516-451F-9B26-550E24C9FB0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
54 changes: 54 additions & 0 deletions C#/Demo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;

class Demo
{
static void Main(string[] args)
{
string ret = String.Empty;

BismuthAPI connectionBismuthAPI = new BismuthAPI("127.0.0.1", 5658);

// Ask for general status
Console.WriteLine("statusjson:");
ret = connectionBismuthAPI.Command("statusjson");
Console.WriteLine(ret);
Console.WriteLine();

// Gets a specific block details
Console.WriteLine("blockget(558742):");
ret = connectionBismuthAPI.Command("blockget", 558742);
Console.WriteLine(ret);
Console.WriteLine();

// Gets latest block and diff
Console.WriteLine("difflast:");
ret = connectionBismuthAPI.Command("difflast");
Console.WriteLine(ret);
Console.WriteLine();

// Gets tx detail, raw format
Console.WriteLine("api_gettransaction('K1iuKwkOac4HSuzEBDxmqb5dOmfXEK98BaWQFHltdrbDd0C5iIEbh/Fj', false):");
ret = connectionBismuthAPI.Command("api_gettransaction", "K1iuKwkOac4HSuzEBDxmqb5dOmfXEK98BaWQFHltdrbDd0C5iIEbh/Fj", false);
Console.WriteLine(ret);
Console.WriteLine();

// Gets tx detail, json format
Console.WriteLine("api_gettransaction('K1iuKwkOac4HSuzEBDxmqb5dOmfXEK98BaWQFHltdrbDd0C5iIEbh/Fj', true):");
ret = connectionBismuthAPI.Command("api_gettransaction", "K1iuKwkOac4HSuzEBDxmqb5dOmfXEK98BaWQFHltdrbDd0C5iIEbh/Fj", true);
Console.WriteLine(ret);
Console.WriteLine();

// Gets addresses balances
Console.WriteLine("api_listbalance(['731337bb0f76463d578626a48367dfea4c6efcfa317604814f875d10','340c195f768be515488a6efedb958e135150b2ef3e53573a7017ac7d'], 0, true):");
string[] addresses = new string[] { "731337bb0f76463d578626a48367dfea4c6efcfa317604814f875d10", "340c195f768be515488a6efedb958e135150b2ef3e53573a7017ac7d" };
ret = connectionBismuthAPI.Command("api_listbalance", addresses, 0, true);
Console.WriteLine(ret);
Console.WriteLine();

// Ask for a new keys/address set
Console.WriteLine("keygen:");
ret = connectionBismuthAPI.Command("keygen");
Console.WriteLine(ret);
Console.WriteLine();
}
}
46 changes: 46 additions & 0 deletions C#/Demo_getaddress_since.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

class Demo_getaddress_since
{
static string Get_address_since(int since, int min_conf, string address)
{
BismuthAPI connectionBismuthAPI = new BismuthAPI("127.0.0.1", 5658);

// Command first
connectionBismuthAPI.Send("api_getaddresssince");
// Then last block
connectionBismuthAPI.Send(since);
// min confirmations
connectionBismuthAPI.Send(min_conf);
// and finally the address
connectionBismuthAPI.Send(address);

return connectionBismuthAPI.Receive();
}

static void Usage(string message)
{
Console.WriteLine(message);
Console.WriteLine("Usage: BismuthAPI <block_height> <min_conf> <address>");
Environment.Exit(1);
}

static void Main(string[] args)
{
string res_as_native_dict = String.Empty;
int since, min_conf;
string address;

if (args.Length != 3) Usage("Please enter arguments.");
if (int.TryParse(args[0], out since) == false) Usage("Please enter a numeric argument for last known block: <block_height>");
if (int.TryParse(args[1], out min_conf) == false) Usage("Please enter a numeric argument for min confirmations: <min_conf >");
address = args[2];

Console.WriteLine("api_getaddresssince since " + since + " minconf=" + min_conf + " for address " + address);
res_as_native_dict = Get_address_since(since, min_conf, address);
Console.WriteLine(BismuthAPI.JsonDump(res_as_native_dict));
Console.WriteLine();

Environment.Exit(0);
}
}
36 changes: 36 additions & 0 deletions C#/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BismuthAPI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BismuthAPI")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b49f850e-3516-451f-9b26-550e24c9fb0b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
31 changes: 31 additions & 0 deletions C#/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# C# code for Bismuth Native API access

Demonstrates the use of a simple C# client library.

+ [BismuthAPI.cs](BismuthAPI.cs) : C# Client library
+ [Demo.cs](Demo.cs) : A simple demo app, showing a few simple commands. It's needs a running node.
+ [Demo_getaddress_since.cs](Demo_getaddress_since.cs) : A simple demo app, showing the `api_getaddresssince` api command. It's needs a running node.

## Notes

Use [newtonsoft-json](https://www.nuget.org/packages/Newtonsoft.Json/) package to parse JSON API response. To download from NuGet, You can do this a couple of ways.

**Via the "Solution Explorer"**

Simply right-click the "References" folder and select "Manage NuGet Packages..."
Once that window comes up click on the option labeled "Online" in the left most part of the dialog.
Then in the search bar in the upper right type "json.net"
Click "Install" and you're done.

**Via the "Package Manager Console"**

Open the console. "View" > "Other Windows" > "Package Manager Console"
Then type the following:

Install-Package Newtonsoft.Json

For more info on how to use the "Package Manager Console" check out the [nuget docs](https://docs.microsoft.com/fr-fr/nuget/tools/package-manager-console).

### Patches

Anybody can contribute to development. If you are contributing a source code change, use a merge request of a Git branch.
4 changes: 4 additions & 0 deletions C#/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net452" />
</packages>

0 comments on commit 31ba6bf

Please sign in to comment.