UNPKG

@altangent/lib-coinbase-prime

Version:
98 lines 3.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoinbaseClient = void 0; /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ const lib_http_1 = require("@altangent/lib-http"); const querystring_1 = __importDefault(require("querystring")); const crypto_1 = __importDefault(require("crypto")); class CoinbaseClient { constructor(key, keysecret, urlbase = "https://api.coinbase.com", version = "2016-02-11") { this.key = key; this.keysecret = keysecret; this.urlbase = urlbase; this.version = version; } getCurrentUser() { const path = "/v2/user"; return this.request(path); } getUser(userid) { const path = `/v2/user/${userid}`; return this.request(path); } getAccounts(options) { const path = `/v2/accounts`; const query = this._cleanOptions(options); return this.request(path, query); } getAccount(accountid) { const path = `/v2/accounts/${accountid}`; return this.request(path); } getAddresses(accountid, options) { const path = `/v2/accounts/${accountid}/addresses`; const query = this._cleanOptions(options); return this.request(path, query); } getAddress(accountid, address) { const path = `/v2/accounts/${accountid}/addresses/${address}`; return this.request(path); } getTransactions(accountid, options) { const path = `/v2/accounts/${accountid}/transactions`; const query = this._cleanOptions(options); return this.request(path, query); } getTransaction(accountid, transactionId) { const path = `/v2/accounts/${accountid}/transaction/${transactionId}`; return this.request(path); } async candles(marketId, start, end, granularity) { const url = `https://api.exchange.coinbase.com/products/${marketId}/candles?granularity=${granularity}&start=${start}&end=${end}`; const headers = { "User-Agent": "curl/7.74", }; return (0, lib_http_1.request)({ url, method: "GET", headers }); } request(path, query) { const key = this.key; const keysecret = this.keysecret; const pathQuery = query ? path + "?" + querystring_1.default.encode(query) : path; const timestamp = Math.floor(new Date().getTime() / 1000); const method = "GET"; const body = ""; const sig = this._sign(this._createSigData(timestamp, method, pathQuery, body), keysecret); const headers = { "CB-ACCESS-SIGN": sig.toString("hex"), "CB-ACCESS-TIMESTAMP": timestamp, "CB-ACCESS-KEY": key, "CB-VERSION": "2016-02-11", }; const url = this.urlbase + pathQuery; // eslint-disable-next-line @typescript-eslint/no-unsafe-call return (0, lib_http_1.request)({ url, method, headers }); } _cleanOptions(options) { const result = {}; for (const key in options) { if (options[key] !== undefined) { result[key] = options[key]; } } return result; } _createSigData(timestamp, method, path, body) { return `${timestamp}${method.toUpperCase()}${path}${body}`; } _sign(data, key) { const hmac = crypto_1.default.createHmac("sha256", key); hmac.update(data, "utf-8"); return hmac.digest(); } } exports.CoinbaseClient = CoinbaseClient; //# sourceMappingURL=CoinbaseClient.js.map