1

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.

2
  • Try logging the private key to console, just for testing, and see if the length matches what's expected.
    – WBT
    Commented Oct 26, 2018 at 19:18
  • the length of private key is 111. may I use xpriv as private key ? Commented Oct 26, 2018 at 23:56

1 Answer 1

1

With your private key you cannot sign transaction for the child address. You have to derive a wallet for the child address first.

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

const wal = hdkey.fromExtendedPrivateKey(privKey)
const w = wal.deriveChild(index).getWallet()

const childPK = w.getPrivateKey()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.