This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
http.js
158 lines (134 loc) · 4.86 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* @flow */
'use strict';
import semvercmp from 'semver-compare';
import http from '../http';
import * as installers from '../installers';
import {
checkAcquire,
checkEnumerate,
checkCall,
checkInfo,
} from './checks';
import type {DefaultMessageResponse} from '../session';
import type {RequestOptions} from '../http';
import type {DeviceDescriptor} from '../transport';
const DEFAULT_URL = 'https://localback.net:21324';
//
// HTTP transport.
//
export default class HttpTransport {
url: string;
version: string;
configured: boolean;
supportsSync: boolean = true;
isOutdated: boolean;
constructor(url?: string = DEFAULT_URL, info: { version: string, configured: boolean }) {
this.url = url;
this.version = info.version;
this.configured = info.configured;
}
static create(url?: string = DEFAULT_URL, bridgeVersionUrl?: string): Promise<HttpTransport> {
console.log('[trezor.js] [http] Attempting to load HTTP transport at', url);
return HttpTransport.status(url).then(
(info) => {
console.log('[trezor.js] [http] Loaded HTTP transport', info);
return new HttpTransport(url, checkInfo(info));
},
(error) => {
console.warn('[trezor.js] [http] Failed to load HTTP transport', error);
throw error;
}
).then(transport => {
return installers.latestVersionAsync({bridgeUrl: bridgeVersionUrl}).then(version => {
const isOutdated = semvercmp(transport.version, version) < 0;
transport.isOutdated = isOutdated;
return transport;
}, () => {
// latest.txt CORS error shouldn't bring trezor.js down :/
return transport;
});
});
}
// deprecated
static connect(url?: string = DEFAULT_URL): Promise<mixed> {
return HttpTransport.status(url);
}
static status(url?: string = DEFAULT_URL): Promise<mixed> {
return http({ method: 'GET', url });
}
_request(options: RequestOptions): Promise<mixed> {
return http({ ...options, url: this.url + options.url });
}
_requestSync(options: RequestOptions): mixed {
return http.sync({ ...options, url: this.url + options.url });
}
configure(config: string): Promise<void> {
return this._request({
method: 'POST',
url: '/configure',
body: config,
}).then(() => {return;});
}
enumerate(wait?: boolean, previous?: ?Array<DeviceDescriptor>): Promise<Array<DeviceDescriptor>> {
const listenWithPrevious: boolean =
!!(wait) &&
(previous != null) &&
(semvercmp(this.version, '1.1.3') >= 0);
const enRes = listenWithPrevious
? this._request({
method: 'POST',
url: '/listen',
body: previous,
})
: this._request({
method: 'GET',
url: wait ? '/listen' : '/enumerate',
});
const checked = enRes.then(r => checkEnumerate(r));
return checked;
}
acquire(device: DeviceDescriptor, checkPrevious?: boolean): Promise<{session: (string | number)}> {
if (checkPrevious && (semvercmp(this.version, '1.1.3') >= 0)) {
const previousStr = device.session == null ? 'null' : device.session;
const url = '/acquire/' + device.path + '/' + previousStr;
return this._request({
method: 'POST',
url: url,
}).then(r => checkAcquire(r));
}
return this._request({
method: 'POST',
url: '/acquire/' + device.path,
}).then(r => checkAcquire(r));
}
release(sessionId: string|number): Promise<void> {
return this._request(releaseOptions(sessionId)).then(() => {return;});
}
releaseSync(sessionId: string|number): void {
this._requestSync(releaseOptions(sessionId));
}
call(sessionId: string|number, type: string, message: Object): Promise<DefaultMessageResponse> {
return this._request(callOptions(sessionId, type, message))
.then(r => checkCall(r));
}
// Be careful! Blocks the browser thread.
callSync(sessionId: string|number, type: string, message: Object): DefaultMessageResponse {
return checkCall(this._requestSync(callOptions(sessionId, type, message)));
}
}
function releaseOptions(sessionId: string|number): RequestOptions {
return {
method: 'POST',
url: '/release/' + sessionId,
};
}
function callOptions(sessionId: string|number, type: string, message: Object): RequestOptions {
return {
method: 'POST',
url: '/call/' + sessionId,
body: {
type: type,
message: message,
},
};
}