Skip to content

Commit

Permalink
Fixed import private key hex
Browse files Browse the repository at this point in the history
  • Loading branch information
BlobMaster41 committed Oct 22, 2024
1 parent 4a9143c commit 00b3a55
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 70 deletions.
21 changes: 19 additions & 2 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import eventBus from '@/shared/eventBus';
import { SessionEvent } from '@/shared/interfaces/SessionEvent';
import {
Account,
AddressSummary,
AddressType,
AddressUserToSignInput,
BitcoinBalance,
Expand Down Expand Up @@ -182,8 +183,24 @@ export class WalletController extends BaseController {
return data;
};

getMultiAddressAssets = async (addresses: string) => {
return openapiService.getMultiAddressAssets(addresses);
getMultiAddressAssets = async (addresses: string): Promise<AddressSummary[]> => {
const network = this.getChainType();
Web3API.setNetwork(network);

const addressList = addresses.split(',');
const summaries: AddressSummary[] = [];
for (const address of addressList) {
const balance = await Web3API.getBalance(address, true);
const summary: AddressSummary = {
address: address,
totalSatoshis: Number(balance),
loading: false
};

summaries.push(summary);
}

return summaries;
};

findGroupAssets = (groups: { type: number; address_arr: string[]; pubkey_arr: string[] }[]) => {
Expand Down
6 changes: 0 additions & 6 deletions src/background/service/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ export class OpenApiService {
});
}

async getMultiAddressAssets(addresses: string): Promise<AddressSummary[]> {
return this.httpGet('/v5/address/multi-assets', {
addresses
});
}

async findGroupAssets(
groups: { type: number; address_arr: string[] }[]
): Promise<{ type: number; address_arr: string[]; satoshis_arr: number[] }[]> {
Expand Down
5 changes: 1 addition & 4 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export interface BitcoinBalance {

export interface AddressAssets {
total_btc: string;
satoshis?: number;
total_inscription: number;
satoshis: number;
}

export interface TxHistoryInOutItem {
Expand Down Expand Up @@ -265,8 +264,6 @@ export enum WebsiteState {
export interface AddressSummary {
address: string;
totalSatoshis: number;
btcSatoshis: number;
assetSatoshis: number;
loading?: boolean;
}

Expand Down
5 changes: 0 additions & 5 deletions src/ui/components/AddressTypeCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ export function AddressTypeCard(props: AddressTypeCardProps) {
<Icon icon="btc" size={fontSizes.iconMiddle} />
<Text text={`${assets.total_btc} ${btcUnit}`} color="yellow" />
</Row>
<Row>
{assets.total_inscription > 0 && (
<Text text={`${assets.total_inscription} INSCRIPTIONS`} color="gold" preset="bold" />
)}
</Row>
</Row>
)}
</Column>
Expand Down
44 changes: 20 additions & 24 deletions src/ui/pages/Account/CreateSimpleWalletScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ECPairFactory } from 'ecpair';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { ADDRESS_TYPES } from '@/shared/constant';
import { AddressType } from '@/shared/types';
import { AddressAssets, AddressType } from '@/shared/types';
import { getBitcoinLibJSNetwork } from '@/shared/web3/Web3API';
import { Button, Column, Content, Header, Input, Layout, Row, Text } from '@/ui/components';
import { useTools } from '@/ui/components/ActionComponent';
Expand Down Expand Up @@ -132,17 +132,7 @@ function Step2({
}, [contextData]);

const [previewAddresses, setPreviewAddresses] = useState<string[]>(hdPathOptions.map((v) => ''));

const [addressAssets, setAddressAssets] = useState<
Record<
string,
{
total_btc: string;
satoshis: number;
total_inscription: number;
}
>
>({});
const [addressAssets, setAddressAssets] = useState<Record<string, AddressAssets>>({});

const selfRef = useRef({
maxSatoshis: 0,
Expand Down Expand Up @@ -170,13 +160,11 @@ function Step2({
} else if (options.addressType == AddressType.P2WPKH) {
addresses.push(address.p2wpkh);
} else {
try {
addresses.push(
EcKeyPair.getLegacyAddress(ECPair.fromWIF(contextData.wif, bitcoinNetwork), bitcoinNetwork)
);
} catch {}
addresses.push(
EcKeyPair.getLegacyAddress(ECPair.fromWIF(contextData.wif, bitcoinNetwork), bitcoinNetwork)
);
}
} catch {}
} catch (e) {}

try {
const bufferPrivateKey = Buffer.from(contextData.wif.replace('0x', ''), 'hex');
Expand All @@ -191,7 +179,7 @@ function Step2({
} else {
addresses.push(EcKeyPair.getLegacyAddress(keypair, bitcoinNetwork));
}
} catch {}
} catch (e) {}
}

const balances = await wallet.getMultiAddressAssets(addresses.join(','));
Expand All @@ -204,16 +192,24 @@ function Step2({
total_btc: satoshisToAmount(balance.totalSatoshis),
satoshis
};

if (satoshis > self.maxSatoshis) {
self.maxSatoshis = satoshis;
self.recommended = i;
}
}

updateContextData({ addressType: hdPathOptions[self.recommended].addressType });
setAddressAssets(self.addressBalances);
let recommended: AddressType = hdPathOptions[self.recommended].addressType;
if (self.maxSatoshis == 0) {
recommended = AddressType.P2TR;
}

updateContextData({ addressType: recommended });

setAddressAssets(self.addressBalances);
setPreviewAddresses(addresses);
};

useEffect(() => {
void run();
}, [contextData.wif]);
Expand All @@ -229,7 +225,7 @@ function Step2({
await wallet.createKeyringWithPrivateKey(contextData.wif, contextData.addressType);
navigate(RouteTypes.MainScreen);
} catch (e) {
tools.toastError((e as any).message);
tools.toastError((e as Error).message);
}
};
return (
Expand All @@ -240,9 +236,9 @@ function Step2({
const address = previewAddresses[index];
const assets = addressAssets[address] || {
total_btc: '--',
satoshis: 0,
total_inscription: 0
satoshis: 0
};

const hasVault = assets.satoshis > 0;
if (item.isUnisatLegacy && !hasVault) {
return null;
Expand Down
43 changes: 18 additions & 25 deletions src/ui/pages/Settings/AddressTypeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEffect, useMemo, useState } from 'react';

import { ADDRESS_TYPES, KEYRING_TYPE } from '@/shared/constant';
import { AddressAssets } from '@/shared/types';
import { Column, Content, Header, Layout } from '@/ui/components';
import { useTools } from '@/ui/components/ActionComponent';
import { AddressTypeCard } from '@/ui/components/AddressTypeCard';
import { useCurrentAccount, useReloadAccounts } from '@/ui/state/accounts/hooks';
import { useCurrentKeyring } from '@/ui/state/keyrings/hooks';
import { useWallet } from '@/ui/utils';
import { satoshisToAmount, useWallet } from '@/ui/utils';

import { RouteTypes, useNavigate } from '../MainRoute';

Expand All @@ -17,39 +18,31 @@ export default function AddressTypeScreen() {
const navigate = useNavigate();
const reloadAccounts = useReloadAccounts();
const [addresses, setAddresses] = useState<string[]>([]);
const [addressAssets, setAddressAssets] = useState<
Record<
string,
{
total_btc: string;
satoshis: number;
total_inscription: number;
}
>
>({});
const [addressAssets, setAddressAssets] = useState<Record<string, AddressAssets>>({});

const currentKeyring = useCurrentKeyring();

const tools = useTools();
const loadAddresses = async () => {
tools.showLoading(true);

const _res = await wallet.getAllAddresses(currentKeyring, account.index || 0);
const _res = await wallet.getAllAddresses(currentKeyring, account.index ?? 0);
setAddresses(_res);

// TODO: Fix this error (Error: address invalid)
// const balances = await wallet.getMultiAddressAssets(_res.join(','));
// for (let i = 0; i < _res.length; i++) {
// const address = _res[i];
// const balance = balances[i];
// const satoshis = balance.totalSatoshis;
// self.addressAssets[address] = {
// total_btc: satoshisToAmount(balance.totalSatoshis),
// satoshis,
// total_inscription: balance.inscriptionCount
// };
// }
// setAddressAssets(self.addressAssets);
const balances = await wallet.getMultiAddressAssets(_res.join(','));
const addressAssets: Record<string, AddressAssets> = {};

for (let i = 0; i < _res.length; i++) {
const address = _res[i];
const balance = balances[i];
const satoshis = balance.totalSatoshis;
addressAssets[address] = {
total_btc: satoshisToAmount(balance.totalSatoshis),
satoshis
};
}

setAddressAssets(addressAssets);

tools.showLoading(false);
};
Expand Down
4 changes: 0 additions & 4 deletions src/ui/state/accounts/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export const initialState: AccountsState = {
addressSummary: {
address: '',
totalSatoshis: 0,
btcSatoshis: 0,
assetSatoshis: 0,
loading: true
}
};
Expand Down Expand Up @@ -204,8 +202,6 @@ const slice = createSlice({
if (!state.addressSummary) {
state.addressSummary = {
totalSatoshis: 0,
btcSatoshis: 0,
assetSatoshis: 0,
address: ''
};
}
Expand Down

0 comments on commit 00b3a55

Please sign in to comment.