CEX.IO - Powershell API


SUBMITTED BY: Guest

DATE: July 20, 2014, 1:48 a.m.

FORMAT: Text only

SIZE: 7.4 kB

HITS: 937

  1. #CEX.IO - Powershell API Wrapper For CEX.IO Trading Platform
  2. #Created by KazCrypto
  3. #Donation: (Highly Appreciated ^_^)
  4. #BTC: 1DFtAts5oj8AcXkF8AxuJdYThc5hvDUMYr
  5. #LTC: LPfqNz2aKfrRxBLXNUgiLsqPPiXF4jhVzk
  6. #DOGE: DT1Kmq7ba1xq8MWLkzdn1DN5Vkr6RgGm7a
  7. #---------------------------------------------------#
  8. # DEBUGING INFORMATION #
  9. #---------------------------------------------------#
  10. #Uncomment this variable to enable debug message and vice versa
  11. $DebugPreference = "Continue"
  12. $script:_USERID = ""
  13. $script:_APIKEY = ""
  14. $script:_APISEC = ""
  15. $HS256 = New-Object System.Security.Cryptography.HMACSHA256
  16. #---------------------------------------------------#
  17. # API FUNCTION #
  18. #---------------------------------------------------#
  19. #Function --> createVar "USERNAME" "APIKEY" "API_SECRET"
  20. #Usage : Store user information into variable for private api connection purpose
  21. function createVar
  22. {
  23. $script:_USERID = $args[0]
  24. Write-Debug ("Variable _USERID = " + $_USERID)
  25. $script:_APIKEY = $args[1]
  26. Write-Debug ("Variable _APIKEY = " + $_APIKEY)
  27. $script:_APISEC = $args[2]
  28. Write-Debug ("Variable _APISEC = " + $_APISEC)
  29. }
  30. #Function --> createSign
  31. #Usage : Create signature for functioning part of apiCall
  32. function createSign
  33. {
  34. $script:_TimeStamp = [int][double]::Parse((Get-Date -UFormat %s))
  35. #Some hack for maintaining timestamp for request purpose
  36. $script:rTimeStamp = $script:_TimeStamp
  37. Write-Debug ("Using Timestamp As Nonce = " + $script:rTimeStamp)
  38. #Combining nonce, username, and apikey to be hashed
  39. $_HMSG = $script:rTimeStamp.ToString() + $script:_USERID + $script:_APIKEY
  40. Write-Debug ("MSG --> Compute Hash = " + $script:_HMSG)
  41. #Init HMAC-SHA256 Key - Set HMAC-SHA256 Secret Key
  42. $HS256.Key = [System.Text.Encoding]::UTF8.GetBytes($_APISEC)
  43. Write-Debug ("_APISEC <> HS256KEY = " + [System.BitConverter]::ToString($HS256.Key)).Replace("-","").ToUpper()
  44. #Compute $_HMSG with HMAC-SHA256
  45. $script:_HRESULT = $HS256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($_HMSG))
  46. Write-Debug ("SIGHASH = " + [System.BitConverter]::ToString($script:_HRESULT).Replace("-","").ToUpper())
  47. }
  48. #Function --> apiCall
  49. #Usage : Communication function for accessing CEX.IO API
  50. function apiReq
  51. {
  52. #Set baseURL to CEX.IO api server
  53. $baseURL = "https://cex.io/api/"
  54. #Function WhatIF
  55. #Pretty much straight forward here :\
  56. IF ($args[0] -eq "ticker")
  57. {
  58. $URI = $baseURL + $args[0] + "/" + $args[1] + "/"
  59. $restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  60. return $restClient
  61. } elseif ($args[0] -eq "order_book") {
  62. $URI = $baseURL + $args[0] + "/" + $args[1] + "/" + "?depth=" + $args[2]
  63. $restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  64. return $restClient
  65. } elseif ($args[0] -eq "trade_history") {
  66. $URI = $baseURL + $args[0] + "/" + $args[1] + "/" + "?since=" + $args[2]
  67. $restClient = Invoke-RestMethod -Uri $URI -Method Get -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  68. return $restClient
  69. }
  70. #Private API Access - Function WhatIF
  71. #Again, pretty much straight forward here :\
  72. elseif ($args[5] -eq $true) {
  73. #TODO: Add APIKEY,APISECRET,USERNAME value check. Else throw error.
  74. if ($args[0] -eq "balance")
  75. {
  76. Write-Debug ("Received Parameter: " + $args[0] + ", Requesting account balance...")
  77. createSign
  78. $URI = $baseURL + $args[0] + "/"
  79. $_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp}
  80. $restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  81. return $restClient
  82. } elseif ($args[0] -eq "open_orders")
  83. {
  84. Write-Debug ("Received Parameter: " + $args[0] + ", Requesting current open orders... ")
  85. createSign
  86. $URI = $baseURL + $args[0] + "/" + $args[1] + "/"
  87. $_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp}
  88. $restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  89. return $restClient
  90. } elseif ($args[0] -eq "cancel_order")
  91. {
  92. Write-Debug ("Received Parameter: " + $args[0] + ", Cancelling order with order ID " + $Args[2])
  93. createSign
  94. $URI = $baseURL + $args[0] + "/"
  95. $_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp;id=$args[2]}
  96. $restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  97. return $restClient
  98. } elseif ($args[0] -eq "place_order")
  99. {
  100. Write-Debug ("Received Parameter: " + $args[0] + ", Placing order with parameter " + $Args[1] + " - " + $Args[2] + " " + $Args[3] + " @ " + $Args[4])
  101. createSign
  102. $URI = $baseURL + $args[0] + "/" + $args[1] + "/"
  103. $_param = @{key=$_APIKEY;signature=([System.BitConverter]::ToString($_HRESULT).Replace("-","").ToUpper());nonce=$script:rTimeStamp;type=$args[2];amount=$args[3];price=$args[4]}
  104. $restClient = Invoke-RestMethod -Uri $URI -Body $_param -Method POST -UserAgent "CEX-IO|PowerShellR0.1|API-ACCESS"
  105. return $restClient
  106. } else
  107. {
  108. Write-Debug ("Received Parameter: " + $args[0] + ", Unexepected Parameter Received")
  109. $Host.UI.WriteErrorLine("ERROR: Unexpected Parameter have been received. Please check arguments!")
  110. }
  111. } else {
  112. Write-Debug ("Received Parameter: " + $args[0] + ", Unexepected Parameter Received")
  113. $Host.UI.WriteErrorLine("ERROR: Unexpected Parameter have been received. Please check arguments!")
  114. }
  115. }

comments powered by Disqus