UNPKG

19.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5/* eslint-disable no-continue */
6/* eslint-disable no-param-reassign */
7/* eslint-disable no-prototype-builtins */
8var errorClasses = {};
9var deserializers = {};
10var addCustomErrorDeserializer = function (name, deserializer) {
11 deserializers[name] = deserializer;
12};
13var createCustomErrorClass = function (name) {
14 var C = function CustomError(message, fields) {
15 Object.assign(this, fields);
16 this.name = name;
17 this.message = message || name;
18 this.stack = new Error().stack;
19 };
20 C.prototype = new Error();
21 errorClasses[name] = C;
22 return C;
23};
24// inspired from https://github.com/programble/errio/blob/master/index.js
25var deserializeError = function (object) {
26 if (typeof object === "object" && object) {
27 try {
28 // $FlowFixMe FIXME HACK
29 var msg = JSON.parse(object.message);
30 if (msg.message && msg.name) {
31 object = msg;
32 }
33 }
34 catch (e) {
35 // nothing
36 }
37 var error = void 0;
38 if (typeof object.name === "string") {
39 var name_1 = object.name;
40 var des = deserializers[name_1];
41 if (des) {
42 error = des(object);
43 }
44 else {
45 var constructor = name_1 === "Error" ? Error : errorClasses[name_1];
46 if (!constructor) {
47 console.warn("deserializing an unknown class '" + name_1 + "'");
48 constructor = createCustomErrorClass(name_1);
49 }
50 error = Object.create(constructor.prototype);
51 try {
52 for (var prop in object) {
53 if (object.hasOwnProperty(prop)) {
54 error[prop] = object[prop];
55 }
56 }
57 }
58 catch (e) {
59 // sometimes setting a property can fail (e.g. .name)
60 }
61 }
62 }
63 else {
64 error = new Error(object.message);
65 }
66 if (!error.stack && Error.captureStackTrace) {
67 Error.captureStackTrace(error, deserializeError);
68 }
69 return error;
70 }
71 return new Error(String(object));
72};
73// inspired from https://github.com/sindresorhus/serialize-error/blob/master/index.js
74var serializeError = function (value) {
75 if (!value)
76 return value;
77 if (typeof value === "object") {
78 return destroyCircular(value, []);
79 }
80 if (typeof value === "function") {
81 return "[Function: " + (value.name || "anonymous") + "]";
82 }
83 return value;
84};
85// https://www.npmjs.com/package/destroy-circular
86function destroyCircular(from, seen) {
87 var to = {};
88 seen.push(from);
89 for (var _i = 0, _a = Object.keys(from); _i < _a.length; _i++) {
90 var key = _a[_i];
91 var value = from[key];
92 if (typeof value === "function") {
93 continue;
94 }
95 if (!value || typeof value !== "object") {
96 to[key] = value;
97 continue;
98 }
99 if (seen.indexOf(from[key]) === -1) {
100 to[key] = destroyCircular(from[key], seen.slice(0));
101 continue;
102 }
103 to[key] = "[Circular]";
104 }
105 if (typeof from.name === "string") {
106 to.name = from.name;
107 }
108 if (typeof from.message === "string") {
109 to.message = from.message;
110 }
111 if (typeof from.stack === "string") {
112 to.stack = from.stack;
113 }
114 return to;
115}
116
117var AccountNameRequiredError = createCustomErrorClass("AccountNameRequired");
118var AccountNotSupported = createCustomErrorClass("AccountNotSupported");
119var AmountRequired = createCustomErrorClass("AmountRequired");
120var BluetoothRequired = createCustomErrorClass("BluetoothRequired");
121var BtcUnmatchedApp = createCustomErrorClass("BtcUnmatchedApp");
122var CantOpenDevice = createCustomErrorClass("CantOpenDevice");
123var CashAddrNotSupported = createCustomErrorClass("CashAddrNotSupported");
124var CurrencyNotSupported = createCustomErrorClass("CurrencyNotSupported");
125var DeviceAppVerifyNotSupported = createCustomErrorClass("DeviceAppVerifyNotSupported");
126var DeviceGenuineSocketEarlyClose = createCustomErrorClass("DeviceGenuineSocketEarlyClose");
127var DeviceNotGenuineError = createCustomErrorClass("DeviceNotGenuine");
128var DeviceOnDashboardExpected = createCustomErrorClass("DeviceOnDashboardExpected");
129var DeviceOnDashboardUnexpected = createCustomErrorClass("DeviceOnDashboardUnexpected");
130var DeviceInOSUExpected = createCustomErrorClass("DeviceInOSUExpected");
131var DeviceHalted = createCustomErrorClass("DeviceHalted");
132var DeviceNameInvalid = createCustomErrorClass("DeviceNameInvalid");
133var DeviceSocketFail = createCustomErrorClass("DeviceSocketFail");
134var DeviceSocketNoBulkStatus = createCustomErrorClass("DeviceSocketNoBulkStatus");
135var DisconnectedDevice = createCustomErrorClass("DisconnectedDevice");
136var DisconnectedDeviceDuringOperation = createCustomErrorClass("DisconnectedDeviceDuringOperation");
137var EnpointConfigError = createCustomErrorClass("EnpointConfig");
138var EthAppPleaseEnableContractData = createCustomErrorClass("EthAppPleaseEnableContractData");
139var FeeEstimationFailed = createCustomErrorClass("FeeEstimationFailed");
140var FirmwareNotRecognized = createCustomErrorClass("FirmwareNotRecognized");
141var HardResetFail = createCustomErrorClass("HardResetFail");
142var InvalidXRPTag = createCustomErrorClass("InvalidXRPTag");
143var InvalidAddress = createCustomErrorClass("InvalidAddress");
144var InvalidAddressBecauseDestinationIsAlsoSource = createCustomErrorClass("InvalidAddressBecauseDestinationIsAlsoSource");
145var LatestMCUInstalledError = createCustomErrorClass("LatestMCUInstalledError");
146var UnknownMCU = createCustomErrorClass("UnknownMCU");
147var LedgerAPIError = createCustomErrorClass("LedgerAPIError");
148var LedgerAPIErrorWithMessage = createCustomErrorClass("LedgerAPIErrorWithMessage");
149var LedgerAPINotAvailable = createCustomErrorClass("LedgerAPINotAvailable");
150var ManagerAppAlreadyInstalledError = createCustomErrorClass("ManagerAppAlreadyInstalled");
151var ManagerAppRelyOnBTCError = createCustomErrorClass("ManagerAppRelyOnBTC");
152var ManagerAppDepInstallRequired = createCustomErrorClass("ManagerAppDepInstallRequired");
153var ManagerAppDepUninstallRequired = createCustomErrorClass("ManagerAppDepUninstallRequired");
154var ManagerDeviceLockedError = createCustomErrorClass("ManagerDeviceLocked");
155var ManagerFirmwareNotEnoughSpaceError = createCustomErrorClass("ManagerFirmwareNotEnoughSpace");
156var ManagerNotEnoughSpaceError = createCustomErrorClass("ManagerNotEnoughSpace");
157var ManagerUninstallBTCDep = createCustomErrorClass("ManagerUninstallBTCDep");
158var NetworkDown = createCustomErrorClass("NetworkDown");
159var NoAddressesFound = createCustomErrorClass("NoAddressesFound");
160var NotEnoughBalance = createCustomErrorClass("NotEnoughBalance");
161var NotEnoughBalanceToDelegate = createCustomErrorClass("NotEnoughBalanceToDelegate");
162var NotEnoughBalanceInParentAccount = createCustomErrorClass("NotEnoughBalanceInParentAccount");
163var NotEnoughSpendableBalance = createCustomErrorClass("NotEnoughSpendableBalance");
164var NotEnoughBalanceBecauseDestinationNotCreated = createCustomErrorClass("NotEnoughBalanceBecauseDestinationNotCreated");
165var NoAccessToCamera = createCustomErrorClass("NoAccessToCamera");
166var NotEnoughGas = createCustomErrorClass("NotEnoughGas");
167var NotSupportedLegacyAddress = createCustomErrorClass("NotSupportedLegacyAddress");
168var GasLessThanEstimate = createCustomErrorClass("GasLessThanEstimate");
169var PasswordsDontMatchError = createCustomErrorClass("PasswordsDontMatch");
170var PasswordIncorrectError = createCustomErrorClass("PasswordIncorrect");
171var RecommendSubAccountsToEmpty = createCustomErrorClass("RecommendSubAccountsToEmpty");
172var RecommendUndelegation = createCustomErrorClass("RecommendUndelegation");
173var TimeoutTagged = createCustomErrorClass("TimeoutTagged");
174var UnexpectedBootloader = createCustomErrorClass("UnexpectedBootloader");
175var MCUNotGenuineToDashboard = createCustomErrorClass("MCUNotGenuineToDashboard");
176var RecipientRequired = createCustomErrorClass("RecipientRequired");
177var UnavailableTezosOriginatedAccountReceive = createCustomErrorClass("UnavailableTezosOriginatedAccountReceive");
178var UnavailableTezosOriginatedAccountSend = createCustomErrorClass("UnavailableTezosOriginatedAccountSend");
179var UpdateFetchFileFail = createCustomErrorClass("UpdateFetchFileFail");
180var UpdateIncorrectHash = createCustomErrorClass("UpdateIncorrectHash");
181var UpdateIncorrectSig = createCustomErrorClass("UpdateIncorrectSig");
182var UpdateYourApp = createCustomErrorClass("UpdateYourApp");
183var UserRefusedDeviceNameChange = createCustomErrorClass("UserRefusedDeviceNameChange");
184var UserRefusedAddress = createCustomErrorClass("UserRefusedAddress");
185var UserRefusedFirmwareUpdate = createCustomErrorClass("UserRefusedFirmwareUpdate");
186var UserRefusedAllowManager = createCustomErrorClass("UserRefusedAllowManager");
187var UserRefusedOnDevice = createCustomErrorClass("UserRefusedOnDevice"); // TODO rename because it's just for transaction refusal
188var TransportOpenUserCancelled = createCustomErrorClass("TransportOpenUserCancelled");
189var TransportInterfaceNotAvailable = createCustomErrorClass("TransportInterfaceNotAvailable");
190var TransportRaceCondition = createCustomErrorClass("TransportRaceCondition");
191var TransportWebUSBGestureRequired = createCustomErrorClass("TransportWebUSBGestureRequired");
192var DeviceShouldStayInApp = createCustomErrorClass("DeviceShouldStayInApp");
193var WebsocketConnectionError = createCustomErrorClass("WebsocketConnectionError");
194var WebsocketConnectionFailed = createCustomErrorClass("WebsocketConnectionFailed");
195var WrongDeviceForAccount = createCustomErrorClass("WrongDeviceForAccount");
196var WrongAppForCurrency = createCustomErrorClass("WrongAppForCurrency");
197var ETHAddressNonEIP = createCustomErrorClass("ETHAddressNonEIP");
198var CantScanQRCode = createCustomErrorClass("CantScanQRCode");
199var FeeNotLoaded = createCustomErrorClass("FeeNotLoaded");
200var FeeRequired = createCustomErrorClass("FeeRequired");
201var FeeTooHigh = createCustomErrorClass("FeeTooHigh");
202var SyncError = createCustomErrorClass("SyncError");
203var PairingFailed = createCustomErrorClass("PairingFailed");
204var GenuineCheckFailed = createCustomErrorClass("GenuineCheckFailed");
205var LedgerAPI4xx = createCustomErrorClass("LedgerAPI4xx");
206var LedgerAPI5xx = createCustomErrorClass("LedgerAPI5xx");
207var FirmwareOrAppUpdateRequired = createCustomErrorClass("FirmwareOrAppUpdateRequired");
208// db stuff, no need to translate
209var NoDBPathGiven = createCustomErrorClass("NoDBPathGiven");
210var DBWrongPassword = createCustomErrorClass("DBWrongPassword");
211var DBNotReset = createCustomErrorClass("DBNotReset");
212/**
213 * TransportError is used for any generic transport errors.
214 * e.g. Error thrown when data received by exchanges are incorrect or if exchanged failed to communicate with the device for various reason.
215 */
216function TransportError(message, id) {
217 this.name = "TransportError";
218 this.message = message;
219 this.stack = new Error().stack;
220 this.id = id;
221}
222TransportError.prototype = new Error();
223addCustomErrorDeserializer("TransportError", function (e) { return new TransportError(e.message, e.id); });
224var StatusCodes = {
225 PIN_REMAINING_ATTEMPTS: 0x63c0,
226 INCORRECT_LENGTH: 0x6700,
227 MISSING_CRITICAL_PARAMETER: 0x6800,
228 COMMAND_INCOMPATIBLE_FILE_STRUCTURE: 0x6981,
229 SECURITY_STATUS_NOT_SATISFIED: 0x6982,
230 CONDITIONS_OF_USE_NOT_SATISFIED: 0x6985,
231 INCORRECT_DATA: 0x6a80,
232 NOT_ENOUGH_MEMORY_SPACE: 0x6a84,
233 REFERENCED_DATA_NOT_FOUND: 0x6a88,
234 FILE_ALREADY_EXISTS: 0x6a89,
235 INCORRECT_P1_P2: 0x6b00,
236 INS_NOT_SUPPORTED: 0x6d00,
237 CLA_NOT_SUPPORTED: 0x6e00,
238 TECHNICAL_PROBLEM: 0x6f00,
239 OK: 0x9000,
240 MEMORY_PROBLEM: 0x9240,
241 NO_EF_SELECTED: 0x9400,
242 INVALID_OFFSET: 0x9402,
243 FILE_NOT_FOUND: 0x9404,
244 INCONSISTENT_FILE: 0x9408,
245 ALGORITHM_NOT_SUPPORTED: 0x9484,
246 INVALID_KCV: 0x9485,
247 CODE_NOT_INITIALIZED: 0x9802,
248 ACCESS_CONDITION_NOT_FULFILLED: 0x9804,
249 CONTRADICTION_SECRET_CODE_STATUS: 0x9808,
250 CONTRADICTION_INVALIDATION: 0x9810,
251 CODE_BLOCKED: 0x9840,
252 MAX_VALUE_REACHED: 0x9850,
253 GP_AUTH_FAILED: 0x6300,
254 LICENSING: 0x6f42,
255 HALTED: 0x6faa,
256};
257function getAltStatusMessage(code) {
258 switch (code) {
259 // improve text of most common errors
260 case 0x6700:
261 return "Incorrect length";
262 case 0x6800:
263 return "Missing critical parameter";
264 case 0x6982:
265 return "Security not satisfied (dongle locked or have invalid access rights)";
266 case 0x6985:
267 return "Condition of use not satisfied (denied by the user?)";
268 case 0x6a80:
269 return "Invalid data received";
270 case 0x6b00:
271 return "Invalid parameter received";
272 }
273 if (0x6f00 <= code && code <= 0x6fff) {
274 return "Internal error, please report";
275 }
276}
277/**
278 * Error thrown when a device returned a non success status.
279 * the error.statusCode is one of the `StatusCodes` exported by this library.
280 */
281function TransportStatusError(statusCode) {
282 this.name = "TransportStatusError";
283 var statusText = Object.keys(StatusCodes).find(function (k) { return StatusCodes[k] === statusCode; }) ||
284 "UNKNOWN_ERROR";
285 var smsg = getAltStatusMessage(statusCode) || statusText;
286 var statusCodeStr = statusCode.toString(16);
287 this.message = "Ledger device: " + smsg + " (0x" + statusCodeStr + ")";
288 this.stack = new Error().stack;
289 this.statusCode = statusCode;
290 this.statusText = statusText;
291}
292TransportStatusError.prototype = new Error();
293addCustomErrorDeserializer("TransportStatusError", function (e) { return new TransportStatusError(e.statusCode); });
294
295exports.AccountNameRequiredError = AccountNameRequiredError;
296exports.AccountNotSupported = AccountNotSupported;
297exports.AmountRequired = AmountRequired;
298exports.BluetoothRequired = BluetoothRequired;
299exports.BtcUnmatchedApp = BtcUnmatchedApp;
300exports.CantOpenDevice = CantOpenDevice;
301exports.CantScanQRCode = CantScanQRCode;
302exports.CashAddrNotSupported = CashAddrNotSupported;
303exports.CurrencyNotSupported = CurrencyNotSupported;
304exports.DBNotReset = DBNotReset;
305exports.DBWrongPassword = DBWrongPassword;
306exports.DeviceAppVerifyNotSupported = DeviceAppVerifyNotSupported;
307exports.DeviceGenuineSocketEarlyClose = DeviceGenuineSocketEarlyClose;
308exports.DeviceHalted = DeviceHalted;
309exports.DeviceInOSUExpected = DeviceInOSUExpected;
310exports.DeviceNameInvalid = DeviceNameInvalid;
311exports.DeviceNotGenuineError = DeviceNotGenuineError;
312exports.DeviceOnDashboardExpected = DeviceOnDashboardExpected;
313exports.DeviceOnDashboardUnexpected = DeviceOnDashboardUnexpected;
314exports.DeviceShouldStayInApp = DeviceShouldStayInApp;
315exports.DeviceSocketFail = DeviceSocketFail;
316exports.DeviceSocketNoBulkStatus = DeviceSocketNoBulkStatus;
317exports.DisconnectedDevice = DisconnectedDevice;
318exports.DisconnectedDeviceDuringOperation = DisconnectedDeviceDuringOperation;
319exports.ETHAddressNonEIP = ETHAddressNonEIP;
320exports.EnpointConfigError = EnpointConfigError;
321exports.EthAppPleaseEnableContractData = EthAppPleaseEnableContractData;
322exports.FeeEstimationFailed = FeeEstimationFailed;
323exports.FeeNotLoaded = FeeNotLoaded;
324exports.FeeRequired = FeeRequired;
325exports.FeeTooHigh = FeeTooHigh;
326exports.FirmwareNotRecognized = FirmwareNotRecognized;
327exports.FirmwareOrAppUpdateRequired = FirmwareOrAppUpdateRequired;
328exports.GasLessThanEstimate = GasLessThanEstimate;
329exports.GenuineCheckFailed = GenuineCheckFailed;
330exports.HardResetFail = HardResetFail;
331exports.InvalidAddress = InvalidAddress;
332exports.InvalidAddressBecauseDestinationIsAlsoSource = InvalidAddressBecauseDestinationIsAlsoSource;
333exports.InvalidXRPTag = InvalidXRPTag;
334exports.LatestMCUInstalledError = LatestMCUInstalledError;
335exports.LedgerAPI4xx = LedgerAPI4xx;
336exports.LedgerAPI5xx = LedgerAPI5xx;
337exports.LedgerAPIError = LedgerAPIError;
338exports.LedgerAPIErrorWithMessage = LedgerAPIErrorWithMessage;
339exports.LedgerAPINotAvailable = LedgerAPINotAvailable;
340exports.MCUNotGenuineToDashboard = MCUNotGenuineToDashboard;
341exports.ManagerAppAlreadyInstalledError = ManagerAppAlreadyInstalledError;
342exports.ManagerAppDepInstallRequired = ManagerAppDepInstallRequired;
343exports.ManagerAppDepUninstallRequired = ManagerAppDepUninstallRequired;
344exports.ManagerAppRelyOnBTCError = ManagerAppRelyOnBTCError;
345exports.ManagerDeviceLockedError = ManagerDeviceLockedError;
346exports.ManagerFirmwareNotEnoughSpaceError = ManagerFirmwareNotEnoughSpaceError;
347exports.ManagerNotEnoughSpaceError = ManagerNotEnoughSpaceError;
348exports.ManagerUninstallBTCDep = ManagerUninstallBTCDep;
349exports.NetworkDown = NetworkDown;
350exports.NoAccessToCamera = NoAccessToCamera;
351exports.NoAddressesFound = NoAddressesFound;
352exports.NoDBPathGiven = NoDBPathGiven;
353exports.NotEnoughBalance = NotEnoughBalance;
354exports.NotEnoughBalanceBecauseDestinationNotCreated = NotEnoughBalanceBecauseDestinationNotCreated;
355exports.NotEnoughBalanceInParentAccount = NotEnoughBalanceInParentAccount;
356exports.NotEnoughBalanceToDelegate = NotEnoughBalanceToDelegate;
357exports.NotEnoughGas = NotEnoughGas;
358exports.NotEnoughSpendableBalance = NotEnoughSpendableBalance;
359exports.NotSupportedLegacyAddress = NotSupportedLegacyAddress;
360exports.PairingFailed = PairingFailed;
361exports.PasswordIncorrectError = PasswordIncorrectError;
362exports.PasswordsDontMatchError = PasswordsDontMatchError;
363exports.RecipientRequired = RecipientRequired;
364exports.RecommendSubAccountsToEmpty = RecommendSubAccountsToEmpty;
365exports.RecommendUndelegation = RecommendUndelegation;
366exports.StatusCodes = StatusCodes;
367exports.SyncError = SyncError;
368exports.TimeoutTagged = TimeoutTagged;
369exports.TransportError = TransportError;
370exports.TransportInterfaceNotAvailable = TransportInterfaceNotAvailable;
371exports.TransportOpenUserCancelled = TransportOpenUserCancelled;
372exports.TransportRaceCondition = TransportRaceCondition;
373exports.TransportStatusError = TransportStatusError;
374exports.TransportWebUSBGestureRequired = TransportWebUSBGestureRequired;
375exports.UnavailableTezosOriginatedAccountReceive = UnavailableTezosOriginatedAccountReceive;
376exports.UnavailableTezosOriginatedAccountSend = UnavailableTezosOriginatedAccountSend;
377exports.UnexpectedBootloader = UnexpectedBootloader;
378exports.UnknownMCU = UnknownMCU;
379exports.UpdateFetchFileFail = UpdateFetchFileFail;
380exports.UpdateIncorrectHash = UpdateIncorrectHash;
381exports.UpdateIncorrectSig = UpdateIncorrectSig;
382exports.UpdateYourApp = UpdateYourApp;
383exports.UserRefusedAddress = UserRefusedAddress;
384exports.UserRefusedAllowManager = UserRefusedAllowManager;
385exports.UserRefusedDeviceNameChange = UserRefusedDeviceNameChange;
386exports.UserRefusedFirmwareUpdate = UserRefusedFirmwareUpdate;
387exports.UserRefusedOnDevice = UserRefusedOnDevice;
388exports.WebsocketConnectionError = WebsocketConnectionError;
389exports.WebsocketConnectionFailed = WebsocketConnectionFailed;
390exports.WrongAppForCurrency = WrongAppForCurrency;
391exports.WrongDeviceForAccount = WrongDeviceForAccount;
392exports.addCustomErrorDeserializer = addCustomErrorDeserializer;
393exports.createCustomErrorClass = createCustomErrorClass;
394exports.deserializeError = deserializeError;
395exports.getAltStatusMessage = getAltStatusMessage;
396exports.serializeError = serializeError;