Skip to content

Commit

Permalink
feat: BTC External account
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Apr 23, 2024
1 parent 228bdeb commit 5f42550
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { useCallback } from 'react';

import { cloneDeep, isString } from 'lodash';
Expand All @@ -7,12 +9,17 @@ import {
ETHMessageTypes,
getEthProviderMethodFromMessageType,
} from '@onekeyhq/engine/src/types/message';
import type { IUnsignedMessageBtc } from '@onekeyhq/engine/src/vaults/impl/btc/types';
import type {
IEncodedTxBtc,
IUnsignedMessageBtc,
} from '@onekeyhq/engine/src/vaults/impl/btc/types';
import type {
IEncodedTxEvm,
IUnsignedMessageEvm,
} from '@onekeyhq/engine/src/vaults/impl/evm/Vault';
import type { IEncodedTx } from '@onekeyhq/engine/src/vaults/types';
import { OnekeyNetwork } from '@onekeyhq/shared/src/config/networkIds';
import { isBTCNetwork } from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
import type { IDappSourceInfo } from '@onekeyhq/shared/types';

Expand Down Expand Up @@ -156,3 +163,51 @@ export function useSignOrSendOfExternalAccount({
signMsgForExternalAccount,
};
}

export function useSignOrSendOfBtcExternalAccount({
encodedTx,
networkId,
}: {
encodedTx: IEncodedTx | undefined;
sourceInfo?: IDappSourceInfo | undefined;
networkId: string;
accountId: string;
signOnly: boolean;
}) {
const intl = useIntl();
const { engine } = backgroundApiProxy;

const sendTxForBtcExternalAccount = useCallback(async () => {
const currentNetwork = await engine.getNetwork(networkId);
if (!isBTCNetwork(currentNetwork.id)) {
throw new Error('Network is not BTC');
}
const tx = encodedTx as IEncodedTxBtc;
if (!tx) {
throw new Error('encodedTx is missing!');
}
const result =
await window.$onekey.$privateExternalAccount?.btc_signTransaction({
encodedTx: {
inputs: tx.inputs,
outputs: tx.outputs,
// @ts-expect-error
inputsForCoinSelect: tx.inputsForCoinSelect,
// @ts-expect-error
outputsForCoinSelect: tx.outputsForCoinSelect,
fee: tx.totalFee,
},
network:
currentNetwork.id === OnekeyNetwork.btc ? 'mainnet' : 'testnet',
});
return {
txid: result.txid,
rawTx: result.rawTx,
encodedTx,
};
}, [encodedTx, networkId, engine]);

return {
sendTxForBtcExternalAccount,
};
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { useCallback } from 'react';

import { useIntl } from 'react-intl';

import { Box, ToastManager } from '@onekeyhq/components';
import LogoOneKey from '@onekeyhq/kit/assets/logo_black.png';
import {
COINTYPE_BTC,
INDEX_PLACEHOLDER,
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import backgroundApiProxy from '../../../../background/instance/backgroundApiProxy';
Expand All @@ -20,44 +18,45 @@ const BTCExternalWallet = () => {
const intl = useIntl();
const onboardingDone = useOnboardingDone();

const addBtcExternalAccount = useCallback(async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!window.$onekey?.$privateExternalAccount) {
console.log('OneKey Provider Not Found.: ', window);
ToastManager.show(
{
title: 'OneKey Provider Not Found.',
},
{
type: 'error',
},
const addBtcExternalAccount = useCallback(
async (network: 'mainnet' | 'testnet') => {
if (!window.$onekey?.$privateExternalAccount) {
console.log('OneKey Provider Not Found.: ', window);
ToastManager.show(
{
title: 'OneKey Provider Not Found.',
},
{
type: 'error',
},
);
return;
}
const result =
await window.$onekey.$privateExternalAccount?.btc_requestAccount(
network,
);

debugLogger.walletConnect.info(
'OneKey injected account will create: ',
result,
);
return;
}
console.log('OneKey');
const result =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await window.$onekey.$privateExternalAccount?.btc_requestAccount(
'mainnet',
const addedResult =
await backgroundApiProxy.serviceAccount.addBtcExternalAccount({
externalAccount: result,
});
const accountId = addedResult.account.id;
await backgroundApiProxy.serviceAccount.changeActiveExternalWalletName(
accountId,
);
debugLogger.walletConnect.info(
'OneKey injected account will create: ',
result,
);
const addedResult =
await backgroundApiProxy.serviceAccount.addBtcExternalAccount({
externalAccount: result,
await onboardingDone();
await wait(600);
ToastManager.show({
title: intl.formatMessage({ id: 'msg__account_imported' }),
});
const accountId = addedResult.account.id;
await backgroundApiProxy.serviceAccount.changeActiveExternalWalletName(
accountId,
);
await onboardingDone();
await wait(600);
ToastManager.show({
title: intl.formatMessage({ id: 'msg__account_imported' }),
});
}, [intl, onboardingDone]);
},
[intl, onboardingDone],
);

return (
<Layout title={intl.formatMessage({ id: 'title__connect_with' })}>
Expand All @@ -68,7 +67,14 @@ const BTCExternalWallet = () => {
label="OneKey Injected"
logoSource={LogoOneKey}
isLoading={false}
onPress={addBtcExternalAccount}
onPress={() => addBtcExternalAccount('mainnet')}
/>
<ConnectWalletListItem
available
label="OneKey Injected Testnet"
logoSource={LogoOneKey}
isLoading={false}
onPress={() => addBtcExternalAccount('testnet')}
/>
</Box>
</Layout>
Expand Down
18 changes: 17 additions & 1 deletion packages/kit/src/views/Send/modals/SendAuthentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
IEncodedTx,
ISignedTxPro,
} from '@onekeyhq/engine/src/vaults/types';
import { isBTCNetwork } from '@onekeyhq/shared/src/engine/engineConsts';
import { isExternalAccount } from '@onekeyhq/shared/src/engine/engineUtils';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

Expand All @@ -21,7 +22,10 @@ import { closeExtensionWindowIfOnboardingFinished } from '../../../hooks/useOnbo
import { deviceUtils } from '../../../utils/hardware';
import { wait } from '../../../utils/helper';
import { AuthExternalAccountInfo } from '../../ExternalAccount/SendConfirm/AuthExternalAccountInfo';
import { useSignOrSendOfExternalAccount } from '../../ExternalAccount/SendConfirm/useSignOrSendOfExternalAccount';
import {
useSignOrSendOfBtcExternalAccount,
useSignOrSendOfExternalAccount,
} from '../../ExternalAccount/SendConfirm/useSignOrSendOfExternalAccount';
import { BaseSendModal } from '../components/BaseSendModal';
import { DecodeTxButtonTest } from '../components/DecodeTxButtonTest';

Expand Down Expand Up @@ -96,8 +100,19 @@ const SendAuth: FC<EnableLocalAuthenticationProps> = ({
signOnly,
});

const { sendTxForBtcExternalAccount } = useSignOrSendOfBtcExternalAccount({
encodedTx,
sourceInfo,
networkId,
accountId,
signOnly,
});

const sendTx = useCallback(async (): Promise<ISignedTxPro | undefined> => {
if (isExternal) {
if (isBTCNetwork(networkId)) {
return sendTxForBtcExternalAccount();
}
return sendTxForExternalAccount();
}

Expand All @@ -124,6 +139,7 @@ const SendAuth: FC<EnableLocalAuthenticationProps> = ({
accountId,
encodedTx,
sendTxForExternalAccount,
sendTxForBtcExternalAccount,
signOnly,
]);

Expand Down

0 comments on commit 5f42550

Please sign in to comment.