-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathresponses.ts
More file actions
323 lines (287 loc) · 9.89 KB
/
responses.ts
File metadata and controls
323 lines (287 loc) · 9.89 KB
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
import {
FeeOption,
IntentDataSendTransaction,
IntentResponseAccountFederated,
IntentResponseAccountList,
IntentResponseAdopter,
IntentResponseAuthInitiated,
IntentResponseChildWalletAdopted,
IntentResponseCode,
IntentResponseGetSession,
IntentResponseIdToken,
IntentResponseValidationFinished,
IntentResponseValidationStarted,
IntentResponseConfirmationRequired,
} from '../clients/intent.gen'
import { WebrpcEndpointError, WebrpcError } from '../clients/authenticator.gen'
export type PayloadResponse<T> = {
code: string
data: T
}
export type ValidationRequiredResponse = {
code: 'validationRequired'
data: {
sessionId: string
}
}
type MetaTxnReceiptLog = {
address: string
topics: string[]
data: string
}
type MetaTxnReceipt = {
id: string
status: string
revertReason?: string | null
index: number
logs: MetaTxnReceiptLog[]
receipts: MetaTxnReceipt[]
txnReceipt: string
}
type SimulateResult = {
executed: boolean
succeeded: boolean
result: string | null
reason: string | null
gasUsed: number
gasLimit: number
}
export type SentTransactionResponse = {
code: 'transactionReceipt'
data: {
txHash: string
metaTxHash: string
request: IntentDataSendTransaction
receipt: MetaTxnReceipt
nativeReceipt?: any | null
simulations?: SimulateResult[]
}
}
export type TransactionFailedResponse = {
code: 'transactionFailed'
data: {
error: string
request: IntentDataSendTransaction
simulations: SimulateResult[]
}
}
export type MaySentTransactionResponse = SentTransactionResponse | TransactionFailedResponse
export type FeeOptionsResponse = {
code: 'feeOptions'
data: {
feeOptions: FeeOption[]
feeQuote?: string
}
}
export type OpenSessionResponse = {
code: 'sessionOpened'
data: {
sessionId: string
wallet: string
}
}
export type CloseSessionResponse = {
code: 'sessionClosed'
}
export type ListSessionsResponse = {
code: 'listSessions'
data: {
sessions: any[]
}
}
export type SignedMessageResponse = {
code: 'signedMessage'
data: {
message: string
signature: string
}
}
export type SessionAuthProofResponse = {
code: 'sessionAuthProof'
data: {
sessionId: string
network: string
wallet: string
message: string
signature: string
}
}
export interface Response<Code, Data> {
code: Code
data: Data
}
export type InitiateAuthResponse = Response<IntentResponseCode.authInitiated, IntentResponseAuthInitiated>
export type ValidateSessionResponse = Response<IntentResponseCode.validationStarted, IntentResponseValidationStarted>
export type ConfirmationRequiredResponse = Response<IntentResponseCode.confirmationRequired, IntentResponseConfirmationRequired>
export type FinishValidateSessionResponse = Response<IntentResponseCode.validationFinished, IntentResponseValidationFinished>
export type GetSessionResponse = Response<IntentResponseCode.getSessionResponse, IntentResponseGetSession>
export type LinkAccountResponse = Response<IntentResponseCode.accountFederated, IntentResponseAccountFederated>
export type ListAccountsResponse = Response<IntentResponseCode.accountList, IntentResponseAccountList>
export type IdTokenResponse = Response<IntentResponseCode.idToken, IntentResponseIdToken>
export type AdopterResponse = Response<IntentResponseCode.adopter, IntentResponseAdopter>
export type ChildWalletAdoptedResponse = Response<IntentResponseCode.childWalletAdopted, IntentResponseChildWalletAdopted>
export function isInitiateAuthResponse(receipt: any): receipt is InitiateAuthResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.authInitiated &&
typeof receipt.data === 'object' &&
typeof receipt.data.sessionId === 'string' &&
typeof receipt.data.identityType === 'string' &&
typeof receipt.data.expiresIn === 'number'
)
}
export function isOpenSessionResponse(receipt: any): receipt is OpenSessionResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'sessionOpened' &&
typeof receipt.data === 'object' &&
typeof receipt.data.sessionId === 'string' &&
typeof receipt.data.wallet === 'string'
)
}
export function isSentTransactionResponse(receipt: any): receipt is SentTransactionResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'transactionReceipt' &&
typeof receipt.data === 'object' &&
typeof receipt.data.txHash === 'string' &&
typeof receipt.data.receipt === 'object' &&
typeof receipt.data.request === 'object'
)
}
export function isTimedOutTransactionResponse(receipt: any): receipt is SentTransactionResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'transactionReceipt' &&
typeof receipt.data === 'object' &&
typeof receipt.data.metaTxHash === 'string' &&
!receipt.data.txHash &&
typeof receipt.data.request === 'object'
)
}
export function isFailedTransactionResponse(receipt: any): receipt is TransactionFailedResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'transactionFailed' &&
typeof receipt.data === 'object' &&
typeof receipt.data.request === 'object' &&
Array.isArray(receipt.data.simulations) &&
typeof receipt.data.error === 'string'
)
}
export function isMaySentTransactionResponse(receipt: any): receipt is MaySentTransactionResponse {
return isSentTransactionResponse(receipt) || isFailedTransactionResponse(receipt) || isTimedOutTransactionResponse(receipt)
}
export function isSignedMessageResponse(receipt: any): receipt is SignedMessageResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'signedMessage' &&
typeof receipt.data === 'object' &&
typeof receipt.data.message === 'string' &&
typeof receipt.data.signature === 'string'
)
}
export function isSessionAuthProofResponse(receipt: any): receipt is SessionAuthProofResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'sessionAuthProof' &&
typeof receipt.data === 'object' &&
typeof receipt.data.sessionId === 'string' &&
typeof receipt.data.network === 'string' &&
typeof receipt.data.wallet === 'string' &&
typeof receipt.data.message === 'string' &&
typeof receipt.data.signature === 'string'
)
}
export function isFeeOptionsResponse(receipt: any): receipt is FeeOptionsResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'feeOptions' &&
typeof receipt.data === 'object' &&
Array.isArray(receipt.data.feeOptions)
)
}
export function isValidationRequiredResponse(receipt: any): receipt is ValidationRequiredResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.validationRequired &&
typeof receipt.data === 'object' &&
typeof receipt.data.sessionId === 'string'
)
}
export function isValidateSessionResponse(receipt: any): receipt is ValidateSessionResponse {
return typeof receipt === 'object' && receipt.code === IntentResponseCode.validationStarted && typeof receipt.data === 'object'
}
export function isFinishValidateSessionResponse(receipt: any): receipt is FinishValidateSessionResponse {
return typeof receipt === 'object' && receipt.code === IntentResponseCode.validationFinished && typeof receipt.data === 'object'
}
export function isConfirmationRequiredResponse(receipt: any): receipt is ConfirmationRequiredResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.confirmationRequired &&
typeof receipt.data === 'object' &&
typeof receipt.data.salt === 'string'
)
}
export function isCloseSessionResponse(receipt: any): receipt is CloseSessionResponse {
return typeof receipt === 'object' && typeof receipt.code === 'string' && receipt.code === 'sessionClosed'
}
export function isGetSessionResponse(receipt: any): receipt is GetSessionResponse {
return (
typeof receipt === 'object' &&
typeof receipt.code === 'string' &&
receipt.code === 'getSessionResponse' &&
typeof receipt.data === 'object' &&
typeof receipt.data.session === 'string' &&
typeof receipt.data.wallet === 'string'
)
}
export function isLinkAccountResponse(receipt: any): receipt is LinkAccountResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.accountFederated &&
typeof receipt.data === 'object' &&
typeof receipt.data.account === 'object'
)
}
export function isListAccountsResponse(receipt: any): receipt is ListAccountsResponse {
return typeof receipt === 'object' && receipt.code === IntentResponseCode.accountList && typeof receipt.data === 'object'
}
export function isIntentTimeError(error: any): error is WebrpcEndpointError {
return !!(
error instanceof WebrpcError &&
(error.cause?.endsWith('intent is invalid: intent expired') ||
error.cause?.endsWith('intent is invalid: intent issued in the future'))
)
}
export function isGetIdTokenResponse(receipt: any): receipt is IdTokenResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.idToken &&
typeof receipt.data === 'object' &&
typeof receipt.data.idToken === 'string'
)
}
export function isGetAdopterResponse(receipt: any): receipt is AdopterResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.adopter &&
typeof receipt.data === 'object' &&
typeof receipt.data.adopterAddress === 'string'
)
}
export function isChildWalletAdoptedResponse(receipt: any): receipt is ChildWalletAdoptedResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.childWalletAdopted &&
typeof receipt.data === 'object' &&
typeof receipt.data.adopterAddress === 'string'
)
}