Skip to content

Commit

Permalink
Clarify generateDeterministicEntropy usage examples (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson authored Apr 10, 2024
1 parent 6d0ae21 commit ade0151
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-suits-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bitauth/libauth': patch
---

clarify `generateDeterministicEntropy` usage examples
14 changes: 8 additions & 6 deletions docs/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ const flip128 =

const faces = 2;
const events = splitEvery(flip128, 1).map(parseInt);
// `assertSuccess` simply throws any errors
/* `assertSuccess` simply throws any errors */
const entropy = assertSuccess(generateDeterministicEntropy(faces, events));

const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));
/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
console.log(phrase);
// => "crawl actual tool rally crazy lab work paper fragile favorite draft initial amount lawsuit task pupil clean crater genre rotate shoulder plate prevent bone"
// => "crawl actual tool rally crazy lab work paper fragile favorite draft income"
```

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)):
Expand Down Expand Up @@ -164,9 +164,11 @@ const faces = 6;
const entropy = assertSuccess(generateDeterministicEntropy(faces, events));
console.log(binToHex(entropy));
// => "8d270d32340c28d8708023a5becf5dd8d55da45808c2ba97cfb7c2b0dcfefad1"
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy));

/* Slice produced entropy at 16 bytes (128 bits) for 12 words: */
const { phrase } = assertSuccess(encodeBip39Mnemonic(entropy.slice(0, 16)));
console.log(phrase);
// => "minor debris erode gym secret history search afford pizza wait student random fiction split gasp blue ritual salmon unknown lyrics assist legal twice cactus"
// => "minor debris erode gym secret history search afford pizza wait student ranch"
```

#### BIP39 Mnemonic Phrase to BCH Wallet
Expand Down
15 changes: 9 additions & 6 deletions src/lib/docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,16 @@ test('keys.md: BIP39 Mnemonic Phrase from Coin Flips', (t) => {

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

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

Expand Down

0 comments on commit ade0151

Please sign in to comment.