L3 Transactions

Transactions

 

Minicoin uses the same kind of transactions as Bitcoin does, except without scripting. You should be familiar with the concepts of public key cryptography and the UTXO (unspent transaction output) model to understand this chapter.

main.js

Note the new .utxos, .balance, .key, and .send commands. Using .send makes one transaction and starts trying to mine a block with it. In lesson 4 you’ll make a mechanism for multiple transactions per block.

txs.js

See the extensive comments in this module for details about the Minicoin transaction format.

 

A valid transaction has some signed inputs that spend some earlier transactions’ outputs.

 

The first output is the amount sent and the recipient’s public key.

 

The second output is the change left over after deducting the amount sent from the total input amounts, minus a burn of 1 per transaction and a fee of 1 per input used.

 

The burn prevents miners from attacking the network with huge spam blocks filled with redundant transactions to themselves. You wouldn’t need it in a private blockchain with trusted miners.

 

The fee prevents transaction spam and incentivises miners to include the transaction instead of mining faster, blank blocks. You wouldn’t need it in a private blockchain with trusted users.

 

A miner puts a special coinbase transaction at the end of their block. It has no inputs. It has one block reward output of 10 per block + the fees from the block’s other transactions. You wouldn’t need a block reward in a private blockchain with a big genesis block output.

 

Each transaction input specifies the transaction hash and index of the output that it spends. It also has the signature[1] made with the private key of its spent output’s public key.

 

It would be wasteful to rescan the blockchain every time you wanted to make or validate a transaction input. Minicoin keeps an internal list of UTXOs, which are a “running total” of usable outputs. The UTXO set is for performance only – it isn’t shared with other peers, and you could write a fully-compatible Minicoin client without it.

 

addTx(tx, block, UTXOs) validates a transaction’s signed inputs against a UTXO set, adds it to the referenced block, deletes its spent UTXOs, and adds its output UTXOs to the referenced UTXO set.

 

addCoinbase(coinbase, block, UTXOs) validates and adds a coinbase as the last transaction in a referenced block and adds the coinbase’s output to the referenced UTXO set.

wallet.js

This module makes new transactions with your local keypair.

 

makeTx(amountSent, publicKey, UTXOs) returns a new transaction spending from your UTXOs to another public key. It returns false, if your balance is too low. You will need to implement something like this:

 

inputN = {

hash: <the previous tx’s hex hash>,

index: <the spent output’s index in the previous outputs array, 0 or 1>

};

 

output_sent = {

publicKey: <the recipient’s compressed hex public key>,
amount: <integer amount sent>

};

output_change = {
publicKey: <your compressed hex public key>,
amount: <total inputs – amount sent – 1 burn – 1*<n inputs> fee>

};

 

tx = {

inputs: [input0, input1, …],

outputs: [output_sent, output_change]

};

 

tx.hash = SHA256().update(JSON.stringify(tx)).digest(‘hex’);

 

for (const input of tx.inputs) {

input.signature = keyPair.sign(tx.hash, ‘hex’).toDER(‘hex’);

}

 

Note that the inputs and outputs are made, then the transaction hash is made, then the inputs are signed.

miner.js

mineBlock(txs, publicKey, chain) will also include a coinbase with you as the beneficiary.

blocks.js

There’s now a genesis coinbase.

 

exports.UTXOs is the local UTXO set for all previous blocks. Don’t modify this until you’re sure a new block’s transactions are valid!

 

addBlock(block, chain, UTXOs) now calls the Txs module to validate its transactions. It also updates the referenced UTXO set.

 

At this point, you should be able to run a Minicoin network with mining rewards and workable transactions.

[1] Minicoin uses unmalleable, deterministic DER-encoded signatures. https://tools.ietf.org/html/rfc6979