-
-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(3742): Feature Flag Values with Scope Based on threshold #5051
Changes from 1 commit
2122627
f2bc3bf
93d8351
63a7274
1f94695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
|
@@ -7,10 +7,10 @@ import { | |
} from './user-segmentation-utils'; | ||
|
||
const MOCK_METRICS_IDS = [ | ||
'0x1234567890abcdef', | ||
NicolasMassart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'0xdeadbeefdeadbeef', | ||
'0xabcdef0123456789', | ||
'0xfedcba9876543210', | ||
'123e4567-e89b-12d3-a456-426614174000', | ||
'987fcdeb-51a2-3c4b-9876-543210fedcba', | ||
'a1b2c3d4-e5f6-7890-abcd-ef1234567890', | ||
'f9e8d7c6-b5a4-3210-9876-543210fedcba', | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are not valid UUIDv4.
|
||
|
||
const MOCK_FEATURE_FLAGS = { | ||
|
@@ -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'; | ||
|
||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is non-deterministic, and therefore unsuitable for determining whether a feature flag should be enabled or not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the best way of handling this would be to accept a callback for generating the metametrics ID? Ideally we'd use a messenger action, but that would be tricky because of the separate metrics implementations, so a callback should work instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or as you've just pointed out in the extension PR @DDDDDanica , we can rely on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed in slack thread, we will remove the callback and add this logic to client side if undefined 🙏🏻 |
||
const random = new Uint8Array(32); | ||
crypto.getRandomValues(random); | ||
const hash = sha256(random); | ||
return `0x${bytesToHex(hash)}`; | ||
return uuidV4(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a valid UUIDv4, this is UUIDv3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch ! addressed 93d8351