diff --git a/packages/engine/src/vaults/impl/ada/Vault.ts b/packages/engine/src/vaults/impl/ada/Vault.ts index 07b3de15d0b..3498dbc589e 100644 --- a/packages/engine/src/vaults/impl/ada/Vault.ts +++ b/packages/engine/src/vaults/impl/ada/Vault.ts @@ -7,12 +7,12 @@ import { isUndefined } from 'lodash'; import { TransactionStatus } from '@onekeyhq/engine/src/types/provider'; import type { PartialTokenInfo } from '@onekeyhq/engine/src/types/provider'; import { getTimeDurationMs } from '@onekeyhq/kit/src/utils/helper'; +import { COINTYPE_ADA } from '@onekeyhq/shared/src/engine/engineConsts'; import { - COINTYPE_ADA, - IMPL_ADA, -} from '@onekeyhq/shared/src/engine/engineConsts'; + isHdWallet, + isImportedWallet, +} from '@onekeyhq/shared/src/engine/engineUtils'; import debugLogger from '@onekeyhq/shared/src/logger/debugLogger'; -import platformEnv from '@onekeyhq/shared/src/platformEnv'; import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils'; import { @@ -22,10 +22,7 @@ import { OneKeyInternalError, PreviousAccountIsEmpty, } from '../../../errors'; -import { - getDefaultPurpose, - getLastAccountId, -} from '../../../managers/derivation'; +import { getLastAccountId } from '../../../managers/derivation'; import { getAccountNameInfoByTemplate } from '../../../managers/impl'; import { AccountType } from '../../../types/account'; import { @@ -39,6 +36,7 @@ import { validBootstrapAddress, validShelleyAddress } from './helper/addresses'; import { decodePrivateKeyByXprv, generateExportedCredential, + generateExportedCredentialForImportedAccount, } from './helper/bip32'; import { getChangeAddress } from './helper/cardanoUtils'; import ClientAda from './helper/ClientAda'; @@ -52,6 +50,7 @@ import settings from './settings'; import type { ExportedSeedCredential } from '../../../dbs/base'; import type { Account, DBUTXOAccount } from '../../../types/account'; import type { Token } from '../../../types/token'; +import type { KeyringSoftwareBase } from '../../keyring/KeyringSoftwareBase'; import type { IApproveInfo, IDecodedTx, @@ -224,13 +223,24 @@ export default class Vault extends VaultBase { } override async getExportedCredential(password: string): Promise { - const { entropy } = (await this.engine.dbApi.getCredential( - this.walletId, - password, - )) as ExportedSeedCredential; - - const dbAccount = (await this.getDbAccount()) as DBUTXOAccount; - return generateExportedCredential(password, entropy, dbAccount.path); + if (isHdWallet({ walletId: this.walletId })) { + const { entropy } = (await this.engine.dbApi.getCredential( + this.walletId, + password, + )) as ExportedSeedCredential; + const dbAccount = (await this.getDbAccount()) as DBUTXOAccount; + return generateExportedCredential(password, entropy, dbAccount.path); + } + if (isImportedWallet({ walletId: this.walletId })) { + const account = await this.getDbAccount(); + const keyring = this.keyring as KeyringSoftwareBase; + const privateKeys = await keyring.getPrivateKeys(password); + return generateExportedCredentialForImportedAccount( + password, + privateKeys[account.path], + ); + } + throw new Error('Not implemented'); } override attachFeeInfoToEncodedTx(params: { diff --git a/packages/engine/src/vaults/impl/ada/helper/bip32.ts b/packages/engine/src/vaults/impl/ada/helper/bip32.ts index 49809261ead..8e4539920bb 100644 --- a/packages/engine/src/vaults/impl/ada/helper/bip32.ts +++ b/packages/engine/src/vaults/impl/ada/helper/bip32.ts @@ -7,6 +7,8 @@ import { bech32, mnemonicToRootKeypair, toPublic } from 'cardano-crypto.js'; import { mnemonicFromEntropy } from '@onekeyhq/engine/src/secret'; +import { decrypt } from '../../../../secret/encryptors/aes256'; + import { DERIVATION_SCHEME, HARDENED_THRESHOLD } from './constants'; import type { BIP32Path } from '../types'; @@ -75,6 +77,24 @@ export async function generateExportedCredential( return xprv; } +export async function generateExportedCredentialForImportedAccount( + password: string, + encodedPrivateKey: Buffer, +) { + const privateKey = decrypt(password, encodedPrivateKey); + // eslint-disable-next-line @typescript-eslint/no-use-before-define + const { rootKey, index } = encodePrivateKey(privateKey); + const xprv = bech32.encode( + 'xprv', + Buffer.concat([ + rootKey.slice(0, 64), + rootKey.slice(96, 128), + Buffer.from(index, 'utf8'), + ]), + ) as string; + return Promise.resolve(xprv); +} + /* * @param xprv xprv string, 165 length, generate from OneKey wallet */ diff --git a/packages/shared/src/engine/engineUtils.ts b/packages/shared/src/engine/engineUtils.ts index 786c40e00e3..7b1d6c423c2 100644 --- a/packages/shared/src/engine/engineUtils.ts +++ b/packages/shared/src/engine/engineUtils.ts @@ -6,6 +6,7 @@ import { WALLET_TYPE_EXTERNAL, WALLET_TYPE_HD, WALLET_TYPE_HW, + WALLET_TYPE_IMPORTED, WALLET_TYPE_WATCHING, } from '@onekeyhq/engine/src/types/wallet'; import type { Wallet } from '@onekeyhq/engine/src/types/wallet'; @@ -45,6 +46,11 @@ export function isHardwareWallet({ walletId }: { walletId: string }) { return !!(walletId && walletId.startsWith(`${WALLET_TYPE_HW}-`)); } +// walletIsImported +export function isImportedWallet({ walletId }: { walletId: string }) { + return !!(walletId && walletId.startsWith(`${WALLET_TYPE_IMPORTED}`)); +} + export function isExternalWallet({ walletId }: { walletId: string }) { return !!(walletId && walletId === WALLET_TYPE_EXTERNAL); }