UNPKG

trezor-link

Version:
137 lines (105 loc) 5.39 kB
"use strict"; // Helper module for converting Trezor's raw input to // ProtoBuf's message and from there to regular JSON to trezor.js function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageDecoder = void 0; var ProtoBuf = _interopRequireWildcard(require("protobufjs-old-fixed-webpack")); var _messages = require("./messages.js"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ByteBuffer = ProtoBuf.ByteBuffer; var Long = ProtoBuf.Long; var MessageInfo = function MessageInfo(messageConstructor, name) { _classCallCheck(this, MessageInfo); this.messageConstructor = messageConstructor; this.name = name; }; var MessageDecoder = /*#__PURE__*/function () { // Builders, generated by reading config // message type number // raw data to push to Trezor function MessageDecoder(messages, type, data) { _classCallCheck(this, MessageDecoder); this.type = type; this.data = data; this.messages = messages; } // Returns an info about this message, // which includes the constructor object and a name _createClass(MessageDecoder, [{ key: "_messageInfo", value: function _messageInfo() { var r = this.messages.messagesByType[this.type]; if (r == null) { throw new Error("Method type not found - ".concat(this.type)); } return new MessageInfo(r.constructor, r.name); } // Returns the name of the message }, { key: "messageName", value: function messageName() { return this._messageInfo().name; } // Returns the actual decoded message, as a ProtoBuf.js object }, { key: "_decodedMessage", value: function _decodedMessage() { var constructor = this._messageInfo().messageConstructor; return constructor.decode(this.data); } // Returns the message decoded to JSON, that could be handed back // to trezor.js }, { key: "decodedJSON", value: function decodedJSON() { var decoded = this._decodedMessage(); var converted = messageToJSON(decoded); return JSON.parse(JSON.stringify(converted)); } }]); return MessageDecoder; }(); // Converts any ProtoBuf message to JSON in Trezor.js-friendly format exports.MessageDecoder = MessageDecoder; function messageToJSON(message) { var res = {}; var meta = message.$type; var _loop = function _loop(key) { var value = message[key]; if (typeof value === "function") {// ignoring } else if (value instanceof ByteBuffer) { var hex = value.toHex(); res[key] = hex; } else if (value instanceof Long) { var num = value.toNumber(); res[key] = num; } else if (Array.isArray(value)) { var decodedArr = value.map(function (i) { if (typeof i === "object") { return messageToJSON(i); } else { return i; } }); res[key] = decodedArr; } else if (value instanceof ProtoBuf.Builder.Message) { res[key] = messageToJSON(value); } else if (meta._fieldsByName[key].type.name === "enum") { if (value == null) { res[key] = null; } else { var enumValues = meta._fieldsByName[key].resolvedType.getChildren(); res[key] = enumValues.find(function (e) { return e.id === value; }).name; } } else { res[key] = value; } }; for (var key in message) { _loop(key); } return res; }