Skip to content

feat: Add transaction hash to STX in analytics for opt-in users#506

Closed
httpJunkie wants to merge 5 commits intomainfrom
feat/tx-hash-analytics
Closed

feat: Add transaction hash to STX in analytics for opt-in users#506
httpJunkie wants to merge 5 commits intomainfrom
feat/tx-hash-analytics

Conversation

@httpJunkie
Copy link
Copy Markdown
Contributor

Current State and Rationale for Change

Currently, the Smart Transactions (STX) Controller doesn't include transaction hash in analytics events, unlike regular transactions which have been updated to support this functionality. The Data team requires transaction hash in specific transaction events to improve analytics capabilities, and we need to extend this support to Smart Transactions for consistency.

Solution

This PR extends transaction hash analytics support to Smart Transactions (STX), completing the implementation started in TXL-714:

  • Updates the Smart Transactions Controller to support the remote feature flag transactionsTxHashInAnalytics
  • Adds transaction hash to sensitiveProperties in STX analytics events
  • Applies the same privacy controls as regular transactions:
    • Only collects hash when users have opted into MetaMetrics
    • Only enables collection when the remote feature flag is active
  • Adds proper type definitions for sensitive properties in STX analytics
  • Updates tests to support new controller parameters

The implementation extracts transaction hash from the minedHash property in the STX status metadata, ensuring consistent behavior with regular transactions.

Related Links

The corresponding PR in MetaMask Extension is:
MetaMask/metamask-extension#31048

Manual Testing Steps

We need to test MetaMask Extension with the changes from this PR for smart-transactions-controller.

  1. Enable MetaMetrics in wallet settings
  2. Set up local override for the remote feature flag in .manifest-overrides.json:
{
  "_flags": {
    "remoteFeatureFlags": {
      "transactionsTxHashInAnalytics": {
        "name": "transactionsTxHashInAnalytics",
        "value": true
      }
    }
  }
}
  1. Perform a Smart Transaction (e.g., a swap that uses STX)
  2. Verify through console logs that transaction hash is included in STX analytics events

- Update Smart Transactions Controller to support tx hash in analytics when feature flag is enabled
- Add tx hash to sensitiveProperties with proper type definitions
- Only include hash when both remote feature flag is enabled and user has opted into MetaMetrics
- Update tests to support new "required" parameters
@httpJunkie httpJunkie requested a review from a team March 24, 2025 20:42
@httpJunkie httpJunkie self-assigned this Mar 24, 2025
- The second check for getParticipateInMetrics is redundant since MetaMetrics events would only be sent if the user has already opted in
- The MetaMetrics system handles this permission check at a higher level before any events are even tracked
- Our implementation didn't need to check it
Fix TypeScript error by properly converting hex string to `Uint8Array` for
`TransactionFactory.fromSerializedData`. This maintains the same functionality
while providing the correct type expected by the function.

The change converts the hex string to bytes using `Buffer.from()` first, then
creates a `Uint8Array` from those bytes, ensuring type safety without changing
the underlying data.
Replaces unsafe `Buffer` usage with `hexToBytes` utility and adds proper string typing,
ensuring reliable transaction hash generation for MetaMetrics events.
The getTxHash function has been enhanced to handle hex strings more robustly:
- Normalizes case (upper/lower) before processing
- Properly handles 0x-prefixed and unprefixed hex strings
- Prevents double-prefix issues that could cause transaction failures

This change makes the function more resilient to varying hex string formats
that may come from different parts of the system, while maintaining consistent
hash generation regardless of input format.

Tests added to verify:
- Prefix handling (with/without 0x)
- Case consistency (upper/lower/mixed)
- Hash determinism across different string representations
@httpJunkie httpJunkie closed this Apr 2, 2025
@httpJunkie
Copy link
Copy Markdown
Contributor Author

We did not need these changes for Privacy Policy and Transaction Hash work in related PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant