Skip to content

Commit 71c80b9

Browse files
committed
fix: not being able to conver bech32 to b256
1 parent bd3626f commit 71c80b9

File tree

15 files changed

+143
-112
lines changed

15 files changed

+143
-112
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
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/playwright/e2e/Accounts.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,15 @@ test.describe('Existing Accounts', () => {
183183

184184
test('can add accounts using correct derivation path after importing from private key', async () => {
185185
// at this point 2 accounts have already been created
186-
const fuelAddress1 =
187-
'fuel1kfnz04g7k8wjw22s03s3kk46wxr63he3v5v6kyrv76m7wzh7x9jqvqffua';
186+
const fuelAddress1 = 'fuelsequencer1mt7k6ynlayacjwpqt0lwdn7ksv72ek2lwt95nu';
188187
const fuelAddress2 =
189-
'fuel1kyxzyv5z39fuxnr6k9ncxujxn4y07fu6pf73vslmemgpex325vrsytpqks';
188+
'fuelsequencervaloper163rsv65t4893t2rz5rmda9sly7lgdlq2s2kqtn';
190189
const fuelAddress3 =
191-
'fuel152720qgc5wthxu4g7a2g6s7xy9d8wjgtffl489k706xyd2fas0wqyv0vsw';
190+
'fuelsequencervaloper1vtfzrk6f4m6kxt6ehyqt9j5su5hvcz5qn0wwpn';
192191
const fuelPrivKey =
193192
'0x7f802a2a277872af1204140bd2c77c2193309c366e3c71ff1c4c31cea0a53f38';
194193
const fuelAddPriv =
195-
'fuel1szu0uagadwpgl0fuz2thrtzn7artghvhexg5d9at4t76nzeesqasrdmjxy';
194+
'fuelsequencervaloper163rsv65t4893t2rz5rmda9sly7lgdlq2s2kqtn';
196195

197196
// import account from private key
198197
await createAccountFromPrivateKey(page, fuelPrivKey, 'Account 3');

packages/app/src/systems/Account/__mocks__/accounts.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const MOCK_ACCOUNTS = [
2727
},
2828
{
2929
name: 'Account 4',
30-
address: 'fuel10va6297tkerdcn5u8mxjm9emudsmkj85pq5x7t7stkmzmc4nvs3qvn99qz',
30+
address: 'fuelsequencer1mt7k6ynlayacjwpqt0lwdn7ksv72ek2lwt95nu',
3131
publicKey: '0x00',
3232
},
3333
];

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ import { TestWrapper } from '~/systems/Core';
33
import { renderWithProvider } from '~/systems/Core/__tests__/utils';
44
import { FuelAddress } from './FuelAddress';
55

6+
const accountAddressHex = '0xdafd6d127fe93b8938205bfee6cfd6833cacd95f';
67
const accountAddressB256 =
7-
'0x2230bd556418ddc58b48065208b3958a8db247f11394023e17ff143db7235c6c';
8+
'0x00000000000000000000000cfe7b143aca69a0984b481ab950716047586772586475aee82fb10b719d52a76';
9+
const shorten = (address: string) =>
10+
`0x${address.slice(2, 6)}...${address.slice(-4)}`;
811
const accountAddressBech32 =
9-
'fuel1ygct64tyrrwutz6gqefq3vu432xmy3l3zw2qy0shlu2rmdert3kqx97pfj';
12+
'fuelsequencer1mt7k6ynlayacjwpqt0lwdn7ksv72ek2lwt95nu';
1013

1114
describe('FuelAddress', () => {
1215
it('a11y', async () => {
13-
await testA11y(<FuelAddress address={accountAddressB256} />, {
16+
await testA11y(<FuelAddress address={accountAddressHex} />, {
1417
wrapper: TestWrapper,
1518
});
1619
});
1720

1821
it('should show b256 address from b256', () => {
1922
renderWithProvider(<FuelAddress address={accountAddressB256} />);
20-
expect(screen.getByText('0x2230...5C6C')).toBeInTheDocument();
23+
expect(screen.getByText(shorten(accountAddressB256))).toBeInTheDocument();
24+
});
25+
it('should show b256 address from eth', () => {
26+
renderWithProvider(<FuelAddress address={accountAddressHex} />);
27+
expect(screen.getByText(shorten(accountAddressB256))).toBeInTheDocument();
2128
});
2229

2330
it('should show b256 address from bech32', () => {
2431
renderWithProvider(<FuelAddress address={accountAddressBech32} />);
25-
expect(screen.getByText('0x2230...5C6C')).toBeInTheDocument();
32+
expect(screen.getByText(accountAddressB256)).toBeInTheDocument();
2633
});
2734
});

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
isB256,
1010
} from 'fuels';
1111
import { useMemo } from 'react';
12+
import { shortAddress } from '~/systems/Core';
1213
import {
14+
convertBech32ToB256,
15+
isBech32,
16+
isValidEthAddress,
1317
safeConvertToB256,
1418
safeDynamicAddress,
15-
shortAddress,
16-
} from '~/systems/Core';
19+
} from '~/systems/Core/utils/address';
1720
import { useExplorerLink } from '../../hooks/useExplorerLink';
1821

1922
export type AddressProps = {
@@ -32,7 +35,10 @@ export const FuelAddress = ({
3235
const account = useMemo<string>(() => {
3336
if (!address) return '';
3437
if (isContract) return safeConvertToB256(address);
35-
return safeDynamicAddress(address).toString();
38+
if (isBech32(address)) return convertBech32ToB256(address);
39+
return isValidEthAddress(address)
40+
? address
41+
: safeDynamicAddress(address).toString();
3642
}, [isContract, address]);
3743

3844
const { openExplorer, href } = useExplorerLink(account);

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 25 additions & 7 deletions
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,21 +14,38 @@ export function isValidEthAddress(address = '') {
1314
return isPadded || isEthAddress;
1415
}
1516

17+
export function convertBech32ToB256(bech32Address: string): string {
18+
// Decode the Bech32 address
19+
const { words } = bech32.decode(bech32Address);
20+
21+
// Convert from 5-bit words to 8-bit bytes
22+
const bytes = bech32.fromWords(words);
23+
24+
// Convert the byte array to a hexadecimal string
25+
const ethAddress = `0x${Buffer.from(bytes).toString('hex')}`;
26+
27+
return ethAddress;
28+
}
29+
export function isBech32(address: string): boolean {
30+
try {
31+
bech32.decode(address);
32+
return true;
33+
} catch {
34+
return false;
35+
}
36+
}
37+
1638
export function safeConvertToB256(address: string) {
1739
try {
1840
if (isB256(address)) return address;
41+
if (isBech32(address)) return convertBech32ToB256(address);
1942
return Address.fromDynamicInput(address).toB256();
2043
} catch (error) {
21-
console.error(error);
44+
console.log(error);
2245
return address;
2346
}
2447
}
2548

2649
export function safeDynamicAddress(address: string) {
27-
try {
28-
return Address.fromDynamicInput(safeConvertToB256(address));
29-
} catch (error) {
30-
console.error(error);
31-
return address;
32-
}
50+
return Address.fromDynamicInput(safeConvertToB256(address));
3351
}

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

Lines changed: 1 addition & 1 deletion
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/components/QRCode/QRCode.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { TestWrapper } from '~/systems/Core/components/TestWrapper';
33

44
import { ReceiverQRCode } from './QRCode';
55

6-
const TEST_ACCOUNT =
7-
'fuel1auahknz6mjuu0am034mlggh55f0tgp9j7fkzrc6xl48zuy5zv7vqa07n30';
6+
const TEST_ACCOUNT = 'fuelsequencer1mt7k6ynlayacjwpqt0lwdn7ksv72ek2lwt95nu';
87

98
describe('QR Code Tests', () => {
109
it('should show the qr code on screen', async () => {

0 commit comments

Comments
 (0)