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 af2951f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 86 deletions.
1 change: 1 addition & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@storybook/addon-viewport": "7.4.6",
"@storybook/jest": "0.2.3",
"@xstate/react": "3.2.2",
"bech32": "2.0.0",
"compare-versions": "6.1.0",
"cross-fetch": "4.0.0",
"dayjs": "1.11.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
isB256,
} from 'fuels';
import { useMemo } from 'react';
import { shortAddress } from '~/systems/Core';
import {
safeConvertToB256,
safeDynamicAddress,
shortAddress,
} from '~/systems/Core';
} from '~/systems/Core/utils/address';
import { useExplorerLink } from '../../hooks/useExplorerLink';

export type AddressProps = {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/systems/Asset/machines/assetsMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ export const assetsMachine = createMachine(
services: {
setListedAssets: FetchMachine.create<null, void>({
showError: true,
async fetch() {
await AssetService.setListedAssets();
fetch: async (_input, abortController) => {
await AssetService.setListedAssets(abortController);
},
}),
fetchAssets: FetchMachine.create<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ConnectionService } from '~/systems/DApp/services';
import { NetworkService } from '~/systems/Network/services';
import { AbiService } from '~/systems/Settings/services';

import { safeDynamicAddress } from '~/systems/Core';
import { safeDynamicAddress } from '~/systems/Core/utils/address';
import type { CommunicationProtocol } from './CommunicationProtocol';
import { PopUpService } from './PopUpService';
import type { MessageInputs } from './types';
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IS_CRX } from '~/config';
import { DEFAULT_NETWORKS } from '~/networks';
import { useAccounts } from '~/systems/Account';
import { openTab } from '~/systems/CRX/utils';
import { safeConvertToB256 } from '~/systems/Core';
import { safeConvertToB256 } from '~/systems/Core/utils/address';
import { useNetworks } from '~/systems/Network';

export function useFundWallet() {
Expand Down
1 change: 0 additions & 1 deletion packages/app/src/systems/Home/pages/Receive/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
safeConvertToB256,
} from '~/systems/Core';

import { Address } from 'fuels';
import { ReceiverQRCode } from '../../components/QRCode';
import { UserAddressCard } from '../../components/UserAddressCard';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { Box, Card, Copyable, Icon, Text } from '@fuel-ui/react';
import { Address, type TransactionSummary } from 'fuels';
import { type FC, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import {
Pages,
safeConvertToB256,
safeDynamicAddress,
shortAddress,
} from '~/systems/Core';
import { Pages, safeConvertToB256, shortAddress } from '~/systems/Core';

import { useTxMetadata } from '../../hooks/useTxMetadata';
import { TxIcon } from '../TxIcon';
Expand Down
154 changes: 81 additions & 73 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit af2951f

Please sign in to comment.