herumi / bls-wasm

BLS signature for Node.js by WebAssembly
47 stars 26 forks source link

How to recover keys using bls-wasm. #6

Closed adyvaishnav closed 4 years ago

adyvaishnav commented 4 years ago

Is there any way in bls-wasm to recover keys from a mnemonic/seed. I saw the bls-go package also and there I found, we can recover keys like this:

seed := bip39.NewSeed(b0.Mnemonic,'Some random string')
r := bytes.NewReader(seed)
bls.SetRandFunc(r)

Currently, I am generating keys like this:

const blsSecret = new bls.SecretKey();
blsSecret.setByCSPRNG()
const key = blsSecret.getPublicKey().serializeToHexStr();
const id = sha3.sha3_256(utils.hexStringToByte(key));
const sKey = blsSecret.serializeToHexStr();

but not sure, how we can use mnemonic to get/recover the keys?

herumi commented 4 years ago

Please use deserializeHexStr. cf. https://github.com/herumi/bls-wasm/blob/master/test.js#L34-L53

adyvaishnav commented 4 years ago

Hi Herumi, I went through the solution you suggested but still I am not sure, how we are using the mnemonic or seed for generating the keys(public and private keys). Can you give me an example where you generate the same keys with a mnemonic. Let's say, we have this mnemonic: gloom member visual join oxygen velvet major mail zone select couch luxury

Where can I put this mnemonic in my code to get the same keys if I use the same mnemonic:

const blsSecret = new bls.SecretKey();
blsSecret.setByCSPRNG()
key = blsSecret.getPublicKey().serializeToHexStr();
sKey = blsSecret.serializeToHexStr();
herumi commented 4 years ago

how we are using the mnemonic or seed for generating the keys(public and private keys).

This library does not provide a feature generating a secret from a password. Please use PBKDF2 such as https://www.npmjs.com/package/pbkdf2 .

const pbkdf2 = require('pbkdf2')
const key = pbkdf2.pbkdf2Sync('password', 'salt', 100000, 32, 'sha256')
const sec = new bls.SecretKey()
sec.setLittleEndianMod(key)
pub = sec.getPublicKey()
herumi commented 4 years ago

I failed to understand your question. I'm sorry.

Is there any way in bls-wasm to recover keys from a mnemonic/seed.

The current version does not support the feature. I'll add the function to set crypto.getRandomValues in bls.js by user-random-generator.

Please wait a moment.

herumi commented 4 years ago

I've added bls.setRandFunc(f) at https://github.com/herumi/bls-wasm/commit/b326c3fa8bb13abb58e4b3db7752e0cc9d45bc5f , where f is a function that takes an array and fills it with random numbers.

adyvaishnav commented 4 years ago

I've added bls.setRandFunc(f) at b326c3f , where f is a function that takes an array and fills it with random numbers.

Can you give an example, how this can be used to generate keys (public and private) with a mnemonic/seed?

herumi commented 4 years ago

What module do you want to use for mnemonic/seed?

herumi commented 4 years ago

For example, for bip39, I think that it is better to use setLittleEndianMod instead of setRandFunc.

const bip39 = require('bip39')
const m = bip39.generateMnemonic()
const sec = new bls.SecretKey()
sec.setLittleEndianMod(bip39.mnemonicToSeedSync('abc'))
const pub = sec.getPublicKey()
KrishnaDeqode commented 4 years ago

Thanks for the solution herumi. Actually we will be having mnemonic as a string given by the user, We are implementing recover wallet functionality. Yes we will be using bip39 to generate the mnemonic.

adyvaishnav commented 4 years ago

Thanks Herumi, the solution is working for me.