Skip to content

Generating private key (as string) from generated Uint8Array #28

Answered by paulmillr
HopiumArtist asked this question in Q&A
Discussion options

You must be logged in to vote

I assume you're talking about secp256k1. secp256k1 can receive 32 bytes as private key. It can directly work with Uint8Arrays. mnemonicToSeed produces 64 bytes of output. Which means you can strip its 32 bytes:

const mn = bip39.generateMnemonic(wordlist);
const seed = await bip39.mnemonicToSeed(mn);
const privateKey = seed.slice(0, 32);
const publicKey = secp256k1.getPublicKey(privateKey);

There is a caveat: not all 32-byte values are not valid private keys. E.g. "ff".repeat(32) is not a valid key becasuse it's bigger than CURVE.n. You can modulo divide it to get a valid key in these rare circumstances:

const mn = bip39.generateMnemonic(wordlist);
const seed = await bip39.mnemonicToSeed(mn);

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by HopiumArtist
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants