UNPKG

7.55 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.receiveOne = receiveOne;
7exports.receiveAndParse = receiveAndParse;
8
9var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
10
11var _message_decoder = require("./protobuf/message_decoder.js");
12
13var _protobufjsOldFixedWebpack = require("protobufjs-old-fixed-webpack");
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
18
19function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _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); } }
24
25function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
26
27var MESSAGE_HEADER_BYTE = 0x23; // input that might or might not be fully parsed yet
28
29var PartiallyParsedInput =
30/*#__PURE__*/
31function () {
32 // Message type number
33 // Expected length of the raq message, in bytes
34 // Buffer with the beginning of message; can be non-complete and WILL be modified
35 // during the object's lifetime
36 function PartiallyParsedInput(typeNumber, length) {
37 _classCallCheck(this, PartiallyParsedInput);
38
39 this.typeNumber = typeNumber;
40 this.expectedLength = length;
41 this.buffer = new _protobufjsOldFixedWebpack.ByteBuffer(length);
42 }
43
44 _createClass(PartiallyParsedInput, [{
45 key: "isDone",
46 value: function isDone() {
47 return this.buffer.offset >= this.expectedLength;
48 }
49 }, {
50 key: "append",
51 value: function append(buffer) {
52 this.buffer.append(buffer);
53 }
54 }, {
55 key: "arrayBuffer",
56 value: function arrayBuffer() {
57 var byteBuffer = this.buffer;
58 byteBuffer.reset();
59 return byteBuffer.toArrayBuffer();
60 }
61 }]);
62
63 return PartiallyParsedInput;
64}(); // Parses first raw input that comes from Trezor and returns some information about the whole message.
65
66
67function parseFirstInput(bytes) {
68 // convert to ByteBuffer so it's easier to read
69 var byteBuffer = _protobufjsOldFixedWebpack.ByteBuffer.concat([bytes]); // checking first two bytes
70
71
72 var sharp1 = byteBuffer.readByte();
73 var sharp2 = byteBuffer.readByte();
74
75 if (sharp1 !== MESSAGE_HEADER_BYTE || sharp2 !== MESSAGE_HEADER_BYTE) {
76 throw new Error("Didn't receive expected header signature.");
77 } // reading things from header
78
79
80 var type = byteBuffer.readUint16();
81 var length = byteBuffer.readUint32(); // creating a new buffer with the right size
82
83 var res = new PartiallyParsedInput(type, length);
84 res.append(byteBuffer);
85 return res;
86} // If the whole message wasn't loaded in the first input, loads more inputs until everything is loaded.
87// note: the return value is not at all important since it's still the same parsedinput
88
89
90function receiveRest(_x, _x2) {
91 return _receiveRest.apply(this, arguments);
92} // Receives the whole message as a raw data buffer (but without headers or type info)
93
94
95function _receiveRest() {
96 _receiveRest = _asyncToGenerator(
97 /*#__PURE__*/
98 _regenerator["default"].mark(function _callee(parsedInput, receiver) {
99 var data;
100 return _regenerator["default"].wrap(function _callee$(_context) {
101 while (1) {
102 switch (_context.prev = _context.next) {
103 case 0:
104 if (!parsedInput.isDone()) {
105 _context.next = 2;
106 break;
107 }
108
109 return _context.abrupt("return");
110
111 case 2:
112 _context.next = 4;
113 return receiver();
114
115 case 4:
116 data = _context.sent;
117
118 if (!(data == null)) {
119 _context.next = 7;
120 break;
121 }
122
123 throw new Error("Received no data.");
124
125 case 7:
126 parsedInput.append(data);
127 return _context.abrupt("return", receiveRest(parsedInput, receiver));
128
129 case 9:
130 case "end":
131 return _context.stop();
132 }
133 }
134 }, _callee);
135 }));
136 return _receiveRest.apply(this, arguments);
137}
138
139function receiveBuffer(_x3) {
140 return _receiveBuffer.apply(this, arguments);
141}
142
143function _receiveBuffer() {
144 _receiveBuffer = _asyncToGenerator(
145 /*#__PURE__*/
146 _regenerator["default"].mark(function _callee2(receiver) {
147 var data, partialInput;
148 return _regenerator["default"].wrap(function _callee2$(_context2) {
149 while (1) {
150 switch (_context2.prev = _context2.next) {
151 case 0:
152 _context2.next = 2;
153 return receiver();
154
155 case 2:
156 data = _context2.sent;
157 partialInput = parseFirstInput(data);
158 _context2.next = 6;
159 return receiveRest(partialInput, receiver);
160
161 case 6:
162 return _context2.abrupt("return", partialInput);
163
164 case 7:
165 case "end":
166 return _context2.stop();
167 }
168 }
169 }, _callee2);
170 }));
171 return _receiveBuffer.apply(this, arguments);
172}
173
174function receiveOne(messages, data) {
175 var byteBuffer = _protobufjsOldFixedWebpack.ByteBuffer.concat([data]);
176
177 var typeId = byteBuffer.readUint16();
178 byteBuffer.readUint32(); // length, ignoring
179
180 var decoder = new _message_decoder.MessageDecoder(messages, typeId, byteBuffer.toArrayBuffer());
181 return {
182 message: decoder.decodedJSON(),
183 type: decoder.messageName()
184 };
185} // Reads data from device and returns decoded message, that can be sent back to trezor.js
186
187
188function receiveAndParse(_x4, _x5) {
189 return _receiveAndParse.apply(this, arguments);
190}
191
192function _receiveAndParse() {
193 _receiveAndParse = _asyncToGenerator(
194 /*#__PURE__*/
195 _regenerator["default"].mark(function _callee3(messages, receiver) {
196 var received, typeId, buffer, decoder;
197 return _regenerator["default"].wrap(function _callee3$(_context3) {
198 while (1) {
199 switch (_context3.prev = _context3.next) {
200 case 0:
201 _context3.next = 2;
202 return receiveBuffer(receiver);
203
204 case 2:
205 received = _context3.sent;
206 typeId = received.typeNumber;
207 buffer = received.arrayBuffer();
208 decoder = new _message_decoder.MessageDecoder(messages, typeId, buffer);
209 return _context3.abrupt("return", {
210 message: decoder.decodedJSON(),
211 type: decoder.messageName()
212 });
213
214 case 7:
215 case "end":
216 return _context3.stop();
217 }
218 }
219 }, _callee3);
220 }));
221 return _receiveAndParse.apply(this, arguments);
222}
\No newline at end of file