14

I want to create a signed transaction using C#, which will later be used by Nethereum via JSON-RPC to call eth_sendRawTransaction.

I have the raw transaction and the private key, now how do I sign the data with the key?

3
  • Not sure if this helps but I asked something similar here: ethereum.stackexchange.com/questions/3386/…
    – Nico
    May 11, 2016 at 15:05
  • @OriMarron How did you run ethereumjs-tx in the .NET environment?
    – bozzle
    Jul 19, 2016 at 2:40
  • It's quite dirty, I loaded phantomJS using Selenium, which allowed me to simulate a browser to call the JS code. A less dirty solution could be to use a nodeJS server
    – Ori Marron
    Jul 20, 2016 at 8:32

2 Answers 2

7

To expand the other response, to provide offline transaction signing in Nethereum you can do the following:

First, you will need your private key, and sender address. You can retrieve the sender address from your private key using Nethereum.Core.Signing.Crypto.EthECKey.GetPublicAddress(privateKey); if you only have the private key.

var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7"; var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";

Now using web3 first you will need to retrieve the total number of transactions of your sender address.

var web3 = new Web3(); var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(senderAddress);

The txCount will be used as the nonce to sign the transaction.

Now using web3 again, you can build an encoded transaction as following:

var encoded = web3.OfflineTransactionSigning.SignTransaction(privateKey, receiveAddress, 10, txCount.Value);

If you need to include the data and gas there are overloads for it.

You can verify an encoded transaction: Assert.True(web3.OfflineTransactionSigning.VerifyTransaction(encoded));

Or get the sender address from an encoded transaction:

web3.OfflineTransactionSigning.GetSenderAddress(encoded);

To send the encoded transaction you will "SendRawTransaction"

var txId = await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + encoded);

You can see this example on the unit test

Or a "real" implementation on the Game sample

2
  • hey Juan, your unit test link got outdated :) you should use a git-tag-based link ;)
    – knocte
    May 7, 2017 at 6:19
  • do you have example with curl + json rpc. If yes please share Nov 6, 2017 at 10:10
6

Nethereum now supports offline transaction signing, so there is no longer a need to use EdgeJs or similar to run node scripts inside the .NET environment.

Your Answer

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

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