-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix: using sr25519 to generate keys
- Loading branch information
Michael
committed
Jul 27, 2019
1 parent
74cfb26
commit e1c3976
Showing
5 changed files
with
58 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,38 @@ | ||
import { entropyToMnemonic } from 'bip39' | ||
import { randomBytes } from 'react-native-randombytes' | ||
|
||
const STRENGTH_MAP = { | ||
12: 16 * 8, | ||
15: 20 * 8, | ||
18: 24 * 8, | ||
21: 28 * 8, | ||
24: 32 * 8 | ||
} | ||
|
||
/** | ||
* Generate random bytes | ||
* | ||
* @param {Number} length | ||
* @returns {Buffer} Random bytes | ||
*/ | ||
const randomAsU8a = length => | ||
new Promise((resolve, reject) => { | ||
randomBytes(length, (err, bytes) => { | ||
if (err) return reject(err) | ||
resolve(bytes) | ||
}) | ||
}) | ||
|
||
/** | ||
* Use bip39 to generate mnemonic words | ||
* | ||
* @param {Number} words length | ||
* @returns {String} Mnemonic words | ||
*/ | ||
const mnemonicGenerate = async (words = 12) => { | ||
const strength = STRENGTH_MAP[words] | ||
const entropy = await randomAsU8a(strength / 8) | ||
return entropyToMnemonic(entropy) | ||
} | ||
|
||
export { mnemonicGenerate, randomAsU8a } |
This file contains 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