UNPKG

trezor-link

Version:
140 lines (109 loc) 4.4 kB
"use strict"; // Helper module for converting Trezor's raw input to // ProtoBuf's message and from there to regular JSON to trezor.js Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageDecoder = undefined; 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 _protobufjsOldFixedWebpack = require("protobufjs-old-fixed-webpack"); var ProtoBuf = _interopRequireWildcard(_protobufjsOldFixedWebpack); var _messages = require("./messages.js"); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } 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 MessageDecoder = exports.MessageDecoder = function () { // message type number 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 // raw data to push to Trezor // Builders, generated by reading config _createClass(MessageDecoder, [{ key: "_messageInfo", value: function _messageInfo() { var r = this.messages.messagesByType[this.type]; if (r == null) { throw new Error("Method type not found - " + 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; }(); var MessageInfo = function MessageInfo(messageConstructor, name) { _classCallCheck(this, MessageInfo); this.messageConstructor = messageConstructor; this.name = name; }; // Converts any ProtoBuf message to JSON in Trezor.js-friendly format 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; }