Skip to content

Commit

Permalink
Decrypt wallet creation dates
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Sep 6, 2024
1 parent c242568 commit 87ab73a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/core/fake/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function createLogin(request: ApiRequest, login?: DbLogin): HttpResponse {
...clean,
...secret,
created: date,
keyBoxes: keys.keyBoxes,
keyBoxes: keys.keyBoxes.map(box => ({ ...box, created: date })),
vouchers: [],

// Optional fields:
Expand Down Expand Up @@ -321,14 +321,19 @@ const createLoginRoute = withLogin2(

const addKeysRoute = withLogin2(request => {
const { db, login, payload } = request
const date = new Date()

const [clean, cleanError] = cleanRequest(asCreateKeysPayload, payload)
if (clean == null) return cleanError

// Set up repos:
for (const syncKey of clean.newSyncKeys) {
db.repos.set(syncKey, {})
}
login.keyBoxes = softCat(login.keyBoxes, clean.keyBoxes)
login.keyBoxes = softCat(
login.keyBoxes,
clean.keyBoxes.map(box => ({ ...box, created: date }))
)

return statusResponse()
})
Expand Down
35 changes: 27 additions & 8 deletions src/core/login/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ export function makeKeysKit(
ai.props.log.breadcrumb('makeKeysKit', {})

const { io } = ai.props
const keyBoxes = keyInfos.map(info =>
encrypt(
const keyBoxes = keyInfos.map(info => ({
created: new Date(),
...encrypt(
io,
utf8.parse(JSON.stringify(wasEdgeWalletInfo(info))),
sessionKey.loginKey
)
)
}))

const newSyncKeys: string[] = []
for (const info of keyInfos) {
Expand Down Expand Up @@ -147,7 +148,8 @@ export function mergeKeyInfos(keyInfos: EdgeWalletInfo[]): EdgeWalletInfo[] {
*/
export function decryptKeyInfos(
stash: LoginStash,
loginKey: Uint8Array
loginKey: Uint8Array,
keyDates = new Map<string, Date>()
): EdgeWalletInfo[] {
const { appId, keyBoxes = [] } = stash

Expand All @@ -174,9 +176,12 @@ export function decryptKeyInfos(
}

// Keys:
const keyInfos = keyBoxes.map(box =>
asEdgeWalletInfo(JSON.parse(decryptText(box, loginKey)))
)
const keyInfos = keyBoxes.map(box => {
const keys = asEdgeWalletInfo(JSON.parse(decryptText(box, loginKey)))
const created = mergeKeyDate(box.created, keyDates.get(keys.id))
if (created != null) keyDates.set(keys.id, created)
return keys
})
return mergeKeyInfos([...legacyKeys, ...keyInfos]).map(walletInfo =>
fixWalletInfo(walletInfo)
)
Expand All @@ -192,6 +197,7 @@ export function decryptAllWalletInfos(
walletStates: EdgeWalletStates
): EdgeWalletInfoFull[] {
// Maps from walletId's to appId's:
const dates = new Map<string, Date>()
const appIdMap = new Map<string, string[]>()
const walletInfos: EdgeWalletInfo[] = [...legacyWalletInfos]

Expand All @@ -212,7 +218,7 @@ export function decryptAllWalletInfos(
loginKey: Uint8Array
): void {
// Add our own walletInfos:
const keyInfos = decryptKeyInfos(stash, loginKey)
const keyInfos = decryptKeyInfos(stash, loginKey, dates)
for (const info of keyInfos) {
walletInfos.push(info)

Expand All @@ -235,6 +241,7 @@ export function decryptAllWalletInfos(

// Defaults to be overwritten:
archived: false,
created: dates.get(info.id),
deleted: false,
hidden: false,
sortIndex: walletInfos.length,
Expand Down Expand Up @@ -388,3 +395,15 @@ export async function finishWalletCreation(
function getLast<T>(array: T[]): T {
return array[array.length - 1]
}

/**
* Returns the earliest date, or undefined if neither date exists.
*/
function mergeKeyDate(
a: Date | undefined,
b: Date | undefined
): Date | undefined {
if (a == null) return b
if (b == null) return a
return new Date(Math.min(a.valueOf(), b.valueOf()))
}
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export interface EdgeWalletInfo {
export type EdgeWalletInfoFull = EdgeWalletInfo & {
appIds: string[]
archived: boolean
created?: Date
deleted: boolean
hidden: boolean
migratedFromWalletId?: string
Expand Down
3 changes: 3 additions & 0 deletions test/core/account/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ describe('account', function () {
const account = await context.loginWithPIN(fakeUser.username, fakeUser.pin)

const walletId = 'narfavJN4rp9ZzYigcRj1i0vrU2OAGGp4+KksAksj54='
expect(account.getWalletInfo(walletId)?.created).deep.equals(
new Date('2024-01-01')
)
expect(await account.getRawPrivateKey(walletId)).deep.equals({
dataKey: 'RlY1l6wQ5ntQgUHE70vG/2M/qiLdvWMnIAM7KJIcsDs=',
fakecoinKey: 'zARFBBkgUe6pYB6l',
Expand Down
1 change: 1 addition & 0 deletions test/fake/fake-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const fakeUserDump: EdgeFakeUser = {
},
keyBoxes: [
{
created: '2024-01-01T00:00:00.000Z',
data_base64:
'ABM8mwYTGuODvGXR6BMN0/2AXK6nO+AaZ0QZamdLsZGd1YnfdnERXyYHZJPXD3CtUPY7lpXuohawb8H9MMce6fDxI9Dx+xEb6FjZZYcQsRD0r6fCWHgGSbXhQyo5AQ8xMLQrs0Y34bOH5MWnIlKU8zci5P0RvryZNDa9QvaHWTK5Klxsd9dD7MlkGAjeCloyN+aLi11SdK0IaCz14FqEwQatcbSQeStY4xGh2fKIKQp41ycvEYg6y94UB9kmQ1ncYSnpElfNb7Eb8XjzH7xOje4Bxs29gYC3uE72u4RslcpjxGZJFApKxYhn5gNfTfA4z1PGBSl0ihe5ABq1F8AavkGzY65+IwVmYqDYVum9JoErXG3qM8eE4W2WU+SomAsU',
encryptionType: 0,
Expand Down

0 comments on commit 87ab73a

Please sign in to comment.