First of all we will be using ethereumjs-lib in this post. You can install it from npm
npm install ethereumjs-lib or directly from git
npm install git+https://github.com/ethereum/ethereumjs-lib
Creating a transaction
After that let create a new file and put the following in it. For the those how are cut and paste handicap here is the full example.
Hopefull this is pretty self explanatory.
nonce is the number of transaction that the sending account has sent.
gasPrice is the amount you are will to pay for gas.
gasLimit if the amount of gas you are will to spend.
to is the address we are sending to, in this case the zero address which creates a new contract. If we where to put a valid address here this account would become a message instead of a contract. To check which type of transaction you have, you can do
value is the is the amount you are sending and
data is data that you are sending in this case it is initialization code for a contract.
The data is a Name Registrar contract. You can use one of the compilers to generate the code. The two main ones are lllc which is compiled with cpp-ethereum and serpent. Ok now we have a transaction with data. Now we need to sign it. To do this we will need a private key. There are several way to acquire one but for now we are just going to steal one from AlethZero. That way I know that I’m create valid transaction that actually has the ether that this transaction is says that it has. If you have AlethZero running. You can select
tools>export selected key, and the copy the private key out. Here is mine.
Now we have a signed transaction, but for it to be valid, the account that we signed it with needs to have a certain amount of wei in to. To see how much that this account needs we can use
this gives the amount in wei that the account needs to have. If your wondering how this is caculated it is
- data lenght in bytes * 5
- + 500 Default transaction fee
- + gasLimit * gasPrice
Lets serialize the transaction
Now that we have the serialized transaction we can get AlethZero to except it by selecting
debug>inject transaction and pasting in the transaction serialization. It should then show up in the pending transaction pane.
Parsing & Validating transactions
If you have a transaction that you want to verify you can parse it. If you got it directly from the network it will be rlp encoded. You can decode it using the rlp module. After decoding it that you should have something like this.
rlp.decode will actully produce an array of buffers
new Transaction will take either and array of buffers or and array of hex strings. So assuming that you were able to parse the transaction, we will now get the sender’s address
Cool now we know who sent the tx! Lets verify the signature to make sure it not some poser.
And hopefull its verified. For the transaction to be total valid we would also need to check the account of the sender and see if they have at least
And that is all for now I hope you have enjoyed the read. You can view the documentation for transaction here to see all the exciting things it can do. Let me know if you have any questions or comments.
spare a few bits?