Skip to content

Commit a4bae4b

Browse files
authored
Merge pull request #164 from hyperweb-io/refactor-eth
Refactor and move utils to packages
2 parents 18a3f2d + f6d3bef commit a4bae4b

File tree

44 files changed

+2779
-718
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2779
-718
lines changed

dev-docs/networks-refactoring-analysis.md

Lines changed: 904 additions & 0 deletions
Large diffs are not rendered by default.

docs/advanced/package-functionality-guide.md

Lines changed: 916 additions & 0 deletions
Large diffs are not rendered by default.

libs/interchainjs/starship/__tests__/token.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import './setup.test';
22

33
import { ChainInfo } from '@chain-registry/client';
44
import { Asset } from '@chain-registry/types';
5-
import { generateMnemonic } from '../src/utils';
65
import { OfflineDirectSigner } from '@interchainjs/cosmos';
76
import { Secp256k1HDWallet } from '@interchainjs/cosmos/wallets/secp256k1hd';
87
import { HDPath } from '@interchainjs/types';
98
import { getBalance } from "interchainjs/cosmos/bank/v1beta1/query.rpc.func";
109
import { send } from "interchainjs/cosmos/bank/v1beta1/tx.rpc.func";
11-
import { useChain } from 'starshipjs';
10+
import { generateMnemonic, useChain } from 'starshipjs';
1211
import { getSigner, GetSignerOptions } from '../../src/interchain/core/getSigner';
1312
import { DirectSigner } from '@interchainjs/cosmos';
1413
import { ICosmosQueryClient, createCosmosQueryClient } from '@interchainjs/cosmos';
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
export * from './utils';

libs/interchainjs/starship/src/utils.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

networks/cosmos/src/adapters/base.ts

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { fromBase64, fromHex } from '@interchainjs/encoding';
1+
import { apiToNumber as encApiToNumber, apiToBigInt as encApiToBigInt, maybeFromBase64 as encMaybeFromBase64, safeFromBase64 as encSafeFromBase64, maybeFromHex as encMaybeFromHex } from '@interchainjs/encoding';
2+
import { snakeToCamel, snakeCaseRecursive } from '@interchainjs/utils';
23
import { RpcMethod, ProtocolVersion, ProtocolInfo, ProtocolCapabilities } from '../types/protocol';
34
import {
45
AbciInfoResponse
@@ -239,72 +240,17 @@ export interface ICosmosProtocolAdapter extends IProtocolAdapter, RequestEncoder
239240
export abstract class BaseAdapter implements RequestEncoder, ResponseDecoder, ICosmosProtocolAdapter {
240241
constructor(protected version: ProtocolVersion) {}
241242

242-
// Recursive snake_case to camelCase transformation
243-
protected toCamelCase(str: string): string {
244-
return str.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase());
245-
}
246-
243+
// Use shared utilities from @interchainjs/utils
247244
protected transformKeys(obj: any): any {
248-
if (obj === null || obj === undefined) {
249-
return obj;
250-
}
251-
252-
if (Array.isArray(obj)) {
253-
return obj.map(item => this.transformKeys(item));
254-
}
255-
256-
if (typeof obj === 'object') {
257-
const transformed: any = {};
258-
for (const [key, value] of Object.entries(obj)) {
259-
const camelKey = this.toCamelCase(key);
260-
transformed[camelKey] = this.transformKeys(value);
261-
}
262-
return transformed;
263-
}
264-
265-
return obj;
245+
return snakeCaseRecursive(obj);
266246
}
267247

268-
protected apiToNumber(value: string | undefined | null): number {
269-
if (!value) return 0;
270-
const num = parseInt(value, 10);
271-
if (Number.isNaN(num)) return 0;
272-
return num;
273-
}
274-
275-
protected apiToBigInt(value: string | undefined | null): bigint {
276-
if (!value) return BigInt(0);
277-
return BigInt(value);
278-
}
279-
280-
protected maybeFromBase64(value: string | undefined | null): Uint8Array | undefined {
281-
if (!value) return undefined;
282-
return this.safeFromBase64(value);
283-
}
284-
285-
protected safeFromBase64(value: string): Uint8Array {
286-
if (!value) return new Uint8Array(0);
248+
protected apiToNumber(value: string | undefined | null): number { return encApiToNumber(value as any); }
249+
protected apiToBigInt(value: string | undefined | null): bigint { return encApiToBigInt(value as any); }
250+
protected maybeFromBase64(value: string | undefined | null): Uint8Array | undefined { return encMaybeFromBase64(value as any); }
251+
protected safeFromBase64(value: string): Uint8Array { return encSafeFromBase64(value); }
252+
protected maybeFromHex(value: string | undefined | null): Uint8Array | undefined { return encMaybeFromHex(value as any); }
287253

288-
// Fix base64 padding if needed
289-
let paddedValue = value;
290-
const remainder = value.length % 4;
291-
if (remainder > 0) {
292-
paddedValue = value + '='.repeat(4 - remainder);
293-
}
294-
295-
try {
296-
return fromBase64(paddedValue);
297-
} catch (error) {
298-
// If base64 decoding fails, return empty array
299-
console.warn(`Failed to decode base64 value: ${value}`, error);
300-
return new Uint8Array(0);
301-
}
302-
}
303-
304-
protected maybeFromHex(value: string | undefined | null): Uint8Array | undefined {
305-
if (!value) return undefined;
306-
return fromHex(value);
307-
}
308254

309255
protected decodeTime(timestamp: string): Date {
310256
return new Date(timestamp);
@@ -444,31 +390,6 @@ export abstract class BaseAdapter implements RequestEncoder, ResponseDecoder, IC
444390
return true; // All versions support basic consensus queries
445391
}
446392

447-
private camelToSnake(str: string): string {
448-
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
449-
}
450-
451-
private snakeToCamel(str: string): string {
452-
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
453-
}
454-
455-
private convertKeysToCamelCase(obj: any): any {
456-
if (obj === null || typeof obj !== 'object') {
457-
return obj;
458-
}
459-
460-
if (Array.isArray(obj)) {
461-
return obj.map(item => this.convertKeysToCamelCase(item));
462-
}
463-
464-
const converted: any = {};
465-
for (const [key, value] of Object.entries(obj)) {
466-
const camelKey = this.snakeToCamel(key);
467-
converted[camelKey] = this.convertKeysToCamelCase(value);
468-
}
469-
return converted;
470-
}
471-
472393
// Common decode methods that work across all versions
473394
decodeAbciInfo<T extends AbciInfoResponse = AbciInfoResponse>(response: unknown): T {
474395
const resp = response as Record<string, unknown>;

networks/cosmos/src/auth/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { COSMOS_ADDRESS_STRATEGY } from './strategy';
21
export { createCosmosConfig } from './config';

networks/cosmos/src/auth/strategy.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

networks/cosmos/src/types/codec/converters.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,20 @@
22
* Common converter functions for API response transformation
33
*/
44

5-
import { fromBase64, fromHex } from '@interchainjs/encoding';
5+
import { fromBase64, fromHex, apiToNumber as encApiToNumber, apiToBigInt as encApiToBigInt } from '@interchainjs/encoding';
66

7-
/**
8-
* Convert API value to number (handles string numbers)
9-
*/
10-
export const apiToNumber = (value: unknown): number | undefined => {
11-
if (value === null || value === undefined) return undefined;
12-
if (typeof value === 'number') return value;
13-
if (typeof value === 'string') {
14-
const num = parseInt(value, 10);
15-
return isNaN(num) ? undefined : num;
16-
}
17-
return undefined;
7+
// Create wrapper functions that match the expected signatures for backward compatibility
8+
export const apiToNumber = (value: unknown): number => {
9+
return encApiToNumber(value as string | number | undefined | null);
1810
};
1911

20-
/**
21-
* Convert API value to bigint (handles string numbers)
22-
*/
2312
export const apiToBigInt = (value: unknown): bigint | undefined => {
2413
if (value === null || value === undefined) return undefined;
25-
if (typeof value === 'bigint') return value;
26-
if (typeof value === 'string' || typeof value === 'number') {
27-
try {
28-
return BigInt(value);
29-
} catch {
30-
return undefined;
31-
}
14+
try {
15+
return encApiToBigInt(value as string | number | undefined | null);
16+
} catch {
17+
return undefined;
3218
}
33-
return undefined;
3419
};
3520

3621
/**

networks/cosmos/src/utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { Bip39, Random } from '@interchainjs/crypto';
1+
import { generateMnemonic } from '@interchainjs/crypto';
22
import { AminoConverter, Encoder } from './types/signing-client';
33
import { TelescopeGeneratedCodec } from '@interchainjs/types';
44
import { assertEmpty } from '@interchainjs/utils';
55

6-
export function generateMnemonic(): string {
7-
return Bip39.encode(Random.getBytes(16)).toString();
8-
}
9-
106
/**
117
* from telescope generated codec to AminoConverter
128
*/

0 commit comments

Comments
 (0)