Skip to content

Commit

Permalink
feat(3744): use UUIDV4 as callback of metricsId
Browse files Browse the repository at this point in the history
  • Loading branch information
DDDDDanica committed Dec 11, 2024
1 parent 96b3edc commit 209a912
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/remote-feature-flag-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"dependencies": {
"@metamask/base-controller": "^7.0.2",
"@metamask/utils": "^10.0.0",
"@noble/hashes": "^1.4.0",
"cockatiel": "^3.1.2"
"cockatiel": "^3.1.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^3.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const MOCK_FLAGS_WITH_THRESHOLD = {
],
};

const MOCK_METRICS_ID = '0x1234567890abcdef';
const MOCK_METRICS_ID = 'f9e8d7c6-b5a4-3210-9876-543210fedcba';

/**
* Creates a controller instance with default parameters for testing
Expand Down Expand Up @@ -72,12 +72,6 @@ function createController(
}

describe('RemoteFeatureFlagController', () => {
beforeEach(() => {
jest
.spyOn(userSegmentationUtils, 'generateFallbackMetaMetricsId')
.mockReturnValue(MOCK_METRICS_ID);
});

describe('constructor', () => {
it('initializes with default state', () => {
const controller = createController();
Expand Down Expand Up @@ -304,6 +298,9 @@ describe('RemoteFeatureFlagController', () => {
});

it('uses a fallback metaMetricsId if none is provided', async () => {
jest
.spyOn(userSegmentationUtils, 'generateFallbackMetaMetricsId')
.mockReturnValue(MOCK_METRICS_ID);
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: MOCK_FLAGS_WITH_THRESHOLD,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { webcrypto } from 'crypto';
import { validate as uuidValidate, version as uuidVersion } from 'uuid';

import {
generateDeterministicRandomNumber,
Expand All @@ -7,10 +7,10 @@ import {
} from './user-segmentation-utils';

const MOCK_METRICS_IDS = [
'0x1234567890abcdef',
'0xdeadbeefdeadbeef',
'0xabcdef0123456789',
'0xfedcba9876543210',
'123e4567-e89b-12d3-a456-426614174000',
'987fcdeb-51a2-3c4b-9876-543210fedcba',
'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
'f9e8d7c6-b5a4-3210-9876-543210fedcba',
];

const MOCK_FEATURE_FLAGS = {
Expand Down Expand Up @@ -77,27 +77,10 @@ describe('user-segmentation-utils', () => {
});

describe('generateFallbackMetaMetricsId', () => {
beforeAll(() => {
// Set up crypto for tests
Object.defineProperty(global, 'crypto', {
value: webcrypto,
writable: true,
configurable: true,
});
});

it('returns a properly formatted hex string', () => {
it('returns a valid uuidv4', () => {
const result = generateFallbackMetaMetricsId();
expect(result.startsWith('0x')).toBe(true);
expect(result).toHaveLength(66);
expect(result.slice(2)).toMatch(/^[0-9a-f]+$/u);
});

it('generates unique values for each revoke', () => {
const result1 = generateFallbackMetaMetricsId();
const result2 = generateFallbackMetaMetricsId();

expect(result1).not.toBe(result2);
expect(uuidValidate(result)).toBe(true);
expect(uuidVersion(result)).toBe(4);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Json } from '@metamask/utils';
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex } from '@noble/hashes/utils';
import { v4 as uuidV4 } from 'uuid';

import type { FeatureFlagScopeValue } from '../remote-feature-flag-controller-types';

Expand Down Expand Up @@ -42,12 +41,9 @@ export const isFeatureFlagWithScopeValue = (
};

/**
* Generates a SHA-256 hash to use as a fallback metaMetricsId
* @returns A 32-byte hex string prefixed with '0x'
* Generates UUIDv4 as a fallback metaMetricsId
* @returns A UUIDv4 string
*/
export function generateFallbackMetaMetricsId(): string {
const random = new Uint8Array(32);
crypto.getRandomValues(random);
const hash = sha256(random);
return `0x${bytesToHex(hash)}`;
return uuidV4();
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3458,7 +3458,6 @@ __metadata:
"@metamask/base-controller": "npm:^7.0.2"
"@metamask/controller-utils": "npm:^11.4.4"
"@metamask/utils": "npm:^10.0.0"
"@noble/hashes": "npm:^1.4.0"
"@types/jest": "npm:^27.4.1"
cockatiel: "npm:^3.1.2"
deepmerge: "npm:^4.2.2"
Expand All @@ -3468,6 +3467,7 @@ __metadata:
typedoc: "npm:^0.24.8"
typedoc-plugin-missing-exports: "npm:^2.0.0"
typescript: "npm:~5.2.2"
uuid: "npm:^8.3.2"
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 209a912

Please sign in to comment.