IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

API Bittrex en powershell depuis curl [PowerShell]


Sujet :

Scripts/Batch

  1. #1
    Membre confirmé
    Homme Profil pro
    Etudiant administrateur systèmes et réseaux
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Etudiant administrateur systèmes et réseaux

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 467
    Points
    467
    Par défaut API Bittrex en powershell depuis curl
    Bonjour,

    Je souhaite convertir le code curl suivant vers powershell

    Code Curl : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $apikey='xxx';
    $apisecret='xxx';
    $nonce=time();
    $uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
    $sign=hash_hmac('sha512',$uri,$apisecret);
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
    $execResult = curl_exec($ch);
    $obj = json_decode($execResult);

    J'en ai déduis ceci, a tort ou a raison.

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    $api_publicKey = "X"
    $api_privatKey = "Y"
    $nonce = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat "%s"))
    echo $nonce
    $uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + $api_publicKey + '&nonce=' + $nonce
    echo $uri
    $hmac_sha = New-Object System.Security.Cryptography.HMACSHA512
    $hmac_sha.key = [Text.Encoding]::ASCII.GetBytes($api_privatKey)
    $signature = $hmac_sha.ComputeHash([Text.Encoding]::ASCII.GetBytes($uri))
    $signature = [Convert]::ToBase64String($signature)
    echo $signature
    $hdrs = @{}
    $hdrs.Add("apisign",$signature)
    echo $hdrs
    $json_bittrex_api = (Invoke-WebRequest $uri -Headers $hdrs).content | ConvertFrom-Json 
    echo $json_bittrex_api

    Résultat après exécution.

    success : False
    message : INVALID_SIGNATURE
    result :

    Comme vous pouvez le constater la requête aboutie mais visiblement mon apisign n'est pas bonne.
    Je suppose que problème provient de mon code powershell pour le calcul de la signature.
    Je n'arrive pas à trouver l'erreur.
    Auriez-vous une idée.

    Merci d'avance.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  2. #2
    Membre confirmé
    Homme Profil pro
    Etudiant administrateur systèmes et réseaux
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Etudiant administrateur systèmes et réseaux

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 467
    Points
    467
    Par défaut
    Bon après quelques bonnes heures de galères ... XD Voilà une fonction...fonctionelle
    Le problème venait du fait que le SHA devait être au format hexadecimal et pas string...
    Pour le curl la conversion semble implicite. Ce n'est pas le cas pour le PS XD
    Ca m'a forcé à reprendre le code plus ou moins entièrement.
    En espérant que ça serve à d'autre.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    Function Get-EpochTime()
    {
        $D = ("01/01/1970" -as [DateTime])
        [int]$int_nonce = ((New-TimeSpan -Start $D -End ([DateTime]::UtcNow)).TotalSeconds -as [string])
        [string]$str_nonce = ($int_nonce -as [string])
        return $str_nonce
    }
    
    Function Get-jsonBittrex()
    {
        ### Bittrex informations
        $bittrex_api_public_key = 'X'
        $bittrex_api_privat_key = 'Y'
        $byte_privat = [Text.Encoding]::ASCII.GetBytes($bittrex_api_privat_key)
    
        ### Bittrex nonce value
        $nonce = Get-EpochTime
    
        $bittrex_api_url_raw   = "https://bittrex.com/api/v1.1/account/getbalances?apikey=$($bittrex_api_public_key)&nonce=$($nonce)"
        $bittrex_api_url_utf8  = New-Object System.Text.UTF8Encoding
        $bittrex_api_url_bytes = $bittrex_api_url_utf8.GetBytes($bittrex_api_url_raw)
    
        ### Converting the sha to hexadecimal
        $sha_raw     = New-Object System.Security.Cryptography.HMACSHA512
        $sha_raw.key = $byte_privat
        $sha_byt     = $sha_raw.ComputeHash($bittrex_api_url_bytes)
        $sha_hex     = [System.BitConverter]::ToString($sha_byt) -replace "-";
    
        ### Http Header that contains the Bittrex signature
        $header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $header.Add("apisign",$sha_hex)
    
        ### Time to ROX
        $http_request = (Invoke-WebRequest $bittrex_api_url_raw -Headers $header).content 
        $bittrex_json = $http_request | ConvertFrom-Json
        return $bittrex_json;   
    }
    et quelques lignes pour récupérer des infos

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    $bittrex_json = Get-jsonBittrex
    $XRP = $bittrex_json.Result | where { $_.Currency -eq "XRP" }
    $SYS = $bittrex_json.Result | where { $_.Currency -eq "SYS" }
    echo $XRP.Currency $XRP.Available
    echo $SYS.Currency $SYS.Available
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Besoin d'aide pour transcription C# vers VB.NET
    Par Stephane_br dans le forum C#
    Réponses: 5
    Dernier message: 09/02/2012, 15h51
  2. Besoin d'aide pour un script shell
    Par lecharcutierdelinux dans le forum Linux
    Réponses: 5
    Dernier message: 20/05/2006, 10h36
  3. aide pour un script javascript
    Par speedylol dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 22/03/2006, 14h28
  4. [Tableaux] aide pour un script
    Par jim1 dans le forum Langage
    Réponses: 3
    Dernier message: 22/01/2006, 20h48
  5. [langage] aide pour un script pliz
    Par biog dans le forum Langage
    Réponses: 6
    Dernier message: 17/05/2005, 13h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo