Code Monkey home page Code Monkey logo

polo-spot-sdk's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

polo-spot-sdk's Issues

docs make no sense

image

do you not proof-read what your intern publishes, or something? in what world does this make sense? is adding timestamp to query necessary, or not? considering it's in header already

No method in API using example

In example file of Node.js requests there are no method getSign in class Sign. Buy it is used in delete and put methods

401 error

Hi on Dart i try to reach AUth https. However i got 401 error. I followed every single instructions on api document. Signature i correct, payload is correct.. but i got 401 error.. Could you please help about this issue?

Thanks

import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

const String poloniexPrivateApiKey = "xxxxxxxxxx";
const String poloniexPrivateSecretKey =  "xxxxxxxx";


const String url = "https://api.poloniex.com";
const String publicApiUrl = "https://poloniex.com/public";

class PoloniexApiClient {
  static String v2Accounts = "/accounts";
  static String v2Orders = "/orders";
  static String v2Markets = "/markets";

  String apiKey;
  String secretKey;
  String host;

  PoloniexApiClient({this.apiKey = poloniexPrivateApiKey, this.secretKey = poloniexPrivateSecretKey, this.host = url});
  Future accounts() async {
    try {
      Map<String, dynamic> map = {};

      var response = await PoloniexSignatureHelper.generateResponse(apiKey, secretKey, host, v2Accounts, map);
      debugPrint("accountResponse: ${response.toString()}");
    } catch (e) {
      debugPrint(e.toString());
    }
  }
}

class PoloniexSignatureHelper {
  static String contentType = "Content-Type";
  static String jsonType = "application/json";
  static String requestMethodGet = "GET";
  static String requestMethodPost = "POST";
  static String requestMethodDelete = "DELETE";
  static String headerTimeStamp = "signTimestamp";
  static String headerKey = "key";
  static String headerSignMethod = "signatureMethod";
  static String headerSignVersion = "signatureVersion";
  static String headerSignature = "signature";
  static String signatureMethodValue = "HmacSHA256";
  static String signatureVersionValue = "2";

  static Future<String> generateResponse(String apiKey, secretKey, host, path, Map<String, dynamic> paramMap) async {
    String timestamp = DateTime.now().millisecondsSinceEpoch.toString();

    String payload = generateSignatureRequestString(requestMethodGet, path, timestamp, paramMap);
    print(payload);

    String signature = generateSignature(secretKey, payload);
    print(signature);

    String response = await executeRequest(host, path, apiKey, timestamp, signature, paramMap);
    print(response);
    return response;
  }

  static String generateSignatureRequestString(String method, path, timestamp, Map<String, dynamic> paramMap) {
    var sortedParamMap = sortMapASCII(paramMap, timestamp);

    var encoded;
    if (method == requestMethodGet) {
      encoded = sortedParamMap.keys
          .map((key) => "${Uri.encodeComponent(key)}=${Uri.encodeComponent(paramMap[key].toString())}")
          .join("&");
    } else {
      sortedParamMap.remove(headerTimeStamp);
      var requestBody = jsonEncode(sortedParamMap); 
      encoded = "requestBody=$requestBody&signTimestamp=$timestamp"; 
    }

    var requestString = method + "\n" + path + "\n" + encoded;

    return requestString;
  }

  static String generateSignature(String secretKey, String requestString) {
    final keyBytes = base64Decode(secretKey);
    final dataBytes = utf8.encode(requestString);

    final hMacBytes = Hmac(sha256, keyBytes).convert(dataBytes).bytes;
    final hMacBase64 = base64Encode(hMacBytes);
    var sign = hMacBase64;

    return sign;
  }

  static Future<String> executeRequest(
      String host, path, apiKey, timestamp, signature, Map<String, dynamic> paramMap) async {
    String url = host + path;

    if (paramMap.isNotEmpty && paramMap.length > 0) {
      var queryStringBuffer = "?";
      paramMap.forEach((key, value) {
        queryStringBuffer = queryStringBuffer + key + "=" + value.toString() + "&";
      });

      String queryString = queryStringBuffer.substring(0, queryStringBuffer.length - 1);
      url = url + queryString;
    }

    print(url);

    Map<String, String> _headers = {
      // contentType: jsonType,
      // contentType: "application/x-www-form-urlencoded",
      headerKey: apiKey,
      headerSignMethod: signatureMethodValue,
      headerSignVersion: signatureVersionValue,
      headerTimeStamp: timestamp,
      headerSignature: signature
    };

    var result;
    try {
      var response = await http.get(Uri.parse(url), headers: _headers);
      // if (response.statusCode == 200) {
      //   String data = response.body;
      //   result = data;
      // }

      result = response.statusCode.toString(); // return 401??
    } catch (e) {
      result = e.toString();
    }

    return result;
  }

  static Map<String, dynamic> sortMapASCII(Map<String, dynamic> paramMap, timestamp) {
    paramMap.addAll({headerTimeStamp: timestamp});
    var paramKeys = paramMap.keys.map((key) => "$key").toList();
    paramKeys.sort(((a, b) => a.compareTo(b)));

    Map<String, dynamic> sortedParamMap = {};
    paramKeys.forEach((key) => sortedParamMap.addAll({"$key": "${paramMap[key]}"}));
    print(sortedParamMap);
    return sortedParamMap;
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.