-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #593 from EdgeApp/william/load-airbitz-stashes
Load Airbitz stashes
- Loading branch information
Showing
9 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { asCodec, asObject, asOptional, asString, Cleaner } from 'cleaners' | ||
import { justFolders, navigateDisklet } from 'disklet' | ||
|
||
import { fixUsername } from '../../client-side' | ||
import { asBase32, asEdgeBox, asEdgeSnrp } from '../../types/server-cleaners' | ||
import { EdgeIo } from '../../types/types' | ||
import { base58, utf8 } from '../../util/encoding' | ||
import { makeJsonFile } from '../../util/file-helpers' | ||
import { userIdSnrp } from '../scrypt/scrypt-selectors' | ||
import { LoginStash } from './login-stash' | ||
|
||
/** | ||
* Reads legacy Airbitz login stashes from disk. | ||
*/ | ||
export async function loadAirbitzStashes( | ||
io: EdgeIo, | ||
avoidUsernames: Set<string> | ||
): Promise<LoginStash[]> { | ||
const out: LoginStash[] = [] | ||
|
||
const paths = await io.disklet.list('Accounts').then(justFolders) | ||
for (const path of paths) { | ||
const folder = navigateDisklet(io.disklet, path) | ||
const [ | ||
carePackage, | ||
loginPackage, | ||
otp, | ||
pin2Key, | ||
recovery2Key, | ||
usernameJson | ||
] = await Promise.all([ | ||
await carePackageFile.load(folder, 'CarePackage.json'), | ||
await loginPackageFile.load(folder, 'LoginPackage.json'), | ||
await otpFile.load(folder, 'OtpKey.json'), | ||
await pin2KeyFile.load(folder, 'Pin2Key.json'), | ||
await recovery2KeyFile.load(folder, 'Recovery2Key.json'), | ||
await usernameFile.load(folder, 'UserName.json') | ||
]) | ||
|
||
if (usernameJson == null) continue | ||
const username = fixUsername(usernameJson.userName) | ||
if (avoidUsernames.has(username)) continue | ||
const userId = await io.scrypt( | ||
utf8.parse(username), | ||
userIdSnrp.salt_hex, | ||
userIdSnrp.n, | ||
userIdSnrp.r, | ||
userIdSnrp.p, | ||
32 | ||
) | ||
|
||
// Assemble a modern stash object: | ||
const stash: LoginStash = { | ||
appId: '', | ||
loginId: userId, | ||
pendingVouchers: [], | ||
username | ||
} | ||
if (carePackage != null && loginPackage != null) { | ||
stash.passwordKeySnrp = carePackage.SNRP2 | ||
stash.passwordBox = loginPackage.EMK_LP2 | ||
stash.syncKeyBox = loginPackage.ESyncKey | ||
stash.passwordAuthBox = loginPackage.ELP1 | ||
} | ||
if (otp != null) { | ||
stash.otpKey = otp.TOTP | ||
} | ||
if (pin2Key != null) { | ||
stash.pin2Key = pin2Key.pin2Key | ||
} | ||
if (recovery2Key != null) { | ||
stash.recovery2Key = recovery2Key.recovery2Key | ||
} | ||
|
||
out.push(stash) | ||
} | ||
|
||
return out | ||
} | ||
|
||
/** | ||
* A string of base58-encoded binary data. | ||
*/ | ||
const asBase58: Cleaner<Uint8Array> = asCodec( | ||
raw => base58.parse(asString(raw)), | ||
clean => base58.stringify(clean) | ||
) | ||
|
||
const carePackageFile = makeJsonFile( | ||
asObject({ | ||
SNRP2: asEdgeSnrp, // passwordKeySnrp | ||
SNRP3: asOptional(asEdgeSnrp), // recoveryKeySnrp | ||
SNRP4: asOptional(asEdgeSnrp), // questionKeySnrp | ||
ERQ: asOptional(asEdgeBox) // questionBox | ||
}) | ||
) | ||
|
||
const loginPackageFile = makeJsonFile( | ||
asObject({ | ||
EMK_LP2: asEdgeBox, // passwordBox | ||
EMK_LRA3: asOptional(asEdgeBox), // recoveryBox | ||
|
||
ESyncKey: asEdgeBox, // syncKeyBox | ||
ELP1: asEdgeBox // passwordAuthBox | ||
}) | ||
) | ||
|
||
const otpFile = makeJsonFile(asObject({ TOTP: asBase32 })) | ||
const pin2KeyFile = makeJsonFile(asObject({ pin2Key: asBase58 })) | ||
const recovery2KeyFile = makeJsonFile(asObject({ recovery2Key: asBase58 })) | ||
const usernameFile = makeJsonFile(asObject({ userName: asString })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect } from 'chai' | ||
import { describe, it } from 'mocha' | ||
|
||
import { makeFakeEdgeWorld } from '../../../src/index' | ||
import { airbitzFiles, fakeUser } from '../../fake/fake-user' | ||
|
||
const quiet = { onLog() {} } | ||
|
||
describe('airbitz stashes', function () { | ||
it('can log into legacy airbitz files', async function () { | ||
const world = await makeFakeEdgeWorld([fakeUser], quiet) | ||
const context = await world.makeEdgeContext({ | ||
airbitzSupport: true, | ||
apiKey: '', | ||
appId: '', | ||
cleanDevice: true, | ||
extraFiles: airbitzFiles | ||
}) | ||
|
||
expect(context.localUsers).deep.equals([ | ||
{ | ||
keyLoginEnabled: true, | ||
lastLogin: undefined, | ||
loginId: 'BTnpEn7pabDXbcv7VxnKBDsn4CVSwLRA25J8U84qmg4h', | ||
pinLoginEnabled: true, | ||
recovery2Key: 'NVADGXzb5Zc55PYXVVT7GRcXPnY9NZJUjiZK8aQnidc', | ||
username: 'js test 0', | ||
voucherId: undefined | ||
} | ||
]) | ||
|
||
await context.loginWithPIN(fakeUser.username, fakeUser.pin) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters