UNPKG

@ledgerhq/hw-transport-http

Version:
76 lines 2.94 kB
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 _a; import Transport from "@ledgerhq/hw-transport"; import { TransportError } from "@ledgerhq/errors"; import axios from "axios"; import { log } from "@ledgerhq/logs"; /** * HTTP transport implementation */ class HttpTransport extends Transport { 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"); log("apdu", "=> " + apduHex); const response = yield axios({ method: "POST", url: this.url, headers: { Accept: "application/json", "Content-Type": "application/json", }, data: JSON.stringify({ apduHex, }), }); if (response.status !== 200) { throw new TransportError("failed to communicate to server. code=" + response.status, "HttpTransportStatus" + response.status); } const body = yield response.data; if (body.error) throw body.error; 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 axios({ url, timeout, }); if (response.status !== 200) { throw new TransportError("failed to access HttpTransport(" + url + "): status " + response.status, "HttpTransportNotAccessible"); } }); export default HttpTransport; //# sourceMappingURL=HttpTransport.js.map