Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgwilliams committed Mar 7, 2014
0 parents commit 155c13c
Show file tree
Hide file tree
Showing 36 changed files with 625 additions and 0 deletions.
32 changes: 32 additions & 0 deletions CoinbaseConnector.sln
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoinbaseConnector", "CoinbaseConnector\CoinbaseConnector.csproj", "{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{C8C057A2-5B9A-448A-8EC0-1110A951599A}"
EndProject
Global
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}.Release|Any CPU.Build.0 = Release|Any CPU
{C8C057A2-5B9A-448A-8EC0-1110A951599A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8C057A2-5B9A-448A-8EC0-1110A951599A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8C057A2-5B9A-448A-8EC0-1110A951599A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8C057A2-5B9A-448A-8EC0-1110A951599A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added CoinbaseConnector.v12.suo
Binary file not shown.
15 changes: 15 additions & 0 deletions CoinbaseConnector/APIKeys.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace CoinbaseConnector
{
class APIKeys
{
public static string API_KEY = ConfigurationManager.AppSettings["API_KEY"];
public static string API_SECRET = ConfigurationManager.AppSettings["API_SECRET"];
}
}
61 changes: 61 additions & 0 deletions CoinbaseConnector/CoinbaseConnector.csproj
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>{2A9D52D4-0F7E-49F4-AB3E-AB79B67AA3C4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CoinbaseConnector</RootNamespace>
<AssemblyName>CoinbaseConnector</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>Svn</SccProjectName>
<SccLocalPath>Svn</SccLocalPath>
<SccAuxPath>Svn</SccAuxPath>
<SccProvider>SubversionScc</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<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' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="APIKeys.cs" />
<Compile Include="Connector.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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>
278 changes: 278 additions & 0 deletions CoinbaseConnector/Connector.cs
@@ -0,0 +1,278 @@
using System.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;

namespace CoinbaseConnector
{
public class Connector
{
private string API_KEY = APIKeys.API_KEY;
private string API_SECRET = APIKeys.API_SECRET;

private string URL_BASE = "https://coinbase.com/api/v1/";
private const string GET = "GET";
private const string POST = "POST";

// Account Changes
public string GetAccountChanges(String page = "1")
{
// Page field is optional. Default is 1
return JsonRequest(URL_BASE + "account_changes?page=" + page, GET);
}

// Account
public string GetAccountBalance()
{
return JsonRequest(URL_BASE + "account/balance", GET);
}
public string GetCurrentReceiveAddress()
{
return JsonRequest(URL_BASE + "account/receive_address", GET);
}
public string GenerateReceiveAddress()
{
return JsonRequest(URL_BASE + "account/generate_receive_address", POST);
}
public string GenerateReceiveAddress(String callbackURL, String label)
{
return JsonRequest(URL_BASE + "account/generate_receive_address?address[callback_url]=" + callbackURL+"&address[label]=" + label, POST);
}

// Addresses
public string GetAddressList(String page = "1", String limit = "25", String query = "")
{
// Page field is optional. Default is 1
return JsonRequest(URL_BASE + "addresses?page=" + page + "&limit=" + limit + "&query=" + query, GET);
}

// OAuth Applications
public string GetOAuthApplicationsList(String page = "1")
{
return JsonRequest(URL_BASE + "oauth/applications?page=" + page, GET);
}
public string GetOauthApplicationByID(String ID)
{
return JsonRequest(URL_BASE + "oauth/applications/" + ID, GET);
}
public string CreateOAuthApplication()
{
return JsonRequest(URL_BASE + "oauth/applications/", POST);
}

// Authorization
public string GetAccountAuthorizationInfo()
{
return JsonRequest(URL_BASE + "authorization", GET);
}

// Buttons
public string CreatePaymentButton()
{
string jsonParams=""; // TODO
return JsonRequest(URL_BASE + "buttons", POST, jsonParams);
}
public string CreateOrderForButton()
{
string jsonParams=""; // TODO
return JsonRequest(URL_BASE + "buttons/:code/create_order", POST, jsonParams);
}

// Buys
public string PurchaseBitcoin()
{
string jsonParams=""; // TODO
return JsonRequest(URL_BASE + "buys", POST, jsonParams);
}

// Contacts
public string GetEmailContacts()
{
return JsonRequest(URL_BASE + "contacts", GET);
}

// Currencies
public string GetSupportedCurrencies()
{
return JsonRequest(URL_BASE + "currencies", GET);
}
public string GetBTCExchangeRate()
{
return JsonRequest(URL_BASE + "exchange_rates", GET);
}

// Orders
public string GetReceivedMerchantOrders(string page = "1")
{
// Page field is optional. Default is 1
return JsonRequest(URL_BASE + "orders?page=" + page, GET);
}
public string CreateNewOrder()
{
string jsonParams = ""; // TODO
return JsonRequest(URL_BASE + "orders", POST, jsonParams);
}
public string GetMerchantOrderByID(string ID = "")
{
return JsonRequest(URL_BASE + "orders/" + ID, GET);
}

// Payment Methods
public string GetAssociatedPaymentMethods()
{
return JsonRequest(URL_BASE + "payment_methods", GET);
}

// Prices
public string GetTotalBuyPriceForBitcoin(float qty = 1, String currency = "USD")
{
return JsonRequest(URL_BASE + "prices/buy?qty=" + qty + "&currency=" + currency, GET);
}
public string GetTotalSellPriceForBitcoin(float qty = 1, String currency = "USD")
{
return JsonRequest(URL_BASE + "prices/sell?qty=" + qty + "&currency=" + currency, GET);
}
public string GetSpotPriceForBitcoin(String currency = "USD")
{
// Currency must be an ISO 4217 Currency Code. Default is USD
return JsonRequest(URL_BASE + "prices/spot_rate?currency=" + currency, GET);
}
public string GetHistoricalSpotPriceForBitcoin(String page = "1")
{
// Page field is optional. Default is 1
return JsonRequest(URL_BASE + "prices/historical?page=" + page, GET);
}

// Recurring Payments
public string GetRecurringPayments(String ID = "")
{
// ID field is optional. Default is no parameter.
return JsonRequest(URL_BASE + "recurring_payments/" + ID, GET);
}

// Sells
public string SellBitcoin(float qty, String payment_method_id = "")
{
// Quantity of Bitcoin to sell is required.
// Payment Method ID is optional. Will use default account ID. Must have verified bank account to work.
return JsonRequest(URL_BASE + "sells?qty=" + qty + "&payment_method_id=" + payment_method_id, POST);
}



#region Supporting Classes for API Parameters

public class Order
{
public Button button {get; set;}
}

public class Button
{
public string name { get; set; }
public string price_string { get; set; }
public string price_currency_iso { get; set; }
public string type { get; set; }
public string repeat { get; set; }
public string style { get; set; }
public string text { get; set; }
public string description { get; set; }
public string custom { get; set; }
public bool custom_secure { get; set; }
public string callback_url { get; set; }
public string success_url { get; set; }
public string cancel_url { get; set; }
public string info_url { get; set; }
public bool auto_redirect { get; set; }
public bool variable_price { get; set; }
public bool choose_price { get; set; }
public bool include_address { get; set; }
public bool include_email { get; set; }
public string price1 { get; set; }
public string price2 { get; set; }
public string price3 { get; set; }
public string price4 { get; set; }
public string price5 { get; set; }
}

#endregion


private string JsonRequest(string url, string method, String postdata = "")
{
string returnData = String.Empty;

// strip out any escape characters in the optional parameters
postdata = postdata.Replace(@"\", "");

var webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Accept = "*/*";
webRequest.UserAgent = ".NET";
webRequest.Method = method;
webRequest.ContentType = "application/json";
webRequest.Host = "coinbase.com";

string nonce = Convert.ToInt64(DateTime.Now.Ticks).ToString();
string message = nonce + url + postdata;
string signature = HashEncode(HashHMAC(StringEncode(API_SECRET), StringEncode(message)));

var whc = new WebHeaderCollection();
whc.Add("ACCESS_KEY: " + API_KEY);
whc.Add("ACCESS_SIGNATURE: " + signature);
whc.Add("ACCESS_NONCE: " + nonce);
webRequest.Headers = whc;

if (postdata != "")
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postdata);

// Set the content length of the string being posted.
webRequest.ContentLength = byte1.Length;
Stream newStream = webRequest.GetRequestStream();

newStream.Write(byte1, 0, byte1.Length);
}

using (WebResponse response = webRequest.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
returnData = reader.ReadToEnd();
}
}
}

return returnData;
}

private static byte[] StringEncode(string text)
{
var encoding = new ASCIIEncoding();
return encoding.GetBytes(text);
}

private static string HashEncode(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}

private static byte[] HashHMAC(byte[] key, byte[] message)
{
var hash = new HMACSHA256(key);
return hash.ComputeHash(message);
}
}
}

0 comments on commit 155c13c

Please sign in to comment.