0
const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
const web3 = new Web3('https://ropsten.infura.io/161d7c8443f14babb3c176023628a055');


// console.log(web3.eth.accounts.create())


var account = '0xc3834D08D507CdaC564E9fa8695095D3bE4D289C';
var rawKey = 'DB06DFAD4137CB583C8AD821ACCB12AA6CFC8055E012AA6593AE95EXXXX'
var privateKey = Buffer.from(rawKey,'hex');

web3.eth.getTransactionCount(account, (err,txCount)=>{
    //smart Contract Data
    var data = '0x6080604052600160005560006001553480156200001b57600080fd5b506080604051908101604052806040805190810160405280600581526020017f61726a756e00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600881526020017f746f70733f31323300000000000000000000000000000000000000000000000081525081526020013373ffffffffffffffffffffffffffffffffffffffff16815260200160011515815250600260008081526020019081526020016000206000820151816000019080519060200190620000f292919062000182565b5060208201518160010190805190602001906200011192919062000182565b5060408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060608201518160020160146101000a81548160ff02191690831515021790555090505062000231565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620001c557805160ff1916838001178555620001f6565b82800160010185558215620001f6579182015b82811115620001f5578251825591602001919060010190620001d8565b5b50905062000205919062000209565b5090565b6200022e91905b808211156200022a57600081600090555060010162000210565b5090565b90565b610f7d80620002416000396000f3fe60806040526004361061007d576000357c010000000000000000000000000000000000000000000000000000000090048063174e3616146100825780639371de2b146100bf578063a0d09221146100fc578063af598f9c14610125578063b3f40d6a14610162578063c32c869b1461018b578063e2842d79146101b4575b600080fd5b34801561008e57600080fd5b506100a960048036036100a491908101906109da565b6101e0565b6040516100b69190610d43565b60405180910390f35b3480156100cb57600080fd5b506100e660048036036100e191908101906109da565b6102d2565b6040516100f39190610d65565b60405180910390f35b34801561010857600080fd5b50610123600480360361011e9190810190610a3f565b610368565b005b34801561013157600080fd5b5061014c60048036036101479190810190610b26565b610431565b6040516101599190610d80565b60405180910390f35b34801561016e57600080fd5b5061018960048036036101849190810190610a03565b61052c565b005b34801561019757600080fd5b506101b260048036036101ad9190810190610a93565b610585565b005b3480156101c057600080fd5b506101c9610689565b6040516101d7929190610d0c565b60405180910390f35b6060806001546040519080825280602002602001820160405280156102145781602001602082028038833980820191505090505b509050600080905060008090505b6001548110156102c7578473ffffffffffffffffffffffffffffffffffffffff166003600083815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614156102ba578083838151811015156102a557fe5b90602001906020020181815250506001820191505b8080600101915050610222565b508192505050919050565b60008060009050600054811015610361578273ffffffffffffffffffffffffffffffffffffffff166002600083815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610357576001915050610363565b6000915050610363565b505b919050565b60606040519081016040528060015481526020018381526020018273ffffffffffffffffffffffffffffffffffffffff168152506003600060015481526020019081526020016000206000820151816000015560208201518160010190805190602001906103d79291906108a3565b5060408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555090505060018054016001819055505050565b606060008090505b60015481101561052557826003600083815260200190815260200160002060000154141561051857600360008281526020019081526020016000206001018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050b5780601f106104e05761010080835404028352916020019161050b565b820191906000526020600020905b8154815290600101906020018083116104ee57829003601f168201915b5050505050915050610527565b8080600101915050610439565b505b919050565b816003600083815260200190815260200160002060020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050565b6080604051908101604052808581526020018481526020018373ffffffffffffffffffffffffffffffffffffffff168152602001821515815250600260008054815260200190815260200160002060008201518160000190805190602001906105ef9291906108a3565b50602082015181600101908051906020019061060c9291906108a3565b5060408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060608201518160020160146101000a81548160ff02191690831515021790555090505060016000540160008190555050505050565b60608060606000546040519080825280602002602001820160405280156106bf5781602001602082028038833980820191505090505b50905060606000546040519080825280602002602001820160405280156106fa57816020015b60608152602001906001900390816106e55790505b50905060008090505b60005481101561089657600015156002600083815260200190815260200160002060020160149054906101000a900460ff1615151415610889576002600082815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16838281518110151561078257fe5b9060200190602002019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff1681525050600260008281526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156108685780601f1061083d57610100808354040283529160200191610868565b820191906000526020600020905b81548152906001019060200180831161084b57829003601f168201915b5050505050828281518110151561087b57fe5b906020019060200201819052505b8080600101915050610703565b5081819350935050509091565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106108e457805160ff1916838001178555610912565b82800160010185558215610912579182015b828111156109115782518255916020019190600101906108f6565b5b50905061091f9190610923565b5090565b61094591905b80821115610941576000816000905550600101610929565b5090565b90565b60006109548235610ec8565b905092915050565b60006109688235610eda565b905092915050565b600082601f830112151561098357600080fd5b813561099661099182610dcf565b610da2565b915080825260208301602083018583830111156109b257600080fd5b6109bd838284610ef0565b50505092915050565b60006109d28235610ee6565b905092915050565b6000602082840312156109ec57600080fd5b60006109fa84828501610948565b91505092915050565b60008060408385031215610a1657600080fd5b6000610a2485828601610948565b9250506020610a35858286016109c6565b9150509250929050565b60008060408385031215610a5257600080fd5b600083013567ffffffffffffffff811115610a6c57600080fd5b610a7885828601610970565b9250506020610a8985828601610948565b9150509250929050565b60008060008060808587031215610aa957600080fd5b600085013567ffffffffffffffff811115610ac357600080fd5b610acf87828801610970565b945050602085013567ffffffffffffffff811115610aec57600080fd5b610af887828801610970565b9350506040610b0987828801610948565b9250506060610b1a8782880161095c565b91505092959194509250565b600060208284031215610b3857600080fd5b6000610b46848285016109c6565b91505092915050565b610b5881610e80565b82525050565b6000610b6982610e22565b808452602084019350610b7b83610dfb565b60005b82811015610bad57610b91868351610b4f565b610b9a82610e59565b9150602086019550600181019050610b7e565b50849250505092915050565b6000610bc482610e2d565b80845260208401935083602082028501610bdd85610e08565b60005b84811015610c16578383038852610bf8838351610cc7565b9250610c0382610e66565b9150602088019750600181019050610be0565b508196508694505050505092915050565b6000610c3282610e38565b808452602084019350610c4483610e15565b60005b82811015610c7657610c5a868351610cfd565b610c6382610e73565b9150602086019550600181019050610c47565b50849250505092915050565b610c8b81610e92565b82525050565b6000610c9c82610e4e565b808452610cb0816020860160208601610eff565b610cb981610f32565b602085010191505092915050565b6000610cd282610e43565b808452610ce6816020860160208601610eff565b610cef81610f32565b602085010191505092915050565b610d0681610ebe565b82525050565b60006040820190508181036000830152610d268185610b5e565b90508181036020830152610d3a8184610bb9565b90509392505050565b60006020820190508181036000830152610d5d8184610c27565b905092915050565b6000602082019050610d7a6000830184610c82565b92915050565b60006020820190508181036000830152610d9a8184610c91565b905092915050565b6000604051905081810181811067ffffffffffffffff82111715610dc557600080fd5b8060405250919050565b600067ffffffffffffffff821115610de657600080fd5b601f19601f8301169050602081019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b600081519050919050565b600081519050919050565b600081519050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000610e8b82610e9e565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b6000610ed382610e9e565b9050919050565b60008115159050919050565b6000819050919050565b82818337600083830152505050565b60005b83811015610f1d578082015181840152602081019050610f02565b83811115610f2c576000848401525b50505050565b6000601f19601f830116905091905056fea265627a7a72305820a21d4db0e7825c74f8443c332683d69e773b0b83a6eec48a1a5d397e039b5cf66c6578706572696d656e74616cf50037';

    //create transction object..
    const txObject = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(1000000000),
        gasPrice: web3.utils.toHex(web3.utils.toWei('100','gwei')),
        // data: data,
    }

    console.log(txObject)
    // const txObject = {
    //     nonce: txCount,
    //     gasLimit: 1000000,
    //     gasPrice: 10000000,
    //     data: data,
    //     value:100000,
    //     chainId: 3
    // }

    // sign the transction..
    const tx = new Tx(txObject);
    tx.sign(privateKey);

    const serializedTx = tx.serialize();
    const raw = '0x' + serializedTx.toString();


    // console.log(raw)
    //broadcast the transction..
    web3.eth.sendSignedTransaction(raw).then(console.log).catch(console.log)
    // web3.eth.sendSignedTransaction(raw, (err,txHash)=>{
    //     console.log('err: ', err, 'txHash: ', txHash);
    //     //use this txhash to find the transction on etherscan;
    // })
})

--edit-- above code return an error cannot unmarshal hex string of odd length into Go value of type hexutil.Bytes

  • I am sorry. I could not understand what is the error you are facing. – Sanjay S B Jun 10 at 11:29
  • above code return an error cannot unmarshal hex string of odd length into Go value of type hexutil.Bytes? – Arjun Trivedi Jun 10 at 11:30
  • You can try out this link: github.com/ethereum/web3.js/issues/1268 – Sanjay S B Jun 10 at 11:37
  • you private key look weird – Majd TL Jun 10 at 13:38

Your Answer

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

Browse other questions tagged or ask your own question.