Estoy tratando de hacer un hd wallet
basado en nodejs
. Entonces, para tener una billetera segura, he creado una billetera en mi máquina fuera de línea
const hdkey = require('ethereumjs-wallet/hdkey');
const bip39 = require('bip39');
var seed = bip39.mnemonicToSeed('my_mnemonic', 'my_password');
var chain = hdkey.fromMasterSeed(seed);
var xpub = chain.publicExtendedKey();
var xpriv = chain.privateExtendedKey();
Que usando xpub
estoy generando direcciones para los usuarios.
const wallet = require('ethereumjs-wallet');
const hdkey = require('ethereumjs-wallet/hdkey');
const _____ = require('dotenv').config();
const generate = async (index) => {
const xpub = process.env.ETH_XPUB;
const hdk = hdkey.fromExtendedKey(xpub);
index = index || 0;
const child = hdk.deriveChild(index);
const w = wallet.fromExtendedPublicKey(child.publicExtendedKey());
return w.getAddressString();
};
generate().then(v => {
console.log(v);
}).catch(error => {
console.log(error);
});
Ok, ahora quiero enviar monedas desde estas direcciones a mi propia dirección usando una clave privada extendida:xpriv
const ethTx = require('ethereumjs-tx');
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('http://localhost:8545')
);
//Verify connection is successful
web3.eth.net.isListening()
.then(() => console.log('is connected'))
.catch(e => console.log('Wow. Something went wrong'));
const params = {
nonce: 0,
to: '0x4584158529818ef77D1142bEeb0b6648BD8eDb2f',
value: '0.1',
gasPrice: 5000000000,
gasLimit: 21000,
chainId: 3
};
const privKey = new Buffer('xprv9s21ZrQH143K36eJpjZxNPYbNjzzUMvqLSG6P6Sex1unL8meREsNkcMNRw4ntv3WjJ2tJJX3CrdxkCJdyo7zLnjqdFppov2BAvY4iXYuMCY','base64');
const tx = new ethTx(params);
//Signing the transaction with the correct private key
tx.sign(privKey); //ERROR HERE
const serializedTx = tx.serialize();
Web3.eth.sendSignedTransaction(
`0x${serializedTx.toString('hex')}`,
(error, result) => {
if (error) { console.log(`Error: ${error}`); }
else { console.log(`Result: ${result}`); }
}
);
Cuando intento iniciar sesión, recibo un error de que la clave privada no es válida. Qué clave privada necesito usar para firmar transacciones.
No firma la transacción con la clave privada maestra. Lo firmas con la clave privada que corresponde a la dirección del remitente:
const hdkey = require('ethereumjs-wallet/hdkey');
const bip39 = require('bip39');
var seed = bip39.mnemonicToSeed('my_mnemonic', 'my_password');
var chain = hdkey.fromMasterSeed(seed);
var addr_node = chain.derivePath(senders_derivation_path);
//if addr is the same with the address you want to send from, then its corresponding private_key will also be valid. Otherwise, most probably the senders_derivation_path is wrong
var addr = addr_node.getWallet().getAddressString();
var private_key = addr_node.getWallet().getPrivateKey();
Marco Ottolini
Yur Gasparyan