@ledgerhq/hw-transport-node-speculos-http 6.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ <img src="https://user-images.githubusercontent.com/211411/34776833-6f1ef4da-f618-11e7-8b13-f0697901d6a8.png" height="100" />
2
+
3
+ ## @ledgerhq/hw-transport-node-speculos-http
4
+
5
+ A transport for <https://github.com/LedgerHQ/speculos> Nano simulator using it's http api.
6
+
7
+ [Github](https://github.com/LedgerHQ/ledgerjs/),
8
+ [Ledger Devs Slack](https://ledger-dev.slack.com/)
9
+
10
+ ### Getting started
11
+
12
+ * Install <https://github.com/LedgerHQ/speculos>
13
+ * Make sure to have a speculos running with an api port (default port is 5000).
14
+
15
+ ```js
16
+ import SpeculosTransport from "@ledgerhq/hw-transport-node-speculos-http";
17
+
18
+ async function exampleSimple() {
19
+ const transport = await SpeculosTransport.open();
20
+ const res = await transport.send(0xE0, 0x01, 0x00, 0x00);
21
+ }
22
+
23
+ async function exampleAdvanced() {
24
+ const transport = await SpeculosTransport.open({ baseURL: "http://localhost:5000" });
25
+ transport.eventStream.once("data", {
26
+ // we will click right when the display changes
27
+ transport.button("right");
28
+ });
29
+ // we can also use eventStream.on and match the data buffer with the expected display before taking action
30
+ // derivate btc address and ask for device verification
31
+ const res = await transport.send(0xE0, 0x40, 0x01, 0x00, Buffer.from("058000002c8000000080000000000000000000000f"));
32
+ }
33
+ ```
34
+
35
+ ## API
36
+
37
+ <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
38
+
39
+ #### Table of Contents
40
+
41
+ * [SpeculosHttpTransport](#speculoshttptransport)
42
+ * [Parameters](#parameters)
43
+ * [Examples](#examples)
44
+ * [button](#button)
45
+ * [Parameters](#parameters-1)
46
+
47
+ ### SpeculosHttpTransport
48
+
49
+ **Extends Transport**
50
+
51
+ Speculos TCP transport implementation
52
+
53
+ #### Parameters
54
+
55
+ * `instance` **AxiosInstance**
56
+ * `opts` **SpeculosHttpTransportOpts**
57
+
58
+ #### Examples
59
+
60
+ ```javascript
61
+ import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
62
+ const transport = await SpeculosHttpTransport.open();
63
+ const res = await transport.send(0xE0, 0x01, 0, 0);
64
+ ```
65
+
66
+ #### button
67
+
68
+ Press and release button
69
+ buttons available: left, right, both
70
+
71
+ ##### Parameters
72
+
73
+ * `but` **any**
74
+
75
+ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>**
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" />
2
+ import { AxiosInstance } from "axios";
3
+ import Transport from "@ledgerhq/hw-transport";
4
+ export declare type SpeculosHttpTransportOpts = {
5
+ baseURL?: string;
6
+ timeout?: number;
7
+ };
8
+ /**
9
+ * Speculos TCP transport implementation
10
+ *
11
+ * @example
12
+ * import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
13
+ * const transport = await SpeculosHttpTransport.open();
14
+ * const res = await transport.send(0xE0, 0x01, 0, 0);
15
+ */
16
+ export default class SpeculosHttpTransport extends Transport {
17
+ instance: AxiosInstance;
18
+ opts: SpeculosHttpTransportOpts;
19
+ eventStream: any;
20
+ constructor(instance: AxiosInstance, opts: SpeculosHttpTransportOpts);
21
+ static isSupported: () => Promise<boolean>;
22
+ static list: () => any;
23
+ static listen: (_observer: any) => {
24
+ unsubscribe: () => void;
25
+ };
26
+ static open: (opts: SpeculosHttpTransportOpts) => Promise<SpeculosHttpTransport>;
27
+ /**
28
+ * Press and release button
29
+ * buttons available: left, right, both
30
+ * @param {*} but
31
+ */
32
+ button: (but: string) => Promise<void>;
33
+ exchange(apdu: Buffer): Promise<any>;
34
+ close(): Promise<void>;
35
+ }
36
+ //# sourceMappingURL=SpeculosHttpTransport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpeculosHttpTransport.d.ts","sourceRoot":"","sources":["../src/SpeculosHttpTransport.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEtC,OAAO,SAAS,MAAM,wBAAwB,CAAC;AAG/C,oBAAY,yBAAyB,GAAG;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,SAAS;IAC1D,QAAQ,EAAE,aAAa,CAAC;IACxB,IAAI,EAAE,yBAAyB,CAAC;IAChC,WAAW,EAAE,GAAG,CAAC;gBAEL,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,yBAAyB;IAMpE,MAAM,CAAC,WAAW,QAAO,QAAQ,OAAO,CAAC,CAA0B;IAEnE,MAAM,CAAC,IAAI,QAAO,GAAG,CAAwB;IAC7C,MAAM,CAAC,MAAM,cAAe,GAAG;;MAE5B;IAEH,MAAM,CAAC,IAAI,SACH,yBAAyB,KAC9B,QAAQ,qBAAqB,CAAC,CA+B5B;IAEL;;;;OAIG;IACH,MAAM,QAAS,MAAM,KAAG,QAAQ,IAAI,CAAC,CAYhC;IAEC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAWpC,KAAK;CAKZ"}
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ exports.__esModule = true;
57
+ var axios_1 = __importDefault(require("axios"));
58
+ var errors_1 = require("@ledgerhq/errors");
59
+ var hw_transport_1 = __importDefault(require("@ledgerhq/hw-transport"));
60
+ var logs_1 = require("@ledgerhq/logs");
61
+ /**
62
+ * Speculos TCP transport implementation
63
+ *
64
+ * @example
65
+ * import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
66
+ * const transport = await SpeculosHttpTransport.open();
67
+ * const res = await transport.send(0xE0, 0x01, 0, 0);
68
+ */
69
+ var SpeculosHttpTransport = /** @class */ (function (_super) {
70
+ __extends(SpeculosHttpTransport, _super);
71
+ function SpeculosHttpTransport(instance, opts) {
72
+ var _this = _super.call(this) || this;
73
+ /**
74
+ * Press and release button
75
+ * buttons available: left, right, both
76
+ * @param {*} but
77
+ */
78
+ _this.button = function (but) {
79
+ return new Promise(function (resolve, reject) {
80
+ var action = { action: "press-and-release" };
81
+ (0, logs_1.log)("speculos-button", "press-and-release", but);
82
+ _this.instance
83
+ .post("/button/".concat(but), action)
84
+ .then(function (response) {
85
+ resolve(response.data);
86
+ })["catch"](function (e) {
87
+ reject(e);
88
+ });
89
+ });
90
+ };
91
+ _this.instance = instance;
92
+ _this.opts = opts;
93
+ return _this;
94
+ }
95
+ SpeculosHttpTransport.prototype.exchange = function (apdu) {
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ var hex;
98
+ return __generator(this, function (_a) {
99
+ hex = apdu.toString("hex");
100
+ (0, logs_1.log)("apdu", "=> " + hex);
101
+ return [2 /*return*/, this.instance.post("/apdu", { data: hex }).then(function (r) {
102
+ // r.data is {"data": "hex value of response"}
103
+ var data = r.data.data;
104
+ (0, logs_1.log)("apdu", "<= " + data);
105
+ return Buffer.from(data, "hex");
106
+ })];
107
+ });
108
+ });
109
+ };
110
+ SpeculosHttpTransport.prototype.close = function () {
111
+ return __awaiter(this, void 0, void 0, function () {
112
+ return __generator(this, function (_a) {
113
+ // close event stream
114
+ this.eventStream.destroy();
115
+ return [2 /*return*/, Promise.resolve()];
116
+ });
117
+ });
118
+ };
119
+ SpeculosHttpTransport.isSupported = function () { return Promise.resolve(true); };
120
+ // this transport is not discoverable
121
+ SpeculosHttpTransport.list = function () { return Promise.resolve([]); };
122
+ SpeculosHttpTransport.listen = function (_observer) { return ({
123
+ unsubscribe: function () { }
124
+ }); };
125
+ SpeculosHttpTransport.open = function (opts) {
126
+ return new Promise(function (resolve, reject) {
127
+ var instance = axios_1["default"].create(opts);
128
+ instance.defaults.baseURL = "http://localhost:5000";
129
+ var transport = new SpeculosHttpTransport(instance, opts);
130
+ instance({
131
+ url: "/events?stream=true",
132
+ responseType: "stream"
133
+ })
134
+ .then(function (response) {
135
+ response.data.on("data", function (chunk) {
136
+ (0, logs_1.log)("speculos-event", chunk.toString());
137
+ // XXX: we could process display events here
138
+ // client side automation or UI tests/checks
139
+ });
140
+ response.data.on("close", function () {
141
+ (0, logs_1.log)("speculos-event", "close");
142
+ transport.emit("disconnect", new errors_1.DisconnectedDevice("Speculos exited!"));
143
+ });
144
+ transport.eventStream = response.data;
145
+ // we are connected to speculos
146
+ resolve(transport);
147
+ })["catch"](function (error) {
148
+ reject(error);
149
+ });
150
+ });
151
+ };
152
+ return SpeculosHttpTransport;
153
+ }(hw_transport_1["default"]));
154
+ exports["default"] = SpeculosHttpTransport;
155
+ //# sourceMappingURL=SpeculosHttpTransport.js.map
@@ -0,0 +1 @@
1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B,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;oBACxC,4CAA4C;oBAC5C,4CAA4C;gBAC9C,CAAC,CAAC,CAAC;gBACH,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE;oBACxB,IAAA,UAAG,EAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;oBAC/B,SAAS,CAAC,IAAI,CACZ,YAAY,EACZ,IAAI,2BAAkB,CAAC,kBAAkB,CAAC,CAC3C,CAAC;gBACJ,CAAC,CAAC,CAAC;gBACH,SAAS,CAAC,WAAW,GAAG,QAAQ,CAAC,IAAI,CAAC;gBACtC,+BAA+B;gBAC/B,OAAO,CAAC,SAAS,CAAC,CAAC;YACrB,CAAC,CAAC,CACD,OAAK,CAAA,CAAC,UAAC,KAAK;gBACX,MAAM,CAAC,KAAK,CAAC,CAAC;YAChB,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;IA9BF,CA8BE,CAAC;IAqCP,4BAAC;CAAA,AAxFD,CAAmD,yBAAS,GAwF3D;qBAxFoB,qBAAqB"}
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" />
2
+ import { AxiosInstance } from "axios";
3
+ import Transport from "@ledgerhq/hw-transport";
4
+ export declare type SpeculosHttpTransportOpts = {
5
+ baseURL?: string;
6
+ timeout?: number;
7
+ };
8
+ /**
9
+ * Speculos TCP transport implementation
10
+ *
11
+ * @example
12
+ * import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
13
+ * const transport = await SpeculosHttpTransport.open();
14
+ * const res = await transport.send(0xE0, 0x01, 0, 0);
15
+ */
16
+ export default class SpeculosHttpTransport extends Transport {
17
+ instance: AxiosInstance;
18
+ opts: SpeculosHttpTransportOpts;
19
+ eventStream: any;
20
+ constructor(instance: AxiosInstance, opts: SpeculosHttpTransportOpts);
21
+ static isSupported: () => Promise<boolean>;
22
+ static list: () => any;
23
+ static listen: (_observer: any) => {
24
+ unsubscribe: () => void;
25
+ };
26
+ static open: (opts: SpeculosHttpTransportOpts) => Promise<SpeculosHttpTransport>;
27
+ /**
28
+ * Press and release button
29
+ * buttons available: left, right, both
30
+ * @param {*} but
31
+ */
32
+ button: (but: string) => Promise<void>;
33
+ exchange(apdu: Buffer): Promise<any>;
34
+ close(): Promise<void>;
35
+ }
36
+ //# sourceMappingURL=SpeculosHttpTransport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpeculosHttpTransport.d.ts","sourceRoot":"","sources":["../src/SpeculosHttpTransport.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEtC,OAAO,SAAS,MAAM,wBAAwB,CAAC;AAG/C,oBAAY,yBAAyB,GAAG;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,SAAS;IAC1D,QAAQ,EAAE,aAAa,CAAC;IACxB,IAAI,EAAE,yBAAyB,CAAC;IAChC,WAAW,EAAE,GAAG,CAAC;gBAEL,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,yBAAyB;IAMpE,MAAM,CAAC,WAAW,QAAO,QAAQ,OAAO,CAAC,CAA0B;IAEnE,MAAM,CAAC,IAAI,QAAO,GAAG,CAAwB;IAC7C,MAAM,CAAC,MAAM,cAAe,GAAG;;MAE5B;IAEH,MAAM,CAAC,IAAI,SACH,yBAAyB,KAC9B,QAAQ,qBAAqB,CAAC,CA+B5B;IAEL;;;;OAIG;IACH,MAAM,QAAS,MAAM,KAAG,QAAQ,IAAI,CAAC,CAYhC;IAEC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAWpC,KAAK;CAKZ"}
@@ -0,0 +1,150 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __generator = (this && this.__generator) || function (thisArg, body) {
26
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
+ function verb(n) { return function (v) { return step([n, v]); }; }
29
+ function step(op) {
30
+ if (f) throw new TypeError("Generator is already executing.");
31
+ while (_) try {
32
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
+ if (y = 0, t) op = [op[0] & 2, t.value];
34
+ switch (op[0]) {
35
+ case 0: case 1: t = op; break;
36
+ case 4: _.label++; return { value: op[1], done: false };
37
+ case 5: _.label++; y = op[1]; op = [0]; continue;
38
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
+ default:
40
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
+ if (t[2]) _.ops.pop();
45
+ _.trys.pop(); continue;
46
+ }
47
+ op = body.call(thisArg, _);
48
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
+ }
51
+ };
52
+ import axios from "axios";
53
+ import { DisconnectedDevice } from "@ledgerhq/errors";
54
+ import Transport from "@ledgerhq/hw-transport";
55
+ import { log } from "@ledgerhq/logs";
56
+ /**
57
+ * Speculos TCP transport implementation
58
+ *
59
+ * @example
60
+ * import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
61
+ * const transport = await SpeculosHttpTransport.open();
62
+ * const res = await transport.send(0xE0, 0x01, 0, 0);
63
+ */
64
+ var SpeculosHttpTransport = /** @class */ (function (_super) {
65
+ __extends(SpeculosHttpTransport, _super);
66
+ function SpeculosHttpTransport(instance, opts) {
67
+ var _this = _super.call(this) || this;
68
+ /**
69
+ * Press and release button
70
+ * buttons available: left, right, both
71
+ * @param {*} but
72
+ */
73
+ _this.button = function (but) {
74
+ return new Promise(function (resolve, reject) {
75
+ var action = { action: "press-and-release" };
76
+ log("speculos-button", "press-and-release", but);
77
+ _this.instance
78
+ .post("/button/".concat(but), action)
79
+ .then(function (response) {
80
+ resolve(response.data);
81
+ })["catch"](function (e) {
82
+ reject(e);
83
+ });
84
+ });
85
+ };
86
+ _this.instance = instance;
87
+ _this.opts = opts;
88
+ return _this;
89
+ }
90
+ SpeculosHttpTransport.prototype.exchange = function (apdu) {
91
+ return __awaiter(this, void 0, void 0, function () {
92
+ var hex;
93
+ return __generator(this, function (_a) {
94
+ hex = apdu.toString("hex");
95
+ log("apdu", "=> " + hex);
96
+ return [2 /*return*/, this.instance.post("/apdu", { data: hex }).then(function (r) {
97
+ // r.data is {"data": "hex value of response"}
98
+ var data = r.data.data;
99
+ log("apdu", "<= " + data);
100
+ return Buffer.from(data, "hex");
101
+ })];
102
+ });
103
+ });
104
+ };
105
+ SpeculosHttpTransport.prototype.close = function () {
106
+ return __awaiter(this, void 0, void 0, function () {
107
+ return __generator(this, function (_a) {
108
+ // close event stream
109
+ this.eventStream.destroy();
110
+ return [2 /*return*/, Promise.resolve()];
111
+ });
112
+ });
113
+ };
114
+ SpeculosHttpTransport.isSupported = function () { return Promise.resolve(true); };
115
+ // this transport is not discoverable
116
+ SpeculosHttpTransport.list = function () { return Promise.resolve([]); };
117
+ SpeculosHttpTransport.listen = function (_observer) { return ({
118
+ unsubscribe: function () { }
119
+ }); };
120
+ SpeculosHttpTransport.open = function (opts) {
121
+ return new Promise(function (resolve, reject) {
122
+ var instance = axios.create(opts);
123
+ instance.defaults.baseURL = "http://localhost:5000";
124
+ var transport = new SpeculosHttpTransport(instance, opts);
125
+ instance({
126
+ url: "/events?stream=true",
127
+ responseType: "stream"
128
+ })
129
+ .then(function (response) {
130
+ response.data.on("data", function (chunk) {
131
+ log("speculos-event", chunk.toString());
132
+ // XXX: we could process display events here
133
+ // client side automation or UI tests/checks
134
+ });
135
+ response.data.on("close", function () {
136
+ log("speculos-event", "close");
137
+ transport.emit("disconnect", new DisconnectedDevice("Speculos exited!"));
138
+ });
139
+ transport.eventStream = response.data;
140
+ // we are connected to speculos
141
+ resolve(transport);
142
+ })["catch"](function (error) {
143
+ reject(error);
144
+ });
145
+ });
146
+ };
147
+ return SpeculosHttpTransport;
148
+ }(Transport));
149
+ export default SpeculosHttpTransport;
150
+ //# sourceMappingURL=SpeculosHttpTransport.js.map
@@ -0,0 +1 @@
1
+ {"version":3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AAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,UAAC,KAAK;oBAC7B,GAAG,CAAC,gBAAgB,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;oBACxC,4CAA4C;oBAC5C,4CAA4C;gBAC9C,CAAC,CAAC,CAAC;gBACH,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE;oBACxB,GAAG,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;oBAC/B,SAAS,CAAC,IAAI,CACZ,YAAY,EACZ,IAAI,kBAAkB,CAAC,kBAAkB,CAAC,CAC3C,CAAC;gBACJ,CAAC,CAAC,CAAC;gBACH,SAAS,CAAC,WAAW,GAAG,QAAQ,CAAC,IAAI,CAAC;gBACtC,+BAA+B;gBAC/B,OAAO,CAAC,SAAS,CAAC,CAAC;YACrB,CAAC,CAAC,CACD,OAAK,CAAA,CAAC,UAAC,KAAK;gBACX,MAAM,CAAC,KAAK,CAAC,CAAC;YAChB,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;IA9BF,CA8BE,CAAC;IAqCP,4BAAC;CAAA,AAxFD,CAAmD,SAAS,GAwF3D;eAxFoB,qBAAqB"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@ledgerhq/hw-transport-node-speculos-http",
3
+ "version": "6.22.0",
4
+ "description": "Ledger Hardware Wallet communication layer with speculos Nano simulator using the http api",
5
+ "keywords": [
6
+ "Ledger",
7
+ "LedgerWallet",
8
+ "proxy",
9
+ "http",
10
+ "NanoS",
11
+ "Blue",
12
+ "Hardware Wallet",
13
+ "Speculos"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/LedgerHQ/ledgerjs"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/LedgerHQ/ledgerjs/issues"
21
+ },
22
+ "homepage": "https://github.com/LedgerHQ/ledgerjs",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "main": "lib/SpeculosHttpTransport.js",
27
+ "module": "lib-es/SpeculosHttpTransport.js",
28
+ "types": "lib/SpeculosHttpTransport.d.ts",
29
+ "license": "Apache-2.0",
30
+ "dependencies": {
31
+ "@ledgerhq/errors": "^6.10.0",
32
+ "@ledgerhq/hw-transport": "^6.20.0",
33
+ "@ledgerhq/logs": "^6.10.0",
34
+ "axios": "^0.24.0"
35
+ },
36
+ "scripts": {
37
+ "clean": "bash ../../script/clean.sh",
38
+ "build": "bash ../../script/build.sh",
39
+ "watch": "bash ../../script/watch.sh",
40
+ "doc": "bash ../../script/doc.sh"
41
+ },
42
+ "gitHead": "df473b65fc3a869bc4cbb65de5b057272366f2da"
43
+ }
@@ -0,0 +1,108 @@
1
+ import axios from "axios";
2
+ import { AxiosInstance } from "axios";
3
+ import { DisconnectedDevice } from "@ledgerhq/errors";
4
+ import Transport from "@ledgerhq/hw-transport";
5
+ import { log } from "@ledgerhq/logs";
6
+
7
+ export type SpeculosHttpTransportOpts = {
8
+ baseURL?: string;
9
+ timeout?: number;
10
+ };
11
+
12
+ /**
13
+ * Speculos TCP transport implementation
14
+ *
15
+ * @example
16
+ * import SpeculosHttpTransport from "@ledgerhq/hw-transport-node-speculos-http";
17
+ * const transport = await SpeculosHttpTransport.open();
18
+ * const res = await transport.send(0xE0, 0x01, 0, 0);
19
+ */
20
+ export default class SpeculosHttpTransport extends Transport {
21
+ instance: AxiosInstance;
22
+ opts: SpeculosHttpTransportOpts;
23
+ eventStream: any; // ReadStream?
24
+
25
+ constructor(instance: AxiosInstance, opts: SpeculosHttpTransportOpts) {
26
+ super();
27
+ this.instance = instance;
28
+ this.opts = opts;
29
+ }
30
+
31
+ static isSupported = (): Promise<boolean> => Promise.resolve(true);
32
+ // this transport is not discoverable
33
+ static list = (): any => Promise.resolve([]);
34
+ static listen = (_observer: any) => ({
35
+ unsubscribe: () => {},
36
+ });
37
+
38
+ static open = (
39
+ opts: SpeculosHttpTransportOpts
40
+ ): Promise<SpeculosHttpTransport> =>
41
+ new Promise((resolve, reject) => {
42
+ const instance = axios.create(opts);
43
+ instance.defaults.baseURL = "http://localhost:5000";
44
+
45
+ const transport = new SpeculosHttpTransport(instance, opts);
46
+
47
+ instance({
48
+ url: "/events?stream=true",
49
+ responseType: "stream",
50
+ })
51
+ .then((response) => {
52
+ response.data.on("data", (chunk) => {
53
+ log("speculos-event", chunk.toString());
54
+ // XXX: we could process display events here
55
+ // client side automation or UI tests/checks
56
+ });
57
+ response.data.on("close", () => {
58
+ log("speculos-event", "close");
59
+ transport.emit(
60
+ "disconnect",
61
+ new DisconnectedDevice("Speculos exited!")
62
+ );
63
+ });
64
+ transport.eventStream = response.data;
65
+ // we are connected to speculos
66
+ resolve(transport);
67
+ })
68
+ .catch((error) => {
69
+ reject(error);
70
+ });
71
+ });
72
+
73
+ /**
74
+ * Press and release button
75
+ * buttons available: left, right, both
76
+ * @param {*} but
77
+ */
78
+ button = (but: string): Promise<void> =>
79
+ new Promise((resolve, reject) => {
80
+ const action = { action: "press-and-release" };
81
+ log("speculos-button", "press-and-release", but);
82
+ this.instance
83
+ .post(`/button/${but}`, action)
84
+ .then((response) => {
85
+ resolve(response.data);
86
+ })
87
+ .catch((e) => {
88
+ reject(e);
89
+ });
90
+ });
91
+
92
+ async exchange(apdu: Buffer): Promise<any> {
93
+ const hex = apdu.toString("hex");
94
+ log("apdu", "=> " + hex);
95
+ return this.instance.post("/apdu", { data: hex }).then((r) => {
96
+ // r.data is {"data": "hex value of response"}
97
+ const data = r.data.data;
98
+ log("apdu", "<= " + data);
99
+ return Buffer.from(data, "hex");
100
+ });
101
+ }
102
+
103
+ async close() {
104
+ // close event stream
105
+ this.eventStream.destroy();
106
+ return Promise.resolve();
107
+ }
108
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "lib"
5
+ },
6
+ "include": ["src/**/*"]
7
+ }