Skip to content

Commit af2951f

Browse files
committed
fix: not being able to conver bech32 to b256
1 parent 3a5029f commit af2951f

File tree

9 files changed

+111
-86
lines changed

9 files changed

+111
-86
lines changed

packages/app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@storybook/addon-viewport": "7.4.6",
3636
"@storybook/jest": "0.2.3",
3737
"@xstate/react": "3.2.2",
38+
"bech32": "2.0.0",
3839
"compare-versions": "6.1.0",
3940
"cross-fetch": "4.0.0",
4041
"dayjs": "1.11.10",

packages/app/src/systems/Account/components/FuelAddress/FuelAddress.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
isB256,
1010
} from 'fuels';
1111
import { useMemo } from 'react';
12+
import { shortAddress } from '~/systems/Core';
1213
import {
1314
safeConvertToB256,
1415
safeDynamicAddress,
15-
shortAddress,
16-
} from '~/systems/Core';
16+
} from '~/systems/Core/utils/address';
1717
import { useExplorerLink } from '../../hooks/useExplorerLink';
1818

1919
export type AddressProps = {

packages/app/src/systems/Asset/machines/assetsMachine.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ export const assetsMachine = createMachine(
185185
services: {
186186
setListedAssets: FetchMachine.create<null, void>({
187187
showError: true,
188-
async fetch() {
189-
await AssetService.setListedAssets();
188+
fetch: async (_input, abortController) => {
189+
await AssetService.setListedAssets(abortController);
190190
},
191191
}),
192192
fetchAssets: FetchMachine.create<

packages/app/src/systems/CRX/background/services/BackgroundService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { ConnectionService } from '~/systems/DApp/services';
2020
import { NetworkService } from '~/systems/Network/services';
2121
import { AbiService } from '~/systems/Settings/services';
2222

23-
import { safeDynamicAddress } from '~/systems/Core';
23+
import { safeDynamicAddress } from '~/systems/Core/utils/address';
2424
import type { CommunicationProtocol } from './CommunicationProtocol';
2525
import { PopUpService } from './PopUpService';
2626
import type { MessageInputs } from './types';

packages/app/src/systems/Core/utils/address.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { bech32 } from 'bech32';
12
import { Address, isB256 } from 'fuels';
23

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

17+
export function convertBech32ToB256(address: string): string {
18+
try {
19+
const decoded = bech32.decode(address);
20+
const bytes = bech32.fromWords(decoded.words);
21+
return `0x${Buffer.from(bytes).toString('hex')}`;
22+
} catch (error) {
23+
console.error('Invalid Bech32 address:', error);
24+
return address;
25+
}
26+
}
27+
28+
export function isBech32(address: string): boolean {
29+
try {
30+
bech32.decode(address);
31+
return true;
32+
} catch {
33+
return false;
34+
}
35+
}
36+
1637
export function safeConvertToB256(address: string) {
1738
try {
1839
if (isB256(address)) return address;
40+
if (isBech32(address)) return convertBech32ToB256(address);
1941
return Address.fromDynamicInput(address).toB256();
2042
} catch (error) {
2143
console.error(error);

packages/app/src/systems/FundWallet/hooks/useFundWallet.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IS_CRX } from '~/config';
44
import { DEFAULT_NETWORKS } from '~/networks';
55
import { useAccounts } from '~/systems/Account';
66
import { openTab } from '~/systems/CRX/utils';
7-
import { safeConvertToB256 } from '~/systems/Core';
7+
import { safeConvertToB256 } from '~/systems/Core/utils/address';
88
import { useNetworks } from '~/systems/Network';
99

1010
export function useFundWallet() {

packages/app/src/systems/Home/pages/Receive/Receive.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
safeConvertToB256,
1010
} from '~/systems/Core';
1111

12-
import { Address } from 'fuels';
1312
import { ReceiverQRCode } from '../../components/QRCode';
1413
import { UserAddressCard } from '../../components/UserAddressCard';
1514

packages/app/src/systems/Transaction/components/ActivityItem/ActivityItem.tsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { Box, Card, Copyable, Icon, Text } from '@fuel-ui/react';
33
import { Address, type TransactionSummary } from 'fuels';
44
import { type FC, useMemo } from 'react';
55
import { useNavigate } from 'react-router-dom';
6-
import {
7-
Pages,
8-
safeConvertToB256,
9-
safeDynamicAddress,
10-
shortAddress,
11-
} from '~/systems/Core';
6+
import { Pages, safeConvertToB256, shortAddress } from '~/systems/Core';
127

138
import { useTxMetadata } from '../../hooks/useTxMetadata';
149
import { TxIcon } from '../TxIcon';

pnpm-lock.yaml

+81-73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)