Skip to content

Commit

Permalink
feat(CAB): Log hashed keyshares (#4960)
Browse files Browse the repository at this point in the history
### Description

Log hashed keyshares for CAB so can help a user if they are failing to
restore.

Track analytics event during setup, log in app during restore.

### Test plan

Unit tests added

### Related issues

- Fixes #[ACT-1097]

### Backwards compatibility

Yes

---------

Co-authored-by: Tom McGuire <Mcgtom10@gmail.com>
Co-authored-by: Joe Bergeron <jbergero@alum.mit.edu>
  • Loading branch information
3 people committed Feb 28, 2024
1 parent 5ae3569 commit deca7b0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analytics/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export enum KeylessBackupEvents {
cab_phone_verification_help = 'cab_phone_verification_help',
cab_phone_verification_help_skip = 'cab_phone_verification_help_skip',
cab_phone_verification_help_go_back = 'cab_phone_verification_help_go_back',
cab_setup_hashed_keyshares = 'cab_setup_hashed_keyshares',
}

export enum OnboardingEvents {
Expand Down
4 changes: 4 additions & 0 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ interface KeylessBackupEventsProperties {
[KeylessBackupEvents.cab_phone_verification_help]: CommonKeylessBackupProps
[KeylessBackupEvents.cab_phone_verification_help_skip]: CommonKeylessBackupProps
[KeylessBackupEvents.cab_phone_verification_help_go_back]: CommonKeylessBackupProps
[KeylessBackupEvents.cab_setup_hashed_keyshares]: {
hashedKeysharePhone: string
hashedKeyshareEmail: string
}
}

interface OnboardingEventsProperties {
Expand Down
1 change: 1 addition & 0 deletions src/analytics/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const eventDocs: Record<AnalyticsEventType, string> = {
[KeylessBackupEvents.cab_phone_verification_help_skip]: `When a user is on the Help bottom sheet on CAB phone verification screen, and they hit skip`,
[KeylessBackupEvents.cab_phone_verification_help_go_back]: `When a user is on the Help bottom sheet on CAB phone verification screen, and they hit go back`,
[KeylessBackupEvents.cab_restore_mnemonic_not_found]: `When a user is restoring from CAB and the mnemonic is not found. Meaning one or more of the user's keyshares/auth methods were incorrect`,
[KeylessBackupEvents.cab_setup_hashed_keyshares]: `When a user is setting up CAB, the hashed keyshares from their phone and email are saved`,
[OnboardingEvents.onboarding_education_scroll]: ``,
[OnboardingEvents.onboarding_education_step_impression]: ``,
[OnboardingEvents.create_account_start]: ``,
Expand Down
11 changes: 11 additions & 0 deletions src/keylessBackup/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ import { KeylessBackupFlow } from 'src/keylessBackup/types'
import { getTorusPrivateKey } from 'src/keylessBackup/web3auth'
import { navigate } from 'src/navigator/NavigationService'
import { Screens } from 'src/navigator/Screens'
import Logger from 'src/utils/Logger'
import { assignAccountFromPrivateKey } from 'src/web3/saga'
import { walletAddressSelector } from 'src/web3/selectors'
import { mockPrivateDEK } from 'test/values'
import { Hex } from 'viem'

jest.mock('src/utils/Logger')

describe('keylessBackup saga', () => {
beforeEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -181,6 +184,10 @@ describe('keylessBackup saga', () => {
])
.put(keylessBackupCompleted())
.run()
expect(ValoraAnalytics.track).toBeCalledWith('cab_setup_hashed_keyshares', {
hashedKeysharePhone: 'a0b7675b466da4059cda48c116c0ead195916e045c6d4e9eff7301242b12b9e0',
hashedKeyshareEmail: 'a8ad600b8026607f35817dc15f93a25d9fa6617fae6cfd19b3c927eb633ec331',
})
expect(ValoraAnalytics.track).toBeCalledWith('cab_handle_keyless_backup_success', {
keylessBackupFlow: KeylessBackupFlow.Setup,
})
Expand Down Expand Up @@ -274,6 +281,10 @@ describe('keylessBackup saga', () => {
.call(initializeAccountSaga)
.put(keylessBackupCompleted())
.run()
expect(Logger.info).toHaveBeenCalledWith(
'keylessBackup/saga',
'Phone keyshare: a0b7675b466da4059cda48c116c0ead195916e045c6d4e9eff7301242b12b9e0, Email keyshare: a8ad600b8026607f35817dc15f93a25d9fa6617fae6cfd19b3c927eb633ec331'
)
expect(ValoraAnalytics.track).toBeCalledWith('cab_handle_keyless_backup_success', {
keylessBackupFlow: KeylessBackupFlow.Restore,
})
Expand Down
12 changes: 12 additions & 0 deletions src/keylessBackup/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { getTorusPrivateKey } from 'src/keylessBackup/web3auth'
import { navigate } from 'src/navigator/NavigationService'
import { Screens } from 'src/navigator/Screens'
import Logger from 'src/utils/Logger'
import { calculateSha256Hash } from 'src/utils/random'
import { assignAccountFromPrivateKey } from 'src/web3/saga'
import { walletAddressSelector } from 'src/web3/selectors'
import { call, delay, put, race, select, spawn, take, takeLeading } from 'typed-redux-saga'
Expand All @@ -43,6 +44,11 @@ export function* handleValoraKeyshareIssued({
}: ReturnType<typeof valoraKeyshareIssued>) {
try {
const torusKeyshare = yield* waitForTorusKeyshare()
const hashedKeyshare = calculateSha256Hash(`CAB_PHONE_KEYSHARE_HASH_${keyshare}`)
const hashedTorusKeyshare = calculateSha256Hash(`CAB_EMAIL_KEYSHARE_HASH_${torusKeyshare}`)
if (keylessBackupFlow === KeylessBackupFlow.Restore) {
Logger.info(TAG, `Phone keyshare: ${hashedKeyshare}, Email keyshare: ${hashedTorusKeyshare}`)
}
const torusKeyshareBuffer = Buffer.from(torusKeyshare, 'hex')
const valoraKeyshareBuffer = Buffer.from(keyshare, 'hex')
const { privateKey: encryptionPrivateKey } = yield* call(
Expand All @@ -69,6 +75,12 @@ export function* handleValoraKeyshareIssued({
ValoraAnalytics.track(KeylessBackupEvents.cab_handle_keyless_backup_success, {
keylessBackupFlow,
})
if (keylessBackupFlow === KeylessBackupFlow.Setup) {
ValoraAnalytics.track(KeylessBackupEvents.cab_setup_hashed_keyshares, {
hashedKeysharePhone: hashedKeyshare,
hashedKeyshareEmail: hashedTorusKeyshare,
})
}
} catch (error) {
Logger.error(TAG, `Error handling keyless backup ${keylessBackupFlow}`, error)
ValoraAnalytics.track(KeylessBackupEvents.cab_handle_keyless_backup_failed, {
Expand Down

0 comments on commit deca7b0

Please sign in to comment.