Consensys / eth-lightwallet

Lightweight JS Wallet for Node and the browser
MIT License
1.47k stars 503 forks source link

New Wallet receive eth & token but can't send #189

Open Kotomanga opened 6 years ago

Kotomanga commented 6 years ago

Hello,

i made some experimentations with ethereum & smart contract on Ganache(testRpc). Every thing works corectly, i can create token, send ether ... now i want to create a wallet (on demand) to work with my contract. the wallet can receive ether & token, when a call the balance of thisnew wallet it's return sorme value.

But wen i try to send ether or token from my new generated wallet, i had a json RPC error

{ "error": "Invalid JSON RPC response: {\"id\":3,\"jsonrpc\":\"2.0\"}" }

this my code for sending transaction, i use ethereumjs-tx

        const privateKeyBuff = Buffer.from(privateKey, 'hex');
        const rawTx = {
          nonce: web3.toHex(web3.eth.getTransactionCount(web3.eth.coinbase)),
          from: callerAddress,
          to: contractAddress,
          gasPrice: gasPrice.toString(10),
          gasLimit: gasLimit, // default gas limit to send ether
          chainId: config.eth.chain_id,
          data: methodRef,
        };

        const tx = new Tx(rawTx);

        if (gasLimit === 'auto') {
          console.log('Auto calculating gas limit...');
          gasLimit = web3.eth.estimateGas(rawTx);
          console.log('Estimated gas: ' + gasLimit);
          tx.gasLimit = gasLimit;
        }

        tx.sign(privateKeyBuff);

        const serializedTx = tx.serialize();

        web3.eth.sendRawTransaction(
          '0x' + serializedTx.toString('hex'),
          (error, txHash) => {
            if (error) return reject(error);

            result.tx_hash = txHash;

            return resolve(result);
          }
        );
Grashias commented 6 years ago

Hi convert each parameters to hex in the const rawTx and then try signing the eaw transaction, it will work.

barrard commented 6 years ago

web3.toHex() < version 1.0

web3.utils.toHex() >= version 1.0