@nightlylabs/dex-sdk 0.3.1 → 0.3.2

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.
@@ -0,0 +1,225 @@
1
+ import {
2
+ DisconnectedDevice,
3
+ DisconnectedDeviceDuringOperation,
4
+ TransportError,
5
+ TransportOpenUserCancelled,
6
+ Transport_default,
7
+ hid_framing_default,
8
+ identifyUSBProductId,
9
+ ledgerUSBVendorId,
10
+ log
11
+ } from "./chunk-ISCCOOHD.js";
12
+ import "./chunk-DOLYMX7P.js";
13
+
14
+ // node_modules/.pnpm/@ledgerhq+hw-transport-webhid@6.30.0/node_modules/@ledgerhq/hw-transport-webhid/lib-es/TransportWebHID.js
15
+ var __awaiter = function(thisArg, _arguments, P, generator) {
16
+ function adopt(value) {
17
+ return value instanceof P ? value : new P(function(resolve) {
18
+ resolve(value);
19
+ });
20
+ }
21
+ return new (P || (P = Promise))(function(resolve, reject) {
22
+ function fulfilled(value) {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ }
29
+ function rejected(value) {
30
+ try {
31
+ step(generator["throw"](value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ }
36
+ function step(result) {
37
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
38
+ }
39
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
40
+ });
41
+ };
42
+ var ledgerDevices = [
43
+ {
44
+ vendorId: ledgerUSBVendorId
45
+ }
46
+ ];
47
+ var isSupported = () => Promise.resolve(!!(window.navigator && window.navigator.hid));
48
+ var getHID = () => {
49
+ const { hid } = navigator;
50
+ if (!hid)
51
+ throw new TransportError("navigator.hid is not supported", "HIDNotSupported");
52
+ return hid;
53
+ };
54
+ function requestLedgerDevices() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const device = yield getHID().requestDevice({
57
+ filters: ledgerDevices
58
+ });
59
+ if (Array.isArray(device))
60
+ return device;
61
+ return [device];
62
+ });
63
+ }
64
+ function getLedgerDevices() {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const devices = yield getHID().getDevices();
67
+ return devices.filter((d) => d.vendorId === ledgerUSBVendorId);
68
+ });
69
+ }
70
+ function getFirstLedgerDevice() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const existingDevices = yield getLedgerDevices();
73
+ if (existingDevices.length > 0)
74
+ return existingDevices[0];
75
+ const devices = yield requestLedgerDevices();
76
+ return devices[0];
77
+ });
78
+ }
79
+ var TransportWebHID = class _TransportWebHID extends Transport_default {
80
+ constructor(device) {
81
+ super();
82
+ this.channel = Math.floor(Math.random() * 65535);
83
+ this.packetSize = 64;
84
+ this.inputs = [];
85
+ this.read = () => {
86
+ if (this.inputs.length) {
87
+ return Promise.resolve(this.inputs.shift());
88
+ }
89
+ return new Promise((success) => {
90
+ this.inputCallback = success;
91
+ });
92
+ };
93
+ this.onInputReport = (e) => {
94
+ const buffer = Buffer.from(e.data.buffer);
95
+ if (this.inputCallback) {
96
+ this.inputCallback(buffer);
97
+ this.inputCallback = null;
98
+ } else {
99
+ this.inputs.push(buffer);
100
+ }
101
+ };
102
+ this._disconnectEmitted = false;
103
+ this._emitDisconnect = (e) => {
104
+ if (this._disconnectEmitted)
105
+ return;
106
+ this._disconnectEmitted = true;
107
+ this.emit("disconnect", e);
108
+ };
109
+ this.exchange = (apdu) => __awaiter(this, void 0, void 0, function* () {
110
+ const b = yield this.exchangeAtomicImpl(() => __awaiter(this, void 0, void 0, function* () {
111
+ const { channel, packetSize } = this;
112
+ log("apdu", "=> " + apdu.toString("hex"));
113
+ const framing = hid_framing_default(channel, packetSize);
114
+ const blocks = framing.makeBlocks(apdu);
115
+ for (let i = 0; i < blocks.length; i++) {
116
+ yield this.device.sendReport(0, blocks[i]);
117
+ }
118
+ let result;
119
+ let acc;
120
+ while (!(result = framing.getReducedResult(acc))) {
121
+ try {
122
+ const buffer = yield this.read();
123
+ acc = framing.reduceResponse(acc, buffer);
124
+ } catch (e) {
125
+ if (e instanceof TransportError && e.id === "InvalidChannel") {
126
+ continue;
127
+ }
128
+ throw e;
129
+ }
130
+ }
131
+ log("apdu", "<= " + result.toString("hex"));
132
+ return result;
133
+ })).catch((e) => {
134
+ if (e && e.message && e.message.includes("write")) {
135
+ this._emitDisconnect(e);
136
+ throw new DisconnectedDeviceDuringOperation(e.message);
137
+ }
138
+ throw e;
139
+ });
140
+ return b;
141
+ });
142
+ this.device = device;
143
+ this.deviceModel = typeof device.productId === "number" ? identifyUSBProductId(device.productId) : void 0;
144
+ device.addEventListener("inputreport", this.onInputReport);
145
+ }
146
+ /**
147
+ * Similar to create() except it will always display the device permission (even if some devices are already accepted).
148
+ */
149
+ static request() {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ const [device] = yield requestLedgerDevices();
152
+ return _TransportWebHID.open(device);
153
+ });
154
+ }
155
+ /**
156
+ * Similar to create() except it will never display the device permission (it returns a Promise<?Transport>, null if it fails to find a device).
157
+ */
158
+ static openConnected() {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ const devices = yield getLedgerDevices();
161
+ if (devices.length === 0)
162
+ return null;
163
+ return _TransportWebHID.open(devices[0]);
164
+ });
165
+ }
166
+ /**
167
+ * Create a Ledger transport with a HIDDevice
168
+ */
169
+ static open(device) {
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ yield device.open();
172
+ const transport = new _TransportWebHID(device);
173
+ const onDisconnect = (e) => {
174
+ if (device === e.device) {
175
+ getHID().removeEventListener("disconnect", onDisconnect);
176
+ transport._emitDisconnect(new DisconnectedDevice());
177
+ }
178
+ };
179
+ getHID().addEventListener("disconnect", onDisconnect);
180
+ return transport;
181
+ });
182
+ }
183
+ /**
184
+ * Release the transport device
185
+ */
186
+ close() {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ yield this.exchangeBusyPromise;
189
+ this.device.removeEventListener("inputreport", this.onInputReport);
190
+ yield this.device.close();
191
+ });
192
+ }
193
+ setScrambleKey() {
194
+ }
195
+ };
196
+ TransportWebHID.isSupported = isSupported;
197
+ TransportWebHID.list = getLedgerDevices;
198
+ TransportWebHID.listen = (observer) => {
199
+ let unsubscribed = false;
200
+ getFirstLedgerDevice().then((device) => {
201
+ if (!device) {
202
+ observer.error(new TransportOpenUserCancelled("Access denied to use Ledger device"));
203
+ } else if (!unsubscribed) {
204
+ const deviceModel = typeof device.productId === "number" ? identifyUSBProductId(device.productId) : void 0;
205
+ observer.next({
206
+ type: "add",
207
+ descriptor: device,
208
+ deviceModel
209
+ });
210
+ observer.complete();
211
+ }
212
+ }, (error) => {
213
+ observer.error(new TransportOpenUserCancelled(error.message));
214
+ });
215
+ function unsubscribe() {
216
+ unsubscribed = true;
217
+ }
218
+ return {
219
+ unsubscribe
220
+ };
221
+ };
222
+ var TransportWebHID_default = TransportWebHID;
223
+ export {
224
+ TransportWebHID_default as default
225
+ };
@@ -0,0 +1,258 @@
1
+ import {
2
+ DisconnectedDevice,
3
+ DisconnectedDeviceDuringOperation,
4
+ TransportInterfaceNotAvailable,
5
+ TransportOpenUserCancelled,
6
+ TransportWebUSBGestureRequired,
7
+ Transport_default,
8
+ hid_framing_default,
9
+ identifyUSBProductId,
10
+ ledgerUSBVendorId,
11
+ log
12
+ } from "./chunk-ISCCOOHD.js";
13
+ import "./chunk-DOLYMX7P.js";
14
+
15
+ // node_modules/.pnpm/@ledgerhq+hw-transport-webusb@6.29.4/node_modules/@ledgerhq/hw-transport-webusb/lib-es/webusb.js
16
+ var __awaiter = function(thisArg, _arguments, P, generator) {
17
+ function adopt(value) {
18
+ return value instanceof P ? value : new P(function(resolve) {
19
+ resolve(value);
20
+ });
21
+ }
22
+ return new (P || (P = Promise))(function(resolve, reject) {
23
+ function fulfilled(value) {
24
+ try {
25
+ step(generator.next(value));
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ }
30
+ function rejected(value) {
31
+ try {
32
+ step(generator["throw"](value));
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ }
37
+ function step(result) {
38
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
39
+ }
40
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
41
+ });
42
+ };
43
+ var ledgerDevices = [
44
+ {
45
+ vendorId: ledgerUSBVendorId
46
+ }
47
+ ];
48
+ function requestLedgerDevice() {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const device = yield navigator.usb.requestDevice({
51
+ filters: ledgerDevices
52
+ });
53
+ return device;
54
+ });
55
+ }
56
+ function getLedgerDevices() {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ const devices = yield navigator.usb.getDevices();
59
+ return devices.filter((d) => d.vendorId === ledgerUSBVendorId);
60
+ });
61
+ }
62
+ function getFirstLedgerDevice() {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const existingDevices = yield getLedgerDevices();
65
+ if (existingDevices.length > 0)
66
+ return existingDevices[0];
67
+ return requestLedgerDevice();
68
+ });
69
+ }
70
+ var isSupported = () => Promise.resolve(!!navigator && !!navigator.usb && typeof navigator.usb.getDevices === "function");
71
+
72
+ // node_modules/.pnpm/@ledgerhq+hw-transport-webusb@6.29.4/node_modules/@ledgerhq/hw-transport-webusb/lib-es/TransportWebUSB.js
73
+ var __awaiter2 = function(thisArg, _arguments, P, generator) {
74
+ function adopt(value) {
75
+ return value instanceof P ? value : new P(function(resolve) {
76
+ resolve(value);
77
+ });
78
+ }
79
+ return new (P || (P = Promise))(function(resolve, reject) {
80
+ function fulfilled(value) {
81
+ try {
82
+ step(generator.next(value));
83
+ } catch (e) {
84
+ reject(e);
85
+ }
86
+ }
87
+ function rejected(value) {
88
+ try {
89
+ step(generator["throw"](value));
90
+ } catch (e) {
91
+ reject(e);
92
+ }
93
+ }
94
+ function step(result) {
95
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
96
+ }
97
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
98
+ });
99
+ };
100
+ var configurationValue = 1;
101
+ var endpointNumber = 3;
102
+ var TransportWebUSB = class _TransportWebUSB extends Transport_default {
103
+ constructor(device, interfaceNumber) {
104
+ super();
105
+ this.channel = Math.floor(Math.random() * 65535);
106
+ this.packetSize = 64;
107
+ this._disconnectEmitted = false;
108
+ this._emitDisconnect = (e) => {
109
+ if (this._disconnectEmitted)
110
+ return;
111
+ this._disconnectEmitted = true;
112
+ this.emit("disconnect", e);
113
+ };
114
+ this.device = device;
115
+ this.interfaceNumber = interfaceNumber;
116
+ this.deviceModel = identifyUSBProductId(device.productId);
117
+ }
118
+ /**
119
+ * Similar to create() except it will always display the device permission (even if some devices are already accepted).
120
+ */
121
+ static request() {
122
+ return __awaiter2(this, void 0, void 0, function* () {
123
+ const device = yield requestLedgerDevice();
124
+ return _TransportWebUSB.open(device);
125
+ });
126
+ }
127
+ /**
128
+ * Similar to create() except it will never display the device permission (it returns a Promise<?Transport>, null if it fails to find a device).
129
+ */
130
+ static openConnected() {
131
+ return __awaiter2(this, void 0, void 0, function* () {
132
+ const devices = yield getLedgerDevices();
133
+ if (devices.length === 0)
134
+ return null;
135
+ return _TransportWebUSB.open(devices[0]);
136
+ });
137
+ }
138
+ /**
139
+ * Create a Ledger transport with a USBDevice
140
+ */
141
+ static open(device) {
142
+ return __awaiter2(this, void 0, void 0, function* () {
143
+ yield device.open();
144
+ if (device.configuration === null) {
145
+ yield device.selectConfiguration(configurationValue);
146
+ }
147
+ yield gracefullyResetDevice(device);
148
+ const iface = device.configurations[0].interfaces.find(({ alternates }) => alternates.some((a) => a.interfaceClass === 255));
149
+ if (!iface) {
150
+ throw new TransportInterfaceNotAvailable("No WebUSB interface found for your Ledger device. Please upgrade firmware or contact techsupport.");
151
+ }
152
+ const interfaceNumber = iface.interfaceNumber;
153
+ try {
154
+ yield device.claimInterface(interfaceNumber);
155
+ } catch (e) {
156
+ yield device.close();
157
+ throw new TransportInterfaceNotAvailable(e.message);
158
+ }
159
+ const transport = new _TransportWebUSB(device, interfaceNumber);
160
+ const onDisconnect = (e) => {
161
+ if (device === e.device) {
162
+ navigator.usb.removeEventListener("disconnect", onDisconnect);
163
+ transport._emitDisconnect(new DisconnectedDevice());
164
+ }
165
+ };
166
+ navigator.usb.addEventListener("disconnect", onDisconnect);
167
+ return transport;
168
+ });
169
+ }
170
+ /**
171
+ * Release the transport device
172
+ */
173
+ close() {
174
+ return __awaiter2(this, void 0, void 0, function* () {
175
+ yield this.exchangeBusyPromise;
176
+ yield this.device.releaseInterface(this.interfaceNumber);
177
+ yield gracefullyResetDevice(this.device);
178
+ yield this.device.close();
179
+ });
180
+ }
181
+ /**
182
+ * Exchange with the device using APDU protocol.
183
+ * @param apdu
184
+ * @returns a promise of apdu response
185
+ */
186
+ exchange(apdu) {
187
+ return __awaiter2(this, void 0, void 0, function* () {
188
+ const b = yield this.exchangeAtomicImpl(() => __awaiter2(this, void 0, void 0, function* () {
189
+ const { channel, packetSize } = this;
190
+ log("apdu", "=> " + apdu.toString("hex"));
191
+ const framing = hid_framing_default(channel, packetSize);
192
+ const blocks = framing.makeBlocks(apdu);
193
+ for (let i = 0; i < blocks.length; i++) {
194
+ yield this.device.transferOut(endpointNumber, blocks[i]);
195
+ }
196
+ let result;
197
+ let acc;
198
+ while (!(result = framing.getReducedResult(acc))) {
199
+ const r = yield this.device.transferIn(endpointNumber, packetSize);
200
+ const buffer = Buffer.from(r.data.buffer);
201
+ acc = framing.reduceResponse(acc, buffer);
202
+ }
203
+ log("apdu", "<= " + result.toString("hex"));
204
+ return result;
205
+ })).catch((e) => {
206
+ if (e && e.message && e.message.includes("disconnected")) {
207
+ this._emitDisconnect(e);
208
+ throw new DisconnectedDeviceDuringOperation(e.message);
209
+ }
210
+ throw e;
211
+ });
212
+ return b;
213
+ });
214
+ }
215
+ setScrambleKey() {
216
+ }
217
+ };
218
+ TransportWebUSB.isSupported = isSupported;
219
+ TransportWebUSB.list = getLedgerDevices;
220
+ TransportWebUSB.listen = (observer) => {
221
+ let unsubscribed = false;
222
+ getFirstLedgerDevice().then((device) => {
223
+ if (!unsubscribed) {
224
+ const deviceModel = identifyUSBProductId(device.productId);
225
+ observer.next({
226
+ type: "add",
227
+ descriptor: device,
228
+ deviceModel
229
+ });
230
+ observer.complete();
231
+ }
232
+ }, (error) => {
233
+ if (window.DOMException && error instanceof window.DOMException && error.code === 18) {
234
+ observer.error(new TransportWebUSBGestureRequired(error.message));
235
+ } else {
236
+ observer.error(new TransportOpenUserCancelled(error.message));
237
+ }
238
+ });
239
+ function unsubscribe() {
240
+ unsubscribed = true;
241
+ }
242
+ return {
243
+ unsubscribe
244
+ };
245
+ };
246
+ var TransportWebUSB_default = TransportWebUSB;
247
+ function gracefullyResetDevice(device) {
248
+ return __awaiter2(this, void 0, void 0, function* () {
249
+ try {
250
+ yield device.reset();
251
+ } catch (err) {
252
+ console.warn(err);
253
+ }
254
+ });
255
+ }
256
+ export {
257
+ TransportWebUSB_default as default
258
+ };