Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to recover private key of web3 wallet? #2257

Closed
anjalialance opened this issue Jan 29, 2019 · 5 comments
Closed

How to recover private key of web3 wallet? #2257

anjalialance opened this issue Jan 29, 2019 · 5 comments

Comments

@anjalialance
Copy link

anjalialance commented Jan 29, 2019

Hello, I am creating wallet using web3 web3js.eth.accounts.create(); for mnemonics. I am unable to recover the same private key for the same mnemonics. I tried hdkey but unable to proceed transaction. I also used extended private key derived from hdkey but unable to proceed transaction. Please guide me.

Thanks in advance.

`app.get('/send_tx', (req, res,err) => {

var myAddress = '0x6486b5073Ab7EFe3741acE325295B31556686c3f';

//this private key is from mnemonics using hdkey package
var privateKey = Buffer.from('xprv9s21ZrQH143K4YTuEb1TZ6uxTNCGz9GZNEexbPn2rTWLciD9j6amMVaLSACgFHjeAdom7X5r9FCHykvMJUiiHfSxjPV928Uyq8dUdsnVaEw', 'hex');

const wal = HDKey.fromExtendedKey(privateKey);
const w = wal.deriveChild(0).getWallet();

const childPK = w.getPrivateKey();

console.log(childPK);

var toAddress = '0xb2cB609010ac2cDA463758ce0B6f420AbDeDC0ee';

var contractABI ="[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"version","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":false,"stateMutability":"nonpayable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]"

const user = JSON.parse(contractABI);

var contractAddress = '0x9Bd3F09eda3aF694373faF154Bc74ebEb62d3d42';
var contract = new web3js.eth.Contract(user,contractAddress);
var count;
web3js.eth.getTransactionCount(myAddress).then(function(v){
console.log("Count: "+v);

count = v;

var amount = web3js.utils.toHex(1);

var rawTransaction = {
nonce: web3js.utils.toHex(count),
to: toAddress,
value: web3js.utils.toHex(web3js.utils.toWei('0.001', 'ether')),
gasLimit: web3js.utils.toHex(21000),
gasPrice: web3js.utils.toHex(web3js.utils.toWei('10', 'gwei')),
}
console.log(rawTransaction);

var transaction = new Tx(rawTransaction);

transaction.sign(childPK);

web3js.eth.sendSignedTransaction('0x'+transaction.serialize().toString('hex'),(err,hash)=>{

         if(hash){
              let student = {  
                status:1,
                txhash: hash,

            };let data = JSON.stringify(student);  
                 res.send(data);
            }else if(err){
              
               let student1 = {  
                status:0,
                message:err,

            };let data1 = JSON.stringify(student1);  
                 res.send(data1);
              
              
                // res.send("somethings went wrong");
                // console.log(Error.error);
                 console.log(err);

            }
       
         //web3js.eth.getTransaction(hash)
       
    })
         .on('transactionHash');

          web3js.eth.getTransactionCount(myAddress,console.log)

  });

});`

@dev1644
Copy link

dev1644 commented Jan 29, 2019

web3js.eth.accounts.create() doesn't give you a mnemonic sequence, instead it gives a wallet object.
This wallet object contains -:

  • Address/PrivateKey - Keypair.
  • signTransaction - Function to sign your transaction with Wallet's private key.
  • sign - Function to sign your message with Wallet's private key.
  • encrypt - Function to sign your data with Wallet's private key.

@anjalialance
Copy link
Author

Hii,

Thanks for reply.

I am getting Address/Privatekey using web3js.eth.accounts.create() for mnemonic.But I want same private key again for transaction as i do not want to save the private key in my db.

@dev1644
Copy link

dev1644 commented Jan 30, 2019

web3js.eth.accounts.create() give you a newly generated Wallet each time.
If you don't want to save private keys into Database and derive it the runtime use following steps -:

  • Generate a seed from your mnemonic sequence bip39.mnemonicToSeed('Your Mnemonic Sequence').
  • Get your HDkey by HDKey.fromMasterSeed(new Buffer(seed, 'hex')) .
  • Set path for getting multiple deterministic key pairs ethPath = m/44'/60'/0'/0/${Your Unique Number}& fetch private-key from this path_ethkey = eth.derive(ethPath)`.
  • Use web3 library to convert your private-key into wallet, web3.eth.accounts.privateKeyToAccount(_ethkey._privateKey)
  • You will have your runtime wallet object, that you can use it.

@nivida
Copy link
Contributor

nivida commented Jan 31, 2019

Closed this issue because the issue template was not used.

@nivida nivida closed this as completed Jan 31, 2019
@italoHonoratoSA
Copy link

Web3.js is much more complicated when it comes to handling accounts and extracting private keys through mnemonic.

Check my reviews here:
#1594 (comment)

Check a solution here:
https://ethereum.stackexchange.com/questions/102090/retrieving-the-private-public-key-with-web3js-for-a-specific-wallet-provided-by

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants