-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac7de08
commit de6d416
Showing
12 changed files
with
257 additions
and
66 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
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,76 @@ | ||
import { SupportedAlgorithm, computeHmac } from '@ethersproject/sha2'; | ||
import { Address } from 'viem'; | ||
|
||
import { getWallet } from '~/entries/popup/handlers/wallet'; | ||
import { KeychainType } from '~/core/types/keychainTypes'; | ||
import { RainbowError, logger } from '~/logger'; | ||
|
||
const SECURE_WALLET_HASH_KEY = process.env.SECURE_WALLET_HASH_KEY; | ||
|
||
function securelyHashWalletAddress( | ||
walletAddress: Address | undefined, | ||
): string | undefined { | ||
if (!SECURE_WALLET_HASH_KEY) { | ||
logger.error( | ||
new RainbowError( | ||
`[securelyHashWalletAddress]: Required .env variable SECURE_WALLET_HASH_KEY does not exist`, | ||
), | ||
); | ||
return; | ||
} | ||
|
||
if (!walletAddress) return; | ||
|
||
try { | ||
const hmac = computeHmac( | ||
SupportedAlgorithm.sha256, | ||
// must be hex `0x<key>` string | ||
SECURE_WALLET_HASH_KEY, | ||
// must be hex `0x<key>` string | ||
walletAddress, | ||
); | ||
|
||
logger.debug(`[securelyHashWalletAddress]: Wallet address securely hashed`); | ||
|
||
return hmac; | ||
} catch (e) { | ||
// could be an invalid hashing key, or trying to hash an ENS | ||
logger.error( | ||
new RainbowError( | ||
`[securelyHashWalletAddress]: Wallet address hashing failed`, | ||
), | ||
); | ||
} | ||
} | ||
|
||
export type WalletContext = { | ||
walletType?: 'owned' | 'hardware' | 'watched'; | ||
walletAddressHash?: string; | ||
}; | ||
|
||
export async function getWalletContext( | ||
address: Address, | ||
): Promise<WalletContext> { | ||
// currentAddressStore address is initialized to '' | ||
if (!address || address === ('' as Address)) return {}; | ||
|
||
const walletAddressHash = securelyHashWalletAddress(address); | ||
|
||
// walletType is unavailable when keychain is locked | ||
let walletType; | ||
try { | ||
// expect getWallet error when keychain is locked | ||
const wallet = await getWallet(address); | ||
walletType = ({ | ||
[KeychainType.HdKeychain]: 'owned', | ||
[KeychainType.KeyPairKeychain]: 'owned', | ||
[KeychainType.ReadOnlyKeychain]: 'watched', | ||
[KeychainType.HardwareWalletKeychain]: 'hardware', | ||
} as const)[wallet?.type]; | ||
} catch (e) {} | ||
|
||
return { | ||
walletType, | ||
walletAddressHash, | ||
}; | ||
} |
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,37 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { analytics } from '~/analytics'; | ||
import { getWalletContext } from '~/analytics/util'; | ||
import { useAuth } from '~/entries/popup/hooks/useAuth'; | ||
|
||
import { setSentryUser } from '../sentry'; | ||
import { useCurrentAddressStore, useDeviceIdStore } from '../state'; | ||
|
||
export const TelemetryIdentifier = () => { | ||
const { status: authStatus } = useAuth(); | ||
const { deviceId } = useDeviceIdStore(); | ||
const { currentAddress } = useCurrentAddressStore(); | ||
|
||
// update telemetry wallet each time selected wallet changes | ||
useEffect(() => { | ||
// update wallet context and trigger identify | ||
const identify = async () => { | ||
const { walletType, walletAddressHash } = | ||
await getWalletContext(currentAddress); | ||
setSentryUser({ deviceId, walletAddressHash, walletType }); | ||
// allows calling telemetry before currentAddress is available (i.e. onboarding) | ||
if (walletType || walletAddressHash) | ||
analytics.setWalletContext({ walletAddressHash, walletType }); | ||
analytics.setDeviceId(deviceId); | ||
analytics.identify(); | ||
}; | ||
// Disable analytics & sentry for e2e and dev mode | ||
if (process.env.IS_TESTING !== 'true' && process.env.IS_DEV !== 'true') { | ||
if (authStatus === '') return; // wait for auth state to settle | ||
else if (authStatus === 'READY') identify(); // assign full wallet context | ||
else identify(); // assign partial wallet context immediately if available | ||
} | ||
}, [deviceId, currentAddress, authStatus]); | ||
|
||
return null; | ||
}; |
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
Oops, something went wrong.