This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
env.ts
639 lines (629 loc) · 18.8 KB
/
env.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import { $ElementType } from "utility-types";
// set and get environment & config variables
import { Subject } from "rxjs";
import mapValues from "lodash/mapValues";
type EnvDef<V> = {
desc: string;
def: V;
parser: (arg0: unknown) => V | null | undefined;
};
// type ExtractEnvValue = <V>(arg0: EnvDef<V>) => V;
type EnvDefs = typeof envDefinitions;
type Env = typeof env;
export type EnvName = keyof EnvDefs;
export type EnvValue<Name extends EnvName> = $ElementType<Env, Name>;
const intParser = (v: any): number | null | undefined => {
if (!Number.isNaN(v)) return parseInt(v, 10);
};
const floatParser = (v: any): number | null | undefined => {
if (!Number.isNaN(v)) return parseFloat(v);
};
const boolParser = (v: unknown): boolean | null | undefined => {
if (typeof v === "boolean") return v;
return !(v === "0" || v === "false");
};
const stringParser = (v: unknown): string | null | undefined =>
typeof v === "string" ? v : undefined;
const envDefinitions = {
ANALYTICS_CONSOLE: {
def: false,
parser: boolParser,
desc: "Show tracking overlays on the app UI",
},
API_ALGORAND_BLOCKCHAIN_EXPLORER_API_ENDPOINT: {
def: "https://algorand.coin.ledger.com",
parser: stringParser,
desc: "Node API endpoint for algorand",
},
API_CELO_INDEXER: {
def: "https://celo.coin.ledger.com/indexer/",
parser: stringParser,
desc: "Explorer API for celo",
},
API_CELO_NODE: {
def: "https://celo.coin.ledger.com/archive/",
parser: stringParser,
desc: "Node endpoint for celo",
},
API_COSMOS_BLOCKCHAIN_EXPLORER_API_ENDPOINT: {
def: "https://cosmoshub4.coin.ledger.com/",
parser: stringParser,
desc: "Node endpoint for cosmos",
},
API_COSMOS_NODE: {
def: "STARGATE_NODE",
parser: stringParser,
desc: "Node API to use for cosmos (COSMOS_NODE or STARGATE_NODE are known)",
},
API_COSMOS_TESTNET_BLOCKCHAIN_EXPLORER_API_ENDPOINT: {
def: "https://cosmoshub4.coin.staging.aws.ledger.com",
parser: stringParser,
desc: "Node endpoint for cosmos",
},
API_COSMOS_TESTNET_NODE: {
def: "STARGATE_NODE",
parser: stringParser,
desc: "Node API to use for cosmos_testnet (COSMOS_NODE or STARGATE_NODE are known)",
},
API_RIPPLE_RPC: {
parser: stringParser,
def: "https://xrplcluster.com/ledgerlive",
desc: "XRP Ledger full history open JSON-RPC endpoint",
},
API_FILECOIN_ENDPOINT: {
parser: stringParser,
def: "https://live.ledger.dev.zondax.net/blockchain/filecoin", // FIXME Filecoin - Set the correct value for ledger proxy
desc: "Filecoin API url",
},
API_POLKADOT_INDEXER: {
parser: stringParser,
def: "https://polkadot.coin.ledger.com",
desc: "Explorer API for polkadot",
},
API_POLKADOT_SIDECAR: {
parser: stringParser,
def: "https://polkadot-sidecar.coin.ledger.com",
desc: "Polkadot Sidecar API url",
},
ELROND_API_ENDPOINT: {
parser: stringParser,
def: "https://elrond.coin.ledger.com",
desc: "Elrond API url",
},
API_STELLAR_HORIZON: {
parser: stringParser,
def: "https://stellar.coin.ledger.com",
desc: "Stellar Horizon API url",
},
API_STELLAR_HORIZON_FETCH_LIMIT: {
parser: intParser,
def: 200,
desc: "Limit of operation that Horizon will fetch per page",
},
API_TEZOS_BAKER: {
parser: stringParser,
def: "https://tezos-bakers.api.live.ledger.com",
desc: "bakers API for tezos",
},
API_TEZOS_BLOCKCHAIN_EXPLORER_API_ENDPOINT: {
def: "https://xtz-explorer.api.live.ledger.com/explorer",
parser: stringParser,
desc: "Ledger explorer API for tezos",
},
API_TEZOS_TZKT_API: {
def: "https://xtz-tzkt-explorer.api.live.ledger.com",
parser: stringParser,
desc: "tzkt.io explorer",
},
API_TEZOS_NODE: {
def: "https://xtz-node.api.live.ledger.com",
parser: stringParser,
desc: "node API for tezos (for broadcast only)",
},
API_TRONGRID_PROXY: {
parser: stringParser,
def: "https://tron.coin.ledger.com",
desc: "proxy url for trongrid API",
},
API_SOLANA_PROXY: {
parser: stringParser,
def: "https://solana.coin.ledger.com",
desc: "proxy url for solana API",
},
SOLANA_VALIDATORS_APP_BASE_URL: {
parser: stringParser,
def: "https://validators-solana.coin.ledger.com/api/v1/validators",
desc: "base url for validators.app validator list",
},
BASE_SOCKET_URL: {
def: "wss://scriptrunner.api.live.ledger.com/update",
parser: stringParser,
desc: "Ledger script runner API",
},
BOT_TIMEOUT_SCAN_ACCOUNTS: {
def: 30 * 60 * 1000,
parser: intParser,
desc: "bot's default timeout for scanAccounts",
},
COINAPPS: {
def: "",
parser: stringParser,
desc: "(dev feature) defines the folder for speculos mode that contains Nano apps binaries (.elf) in a specific structure: <device>/<firmware>/<appName>/app_<appVersion>.elf",
},
COMPOUND_API: {
def: "https://api.compound.finance",
parser: stringParser,
desc: "location of the compound API",
},
COSMOS_GAS_AMPLIFIER: {
def: 1.4,
parser: intParser,
desc: "estimate gas multiplier",
},
COSMOS_GAS_PRICE: {
def: 0.025,
parser: floatParser,
desc: "gasLimit * gasPrice to determine the fees price. A too low GAS_PRICE will get rejected before the transaction is broadcast",
},
CRYPTO_ORG_INDEXER: {
def: "https://cryptoorg-rpc-indexer.coin.ledger.com",
parser: stringParser,
desc: "location of the crypto.org indexer API",
},
CRYPTO_ORG_TESTNET_INDEXER: {
def: "https://crypto.org/explorer/croeseid4",
parser: stringParser,
desc: "location of the crypto.org indexer testnet API",
},
CRYPTO_ORG_RPC_URL: {
def: "https://cryptoorg-rpc-node.coin.ledger.com",
parser: stringParser,
desc: "location of the crypto.org chain node",
},
CRYPTO_ORG_TESTNET_RPC_URL: {
def: "https://testnet-croeseid-4.crypto.org:26657",
parser: stringParser,
desc: "location of the crypto.org chain testnet node",
},
DEBUG_UTXO_DISPLAY: {
def: 4,
parser: intParser,
desc: "define maximum number of utxos to display in CLI",
},
DEBUG_HTTP_RESPONSE: {
def: false,
parser: boolParser,
desc: "includes HTTP response body in logs",
},
DEVICE_CANCEL_APDU_FLUSH_MECHANISM: {
def: true,
parser: boolParser,
desc: "enable a mechanism that send a 0x00 apdu to force device to awake from its 'Processing' UI state",
},
DEVICE_PROXY_URL: {
def: "",
parser: stringParser,
desc: "enable a proxy to use instead of a physical device",
},
DEVICE_PROXY_MODEL: {
def: "nanoS",
parser: stringParser,
desc: "allow to override the default model of a proxied device",
},
DISABLE_TRANSACTION_BROADCAST: {
def: false,
parser: boolParser,
desc: "disable broadcast of transactions",
},
DISABLE_SYNC_TOKEN: {
def: true,
parser: boolParser,
desc: "disable a problematic mechanism of our API",
},
ETHEREUM_GAS_LIMIT_AMPLIFIER: {
def: 1.2,
parser: floatParser,
desc: "Ethereum gasLimit multiplier for contracts to prevent out of gas issue",
},
EXPERIMENTAL_BLE: {
def: false,
parser: boolParser,
desc: "enable experimental support of Bluetooth",
},
EXPERIMENTAL_CURRENCIES: {
def: "",
parser: stringParser,
desc: "enable experimental support of currencies (comma separated)",
},
EXPERIMENTAL_EXPLORERS: {
def: false,
parser: boolParser,
desc: "enable experimental explorer APIs",
},
EXPERIMENTAL_FALLBACK_APDU_LISTAPPS: {
def: false,
parser: boolParser,
desc: "if HSM list apps fails, fallback on APDU version (>=1.6.0)",
},
EXPERIMENTAL_LANGUAGES: {
def: false,
parser: boolParser,
desc: "enable experimental languages",
},
EXPERIMENTAL_MANAGER: {
def: false,
parser: boolParser,
desc: "enable an experimental version of Manager",
},
EXPERIMENTAL_ROI_CALCULATION: {
def: false,
parser: boolParser,
desc: "enable an experimental version of the portfolio percentage calculation",
},
EXPERIMENTAL_SEND_MAX: {
def: false,
parser: boolParser,
desc: "force enabling SEND MAX even if not yet stable",
},
EXPERIMENTAL_USB: {
def: false,
parser: boolParser,
desc: "enable an experimental implementation of USB support",
},
EXPERIMENTAL_SWAP: {
def: false,
parser: boolParser,
desc: "enable an experimental swap interface",
},
EXPLORER: {
def: "https://explorers.api.live.ledger.com",
parser: stringParser,
desc: "Ledger generic explorer API",
},
EXPLORER_STAGING: {
def: "https://explorers.api-01.live.ledger-stg.com",
parser: stringParser,
desc: "Ledger staging explorer API",
},
EXPLORER_BETA: {
def: "https://explorers.api.live.ledger.com",
parser: stringParser,
desc: "Ledger generic explorer beta API",
},
EXPLORER_SATSTACK: {
def: "http://localhost:20000",
parser: stringParser,
desc: "Ledger satstack Bitcoin explorer API",
},
DISABLE_APP_VERSION_REQUIREMENTS: {
def: false,
parser: boolParser,
desc: "force an old application version to be accepted regardless of its version",
},
FORCE_PROVIDER: {
def: 1,
parser: intParser,
desc: "use a different provider for app store (for developers only)",
},
GET_CALLS_RETRY: {
def: 2,
parser: intParser,
desc: "how many times to retry a GET http call",
},
GET_CALLS_TIMEOUT: {
def: 60 * 1000,
parser: intParser,
desc: "how much time to timeout a GET http call",
},
HIDE_EMPTY_TOKEN_ACCOUNTS: {
def: false,
parser: boolParser,
desc: "hide the sub accounts when they are empty",
},
KEYCHAIN_OBSERVABLE_RANGE: {
def: 0,
parser: intParser,
desc: "overrides the gap limit specified by BIP44 (default to 20)",
},
LEDGER_COUNTERVALUES_API: {
def: "https://countervalues.live.ledger.com",
parser: stringParser,
desc: "Ledger countervalues API",
},
LEDGER_REST_API_BASE: {
def: "https://explorers.api.live.ledger.com",
parser: stringParser,
desc: "DEPRECATED",
},
LEGACY_KT_SUPPORT_TO_YOUR_OWN_RISK: {
def: false,
parser: boolParser,
desc: "enable sending to KT accounts. Not tested.",
},
MANAGER_API_BASE: {
def: "https://manager.api.live.ledger.com/api",
parser: stringParser,
desc: "Ledger Manager API",
},
MANAGER_DEV_MODE: {
def: false,
parser: boolParser,
desc: "enable visibility of utility apps in Manager",
},
MANAGER_INSTALL_DELAY: {
def: 1000,
parser: intParser,
desc: "defines the time to wait before installing apps to prevent known glitch (<=1.5.5) when chaining installs",
},
MAX_ACCOUNT_NAME_SIZE: {
def: 50,
parser: intParser,
desc: "maximum size of account names",
},
MOCK: {
def: "",
parser: stringParser,
desc: "switch the app into a MOCK mode for test purpose, the value will be used as a seed for the rng. Avoid falsy values.",
},
MOCK_SWAP_KYC: {
def: "",
parser: stringParser,
desc: "mock the server response for the exchange KYC check, options are 'open', 'pending', 'closed' or 'approved'.",
},
/**
* Note: the mocked cryptoassets config and test partner are signed with the
* Ledger test private key
*/
MOCK_EXCHANGE_TEST_CONFIG: {
def: false,
parser: boolParser,
desc: "mock the cryptoassets config and test partner (in the context of app-exchange)",
},
NFT_CURRENCIES: {
def: "ethereum,polygon",
parser: stringParser,
desc: "set the currencies where NFT is active",
},
NFT_ETH_METADATA_SERVICE: {
def: "https://nft.api.live.ledger.com",
parser: stringParser,
desc: "service uri used to get the metadata of an nft",
},
OPERATION_ADDRESSES_LIMIT: {
def: 100,
parser: intParser,
desc: "limit the number of addresses in from/to of operations",
},
OPERATION_OPTIMISTIC_RETENTION: {
def: 30 * 60 * 1000,
parser: intParser,
desc: "timeout to keep an optimistic operation that was broadcasted but not yet visible from the coin implementation or the API",
},
OPERATION_PAGE_SIZE_INITIAL: {
def: 100,
parser: intParser,
desc: "defines the initial default operation length page to use",
},
POLKADOT_ELECTION_STATUS_THRESHOLD: {
def: 25,
parser: intParser,
desc: "in blocks - number of blocks before Polkadot election effectively opens to consider it as open and disable all staking features",
},
SATSTACK: {
def: false,
parser: boolParser,
desc: "Switch to satstack mode",
},
SCAN_FOR_INVALID_PATHS: {
def: false,
parser: boolParser,
desc: "enable searching accounts in exotic derivation paths",
},
SEED: {
def: "",
parser: stringParser,
desc: "(dev feature) seed to be used by speculos (device simulator)",
},
SHOW_LEGACY_NEW_ACCOUNT: {
def: false,
parser: boolParser,
desc: "allow the creation of legacy accounts",
},
SKIP_ONBOARDING: {
def: false,
parser: boolParser,
desc: "dev flag to skip onboarding flow",
},
SWAP_API_BASE: {
def: "https://swap.ledger.com/v3",
parser: stringParser,
desc: "Swap API base",
},
SYNC_ALL_INTERVAL: {
def: 2 * 60 * 1000,
parser: intParser,
desc: "delay between successive sync",
},
SYNC_BOOT_DELAY: {
def: 2 * 1000,
parser: intParser,
desc: "delay before the sync starts",
},
SYNC_PENDING_INTERVAL: {
def: 10 * 1000,
parser: intParser,
desc: "delay between sync when an operation is still pending",
},
SYNC_OUTDATED_CONSIDERED_DELAY: {
def: 2 * 60 * 1000,
parser: intParser,
desc: "delay until Live consider a sync outdated",
},
SYNC_MAX_CONCURRENT: {
def: 4,
parser: intParser,
desc: "maximum limit to synchronize accounts concurrently to limit overload",
},
BOT_MAX_CONCURRENT: {
def: 4,
parser: intParser,
desc: "maximum limit to run bot spec in parallel",
},
USER_ID: {
def: "",
parser: stringParser,
desc: "unique identifier of app instance. used to derivate dissociated ids for difference purposes (e.g. the firmware update incremental deployment).",
},
WALLETCONNECT: {
def: false,
parser: boolParser,
desc: "is walletconnect enabled",
},
WITH_DEVICE_POLLING_DELAY: {
def: 500,
parser: floatParser,
desc: "delay when polling device",
},
ANNOUNCEMENTS_API_URL: {
def: "https://cdn.live.ledger.com/announcements",
parser: stringParser,
desc: "url used to fetch new announcements",
},
ANNOUNCEMENTS_API_VERSION: {
def: 1,
parser: intParser,
desc: "version used for the announcements api",
},
STATUS_API_URL: {
def: "https://ledger.statuspage.io/api",
parser: stringParser,
desc: "url used to fetch ledger status",
},
STATUS_API_VERSION: {
def: 2,
parser: intParser,
desc: "version used for ledger status api",
},
PLATFORM_DEBUG: {
def: false,
parser: boolParser,
desc: "enable visibility of debug apps and tools in Platform Catalog",
},
PLATFORM_EXPERIMENTAL_APPS: {
def: false,
parser: boolParser,
desc: "enable visibility of experimental apps and tools in Platform Catalog",
},
PLATFORM_MANIFEST_API_URL: {
def: "https://cdn.live.ledger.com/platform/apps/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests",
},
PLATFORM_MANIFEST_STAGING_API_URL: {
def: "https://cdn.live.ledger-stg.com/platform/apps/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests (staging)",
},
PLATFORM_LOCAL_MANIFEST_JSON: {
def: "",
parser: stringParser,
desc: 'json manifest for a local (test) platform app manifests. How to use: PLATFORM_LOCAL_MANIFEST_JSON="$(cat /path/to/file.json)"',
},
PLATFORM_GLOBAL_CATALOG_API_URL: {
def: "https://cdn.live.ledger.com/platform/catalog/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests",
},
PLATFORM_GLOBAL_CATALOG_STAGING_API_URL: {
def: "https://cdn.live.ledger-stg.com/platform/catalog/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests (staging)",
},
PLATFORM_RAMP_CATALOG_API_URL: {
def: "https://cdn.live.ledger.com/platform/trade/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests",
},
PLATFORM_RAMP_CATALOG_STAGING_API_URL: {
def: "https://cdn.live.ledger-stg.com/platform/trade/v1/data.json",
parser: stringParser,
desc: "url used to fetch platform app manifests (staging)",
},
PLATFORM_API_URL: {
def: "",
parser: stringParser,
desc: "url used to fetch platform catalog",
},
PLATFORM_API_VERSION: {
def: 1,
parser: intParser,
desc: "version used for the platform api",
},
MARKET_API_URL: {
def: "https://proxycg.api.live.ledger.com/api/v3",
parser: stringParser,
desc: "Market data api",
},
USE_LEARN_STAGING_URL: {
def: false,
parser: boolParser,
desc: "use the staging URL for the learn page",
},
};
const getDefinition = (name: string): EnvDef<any> | null | undefined =>
envDefinitions[name];
envDefinitions as Record<EnvName, EnvDef<any>>;
const defaults: Record<EnvName, any> = mapValues(
envDefinitions,
(o) => o.def
) as unknown as Record<EnvName, any>;
// private local state
const env: Record<EnvName, any> = { ...defaults };
export const getAllEnvNames = (): EnvName[] =>
Object.keys(envDefinitions) as EnvName[];
export const getAllEnvs = (): Env => ({ ...env });
// Usage: you must use getEnv at runtime because the env might be settled over time. typically will allow us to dynamically change them on the interface (e.g. some sort of experimental flags system)
export const getEnv = <Name extends EnvName>(name: Name): EnvValue<Name> =>
env[name];
export const getEnvDefault = <Name extends EnvName>(
name: Name
): EnvValue<Name> => defaults[name];
export const isEnvDefault = <Name extends EnvName>(name: Name): boolean =>
env[name] === defaults[name];
export const getEnvDesc = <Name extends EnvName>(name: Name): string =>
envDefinitions[name].desc;
type ChangeValue<T extends EnvName> = {
name: EnvName;
value: EnvValue<T>;
oldValue: EnvValue<T>;
};
export const changes: Subject<ChangeValue<any>> = new Subject();
// change one environment
export const setEnv = <Name extends EnvName>(
name: Name,
value: EnvValue<Name>
): void => {
const oldValue = env[name];
if (oldValue !== value) {
env[name] = value;
changes.next({
name,
value,
oldValue,
});
}
};
// change one environment with safety. returns true if it succeed
export const setEnvUnsafe = (name: EnvName, unsafeValue: unknown): boolean => {
const definition = getDefinition(name);
if (!definition) return false;
const { parser } = definition;
const value = parser(unsafeValue);
if (value === undefined || value === null) {
console.warn(`Invalid ENV value for ${name}`);
return false;
}
// $FlowFixMe flow don't seem to type proof it
setEnv(name, value);
return true;
};