-
Notifications
You must be signed in to change notification settings - Fork 29
/
Scatter.ts
165 lines (139 loc) · 3.85 KB
/
Scatter.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
import ScatterJS from '@scatterjs/core'
import ScatterEOS from '@scatterjs/eosjs2'
import {
Authenticator, ButtonStyle, Chain,
UALError, UALErrorType, User
} from 'universal-authenticator-library'
import { Name } from './interfaces'
import { scatterLogo } from './scatterLogo'
import { ScatterUser } from './ScatterUser'
import { UALScatterError } from './UALScatterError'
declare let window: any
export class Scatter extends Authenticator {
private users: ScatterUser[] = []
private scatter: any
private appName: string
private scatterIsLoading: boolean = false
private initError: UALError | null = null
/**
* Scatter Constructor.
*
* @param chains
* @param options { appName } appName is a required option to use Scatter
*/
constructor(chains: Chain[], options?: any) {
super(chains)
if (options && options.appName) {
this.appName = options.appName
} else {
throw new UALScatterError('Scatter requires the appName property to be set on the `options` argument.',
UALErrorType.Initialization,
null)
}
}
/**
* Checks Scatter for a live connection. Will set an Initialization Error
* if we cannot connect to scatter.
*/
public async init(): Promise<void> {
this.scatterIsLoading = true
ScatterJS.plugins(new ScatterEOS())
// set an errored state if scatter doesn't connect
if (!await ScatterJS.scatter.connect(this.appName)) {
this.initError = new UALScatterError('Error occurred while connecting',
UALErrorType.Initialization,
null
)
this.scatterIsLoading = false
return
}
this.scatter = ScatterJS.scatter
window.ScatterJS = null
this.scatterIsLoading = false
}
public reset(): void {
this.initError = null
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.init()
}
public isLoading(): boolean {
return this.scatterIsLoading
}
public isErrored(): boolean {
return !!this.initError
}
public getError(): UALError | null {
return this.initError
}
public getStyle(): ButtonStyle {
return {
icon: scatterLogo,
text: Name,
textColor: 'white',
background: '#078CE9'
}
}
/**
* Scatter will only render on Desktop Browser Environments
*/
public shouldRender(): boolean {
if (!this.isMobile()) {
return true
}
return false
}
public shouldAutoLogin(): boolean {
return false
}
public async login(_?: string): Promise<User[]> {
this.users = []
try {
for (const chain of this.chains) {
const user = new ScatterUser(chain, this.scatter)
await user.getKeys()
this.users.push(user)
}
return this.users
} catch (e) {
throw new UALScatterError(
'Unable to login',
UALErrorType.Login,
e)
}
}
/**
* Call logout on scatter. Throws a Logout Error if unsuccessful
*/
public async logout(): Promise<void> {
try {
this.scatter.logout()
} catch (error) {
throw new UALScatterError('Error occurred during logout',
UALErrorType.Logout,
error)
}
}
/**
* Scatter provides account names so it does not need to request it
*/
public async shouldRequestAccountName(): Promise<boolean> {
return false
}
public isMobile(): boolean {
const userAgent = window.navigator.userAgent
const isIOS = userAgent.includes('iPhone') || userAgent.includes('iPad')
const isMobile = userAgent.includes('Mobile')
const isAndroid = userAgent.includes('Android')
const isCustom = userAgent.toLowerCase().includes('eoslynx')
return isIOS || isMobile || isAndroid || isCustom
}
public getOnboardingLink(): string {
return 'https://get-scatter.com/'
}
public requiresGetKeyConfirmation(): boolean {
return false
}
public getName(): string {
return Name
}
}