Skip to content

Commit ade0151

Browse files
authored
Clarify generateDeterministicEntropy usage examples (#130)
1 parent 6d0ae21 commit ade0151

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

.changeset/orange-suits-impress.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@bitauth/libauth': patch
3+
---
4+
5+
clarify `generateDeterministicEntropy` usage examples

docs/keys.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ const flip128 =
119119

120120
const faces = 2;
121121
const events = splitEvery(flip128, 1).map(parseInt);
122-
// `assertSuccess` simply throws any errors
122+
/* `assertSuccess` simply throws any errors */
123123
const entropy = assertSuccess(generateDeterministicEntropy(faces, events));
124-
125-
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));
124+
/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
125+
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
126126
console.log(phrase);
127-
// => "crawl actual tool rally crazy lab work paper fragile favorite draft initial amount lawsuit task pupil clean crater genre rotate shoulder plate prevent bone"
127+
// => "crawl actual tool rally crazy lab work paper fragile favorite draft income"
128128
```
129129

130130
Note that `generateDeterministicEntropy` will return an error if the provided events do not include sufficient entropy for safe key generation (configurable via [`requiredEntropyBits`](https://libauth.org/functions/generateDeterministicEntropy.html)):
@@ -164,9 +164,11 @@ const faces = 6;
164164
const entropy = assertSuccess(generateDeterministicEntropy(faces, events));
165165
console.log(binToHex(entropy));
166166
// => "8d270d32340c28d8708023a5becf5dd8d55da45808c2ba97cfb7c2b0dcfefad1"
167-
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));
167+
168+
/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
169+
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
168170
console.log(phrase);
169-
// => "minor debris erode gym secret history search afford pizza wait student random fiction split gasp blue ritual salmon unknown lyrics assist legal twice cactus"
171+
// => "minor debris erode gym secret history search afford pizza wait student ranch"
170172
```
171173

172174
#### BIP39 Mnemonic Phrase to BCH Wallet

src/lib/docs.spec.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,16 @@ test('keys.md: BIP39 Mnemonic Phrase from Coin Flips', (t) => {
530530

531531
const faces = 2;
532532
const events = splitEvery(flip128, 1).map(parseInt);
533+
/* `assertSuccess` simply throws any errors */
533534
const entropy = assertSuccess(generateDeterministicEntropy(faces, events));
534-
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));
535+
/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
536+
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
535537
// eslint-disable-next-line no-console
536538
console.log(phrase);
537-
// => "crawl actual tool rally crazy lab work paper fragile favorite draft initial amount lawsuit task pupil clean crater genre rotate shoulder plate prevent bone"
539+
// => "crawl actual tool rally crazy lab work paper fragile favorite draft income"
538540
t.deepEqual(
539541
phrase,
540-
'crawl actual tool rally crazy lab work paper fragile favorite draft initial amount lawsuit task pupil clean crater genre rotate shoulder plate prevent bone',
542+
'crawl actual tool rally crazy lab work paper fragile favorite draft income',
541543
);
542544
});
543545

@@ -575,13 +577,14 @@ test('keys.md: BIP39 Mnemonic Phrase from Dice Rolls', (t) => {
575577
binToHex(entropy),
576578
'8d270d32340c28d8708023a5becf5dd8d55da45808c2ba97cfb7c2b0dcfefad1',
577579
);
578-
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));
580+
/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
581+
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
579582
// eslint-disable-next-line no-console
580583
console.log(phrase);
581-
// => "minor debris erode gym secret history search afford pizza wait student random fiction split gasp blue ritual salmon unknown lyrics assist legal twice cactus"
584+
// => "minor debris erode gym secret history search afford pizza wait student ranch"
582585
t.deepEqual(
583586
phrase,
584-
'minor debris erode gym secret history search afford pizza wait student random fiction split gasp blue ritual salmon unknown lyrics assist legal twice cactus',
587+
'minor debris erode gym secret history search afford pizza wait student ranch',
585588
);
586589
});
587590

0 commit comments

Comments
 (0)