This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
ConnectSettings.js
171 lines (148 loc) · 5.94 KB
/
ConnectSettings.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
159
160
161
162
163
164
165
166
167
168
169
170
171
/* @flow */
import type {
Manifest,
ConnectSettings,
} from '../types';
/*
* Initial settings for connect.
* It could be changed by passing values into TrezorConnect.init(...) method
*/
const VERSION = '8.1.7';
const versionN = VERSION.split('.').map(s => parseInt(s));
// const DIRECTORY = `${ versionN[0] }${ (versionN[1] > 0 ? `.${versionN[1]}` : '') }/`;
const DIRECTORY = `${versionN[0]}/`;
const DEFAULT_DOMAIN = `https://connect.trezor.io/${ DIRECTORY }`;
export const DEFAULT_PRIORITY = 2;
const initialSettings: ConnectSettings = {
configSrc: './data/config.json', // constant
version: VERSION, // constant
debug: false,
priority: DEFAULT_PRIORITY,
trustedHost: false,
connectSrc: DEFAULT_DOMAIN,
iframeSrc: `${ DEFAULT_DOMAIN }iframe.html`,
popup: true,
popupSrc: `${ DEFAULT_DOMAIN }popup.html`,
webusbSrc: `${ DEFAULT_DOMAIN }webusb.html`,
transportReconnect: false,
webusb: true,
pendingTransportEvent: true,
supportedBrowser: typeof navigator !== 'undefined' ? !(/Trident|MSIE|Edge/.test(navigator.userAgent)) : true,
manifest: null,
env: 'web',
lazyLoad: false,
timestamp: new Date().getTime(),
};
let currentSettings: ConnectSettings = initialSettings;
const parseManifest = (manifest: ?Manifest): ?Manifest => {
if (!manifest) return;
if (typeof manifest.email !== 'string') return;
if (typeof manifest.appUrl !== 'string') return;
return {
email: manifest.email,
appUrl: manifest.appUrl,
};
};
export const getEnv = () => {
// $FlowIssue: chrome is not declared outside the project
if (typeof chrome !== 'undefined' && chrome.runtime && typeof chrome.runtime.onConnect !== 'undefined') {
return 'webextension';
}
if (typeof navigator !== 'undefined') {
if (typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative') {
return 'react-native';
}
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf(' electron/') > -1) {
return 'electron';
}
}
// if (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative') {
// return 'react-native';
// }
// if (typeof process !== 'undefined' && process.versions.hasOwnProperty('electron')) {
// return 'electron';
// }
return 'web';
};
// Cors validation copied from Trezor Bridge
// see: https://github.com/trezor/trezord-go/blob/05991cea5900d18bcc6ece5ae5e319d138fc5551/server/api/api.go#L229
// Its pointless to allow `trezor-connect` endpoints { connectSrc } for domains other than listed below
// `trezord` will block communication anyway
export const corsValidator = (url?: string) => {
if (typeof url !== 'string') return;
if (url.match(/^https:\/\/([A-Za-z0-9\-_]+\.)*trezor\.io\//)) return url;
if (url.match(/^https?:\/\/localhost:[58][0-9]{3}\//)) return url;
if (url.match(/^https:\/\/([A-Za-z0-9\-_]+\.)*sldev\.cz\//)) return url;
};
export const parse = (input: $Shape<ConnectSettings> = {}) => {
const settings: ConnectSettings = { ...currentSettings };
if (Object.prototype.hasOwnProperty.call(input, 'debug')) {
if (Array.isArray(input)) {
// enable log with prefix
} if (typeof input.debug === 'boolean') {
settings.debug = input.debug;
} else if (typeof input.debug === 'string') {
settings.debug = input.debug === 'true';
}
}
if (typeof input.connectSrc === 'string') {
settings.connectSrc = input.connectSrc;
}
// For debugging purposes `connectSrc` could be defined in `global.__TREZOR_CONNECT_SRC` variable
if (typeof global !== 'undefined' && typeof global.__TREZOR_CONNECT_SRC === 'string') {
settings.connectSrc = corsValidator(global.__TREZOR_CONNECT_SRC);
settings.debug = true;
}
// For debugging purposes `connectSrc` could be defined in url query of hosting page. Usage:
// https://3rdparty-page.com/?trezor-connect-src=https://localhost:8088/
if (typeof window !== 'undefined' && window.location && typeof window.location.search === 'string') {
const vars = window.location.search.split('&');
const customUrl = vars.find(v => v.indexOf('trezor-connect-src') >= 0);
if (customUrl) {
const [, connectSrc] = customUrl.split('=');
settings.connectSrc = corsValidator(decodeURIComponent(connectSrc));
settings.debug = true;
}
}
const src = settings.connectSrc || DEFAULT_DOMAIN;
settings.iframeSrc = `${src}iframe.html`;
settings.popupSrc = `${src}popup.html`;
settings.webusbSrc = `${src}webusb.html`;
if (typeof input.transportReconnect === 'boolean') {
settings.transportReconnect = input.transportReconnect;
}
if (typeof input.webusb === 'boolean') {
settings.webusb = input.webusb;
}
if (typeof input.popup === 'boolean') {
settings.popup = input.popup;
}
if (typeof input.lazyLoad === 'boolean') {
settings.lazyLoad = input.lazyLoad;
}
if (typeof input.pendingTransportEvent === 'boolean') {
settings.pendingTransportEvent = input.pendingTransportEvent;
}
// local files
if (typeof window !== 'undefined' && window.location && window.location.protocol === 'file:') {
settings.origin = `file://${window.location.pathname}`;
settings.webusb = false;
}
if (typeof input.extension === 'string') {
settings.extension = input.extension;
}
if (typeof input.env === 'string') {
settings.env = input.env;
} else {
settings.env = getEnv();
}
if (typeof input.timestamp === 'number') {
settings.timestamp = input.timestamp;
}
if (typeof input.manifest === 'object') {
settings.manifest = parseManifest(input.manifest);
}
currentSettings = settings;
return currentSettings;
};