Skip to content

Commit

Permalink
Merge branch 'onekey' into OK-23670
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoktung authored Oct 16, 2023
2 parents d2edb56 + ccfe635 commit dad633b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/engine/src/vaults/impl/ada/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -224,13 +223,24 @@ export default class Vault extends VaultBase {
}

override async getExportedCredential(password: string): Promise<string> {
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: {
Expand Down
20 changes: 20 additions & 0 deletions packages/engine/src/vaults/impl/ada/helper/bip32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/engine/engineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit dad633b

Please sign in to comment.