-
Notifications
You must be signed in to change notification settings - Fork 53
feat(sdk): add automatic entropy generation for document creation #2839
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed73125
feat(sdk): add automatic entropy generation for document creation
thephez 55ae1f4
chore: lint fixes
thephez 9554285
Merge branch 'v3.0-dev' into wasm-sdk-entropy-util
thephez 99d3939
refactor(sdk): remove dead Node.js randomBytes branch from generateEn…
thephez 9bb53f0
Merge branch 'v3.0-dev' into wasm-sdk-entropy-util
thephez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| import { asJsonString, generateEntropy } from '../../dist/util.js'; | ||
|
|
||
| describe('Util Functions', () => { | ||
| describe('asJsonString', () => { | ||
| it('returns undefined for null', () => { | ||
| expect(asJsonString(null)).to.be.undefined(); | ||
| }); | ||
|
|
||
| it('returns undefined for undefined', () => { | ||
| expect(asJsonString(undefined)).to.be.undefined(); | ||
| }); | ||
|
|
||
| it('returns string as-is', () => { | ||
| expect(asJsonString('hello')).to.equal('hello'); | ||
| }); | ||
|
|
||
| it('converts objects to JSON string', () => { | ||
| const obj = { foo: 'bar', num: 42 }; | ||
| expect(asJsonString(obj)).to.equal(JSON.stringify(obj)); | ||
| }); | ||
|
|
||
| it('converts arrays to JSON string', () => { | ||
| const arr = [1, 2, 'three']; | ||
| expect(asJsonString(arr)).to.equal(JSON.stringify(arr)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateEntropy', () => { | ||
| it('generates a 64-character hex string', () => { | ||
| const entropy = generateEntropy(); | ||
| expect(entropy).to.be.a('string'); | ||
| expect(entropy.length).to.equal(64); | ||
| }); | ||
|
|
||
| it('generates valid hexadecimal', () => { | ||
| const entropy = generateEntropy(); | ||
| expect(entropy).to.match(/^[0-9a-f]{64}$/i); | ||
| }); | ||
|
|
||
| it('generates different values each time', () => { | ||
| const entropy1 = generateEntropy(); | ||
| const entropy2 = generateEntropy(); | ||
| const entropy3 = generateEntropy(); | ||
|
|
||
| // Should be different (extremely unlikely to be the same) | ||
| expect(entropy1).to.not.equal(entropy2); | ||
| expect(entropy2).to.not.equal(entropy3); | ||
| expect(entropy1).to.not.equal(entropy3); | ||
| }); | ||
|
|
||
| it('returns exactly 32 bytes when decoded', () => { | ||
| const entropy = generateEntropy(); | ||
| // Convert hex string to bytes | ||
| const bytes = []; | ||
| for (let i = 0; i < entropy.length; i += 2) { | ||
| bytes.push(parseInt(entropy.substring(i, 2), 16)); | ||
| } | ||
| expect(bytes.length).to.equal(32); | ||
| }); | ||
|
|
||
| it('generates values with good distribution', () => { | ||
| // Generate multiple samples and check that we get a variety of hex digits | ||
| const samples = []; | ||
| for (let i = 0; i < 10; i += 1) { | ||
| samples.push(generateEntropy()); | ||
| } | ||
|
|
||
| // Check that we see various hex digits (not all zeros or all ones) | ||
| const allChars = samples.join(''); | ||
| const uniqueChars = new Set(allChars).size; | ||
|
|
||
| // We should see most of the 16 possible hex digits (0-9, a-f) | ||
| // With 640 characters (10 * 64), we expect to see all 16 | ||
| expect(uniqueChars).to.be.at.least(10); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.