-
Notifications
You must be signed in to change notification settings - Fork 57
Implement KeyGenerator #288
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
Conversation
src/core/format/KeyGenerator.ts
Outdated
/** | ||
* @returns {UInt64} Deterministic uint64 value for the given string | ||
*/ | ||
public static fromString(input: string) { |
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.
why fromString
? should it be a better name e.g gernerateXXX
?
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.
Missing return type in the signature
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.
What name do you suggest, generateUint64? generateKey?
src/core/format/KeyGenerator.ts
Outdated
if (input.length === 0) { | ||
throw Error(`Input must not be empty`); | ||
} | ||
if (input.length > 1024) { |
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 all keys have the same restriction. the check should be case specific
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 sure what you mean?
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.
If I remember correctly only the metadata.value
require this check. Don't think we need this check at all. do we?
src/core/format/KeyGenerator.ts
Outdated
if (input.length === 0) { | ||
throw Error(`Input must not be empty`); | ||
} | ||
if (input.length > 1024) { |
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.
If I remember correctly only the metadata.value
require this check. Don't think we need this check at all. do we?
src/core/format/KeyGenerator.ts
Outdated
throw Error(`Input exceeds 1024 characters (has ${input.length})`); | ||
} | ||
const hex = sha3_256(input) | ||
return UInt64.fromHex(hex.substr(0, 16)) |
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.
if you check the definition of UInt64, it uses 2 Uint32 (low / high). I would convert the hash buffer array to uint32 array and return the new Uint64( ['low', 'high']). Not quite sure about the substring here.
I would do something like
const result = new Uint32Array(hex.arrayBuffer());
return new UInt64([result[0], (result[1] | 0x80000000) >>> 0]);
src/core/format/KeyGenerator.ts
Outdated
* @param {string} input Input string | ||
* @returns {UInt64} Deterministic uint64 value for the given string | ||
*/ | ||
public static fromString(input: string): UInt64 { |
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.
Shall we call it generateUInt64Key
instead?
@evias @dgarcia360
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.
👍
PR adds
core/format/KeyGenerator
. Usage:const u64 = KeyGenerator.fromString('key_name') // returns UInt64