Skip to content

Latest commit

 

History

History
101 lines (72 loc) · 4.7 KB

creating-transactions.md

File metadata and controls

101 lines (72 loc) · 4.7 KB

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.

Creating a new identity

An identity is an object with a privateKey and the corresponding publicKey and its address. To create a fresh identity, the function createIdentity is called which returns one.

const EthCrypto = require('eth-crypto');

const identity = EthCrypto.createIdentity();

console.dir(identity);
/* > {
    address: '0x3f243FdacE01Cfd9719f7359c94BA11361f32471',
    privateKey: '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',
    publicKey: 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...'
} */

When we code things in the ethereum ecosystem, it is standard to represent data as hex-strings. You can see that hex-strings start with a 0x which marks them as so.

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 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 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.

Creating a transaction

An ethereum-transaction is basically a json-object with defined values. Lets create one where we send one ether to another address.

    const rawTransaction = {
        from: identity.address, // sender address
        to: '0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0', // reciever address
        value: 1000000000000000000, // amount of wei we want to send (= 1 ether)
        nonce: 0, // incremental tx-number. Add +1 for every transaction you do
        gasPrice: 5000000000,
        gasLimit: 21000 // normal gasLimit for code-less transactions
    };

Before the transaction can be submitted to an ethereum-node, it must be signed with the privateKey and serialized to a hex-string.

    const serializedTx = EthCrypto.signTransaction(
        rawTransaction,
        identity.privateKey
    );
    console.log(serializedTx);
    // > 'f86c808504a817c80082ea609463dcee1fd1d814858acd4172bb20e1...'

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 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.

Creating the local testnet

To create a local testnet, we will use the ganache-cli and connect it to a web3-instance so we can interact with it.

const Web3 = require('web3');
const ganache = require('ganache-cli');

// create a web3-instance
const web3 = new Web3();

// create a ganache-provider
const ganacheProvider = ganache.provider({
    // we preset the balance of our identity to 10 ether
    accounts: [{
        secretKey: identity.privateKey,
        balance: web3.utils.toWei('10', 'ether')
    }]
});

// set ganache to web3 as provider
web3.setProvider(ganacheProvider);

Submitting the transaction

Call sendSignedTransaction to submit the signed transaction to the testchain. Ganache will instantly mine the transaction and we get a receipt back.

    const receipt = await web3.eth.sendSignedTransaction(serializedTx);

To ensure that the transaction worked, check the balance of the receivers address.

    const balance = await web3.eth.getBalance('0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0');
    console.log(balance);
    // > '1000000000000000000'

Done

Awesome! You created your first ethereum-transaction.