Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
emit(ai.props.output.context.api, 'loginStart', {
username: stashTree.username
})
// Find the appropriate child:
const child = searchTree(stashTree, stash => stash.appId === appId)
if (child == null) {
throw new Error(`Cannot find requested appId: "${appId}"`)
}
// The Airbitz mobile will sometimes send the pin2Key in base58
// instead of base64 due to an unfortunate bug. Fix that:
if (child.pin2Key != null && child.pin2Key.slice(-1) !== '=') {
io.console.warn('Fixing base58 pin2Key')
child.pin2Key = base64.stringify(base58.parse(child.pin2Key))
}
await saveStash(ai, stashTree)
// This is almost guaranteed to blow up spectacularly:
const loginKey = base64.parse(reply.loginKey)
const loginTree = makeLoginTree(stashTree, loginKey, appId)
const login = searchTree(loginTree, login => login.appId === appId)
if (login == null) {
throw new Error(`Cannot find requested appId: "${appId}"`)
}
const newLoginTree = await syncLogin(ai, loginTree, login)
const account = await makeAccount(ai, appId, newLoginTree, 'edgeLogin', opts)
emit(ai.props.output.context.api, 'login', account)
}
if (mnemonicBox != null && rootKeyBox != null) {
const rootKey = decrypt(rootKeyBox, loginKey)
const infoKey = hmacSha256(rootKey, utf8.parse('infoKey'))
const keys = {
mnemonic: utf8.stringify(decrypt(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(login.appId)
const keys = {
syncKey: base64.stringify(syncKey),
dataKey: base64.stringify(loginKey)
}
legacyKeys.push(makeKeyInfo(type, keys, loginKey))
}
// Keys:
const stashKeyBoxes = stash.keyBoxes != null ? stash.keyBoxes : []
const keyInfos = stashKeyBoxes.map(box =>
JSON.parse(utf8.stringify(decrypt(box, loginKey)))
)
login.keyInfos = mergeKeyInfos([...legacyKeys, ...keyInfos]).map(walletInfo =>
fixWalletInfo(walletInfo)
)
// Recurse into children:
const rootKey = decrypt(rootKeyBox, loginKey)
const infoKey = hmacSha256(rootKey, utf8.parse('infoKey'))
const keys = {
mnemonic: utf8.stringify(decrypt(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(login.appId)
const keys = {
syncKey: base64.stringify(syncKey),
dataKey: base64.stringify(loginKey)
}
legacyKeys.push(makeKeyInfo(type, keys, loginKey))
}
// Keys:
const stashKeyBoxes = stash.keyBoxes != null ? stash.keyBoxes : []
const keyInfos = stashKeyBoxes.map(box =>
JSON.parse(utf8.stringify(decrypt(box, loginKey)))
)
login.keyInfos = mergeKeyInfos([...legacyKeys, ...keyInfos]).map(walletInfo =>
fixWalletInfo(walletInfo)
)
// Recurse into children:
const stashChildren = stash.children != null ? stash.children : []
return authRequest(ai, 'GET', '/v2/lobby/' + lobbyId, {}).then(reply => {
const lobbyRequest = reply.request
// Verify the public key:
const pubkey = base64.parse(lobbyRequest.publicKey)
const checksum = sha256(sha256(pubkey))
base58.parse(lobbyId).forEach((value, index) => {
if (value !== checksum[index]) {
throw new Error('Lobby ECDH integrity error')
}
})
return lobbyRequest
})
}
login.otpResetDate = stash.otpResetDate
login.otpTimeout = stash.otpTimeout
// Password:
if (stash.userId != null) {
login.userId = stash.userId
} else if (stash.passwordAuthBox != null) {
login.userId = login.loginId
}
if (stash.passwordAuthBox != null) {
login.passwordAuth = decrypt(stash.passwordAuthBox, loginKey)
}
// PIN v2:
if (stash.pin2Key != null) {
login.pin2Key = base64.parse(stash.pin2Key)
}
if (stash.pin2TextBox != null) {
login.pin = utf8.stringify(decrypt(stash.pin2TextBox, loginKey))
}
// Recovery v2:
if (stash.recovery2Key != null) {
login.recovery2Key = base64.parse(stash.recovery2Key)
}
const legacyKeys = []
// BitID wallet:
const { mnemonicBox, rootKeyBox } = stash
if (mnemonicBox != null && rootKeyBox != null) {
const rootKey = decrypt(rootKeyBox, loginKey)
}
if (stash.passwordAuthBox != null) {
login.passwordAuth = decrypt(stash.passwordAuthBox, loginKey)
}
// PIN v2:
if (stash.pin2Key != null) {
login.pin2Key = base64.parse(stash.pin2Key)
}
if (stash.pin2TextBox != null) {
login.pin = utf8.stringify(decrypt(stash.pin2TextBox, loginKey))
}
// Recovery v2:
if (stash.recovery2Key != null) {
login.recovery2Key = base64.parse(stash.recovery2Key)
}
const legacyKeys = []
// 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: utf8.stringify(decrypt(mnemonicBox, infoKey)),
rootKey: base64.stringify(rootKey)
}
legacyKeys.push(makeKeyInfo('wallet:bitid', keys, rootKey))
}
newWalletType: string
): EdgeWalletInfo {
const { id, type, keys } = walletInfo
if (!keys.dataKey || !keys.syncKey) {
throw new Error(`Wallet ${id} is not a splittable type`)
}
const dataKey = base64.parse(keys.dataKey)
const syncKey = base64.parse(keys.syncKey)
const xorKey = xorData(
hmacSha256(utf8.parse(type), dataKey),
hmacSha256(utf8.parse(newWalletType), dataKey)
)
// Fix the id:
const newWalletId = xorData(base64.parse(id), xorKey)
const newSyncKey = xorData(syncKey, xorKey.subarray(0, syncKey.length))
// Fix the keys:
const networkName = type.replace(/wallet:/, '').replace('-', '')
const newNetworkName = newWalletType.replace(/wallet:/, '').replace('-', '')
const newKeys = {}
for (const key of Object.keys(keys)) {
if (key === networkName + 'Key') {
newKeys[newNetworkName + 'Key'] = keys[key]
} else {
newKeys[key] = keys[key]
}
}
return {
id: base64.stringify(newWalletId),
export function calcSnrpForTarget(
salt: Uint8Array,
benchMs: number,
targetMs: number
): JsonSnrp {
const snrp = {
salt_hex: base16.stringify(salt),
n: 16384,
r: 8,
p: 1
}
if (benchMs === 0) {
snrp.n = 131072
snrp.r = 8
snrp.p = 64
return snrp
}
let timeUsed = benchMs // Estimated time in ms the current setting will take on current device
//
// Add additional r value first. This increases memory usage
createCurrencyWallet (accountId: string, type: string) {
// Hack in basic Ethereum support for Augur:
if (type === 'wallet:ethereum') {
return state.accounts[accountId]
.createWallet(type, {
ethereumKey: base16.stringify(makeRandom(32))
})
.then(walletId => {
const walletInfos = getWalletInfos(state, accountId)
return { walletId, walletInfos }
})
}
return state.accounts[accountId]
.createCurrencyWallet(type, {})
.then(currencyWallet => {
const walletInfos = getWalletInfos(state, accountId)
return { walletId: currencyWallet.id, walletInfos }
})
},
).then(ourChanges => {
// If we have local changes, we need to bundle those:
const request = {}
if (ourChanges.length > 0) {
request.changes = {}
for (const change of ourChanges) {
request.changes[change.name] = change.json
}
}
const method = request.changes ? 'POST' : 'GET'
// Calculate the URI:
let path = '/api/v2/store/' + base16.stringify(syncKey).toLowerCase()
if (status.lastHash != null) {
path += '/' + status.lastHash
}
// Make the request:
return syncRequest(io, log, method, path, request).then(reply => {
const { changes = {}, hash } = reply
// Save the incoming changes into our `data` folder:
return saveChanges(dataDisklet, changes)
.then(
// Delete any changed keys (since the upload is done):
() => Promise.all(ourChanges.map(change => change.file.delete()))
)
.then(() => {
// Update the repo status: