This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Crosschain transfer #658
Draft
LayneHaber
wants to merge
18
commits into
0.2.5-beta.18
Choose a base branch
from
crosschain-transfer
base: 0.2.5-beta.18
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Crosschain transfer #658
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
faeed0d
add : basic crosschain transfer definition
jakekidd 9dbca2c
fix compile issues
jakekidd 212c6c9
remove : expiry
jakekidd 23b9b9c
contract Crosschain -> CrosschainTransfer
jakekidd c301cc1
fix : resolver encoding
jakekidd d1271af
add : crosschain transfer state and resolver types
jakekidd db43fa4
fix encoding tuple string
jakekidd 8d770bd
update : contract
jakekidd 5fa3cd6
Add crosschain transfer to deployment
rhlsthrm ed29dc6
Crosschain transfer special case
rhlsthrm da7df37
Tests WIP
rhlsthrm ede3a77
Fix import
rhlsthrm 527e090
Tests in progress
rhlsthrm 56421af
Test working
rhlsthrm 46f59f4
Fix
rhlsthrm 5d3bc9d
Tests working!
rhlsthrm 51ea515
Fix contract calls
rhlsthrm 82eba83
Remove comments about caching
rhlsthrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
modules/contracts/src.sol/transferDefinitions/CrosschainTransfer.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.7.1; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "./TransferDefinition.sol"; | ||
import "../lib/LibChannelCrypto.sol"; | ||
|
||
/// @title CrosschainTransfer | ||
/// @author Connext <support@connext.network> | ||
/// @notice This contract burns the initiator's funds if a mutually signed | ||
/// transfer can be generated | ||
|
||
contract CrosschainTransfer is TransferDefinition { | ||
using LibChannelCrypto for bytes32; | ||
|
||
struct TransferState { | ||
bytes initiatorSignature; | ||
address initiator; | ||
address responder; | ||
bytes32 data; | ||
uint256 nonce; // Included so that each transfer commitment has a unique hash. | ||
uint256 fee; | ||
address callTo; | ||
bytes callData; | ||
bytes32 lockHash; | ||
} | ||
|
||
struct TransferResolver { | ||
bytes responderSignature; | ||
bytes32 preImage; | ||
} | ||
|
||
// Provide registry information. | ||
string public constant override Name = "CrosschainTransfer"; | ||
string public constant override StateEncoding = | ||
"tuple(bytes initiatorSignature, address initiator, address responder, bytes32 data, uint256 nonce, uint256 fee, address callTo, bytes callData, bytes32 lockHash)"; | ||
string public constant override ResolverEncoding = | ||
"tuple(bytes responderSignature, bytes32 preImage)"; | ||
|
||
function EncodedCancel() external pure override returns (bytes memory) { | ||
TransferResolver memory resolver; | ||
resolver.responderSignature = new bytes(65); | ||
resolver.preImage = bytes32(0); | ||
return abi.encode(resolver); | ||
} | ||
|
||
function create(bytes calldata encodedBalance, bytes calldata encodedState) | ||
external | ||
pure | ||
override | ||
returns (bool) | ||
{ | ||
// Get unencoded information. | ||
TransferState memory state = abi.decode(encodedState, (TransferState)); | ||
Balance memory balance = abi.decode(encodedBalance, (Balance)); | ||
|
||
// Ensure data and nonce provided. | ||
require(state.data != bytes32(0), "CrosschainTransfer: EMPTY_DATA"); | ||
require(state.nonce != uint256(0), "CrosschainTransfer: EMPTY_NONCE"); | ||
|
||
// Initiator balance can be 0 for crosschain contract calls | ||
require( | ||
state.fee <= balance.amount[0], | ||
"CrosschainTransfer: INSUFFICIENT_BALANCE" | ||
); | ||
|
||
// Recipient balance must be 0. | ||
require( | ||
balance.amount[1] == 0, | ||
"CrosschainTransfer: NONZERO_RECIPIENT_BALANCE" | ||
); | ||
|
||
// Valid lockHash to secure funds must be provided. | ||
require( | ||
state.lockHash != bytes32(0), | ||
"CrosschainTransfer: EMPTY_LOCKHASH" | ||
); | ||
|
||
require( | ||
state.data.checkSignature( | ||
state.initiatorSignature, | ||
state.initiator | ||
), | ||
"CrosschainTransfer: INVALID_INITIATOR_SIG" | ||
); | ||
|
||
require( | ||
state.initiator != address(0) && state.responder != address(0), | ||
"CrosschainTransfer: EMPTY_SIGNERS" | ||
); | ||
|
||
// Valid initial transfer state | ||
return true; | ||
} | ||
|
||
function resolve( | ||
bytes calldata encodedBalance, | ||
bytes calldata encodedState, | ||
bytes calldata encodedResolver | ||
) external pure override returns (Balance memory) { | ||
TransferState memory state = abi.decode(encodedState, (TransferState)); | ||
TransferResolver memory resolver = | ||
abi.decode(encodedResolver, (TransferResolver)); | ||
Balance memory balance = abi.decode(encodedBalance, (Balance)); | ||
|
||
require( | ||
state.data.checkSignature( | ||
resolver.responderSignature, | ||
state.responder | ||
), | ||
"CrosschainTransfer: INVALID_RESPONDER_SIG" | ||
); | ||
|
||
// Check hash for normal payment unlock. | ||
bytes32 generatedHash = sha256(abi.encode(resolver.preImage)); | ||
require( | ||
state.lockHash == generatedHash, | ||
"CrosschainTransfer: INVALID_PREIMAGE" | ||
); | ||
|
||
// Reduce CrosschainTransfer amount to optional fee. | ||
// It's up to the offchain validators to ensure that the | ||
// CrosschainTransfer commitment takes this fee into account. | ||
balance.amount[1] = state.fee; | ||
balance.amount[0] = 0; | ||
|
||
return balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things that are not yet validated in the state:
responder
callTo
callData
balance.to
These are probably all okay, but this means that anything could be put into these values and a transfer could be created that is potentially unresolvable (i.e. what if
responder
isn't actually a proper address?)