Socket
Socket
Sign inDemoInstall

@ledgerhq/hw-transport-http

Package Overview
Dependencies
Maintainers
7
Versions
382
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ledgerhq/hw-transport-http - npm Package Compare versions

Comparing version 4.24.0 to 4.26.0-beta.0a38903c

lib/WebSocketTransport.js

148

lib/HttpTransport.js

@@ -6,68 +6,34 @@ "use strict";

});
exports.withStaticURL = undefined;
var _promise = require("babel-runtime/core-js/promise");
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _promise2 = _interopRequireDefault(_promise);
var _hwTransport = require("@ledgerhq/hw-transport");
var _stringify = require("babel-runtime/core-js/json/stringify");
var _hwTransport2 = _interopRequireDefault(_hwTransport);
var _stringify2 = _interopRequireDefault(_stringify);
var _axios = require("axios");
var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of");
var _axios2 = _interopRequireDefault(_axios);
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _regenerator = require("babel-runtime/regenerator");
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var _regenerator2 = _interopRequireDefault(_regenerator);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
var _inherits2 = require("babel-runtime/helpers/inherits");
var _inherits3 = _interopRequireDefault(_inherits2);
var _hwTransport = require("@ledgerhq/hw-transport");
var _hwTransport2 = _interopRequireDefault(_hwTransport);
var _withStaticURL = require("./withStaticURL");
var _withStaticURL2 = _interopRequireDefault(_withStaticURL);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.withStaticURL = _withStaticURL2.default;
/**
* HTTP transport implementation
*/
// NOTE in the future we might want to do WebSocket, because we could have the disconnect lifecycle hooked.
var HttpTransport = function (_Transport) {
_inherits(HttpTransport, _Transport);
var HttpTransport = function (_Transport) {
(0, _inherits3.default)(HttpTransport, _Transport);
(0, _createClass3.default)(HttpTransport, null, [{
_createClass(HttpTransport, null, [{
key: "open",
// this transport is not discoverable
value: function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(url, timeout) {
var response;
return _regenerator2.default.wrap(function _callee$(_context) {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url, timeout) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {

@@ -77,18 +43,8 @@ switch (_context.prev = _context.next) {

_context.next = 2;
return fetch(url, { timeout: timeout });
return HttpTransport.check(url, timeout);
case 2:
response = _context.sent;
if (!(response.status !== 200)) {
_context.next = 5;
break;
}
throw new _hwTransport.TransportError("failed to access HttpTransport(" + url + "): status " + response.status, "HttpTransportNotAccessible");
case 5:
return _context.abrupt("return", new HttpTransport(url));
case 6:
case 3:
case "end":

@@ -107,17 +63,11 @@ return _context.stop();

}()
// this transport is not discoverable
}]);
/**
* The full url of an http server.
* * a GET is expected to return a 200 status on this url.
* * a POST is expected to accept { apduHex, statusList } object in body and
returns { data, error } where either data is filled with an hex result
or error is filled with a text error.
* @example
* new HttpTransport("http://192.168.0.1/")
*/
function HttpTransport(url) {
(0, _classCallCheck3.default)(this, HttpTransport);
_classCallCheck(this, HttpTransport);
var _this = (0, _possibleConstructorReturn3.default)(this, (HttpTransport.__proto__ || (0, _getPrototypeOf2.default)(HttpTransport)).call(this));
var _this = _possibleConstructorReturn(this, (HttpTransport.__proto__ || Object.getPrototypeOf(HttpTransport)).call(this));

@@ -128,8 +78,8 @@ _this.url = url;

(0, _createClass3.default)(HttpTransport, [{
_createClass(HttpTransport, [{
key: "exchange",
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(apdu) {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(apdu) {
var response, body;
return _regenerator2.default.wrap(function _callee2$(_context2) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {

@@ -139,4 +89,5 @@ switch (_context2.prev = _context2.next) {

_context2.next = 2;
return fetch(this.url, {
return (0, _axios2.default)({
method: "POST",
url: this.url,
headers: {

@@ -146,3 +97,3 @@ Accept: "application/json",

},
body: (0, _stringify2.default)({ apduHex: apdu.toString("hex") })
data: JSON.stringify({ apduHex: apdu.toString("hex") })
});

@@ -162,3 +113,3 @@

_context2.next = 7;
return response.json();
return response.data;

@@ -198,5 +149,6 @@ case 7:

value: function close() {
return _promise2.default.resolve();
return Promise.resolve();
}
}]);
return HttpTransport;

@@ -206,7 +158,7 @@ }(_hwTransport2.default);

HttpTransport.isSupported = function () {
return _promise2.default.resolve(typeof fetch === "function");
return Promise.resolve(typeof fetch === "function");
};
HttpTransport.list = function () {
return _promise2.default.resolve([]);
return Promise.resolve([]);
};

@@ -220,3 +172,37 @@

HttpTransport.check = function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(url) {
var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
var response;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return (0, _axios2.default)(url, { timeout: timeout });
case 2:
response = _context3.sent;
if (!(response.status !== 200)) {
_context3.next = 5;
break;
}
throw new _hwTransport.TransportError("failed to access HttpTransport(" + url + "): status " + response.status, "HttpTransportNotAccessible");
case 5:
case "end":
return _context3.stop();
}
}
}, _callee3, undefined);
}));
return function (_x4) {
return _ref3.apply(this, arguments);
};
}();
exports.default = HttpTransport;
//# sourceMappingURL=HttpTransport.js.map
{
"name": "@ledgerhq/hw-transport-http",
"version": "4.24.0",
"version": "4.26.0-beta.0a38903c",
"description": "Ledger Hardware Wallet communication layer over http",

@@ -25,6 +25,8 @@ "keywords": [

},
"main": "lib/HttpTransport.js",
"main": "lib/withStaticURLs.js",
"license": "Apache-2.0",
"dependencies": {
"@ledgerhq/hw-transport": "^4.24.0"
"@ledgerhq/hw-transport": "^4.26.0-beta.0a38903c",
"axios": "^0.18.0",
"ws": "^6.1.0"
},

@@ -31,0 +33,0 @@ "devDependencies": {

//@flow
import Transport, { TransportError } from "@ledgerhq/hw-transport";
import withStaticURL from "./withStaticURL";
export { withStaticURL };
import axios from "axios";

@@ -9,3 +8,2 @@ /**

*/
// NOTE in the future we might want to do WebSocket, because we could have the disconnect lifecycle hooked.
export default class HttpTransport extends Transport<string> {

@@ -21,4 +19,4 @@ static isSupported = (): Promise<boolean> =>

static async open(url: string, timeout?: number) {
const response = await fetch(url, { timeout });
static check = async (url: string, timeout: number = 5000) => {
const response = await axios(url, { timeout });
if (response.status !== 200) {

@@ -33,2 +31,6 @@ throw new TransportError(

}
};
static async open(url: string, timeout?: number) {
await HttpTransport.check(url, timeout);
return new HttpTransport(url);

@@ -39,11 +41,2 @@ }

/**
* The full url of an http server.
* * a GET is expected to return a 200 status on this url.
* * a POST is expected to accept { apduHex, statusList } object in body and
returns { data, error } where either data is filled with an hex result
or error is filled with a text error.
* @example
* new HttpTransport("http://192.168.0.1/")
*/
constructor(url: string) {

@@ -55,4 +48,5 @@ super();

async exchange(apdu: Buffer): Promise<Buffer> {
const response = await fetch(this.url, {
const response = await axios({
method: "POST",
url: this.url,
headers: {

@@ -62,3 +56,3 @@ Accept: "application/json",

},
body: JSON.stringify({ apduHex: apdu.toString("hex") })
data: JSON.stringify({ apduHex: apdu.toString("hex") })
});

@@ -71,3 +65,3 @@ if (response.status !== 200) {

}
const body = await response.json();
const body = await response.data;
if (body.error) throw body.error;

@@ -74,0 +68,0 @@ return Buffer.from(body.data, "hex");

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc