-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Comments
|
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. |
|
Closed this issue because the issue template was not used. |
Web3.js is much more complicated when it comes to handling accounts and extracting private keys through mnemonic. Check my reviews here: Check a solution here: |
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 triedhdkey
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)=>{
});`
The text was updated successfully, but these errors were encountered: