Skip to content

Commit

Permalink
fix: not being able to conver bech32 to b256
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron committed Jan 18, 2025
1 parent 3a5029f commit a7a3764
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/app/src/systems/Core/utils/address.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bech32 } from 'bech32';
import { Address, isB256 } from 'fuels';

export function shortAddress(address = '') {
Expand All @@ -13,9 +14,30 @@ export function isValidEthAddress(address = '') {
return isPadded || isEthAddress;
}

export function convertBech32ToB256(address: string): string {
try {
const decoded = bech32.decode(address);
const bytes = bech32.fromWords(decoded.words);
return `0x${Buffer.from(bytes).toString('hex')}`;
} catch (error) {
console.error('Invalid Bech32 address:', error);
return address;
}
}

export function isBech32(address: string): boolean {
try {
bech32.decode(address);
return true;
} catch {
return false;
}
}

export function safeConvertToB256(address: string) {
try {
if (isB256(address)) return address;
if (isBech32(address)) return convertBech32ToB256(address);
return Address.fromDynamicInput(address).toB256();
} catch (error) {
console.error(error);
Expand Down

0 comments on commit a7a3764

Please sign in to comment.