ethereumjs / ethereumjs-tx

Project is in active development and has been moved to the EthereumJS VM monorepo.
https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/tx
Mozilla Public License 2.0
779 stars 235 forks source link

Can not send transactions #122

Closed gasparyanyur closed 5 years ago

gasparyanyur commented 5 years ago

I have a online shop where users can recieve ETH and do shopping. So to have a secure wallet, I have created an offline service which generates xpub and xpriv which I use to generate addresses in my online service.

I am using xpub in my online service to genreate addresses for users and using xpriv in my second offline service to send transactions.

Here is a code which generates xpub

const hdkey = require('ethereumjs-wallet/hdkey');
const bip39 = require('bip39');

var seed = bip39.mnemonicToSeed('my_mnemonic_phrase', 'my_password');

var chain = hdkey.fromMasterSeed(seed);

var xpub = chain.publicExtendedKey();

var xpriv = chain.privateExtendedKey();

Than using xpub in my online service I am generating addresses for users

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();
};

So all is ok, but I in my offline service I also have a service which should send all coins to my own account from generated addresses.

Here is my second service

onst 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: my_address,
    value: '0.1',
    gasPrice: 5000000000,
    gasLimit: 21000,
    chainId: 3
};

const privKey = new Buffer('xprv_MY_XPRIV_KEY','hex');

const tx = new ethTx(params);
//Signing the transaction with the correct private key
tx.sign(privKey);

const serializedTx = tx.serialize();

Web3.eth.sendSignedTransaction(
    `0x${serializedTx.toString('hex')}`,
    (error, result) => {
        if (error) { console.log(`Error: ${error}`); }
        else { console.log(`Result: ${result}`); }
    }
);

But in this case I am getting an error look like this

private key length is invalid

I am using xpriv as a private key . Is this ok ? or where my mistake ? What I need to add/change to send transactions?

Any suggestions would be much appreciate.

danjm commented 5 years ago

@gasparyanyur It looks like you are using the extended private key instead of the private key. You can learn a little more about the difference here: https://bitcoin.stackexchange.com/questions/50028/difference-between-master-public-private-key-and-public-private-key

The ethereumjs-tx sign method ultimately calls the secp256k1 sign method (https://github.com/cryptocoinjs/secp256k1-node/), which requires the private key to be 32 bytes in length.

holgerd77 commented 5 years ago

@danjm Thanks for figuring this out. 👍 Will close this now.