From 9a716daeee14e5b00d476ec759b304f788919b50 Mon Sep 17 00:00:00 2001 From: William Swanson Date: Fri, 6 Sep 2024 13:51:24 -0700 Subject: [PATCH] fixup! Factor out a decryptKeyInfos function --- src/core/login/keys.ts | 51 ++++++++++++++++++++++++++++++++++-- src/core/login/login.ts | 58 +++-------------------------------------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/core/login/keys.ts b/src/core/login/keys.ts index 9637b5cf..64989095 100644 --- a/src/core/login/keys.ts +++ b/src/core/login/keys.ts @@ -7,7 +7,7 @@ import { EdgeCurrencyWallet, EdgeWalletInfo } from '../../types/types' -import { encrypt } from '../../util/crypto/crypto' +import { decrypt, decryptText, encrypt } from '../../util/crypto/crypto' import { hmacSha256 } from '../../util/crypto/hashes' import { utf8 } from '../../util/encoding' import { changeWalletStates } from '../account/account-files' @@ -17,7 +17,14 @@ import { getCurrencyTools } from '../plugins/plugins-selectors' import { ApiInput } from '../root-pixie' -import { AppIdMap, LoginKit, LoginTree, wasEdgeWalletInfo } from './login-types' +import { LoginStash } from './login-stash' +import { + AppIdMap, + asEdgeWalletInfo, + LoginKit, + LoginTree, + wasEdgeWalletInfo +} from './login-types' import { asEdgeStorageKeys, createStorageKeys, @@ -133,6 +140,46 @@ export function mergeKeyInfos(keyInfos: EdgeWalletInfo[]): EdgeWalletInfo[] { return out } +/** + * Decrypts the private keys contained in a login. + */ +export function decryptKeyInfos( + stash: LoginStash, + loginKey: Uint8Array +): EdgeWalletInfo[] { + const { appId, keyBoxes = [] } = stash + + const legacyKeys: EdgeWalletInfo[] = [] + + // BitID wallet: + const { mnemonicBox, rootKeyBox } = stash + if (mnemonicBox != null && rootKeyBox != null) { + const rootKey = decrypt(rootKeyBox, loginKey) + const infoKey = hmacSha256(rootKey, utf8.parse('infoKey')) + const keys = { + mnemonic: decryptText(mnemonicBox, infoKey), + rootKey: base64.stringify(rootKey) + } + legacyKeys.push(makeKeyInfo('wallet:bitid', keys, rootKey)) + } + + // Account settings: + if (stash.syncKeyBox != null) { + const syncKey = decrypt(stash.syncKeyBox, loginKey) + const type = makeAccountType(appId) + const keys = wasEdgeStorageKeys({ dataKey: loginKey, syncKey }) + legacyKeys.push(makeKeyInfo(type, keys, loginKey)) + } + + // Keys: + const keyInfos = keyBoxes.map(box => + asEdgeWalletInfo(JSON.parse(decryptText(box, loginKey))) + ) + return mergeKeyInfos([...legacyKeys, ...keyInfos]).map(walletInfo => + fixWalletInfo(walletInfo) + ) +} + /** * Returns all the wallet infos accessible from this login object, * as well as a map showing which wallets are in which applications. diff --git a/src/core/login/login.ts b/src/core/login/login.ts index 2733d7d4..701e386f 100644 --- a/src/core/login/login.ts +++ b/src/core/login/login.ts @@ -7,30 +7,18 @@ import { base64 } from 'rfc4648' import { asLoginPayload } from '../../types/server-cleaners' import { LoginPayload, LoginRequestBody } from '../../types/server-types' -import { - asMaybeOtpError, - EdgeAccountOptions, - EdgeWalletInfo -} from '../../types/types' +import { asMaybeOtpError, EdgeAccountOptions } from '../../types/types' import { decrypt, decryptText } from '../../util/crypto/crypto' -import { hmacSha256 } from '../../util/crypto/hashes' import { verifyData } from '../../util/crypto/verify' -import { utf8 } from '../../util/encoding' import { softCat } from '../../util/util' import { ApiInput } from '../root-pixie' -import { - fixWalletInfo, - makeAccountType, - makeKeyInfo, - mergeKeyInfos -} from './keys' +import { decryptKeyInfos, mergeKeyInfos } from './keys' import { loginFetch } from './login-fetch' import { makeSecretKit } from './login-secret' import { getStashById } from './login-selectors' import { LoginStash, saveStash } from './login-stash' -import { asEdgeWalletInfo, LoginKit, LoginTree } from './login-types' +import { LoginKit, LoginTree } from './login-types' import { getLoginOtp, getStashOtp } from './otp' -import { wasEdgeStorageKeys } from './storage-keys' /** * Returns the login that satisfies the given predicate, @@ -335,46 +323,6 @@ export function sanitizeLoginStash( ) } -/** - * Decrypts the private keys contained in a login. - */ -export function decryptKeyInfos( - stash: LoginStash, - loginKey: Uint8Array -): EdgeWalletInfo[] { - const { appId, keyBoxes = [] } = stash - - const legacyKeys: EdgeWalletInfo[] = [] - - // BitID wallet: - const { mnemonicBox, rootKeyBox } = stash - if (mnemonicBox != null && rootKeyBox != null) { - const rootKey = decrypt(rootKeyBox, loginKey) - const infoKey = hmacSha256(rootKey, utf8.parse('infoKey')) - const keys = { - mnemonic: decryptText(mnemonicBox, infoKey), - rootKey: base64.stringify(rootKey) - } - legacyKeys.push(makeKeyInfo('wallet:bitid', keys, rootKey)) - } - - // Account settings: - if (stash.syncKeyBox != null) { - const syncKey = decrypt(stash.syncKeyBox, loginKey) - const type = makeAccountType(appId) - const keys = wasEdgeStorageKeys({ dataKey: loginKey, syncKey }) - legacyKeys.push(makeKeyInfo(type, keys, loginKey)) - } - - // Keys: - const keyInfos = keyBoxes.map(box => - asEdgeWalletInfo(JSON.parse(decryptText(box, loginKey))) - ) - return mergeKeyInfos([...legacyKeys, ...keyInfos]).map(walletInfo => - fixWalletInfo(walletInfo) - ) -} - /** * Logs a user in, using the auth server to retrieve information. * The various login methods (password / PIN / recovery, etc.) share