Skip to content

Commit

Permalink
fixup! Factor out a decryptKeyInfos function
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Sep 6, 2024
1 parent 3cc595a commit 9a716da
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 57 deletions.
51 changes: 49 additions & 2 deletions src/core/login/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
58 changes: 3 additions & 55 deletions src/core/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a716da

Please sign in to comment.