4

I am trying to create a client that signs its own transactions.

To keep it simple, I have stripped everything that is not absolutely required from the example code. Hence the hard coded private key and addresses and gasLimit.

this function demonstrates the transaction:

import Tx from 'ethereumjs-tx';

export const executeTx = (web3) => {
    let privateKey = "fff7b3e0508db1b9059c7542e6287c5ff8013413dd9ced1ca50dc86916c0f353";
    let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000);

    var rawTx = {
        nonce: nonce,
        gasPrice: web3.toHex(web3.eth.gasPrice),
        gasLimit: web3.toHex(22000),
        to: '0xc347f6b2613e02f5d676f0cdc3215035cca4ecb1',
        from: '0x77454e832261aeed81422348efee52d5bd3a3684',
        value: '0x12',
        data: undefined,
    };

    let tx = new Tx(rawTx);
    tx.sign(Buffer.from(privateKey, 'hex'));
    let signedTxString = tx.serialize();

    web3.eth.sendRawTransaction('0x' + signedTxString.toString('hex'), function (err, hash) {
        if (err) {
            console.log(err);
        } else {
            console.log('transaction hash is', hash);
        }
    });
}

the result of this function is a transaction hash and no error. In the log of my local geth node I can see a strange entry which explains why the transaction does not actually result in a transfer:

Tx(127f1a8ecb09f2380b54f9facd5b4102b3b9d1907065661ebc59cad6531fec22) to: &c347f6b2613e02f5d676f0cdc3215035cca4ecb1

notice the & instead of the usual 0x at the beginning of the to address and the missing 0x at the beginning of the Tx value.

This is the log entry for a successful transaction:

Tx(0xa711dfdc9c74b4de63bf9de272fb693507f4566bababfb0550aecb6f5a9c2181) to: 0x196f3a37dc3c7b378f504201545971e9d9b3d466

for completeness, here is a version of the code that uses eth-lightwallet to do the same thing and strangely enough with the same result.

import {signing} from 'eth-lightwallet';
import {keystore} from 'eth-lightwallet';
import {txutils} from 'eth-lightwallet';
import W3 from 'web3';

export const executeTxLW = () => {
    let provider = new W3.providers.HttpProvider('http://localhost:8545');
    let web3 = new W3(provider);

    let privateKey = "fff7b3e0508db1b9059c7542e6287c5ff8013413dd9ced1ca50dc86916c0f353";
    let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000);

    keystore.createVault({
        password: 'ppp',
        seedPhrase: "address wealth torch panel aspect brief edit found invite dumb build neither",
        salt: "jpOdt6rKmZuEEKWDVhdFLaudYweyck+6fZPjJSzRcgc="
    }, (err, ks) => {
        if (err) {
            console.log(err);
            return;
        }

        ks.keyFromPassword('ppp', (err, pwDerivedKey) => {
            if (err) {
                console.log(err);
                return;
            }
            ks.generateNewAddress(pwDerivedKey);
            console.log(ks.getAddresses()[0]); //should be 0x77454e832261aeed81422348efee52d5bd3a3684

            var rawTx = {
                nonce: nonce,
                gasPrice: web3.toHex(web3.eth.gasPrice),
                gasLimit: web3.toHex(22000),
                to: '0xD0362fa02fE45367fd7b5fd126ee94a42a57D779',
                from: '0x77454e832261aeed81422348efee52d5bd3a3684',
                value: web3.toHex(web3.toWei(1, "ether")),
                data: undefined,
            };

            let rawTxString = txutils.valueTx(rawTx);
            let signedTxString = signing.signTx(ks, pwDerivedKey, '0x' + rawTxString, '0x77454e832261aeed81422348efee52d5bd3a3684');

            web3.eth.sendRawTransaction('0x' + signedTxString.toString('hex'), function (err, hash) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('transaction hash is', hash);
                }
            });
        });
    });
}

could this be an issue with geth?

3

the issue was actually the computing of the nonce

it appears that when the nonce is too high geth drops the transaction silently.

I wanted to make sure that my nonces would not conflict by adding a lot to the nonce:

let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000)

this turns out to be an error.

it also appears that the & in the log is not a sign of error. I continue to get the to address prefixed with an & even now that my transactions go through.

1
  • 1
    I believe only the nonce directly following in sequence (i.e., 3 after 2) is even executable. Otherwise, misordered transactions would prevent earlier transactions from even occurring. Feb 19 '17 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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