那真是一段旅程;搜索了一半的网络,学到了很多东西,这就是我想出的。更多功能可参考https://pastebin.com/iuezwGRZ。
如果您也在这里分享您的最终脚本,那就太好了。
$PoloniexKey = "01234567-89ABCDEF-GHIJKLMN-OPQRSTUV"
$PoloniexSecret = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghij"
$TradingUri = 'https://poloniex.com/tradingApi'
$PublicUri = 'https://poloniex.com/public'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# source: https://stackoverflow.com/questions/40680882/trying-to-make-hmac-sha256-work-with-powershell-for-canvas-api
function Buffer($string) {
$c=@()
Foreach ($element in $string.toCharArray()) {$c+= [System.Convert]::ToSByte($element)}
return $c
}
# source: https://www.remkoweijnen.nl/blog/2013/04/05/convert-bin-to-hex-and-hex-to-bin-in-powershell/
function BinToHex {
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true)
]
[Byte[]]$Bin)
# assume pipeline input if we don't have an array (surely there must be a better way)
if ($bin.Length -eq 1) {$bin = @($input)}
$return = -join ($Bin | foreach { "{0:X2}" -f $_ })
return $return.ToLower()
}
# source: https://gist.github.com/jokecamp/2c1a67b8f277797ecdb3
function HmacSHA512($Message, $Secret) {
$HMACSHA512 = new-object System.Security.Cryptography.HMACSHA512
$HMACSHA512.key = Buffer -string $Secret
$StringToHash = [System.Text.Encoding]::UTF8.GetBytes($Message)
$HashByteArray = $HMACSHA512.ComputeHash($StringToHash)
$hash = BinToHex $HashByteArray
return $hash;
}
function Invoke-PoloniexRequest($Request, $Uri=$TradingUri) {
$Nonce = ([int64](Get-Date (get-date).touniversaltime() -UFormat %s))*10
$Request.Add('nonce', $Nonce)
$RequestArray=@()
$Request.GetEnumerator() | %{$RequestArray += ($_.Name,$_.Value -join '=')}
$Body = $RequestArray -join '&'
$Sign = HmacSHA512 -Message $Body -Secret $PoloniexSecret
$Headers = @{
Key = $PoloniexKey
Sign = $Sign
}
Invoke-WebRequest -Uri $Uri -Method POST -Body $Body -Headers $Headers | ConvertFrom-Json
}
function Get-PoloniexBalances {
$request = @{command="returnBalances"}
return Invoke-PoloniexRequest -Request $request
}
$Balances = Get-PoloniexBalances