UNPKG

@zebec-network/exchange-card-sdk

Version:
93 lines (92 loc) 3.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZebecCardAPIService = void 0; const axios_1 = __importDefault(require("axios")); const crypto_1 = __importDefault(require("crypto")); const constants_1 = require("../constants"); class ZebecCardAPIService { apiConfig; sdkVersion = "1.0.0"; api; constructor(apiConfig, sandbox) { this.apiConfig = { ...apiConfig, apiUrl: sandbox ? constants_1.CARD_API_URL.Sandbox : constants_1.CARD_API_URL.Production, }; this.api = axios_1.default.create({ baseURL: this.apiConfig.apiUrl }); } // Generate request signature generateSignature(method, path, timestamp, body) { const stringToSign = [ method.toUpperCase(), path, timestamp, this.apiConfig.apiKey, body ? JSON.stringify(body) : "", ].join(""); return crypto_1.default .createHmac("sha256", this.apiConfig.encryptionKey) .update(stringToSign) .digest("hex"); } // Generate request headers generateRequestHeaders(method, path, body) { const timestamp = Math.floor(Date.now() / 1000); const nonce = crypto_1.default.randomBytes(16).toString("hex"); return { "X-API-Key": this.apiConfig.apiKey, "X-Timestamp": timestamp.toString(), "X-Nonce": nonce, "X-Signature": this.generateSignature(method, path, timestamp, body), "X-SDK-Version": this.sdkVersion, "Content-Type": "application/json", }; } // Encrypt sensitive data fields encryptSensitiveData(data) { const iv = crypto_1.default.randomBytes(16); const key = crypto_1.default.pbkdf2Sync(this.apiConfig.encryptionKey, iv, 1000, 32, "sha256"); const cipher = crypto_1.default.createCipheriv("aes-256-gcm", key, iv); let encrypted = cipher.update(JSON.stringify(data), "utf8", "base64"); encrypted += cipher.final("base64"); const authTag = cipher.getAuthTag(); return `${iv.toString("base64")}:${encrypted}:${authTag.toString("base64")}`; } // Ping API status async ping() { try { await this.api.get("/ping"); return true; } catch (error) { throw new Error("Card service is down. Please try again later."); } } // Purchase Card async purchaseCard(data) { console.debug("Payload data:", data); const encryptedData = this.encryptSensitiveData(data); console.debug("Encrypted Data: %s \n", encryptedData); const method = "POST"; const path = "/orders/create"; const url = this.apiConfig.apiUrl + path; const payload = { data: encryptedData }; const headers = this.generateRequestHeaders(method, path, payload); const response = await axios_1.default.post(url, payload, { headers }); return response; } // Fetch quote async fetchQuote(symbol) { const response = await this.api.get("/exchange/price", { params: { symbol } }); const data = response.data; return data.data; } async fetchVault(symbol) { const { data } = await this.api.get(`/exchange/deposit-address`, { params: { symbol } }); return data.data; } } exports.ZebecCardAPIService = ZebecCardAPIService;