### BCDV 1013 - Advanced Smart Contract #### Introduction to state channel
Dhruvin Parikh, July 2021
### Topics * Scaling overview * Signing and verifying messages on Ethereum * State Channel
### Scaling solutions * Layer 2 Rollups * State channels * Sidechains * Plasma * Validium
States in coinflip game
[Heads] - [Win]?
[Tails] - [Loose]?
States in tic-tac-toe game
[Started], [Playing], [Won], [Loose] and [Tie]
States in rubik's cube solving
[Mixed], [Cross], [First Layer], [Second Layer], [Yellow Cross], [Solved]
What is Layer 1
Layer 1 is the protocol itself
It can be changed by block size, consensys mechanism, sharding,
DAG
(asynchronous fault tolerance…..which should fall under consensys mechanism?) etc
Can also be known as vertical scaling
WHat is Layer 2
Refers to solutions that happen off the blockchain
For scalability this can be
State channels
Bridges
Plasma
Can also be known as horizontal scaling
### State Channel - A blockchain scaling solution for Layer 2 - Conduct blockchain transactions off-chain - Uses cryptographic signatures - to allow secure, instantaneous transactions -
State channels by Jeff Coleman
State channel characteristics
Members allowed to interact with it
Rules of the game
Changing of state
Disputes of changing states
Rules of Tic Tac Toe
Once player A plays, it is player B’s turn
A player wins when there are 3 consecutive letters in one direction
A game may not have a winner
State channel smart contract
State channel is the third party arbitrator
Define the rules of the game
Handle payments, if necessary
Signatures
The way for smart contracts to verify information
Assumption
: The owner of the private key is the owner of the account
Sign data using private key
Unforgeable signature on an important letter
Signing and Verifying Messages in Ethereum
Cryptographic signatures are a powerful primitive in Ethereum.
Signatures are used to authorize transactions
Signatures can be used to prove to a smart contract that a certain account approved a certain message.
### Create Signature (web3js) ``` web3Utils.soliditySha3({ { t: "address", v: "0xF4e1E7a435BF80ebdFB2A9E7Bd05A560367d809E" }, { t: "uint256", v: 5000000000 }, { t: "uint256", v: 0 }, { t: "address", v: "0x0ED8f92224E590f827Bf00D0F32CDFa7cDcB4f5b" } }).then((message) => { const pKey = "6192891cafc2a2aaba01eb205e9026ae70a14e568e8664ff04947cad4b1b2449"; const sig = web3.eth.accounts.sign(message, pKey); }) ``` * **messageHash** : signer's address, amount, nonce, recipient * `sign(
,
)` has built-in support for ECDSA(
Elliptic Curve Digital Signature Algorithm
)
### Create Signature (ethersjs) ``` function signPayment(amount, contractAddress) { const digest = ethers.utils.solidityKeccak256( ["uint256", "address"], [amount, contractAddress] ); const arrayDigest = ethers.utils.arrayify(digest); const signature = wallet.signMessage(arrayDigest); return signature; } ```
Cheque Cashing
### Verifying Signature (on-chain) ``` function isValidSignature (uint256 amount, uint8 v, bytes32 r, bytes32 s) public view returns(bool) { // this recreates the message that // was signed on the client bytes32 hash = keccak256(abi.encodePacked( amount, contractAddress)); bytes32 message = keccak256(abi.encodePacked( "\x19Ethereum Signed Message:\n32", hash)); address signer = ecrecover(message, v, r, s); return( signer == sender); } ```
DEMO
on signing and recovery of the crytographic signatures.
### Cryptographic signature use-cases * Meta-transactions using `Permit()` :
EIP-2612
* Payment channels * Layer 2 solution * Solution to blockchain scalability Problem * Bitcoin:
7
tps * Ethereum:
15
tps * Visa:
47,000
transactions per second (tps)
### Exercise * Follow the [Readme](https://github.com/GeorgeBrownCollege-Toronto/Advanced-Smart-Contracts/blob/master/notes/intro-state-channel/exercise/rarible-app/README.md) to start using IPFS order book NFT exchange based on Rarible protocol.
### Payment Channel - A specific type of state channel - An
example
in solidity document - Uses cryptographic signatures - Make repeated transfers of Ether off-chain - Each message specifies total Ether owed
Image source
### Payment Steps - Alice funds a smart contract with Ether - This “opens” the payment channel - Alice signs payment messages to Bob - This is repeated for each payment - This is done off-chain - Bob “closes” the payment channel - This withdraws Ether
### Closing Channel - Bob closes the channel to receive funds - Bob uses last signed message to close channel ```js function close(uint256 amount, uint8 v, bytes32 r, bytes32 s) public { require(msg.sender == recipient); require(isValidSignature(amount, v, r, s)); recipient.transfer(amount); selfdestruct(sender); } ```
### Channel Timeout - A way for Alice to recover escrowed funds - An expiration is set at contract creation - After expiry, Alice can recover fund - Bob cannot receive fund after channel closure ```js function claimTimeout() public { require(block.timestamp >= expiration); selfdestruct(sender); } ```
Lab
Follow the instructions on the
readme
file of long-lived-payment-channel
### State channel implementation * [Connext](https://connext.network/) * [Kchannels](https://www.kchannels.io/) * [Perun](https://perun.network/) * [Raiden](https://raiden.network/) * [Statechannels.org](https://statechannels.org/)
### Readings on state channels and payment channels * [EthHub on state channels](https://docs.ethhub.io/ethereum-roadmap/layer-2-scaling/state-channels/) * [Making Sense of Ethereum’s Layer 2 Scaling Solutions: State Channels, Plasma, and Truebit](https://medium.com/l4-media/making-sense-of-ethereums-layer-2-scaling-solutions-state-channels-plasma-and-truebit-22cb40dcc2f4) * [State Channels - an explanation](https://www.jeffcoleman.ca/state-channels/) * [Basics of State Channels](https://education.district0x.io/general-topics/understanding-ethereum/basics-state-channels/) * [EthHub on payment channels](https://docs.ethhub.io/ethereum-roadmap/layer-2-scaling/payment-channels/)
References
Signing and verifying messages in Ethereum
Simple payment channel
Long lived payment channel
Signing and Verifying Ethereum Signatures
Scaling
Openzeppelin's ECDSA
NFT order book exchange
## End