-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTokenPocket.ts
167 lines (143 loc) · 4.05 KB
/
TokenPocket.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
import tp from 'tp-eosjs'
import {
Authenticator,
ButtonStyle,
Chain,
UALError,
UALErrorType,
User
} from 'universal-authenticator-library'
import { Name, WalletResponse } from './interfaces'
import { tokenPocketLogo } from './tokenPocketLogo'
import { TokenPocketUser } from './TokenPocketUser'
import { UALTokenPocketError } from './UALTokenPocketError'
export class TokenPocket extends Authenticator {
private users: TokenPocketUser[] = []
private tokenPocketIsLoading: boolean = true
private initError: UALError | null = null
private readonly supportedChains = {
// Token Pocket only supports mainnet
aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906: {},
}
/**
* TokenPocket Constructor.
*
* @param chains
*/
constructor(chains: Chain[]) {
super(chains)
}
private isTokenPocketReady(): boolean {
return tp.isConnected()
}
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
}
/**
* TokenPocket injects into the app from its internal browser, because of that we check on a
* configured interval, allowing up to 5 seconds for TokenPocket to become available before
* throwing an initialization error.
*/
public async init(): Promise<void> {
this.tokenPocketIsLoading = true
try {
if (!this.isTokenPocketReady()) {
throw new Error('Unable to connect')
}
} catch (e) {
this.initError = new UALTokenPocketError(
'Error occurred during autologin',
UALErrorType.Initialization,
e)
} finally {
this.tokenPocketIsLoading = false
}
}
public reset(): void {
this.initError = null
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.init()
}
public getStyle(): ButtonStyle {
return {
icon: tokenPocketLogo,
text: Name,
textColor: 'white',
background: '#347CEE'
}
}
/**
* The TokenPocket authenticator is chain and environment specific, it will only load
* within the Token Pocket browser provided all chains are supported.
*/
public shouldRender(): boolean {
if (this.supportsAllChains() && this.isTokenPocketReady()) {
return true
}
return false
}
public shouldAutoLogin(): boolean {
// Always autologin if should render, since that should only be inside the Token Pocket browser
return this.shouldRender()
}
/**
* Requests the currently active account from Token Pocket, will throw a Login error if Token Pocket
* does not respond or errors out
*/
public async login(): Promise<User[]> {
if (this.users.length === 0) {
try {
const response: WalletResponse = await tp.getCurrentWallet()
if (response.result) {
this.users.push(new TokenPocketUser(this.chains[0], response.data))
} else {
throw new Error('No result returned')
}
} catch (e) {
throw new UALTokenPocketError(
'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 Token Pocket 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.tokenPocketIsLoading
}
public isErrored(): boolean {
return !!this.initError
}
public getError(): UALError | null {
return this.initError
}
public getOnboardingLink(): string {
return 'https://www.mytokenpocket.vip/en/'
}
public requiresGetKeyConfirmation(): boolean {
return false
}
public getName(): string {
return Name
}
}