-
Notifications
You must be signed in to change notification settings - Fork 8
/
Lynx.ts
183 lines (159 loc) · 4.4 KB
/
Lynx.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
import {
Authenticator,
ButtonStyle,
Chain,
UALError,
UALErrorType,
User
} from 'universal-authenticator-library'
import { Name } from './interfaces'
import { lynxLogo } from './lynxLogo'
import { LynxUser } from './LynxUser'
import { UALLynxError } from './UALLynxError'
declare const window: any
export class Lynx extends Authenticator {
// Forces timeout if Lynx object is not found on window in 5 seconds
private static API_LOADED_CHECK_TIMEOUT = 5000
// Reference for Lynx timeout function
private static LYNX_TIMEOUT: any = null
private users: LynxUser[] = []
private lynxIsLoading: boolean = true
private initError: UALError | null = null
private readonly supportedChains = {
// Lynx only supports mainnet
aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906: {},
}
/**
* Lynx Constructor
*
* @param chains
*/
constructor(chains: Chain[]) {
super(chains)
}
private isLynxReady(): Promise<boolean> {
return new Promise((resolve) => {
Lynx.LYNX_TIMEOUT = setTimeout(() => {
resolve(false)
}, Lynx.API_LOADED_CHECK_TIMEOUT)
if (!!window.lynxMobile) {
clearTimeout(Lynx.LYNX_TIMEOUT)
resolve(true)
}
window.addEventListener('lynxMobileLoaded', () => {
clearTimeout(Lynx.LYNX_TIMEOUT)
resolve(true)
})
})
}
private supportsAllChains(): boolean {
if (this.chains.length < 1) {
return false
}
for (const chain of this.chains) {
if (!this.supportedChains.hasOwnProperty(chain.chainId)) {
return false
}
}
return true
}
private isLynxBrowser(): boolean {
return window.navigator.userAgent.toLowerCase().indexOf('eoslynx') !== -1
}
/**
* Lynx injects into the app from its internal browser, because of that we check on a
* configured interval, allowing up to 5 seconds for Lynx to become available before
* throwing an initialization error.
*/
public async init() {
this.lynxIsLoading = true
try {
const lynxReady = await this.isLynxReady()
if (!lynxReady) {
throw new Error('Unable to connect')
}
} catch (e) {
this.initError = new UALLynxError(
'Error occurred during autologin',
UALErrorType.Initialization,
e)
} finally {
this.lynxIsLoading = false
}
}
public reset(): void {
this.initError = null
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.init()
}
public getStyle(): ButtonStyle {
return {
icon: lynxLogo,
text: Name,
textColor: 'white',
background: '#21202D'
}
}
/**
* Lynx is chain and environment specific, it will only load within the Lynx browser
* provided all chains are supported.
*/
public shouldRender(): boolean {
if (this.isLynxBrowser() && this.supportsAllChains()) {
return true
}
return false
}
public shouldAutoLogin(): boolean {
// Always autologin if should render, since that should only be inside the Lynx browser
return this.shouldRender()
}
/**
* Requests the currently active account from Lynx, will throw a Login error if Lynx does
* not respond or errors out
*/
public async login(_?: string): Promise<User[]> {
if (this.users.length === 0) {
try {
const account = await window.lynxMobile.requestSetAccount()
this.users.push(new LynxUser(this.chains[0], account))
} catch (e) {
throw new UALLynxError(
'Unable to get the current account during login',
UALErrorType.Login,
e)
}
}
return this.users
}
/**
* Clears the array of authenticated users
* Note: The name - logout - is slightly misleading in this particular case
* as calling this method will not log a user out of the Lynx app but rather
* refresh the user list on the authenticator
*/
public async logout(): Promise<void> {
this.users = []
}
public async shouldRequestAccountName(): Promise<boolean> {
return false
}
public isLoading(): boolean {
return this.lynxIsLoading
}
public isErrored(): boolean {
return !!this.initError
}
public getError(): UALError | null {
return this.initError
}
public getOnboardingLink(): string {
return 'https://lynxwallet.io/'
}
public requiresGetKeyConfirmation(): boolean {
return false
}
public getName(): string {
return Name
}
}