# Tutorial: Creating Keys and use them for ethereum-transactions
In this tutorial we will create an ethereum-identity and use it so send transactions to the blockchain.
In this tutorial we will create an ethereum-identity and use it to send transactions to the blockchain.
## Creating a new identity
@ -23,8 +23,8 @@ When we code things in the ethereum ecosystem, it is standard to represent data
The identity consists of:
- The **privateKey** which must never be revealed to anyone. It can be used to sign and decrypt messages and to create it's publicKey.
- The **publicKey** is revealed whenever something is signed with the privateKey. It's also common to send the publicKey to other humans so that they can encrypt data with it, which then can only decrypted by the correct privateKey. It's important to know that there are [two ways to represent](https://github.com/bitpay/bitcore-lib/blob/master/docs/publickey.md) a publicKey compresssed and uncompressed. EthCrypto always creates the uncompressed key which starts with `0x04`. Compressed keys start with `0x03` or `0x02`. To represent the key, we strip the starting `04` away from it and internally add it when doing cryptographic calls.
- The **privateKey** which must never be revealed to anyone. It can be used to sign and decrypt messages and to create its publicKey.
- The **publicKey** is revealed whenever something is signed with the privateKey. It's also common to send the publicKey to other humans so that they can encrypt data with it, which then can only decrypted by the correct privateKey. It's important to know that there are [two ways to represent](https://github.com/bitpay/bitcore-lib/blob/master/docs/publickey.md) a publicKey compressed and uncompressed. EthCrypto always creates the uncompressed key which starts with `0x04`. Compressed keys start with `0x03` or `0x02`. To represent the key, we strip the starting `04` away from it and internally add it when doing cryptographic calls.
- The **address** is calculated from the last 20 bytes of the keccak-256-hash of the publicKey. It is used to represent an identity. Notice that there is no way to calculate the publicKey from an address. This means that whenever we want to encrypt data for someone, we first have to get the publicKey. There are two ways to represent an address. The normal address is lowercase and represents just the 20 bytes of the hash. The checksum-format contains uppercase-letters which the purpose of detecting errors when the address is entered manually.
@ -35,7 +35,7 @@ An ethereum-transaction is basically a json-object with defined values. Lets cre
```javascript
const rawTransaction = {
from: identity.address, // sender address
to: '0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0', // reciever address
to: '0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0', // receiver address
value: new BN('1000000000000000000'), // amount of wei we want to send (= 1 ether)
nonce: 0, // incremental tx-number. Add +1 for every transaction you do
gasPrice: 5000000000,
@ -54,7 +54,7 @@ Before the transaction can be submitted to an ethereum-node, it must be signed w
Now the transaction-string could be submitted to the blockchain. If we really wanted to send the value, we could do this by either send it to a public node like [etherscan](https://etherscan.io/pushTx) or by pushing it to our local node. For testing-purposes it is usual to create a local test-chain and try things out there.
Now the transaction-string could be submitted to the blockchain. If we really wanted to send the value, we could do this by either sending it to a public node like [etherscan](https://etherscan.io/pushTx) or by pushing it to our local node. For testing-purposes it is usual to create a local test-chain and try things out there.
With ethereum-keys you cannot only interact with the blockchain, but also use them to send messages over mutual untrusted channels in a secure way. In this tutorial we will use ethereum-identites to send messages like you would do in an decentralized chat-app.
With ethereum-keys you cannot only interact with the blockchain, but also use them to send messages over mutual untrusted channels in a secure way. In this tutorial we will use ethereum-identities to send messages like you would do in a decentralized chat-app.
## Prerequisites
First we create two identities, `Alice` and `Bob`. In our case `Alice` want to send the message `My name is Satoshi Buterin` to `Bob`.
First we create two identities, `Alice` and `Bob`. In our case `Alice` wants to send the message `My name is Satoshi Buterin` to `Bob`.
## Create a smart-contract that can validate signatures
Lets create an example-contract. The contract will be a donation-bag which contains some ether and has an owner. Whenever someone submits a valid donation-signature, he recieves a part of the contracts value. This allows the creator of the contract to give signed data to people **off-chain** which they can later use to claim the value **on-chain**.
Let's create an example-contract. The contract will be a donation-bag which contains some ether and has an owner. Whenever someone submits a valid donation-signature, he receives a part of the contracts value. This allows the creator of the contract to give signed data to people **off-chain** which they can later use to claim the value **on-chain**.
Write the contracts code in a file called `DonationBag.sol`. **Check out it's content [here](../contracts/DonationBag.sol)**.
Write the contracts code in a file called `DonationBag.sol`. **Check out its content [here](../contracts/DonationBag.sol)**.
As you can see, the contract has some methods:
- **DonationBag()**: The constructor which is called when the contract is created. Here we set the owner of the DonationBag
- **default-function**: The default function is called when we send ether to the contract without doing anything. This is needed so the contract can recieve value.
- **default-function**: The default function is called when we send ether to the contract without doing anything. This is needed so the contract can receive value.
- **prefixedHash()**: Creates a hash of the data which must be signed by the creator.
- **isSignatureValid()**: Checks if a given signature is really signed by the sender and contains the correct content.
- **recieveDonation():** This is called by the receiver when the donation is claimed.
As you can see, we did not sign the reciever-address directly but a hash that was build of some concated data:
- **Prefix:** To ensure the creator cannot be tricked into accidentially singing a valid ethereum-transaction, we prefix the signed data with something unique to our system. In this case lets take the string `Signed for DonationBag:`.
- **contractAddress:** It might be possible that the creator has more than one instance of the contract deployed to the blockchain. In this case it's signatures might be replayed to other instances. As prevention of this attack, we also add the contracts address to the signed hash.
- **receiverAddress:** By signing this address, the creator proves that the given address should recieve the donation.
- **Prefix:** To ensure the creator cannot be tricked into accidentally singing a valid ethereum-transaction, we prefix the signed data with something unique to our system. In this case lets take the string `Signed for DonationBag:`.
- **contractAddress:** It might be possible that the creator has more than one instance of the contract deployed to the blockchain. In this case its signatures might be replayed to other instances. As prevention of this attack, we also add the contracts address to the signed hash.
- **receiverAddress:** By signing this address, the creator proves that the given address should receive the donation.
## Recover the signature on the blockchain
The reciever now has a signature from the creator which he can send to the contract to claim the donation.
The receiver now has a signature from the creator which he can send to the contract to claim the donation.