Skip to content

Commit de89897

Browse files
committed
chore: cleanup and HttpClient fix
1 parent 7589783 commit de89897

File tree

13 files changed

+385
-261
lines changed

13 files changed

+385
-261
lines changed

packages/exceptions/src/exceptions/types/modules.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,4 @@ export enum WalletErrorActionModule {
5959
GetNetworkId = 'get-network-id',
6060
GetChainId = 'get-chain-id',
6161
GetEthereumTransactionReceipt = 'get-ethereum-transaction-receipt',
62-
TurnkeyInitEmailOTP = 'turnkey-init-email-otp',
63-
TurnkeyVerifyEmailOTP = 'turnkey-verify-email-otp',
6462
}

packages/sdk-ts/src/core/tx/api/TxRestApi.ts

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2+
HttpClient,
23
BigNumberInBase,
34
DEFAULT_BLOCK_TIMEOUT_HEIGHT,
45
DEFAULT_BLOCK_TIME_IN_SECONDS,
56
DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS,
6-
HttpRestClient,
77
} from '@injectivelabs/utils'
88
import {
99
BroadcastMode,
@@ -14,29 +14,36 @@ import {
1414
import { TxClient } from '../utils/classes/TxClient.js'
1515
import { TxClientBroadcastOptions, TxConcreteApi } from '../types/tx.js'
1616
import {
17+
HttpRequestMethod,
1718
HttpRequestException,
1819
TransactionException,
20+
UnspecifiedErrorCode,
1921
} from '@injectivelabs/exceptions'
22+
import axios, { AxiosError } from 'axios'
2023
import { StatusCodes } from 'http-status-codes'
2124
import { TxResponse } from '../types/tx.js'
25+
import { getErrorMessage } from '../../../utils/helpers.js'
2226
import { CosmosTxV1Beta1Tx } from '@injectivelabs/core-proto-ts'
2327

2428
/**
2529
* It is recommended to use TxGrpcClient instead of TxRestApi
2630
*/
2731
export class TxRestApi implements TxConcreteApi {
28-
public client: HttpRestClient
32+
public httpClient: HttpClient
2933

3034
constructor(endpoint: string, options?: { timeout?: number }) {
31-
this.client = new HttpRestClient(endpoint, {
35+
this.httpClient = new HttpClient(endpoint, {
36+
headers: {
37+
Accept: 'application/json',
38+
},
3239
timeout: options?.timeout || 15000,
3340
})
3441
}
3542

3643
public async fetchTx(txHash: string, params: any = {}): Promise<TxResponse> {
3744
try {
38-
const response = await this.client.$get<TxResultResponse>(
39-
`cosmos/tx/v1beta1/txs/${txHash}`,
45+
const response = await this.getRaw<TxResultResponse>(
46+
`/cosmos/tx/v1beta1/txs/${txHash}`,
4047
params,
4148
)
4249

@@ -132,8 +139,8 @@ export class TxRestApi implements TxConcreteApi {
132139
}
133140

134141
try {
135-
const response = await this.client.$post<SimulationResponse>(
136-
'cosmos/tx/v1beta1/simulate',
142+
const response = await this.postRaw<SimulationResponse>(
143+
'/cosmos/tx/v1beta1/simulate',
137144
{
138145
tx_bytes: TxClient.encode(txRawClone),
139146
},
@@ -236,11 +243,78 @@ export class TxRestApi implements TxConcreteApi {
236243
txRaw: CosmosTxV1Beta1Tx.TxRaw,
237244
mode: BroadcastMode = BroadcastMode.Sync,
238245
): Promise<T> {
239-
const response = await this.client.$post<T>('cosmos/tx/v1beta1/txs', {
246+
const response = await this.postRaw<T>('cosmos/tx/v1beta1/txs', {
240247
tx_bytes: TxClient.encode(txRaw),
241248
mode,
242249
})
243250

244251
return response
245252
}
253+
254+
private async getRaw<T>(
255+
endpoint: string,
256+
params: URLSearchParams | any = {},
257+
): Promise<T> {
258+
try {
259+
return await this.httpClient
260+
.get<URLSearchParams | any, { data: T }>(endpoint, params)
261+
.then((d) => d.data)
262+
} catch (e) {
263+
const error = e as Error | AxiosError
264+
265+
if (axios.isAxiosError(error)) {
266+
if (error.code === 'ECONNABORTED') {
267+
throw new HttpRequestException(new Error(error.message), {
268+
code: StatusCodes.REQUEST_TOO_LONG,
269+
context: endpoint,
270+
})
271+
}
272+
273+
const message = getErrorMessage(error, endpoint)
274+
275+
throw new HttpRequestException(new Error(message), {
276+
context: endpoint,
277+
code: error.response
278+
? error.response.status
279+
: StatusCodes.BAD_REQUEST,
280+
})
281+
}
282+
283+
throw new HttpRequestException(new Error((error as any).message), {
284+
context: endpoint,
285+
code: UnspecifiedErrorCode,
286+
})
287+
}
288+
}
289+
290+
private async postRaw<T>(
291+
endpoint: string,
292+
params: URLSearchParams | any = {},
293+
): Promise<T> {
294+
try {
295+
return await this.httpClient
296+
.post<URLSearchParams | any, { data: T }>(endpoint, params)
297+
.then((d) => d.data)
298+
} catch (e) {
299+
const error = e as Error | AxiosError
300+
301+
if (axios.isAxiosError(error)) {
302+
const message = getErrorMessage(error, endpoint)
303+
304+
throw new HttpRequestException(new Error(message), {
305+
code: error.response
306+
? error.response.status
307+
: StatusCodes.BAD_REQUEST,
308+
context: endpoint,
309+
contextModule: HttpRequestMethod.Post,
310+
})
311+
}
312+
313+
throw new HttpRequestException(new Error((error as any).message), {
314+
code: UnspecifiedErrorCode,
315+
context: endpoint,
316+
contextModule: HttpRequestMethod.Post,
317+
})
318+
}
319+
}
246320
}

packages/sdk-ts/src/utils/address.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
getEthereumAddress,
33
getInjectiveAddress,
44
getDefaultSubaccountId,
5+
getChecksumAddress,
56
} from '../../src/utils/index.js'
67

78
describe('address helper functions', () => {
@@ -31,4 +32,13 @@ describe('address helper functions', () => {
3132
'0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000',
3233
)
3334
})
35+
36+
it('getChecksumAddress returns correct value', () => {
37+
const ethereumAddress =
38+
'0xaf79152ac5df276d9a8e1e2e22822f9713474902'.toLowerCase()
39+
40+
expect(getChecksumAddress(ethereumAddress)).toMatch(
41+
'0xAF79152AC5dF276D9A8e1E2E22822f9713474902',
42+
)
43+
})
3444
})

packages/utils/src/classes/HttpClient.ts

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,60 +28,19 @@ export default class HttpClient {
2828
return this
2929
}
3030

31-
async get<T, P extends Record<string, any>>(
32-
endpoint: string,
33-
params: P = {} as P,
34-
): Promise<T> {
31+
get<T, P>(endpoint: string, params: T = {} as T): Promise<P> {
3532
return this.client.get(endpoint, { params, ...this.config })
3633
}
3734

38-
/**
39-
* This functions parses the `data` property from the response
40-
* and returns the data as a typed object.
41-
*/
42-
async $get<T, P extends Record<string, any>>(
43-
endpoint: string,
44-
params: P = {} as P,
45-
): Promise<T> {
46-
const response = await this.client.get(endpoint, {
47-
params,
48-
...this.config,
49-
})
50-
51-
return 'data' in response ? response.data : response
52-
}
53-
54-
async post<T, P extends Record<string, any>>(
55-
endpoint: string,
56-
data: P = {} as P,
57-
): Promise<T> {
35+
post<T, P>(endpoint: string, data: T = {} as T): Promise<P> {
5836
return this.client.post(endpoint, data, this.config)
5937
}
6038

61-
/**
62-
* This functions parses the `data` property from the response
63-
* and returns the data as a typed object.
64-
*/
65-
async $post<T, P extends Record<string, any>>(
66-
endpoint: string,
67-
data: P = {} as P,
68-
): Promise<T> {
69-
const response = await this.client.post(endpoint, data, this.config)
70-
71-
return 'data' in response ? response.data : response
72-
}
73-
74-
async put<T, P extends Record<string, any>>(
75-
endpoint: string,
76-
data: P = {} as P,
77-
): Promise<T> {
39+
put<T, P>(endpoint: string, data: T = {} as T): Promise<P> {
7840
return this.client.put(endpoint, data, this.config)
7941
}
8042

81-
async delete<T, P extends Record<string, any>>(
82-
endpoint: string,
83-
params: P = {} as P,
84-
): Promise<T> {
43+
delete<T, P>(endpoint: string, params: T = {} as T): Promise<P> {
8544
return this.client.delete(endpoint, { params, ...this.config })
8645
}
8746
}

packages/utils/src/classes/HttpRestClient.ts

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,6 @@ export default class HttpRestClient {
7070
}
7171
}
7272

73-
/**
74-
* This functions parses the `data` property from the response
75-
* and returns the data as a typed object.
76-
*/
77-
public async $get<T>(
78-
endpoint: string,
79-
params: Record<string, any> = {},
80-
): Promise<T> {
81-
try {
82-
return await this.client.$get(endpoint, params)
83-
} catch (e: unknown) {
84-
const error = e as Error | AxiosError
85-
86-
if (axios.isAxiosError(error)) {
87-
if (error.code === 'ECONNABORTED') {
88-
throw new HttpRequestException(new Error(error.message), {
89-
code: StatusCodes.REQUEST_TOO_LONG,
90-
context: endpoint,
91-
})
92-
}
93-
94-
const message = getErrorMessage(error, endpoint)
95-
96-
throw new HttpRequestException(new Error(message), {
97-
context: endpoint,
98-
code: error.response
99-
? error.response.status
100-
: StatusCodes.BAD_REQUEST,
101-
})
102-
}
103-
104-
throw new HttpRequestException(new Error((error as any).message), {
105-
code: UnspecifiedErrorCode,
106-
context: endpoint,
107-
})
108-
}
109-
}
110-
11173
public async retry<TResponse>(
11274
httpCall: Function,
11375
retries: number = 3,
@@ -174,44 +136,4 @@ export default class HttpRestClient {
174136
})
175137
}
176138
}
177-
178-
/**
179-
* This functions parses the `data` property from the response
180-
* and returns the data as a typed object.
181-
*/
182-
public async $post<T>(
183-
endpoint: string,
184-
params: Record<string, any> = {},
185-
): Promise<T> {
186-
try {
187-
return await this.client.$post(endpoint, params)
188-
} catch (e: unknown) {
189-
const error = e as Error | AxiosError
190-
191-
if (axios.isAxiosError(error)) {
192-
if (error.code === 'ECONNABORTED') {
193-
throw new HttpRequestException(new Error(error.message), {
194-
code: StatusCodes.REQUEST_TOO_LONG,
195-
method: HttpRequestMethod.Post,
196-
})
197-
}
198-
199-
const message = getErrorMessage(error, endpoint)
200-
201-
throw new HttpRequestException(new Error(message), {
202-
code: error.response
203-
? error.response.status
204-
: StatusCodes.BAD_REQUEST,
205-
context: endpoint,
206-
contextModule: HttpRequestMethod.Post,
207-
})
208-
}
209-
210-
throw new HttpRequestException(new Error((error as any).message), {
211-
code: UnspecifiedErrorCode,
212-
context: endpoint,
213-
contextModule: HttpRequestMethod.Post,
214-
})
215-
}
216-
}
217139
}

packages/utils/src/classes/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import Status, { StatusType } from './Status.js'
99

1010
export {
1111
Status,
12-
BigNumber,
1312
StatusType,
14-
HttpClient,
1513
LocalStorage,
1614
BigNumberInBase,
1715
BigNumberInWei,
16+
BigNumber,
17+
HttpClient,
1818
HttpRestClient,
1919
StreamManager,
2020
}

packages/wallets/wallet-base/src/types/strategy.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ export interface SendTransactionOptions {
5353
}
5454
}
5555

56-
export enum TurnkeyStatus {
57-
Ready = 'ready',
58-
Error = 'error',
59-
LoggedIn = 'logged-in',
60-
WaitingOtp = 'waiting-otp',
61-
Initializing = 'initializing',
62-
}
63-
6456
export enum TurnkeyProvider {
6557
Email = 'email',
6658
Google = 'google',
@@ -83,13 +75,16 @@ export interface TurnkeyMetadata {
8375
email?: string
8476
session?: TurnkeySession
8577
otpId?: string
78+
otpCode?: string
8679
oidcToken?: string
87-
iframeElementId: string
80+
iframeElementId?: string
8881
iframeContainerId: string
8982
credentialBundle?: string
9083
organizationId?: string
9184
provider?: TurnkeyProvider
92-
onStatusChange?: (status: TurnkeyStatus) => void
85+
otpInitPath?: string
86+
otpVerifyPath?: string
87+
oauthLoginPath?: string
9388
}
9489

9590
export interface WalletMetadata {

packages/wallets/wallet-turnkey/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
"start": "node dist/index.js"
5959
},
6060
"dependencies": {
61-
"@injectivelabs/exceptions": "^1.15.1",
62-
"@injectivelabs/sdk-ts": "^1.15.3",
63-
"@injectivelabs/ts-types": "^1.15.2",
64-
"@injectivelabs/utils": "^1.15.2",
65-
"@injectivelabs/wallet-base": "^1.15.3",
61+
"@injectivelabs/exceptions": "^1.15.2",
62+
"@injectivelabs/sdk-ts": "^1.15.5",
63+
"@injectivelabs/ts-types": "^1.15.3",
64+
"@injectivelabs/utils": "^1.15.3",
65+
"@injectivelabs/wallet-base": "^1.15.5",
6666
"@turnkey/sdk-browser": "^4.1.0",
6767
"@turnkey/viem": "^0.9.0",
6868
"viem": "^2.28.1"

packages/wallets/wallet-turnkey/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { TurnkeyOtpWalletStrategy } from './strategy/strategy/otp.js'
22
export { TurnkeyOauthWalletStrategy } from './strategy/strategy/oauth.js'
33

44
export * from './strategy/strategy/base.js'
5+
export * from './strategy/turnkey/turnkey.js'

0 commit comments

Comments
 (0)