#CEX.IO - Powershell API Wrapper For CEX.IO Trading Platform
#Created by KazCrypto
#Donation: (Highly Appreciated ^_^)
#BTC: 1DFtAts5oj8AcXkF8AxuJdYThc5hvDUMYr
#LTC: LPfqNz2aKfrRxBLXNUgiLsqPPiXF4jhVzk
#DOGE: DT1Kmq7ba1xq8MWLkzdn1DN5Vkr6RgGm7a
#---------------------------------------------------#
# DEBUGING INFORMATION #
#---------------------------------------------------#
#Uncomment this variable to enable debug message and vice versa
$DebugPreference = "Continue"
$script:_USERID = ""
$script:_APIKEY = ""
$script:_APISEC = ""
$HS256 = New-Object System.Security.Cryptography.HMACSHA256
#---------------------------------------------------#
# API FUNCTION #
#---------------------------------------------------#
#Function --> createVar "USERNAME" "APIKEY" "API_SECRET"
#Usage : Store user information into variable for private api connection purpose
function createVar
{
$script:_USERID = $args[0]
Write-Debug ("Variable _USERID = " + $_USERID)
$script:_APIKEY = $args[1]
Write-Debug ("Variable _APIKEY = " + $_APIKEY)
$script:_APISEC = $args[2]
Write-Debug ("Variable _APISEC = " + $_APISEC)
}
#Function --> createSign
#Usage : Create signature for functioning part of apiCall
function createSign
{
$script:_TimeStamp = [int][double]::Parse((Get-Date -UFormat %s))
#Some hack for maintaining timestamp for request purpose
$script:rTimeStamp = $script:_TimeStamp
Write-Debug ("Using Timestamp As Nonce = " + $script:rTimeStamp)
#Combining nonce, username, and apikey to be hashed
$_HMSG = $script:rTimeStamp.ToString() + $script:_USERID + $script:_APIKEY
Write-Debug ("MSG --> Compute Hash = " + $script:_HMSG)
#Init HMAC-SHA256 Key - Set HMAC-SHA256 Secret Key
$HS256.Key = [System.Text.Encoding]::UTF8.GetBytes($_APISEC)
Write-Debug ("_APISEC <> HS256KEY = " + [System.BitConverter]::ToString($HS256.Key)).Replace("-","").ToUpper()
#Compute $_HMSG with HMAC-SHA256
$script:_HRESULT = $HS256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($_HMSG))
Write-Debug ("SIGHASH = " + [System.BitConverter]::ToString($script:_HRESULT).Replace("-","").ToUpper())
}
#Function --> apiCall
#Usage : Communication function for accessing CEX.IO API
function apiReq
{
#Set baseURL to CEX.IO api server
$baseURL = "https://cex.io/api/"
#Function WhatIF
#Pretty much straight forward here :\
IF ($args[0] -eq "ticker")
{
$URI = $baseURL + $args[0] + "/" + $args[1] + "/"
$restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} elseif ($args[0] -eq "order_book") {
$URI = $baseURL + $args[0] + "/" + $args[1] + "/" + "?depth=" + $args[2]
$restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} elseif ($args[0] -eq "trade_history") {
$URI = $baseURL + $args[0] + "/" + $args[1] + "/" + "?since=" + $args[2]
$restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
}
#Private API Access - Function WhatIF
#Again, pretty much straight forward here :\
elseif ($args[5] -eq $true) {
#TODO: Add APIKEY,APISECRET,USERNAME value check. Else throw error.
if ($args[0] -eq "balance")
{
Write-Debug ("Received Parameter: " + $args[0] + ", Requesting account balance...")
createSign
$URI = $baseURL + $args[0] + "/"
$_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp}
$restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} elseif ($args[0] -eq "open_orders")
{
Write-Debug ("Received Parameter: " + $args[0] + ", Requesting current open orders... ")
createSign
$URI = $baseURL + $args[0] + "/" + $args[1] + "/"
$_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp}
$restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} elseif ($args[0] -eq "cancel_order")
{
Write-Debug ("Received Parameter: " + $args[0] + ", Cancelling order with order ID " + $Args[2])
createSign
$URI = $baseURL + $args[0] + "/"
$_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp;id=$args[2]}
$restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} elseif ($args[0] -eq "place_order")
{
Write-Debug ("Received Parameter: " + $args[0] + ", Placing order with parameter " + $Args[1] + " - " + $Args[2] + " " + $Args[3] + " @ " + $Args[4])
createSign
$URI = $baseURL + $args[0] + "/" + $args[1] + "/"
$_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp;type=$args[2];amount=$args[3];price=$args[4]}
$restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
return $restClient
} else
{
Write-Debug ("Received Parameter: " + $args[0] + ", Unexepected Parameter Received")
$Host.UI.WriteErrorLine("ERROR: Unexpected Parameter have been received. Please check arguments!")
}
} else {
Write-Debug ("Received Parameter: " + $args[0] + ", Unexepected Parameter Received")
$Host.UI.WriteErrorLine("ERROR: Unexpected Parameter have been received. Please check arguments!")
}
}