UNPKG

@ledgerhq/hw-transport-http

Version:
81 lines 3.29 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); const hw_transport_1 = __importDefault(require("@ledgerhq/hw-transport")); const errors_1 = require("@ledgerhq/errors"); const axios_1 = __importDefault(require("axios")); const logs_1 = require("@ledgerhq/logs"); /** * HTTP transport implementation */ class HttpTransport extends hw_transport_1.default { static open(url, timeout) { return __awaiter(this, void 0, void 0, function* () { yield _a.check(url, timeout); return new _a(url); }); } constructor(url) { super(); this.url = url; } exchange(apdu) { return __awaiter(this, void 0, void 0, function* () { const apduHex = apdu.toString("hex"); (0, logs_1.log)("apdu", "=> " + apduHex); const response = yield (0, axios_1.default)({ method: "POST", url: this.url, headers: { Accept: "application/json", "Content-Type": "application/json", }, data: JSON.stringify({ apduHex, }), }); if (response.status !== 200) { throw new errors_1.TransportError("failed to communicate to server. code=" + response.status, "HttpTransportStatus" + response.status); } const body = yield response.data; if (body.error) throw body.error; (0, logs_1.log)("apdu", "<= " + body.data); return Buffer.from(body.data, "hex"); }); } setScrambleKey() { } close() { return Promise.resolve(); } } _a = HttpTransport; HttpTransport.isSupported = () => Promise.resolve(typeof fetch === "function"); // this transport is not discoverable HttpTransport.list = () => Promise.resolve([]); HttpTransport.listen = (_observer) => ({ unsubscribe: () => { }, }); HttpTransport.check = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, timeout = 5000) { const response = yield (0, axios_1.default)({ url, timeout, }); if (response.status !== 200) { throw new errors_1.TransportError("failed to access HttpTransport(" + url + "): status " + response.status, "HttpTransportNotAccessible"); } }); exports.default = HttpTransport; //# sourceMappingURL=HttpTransport.js.map