Skip to content

Instantly share code, notes, and snippets.

@jstoxrocky
Created December 23, 2017 14:49
Show Gist options
  • Save jstoxrocky/810aeb761e36d89acdb311034426d88d to your computer and use it in GitHub Desktop.
Save jstoxrocky/810aeb761e36d89acdb311034426d88d to your computer and use it in GitHub Desktop.
class Channel:
def __init__(self, sender, recipient, deposit):
self.sender = to_checksum_address(sender)
self.recipient = to_checksum_address(recipient)
self.deposit = deposit
self.signatures = {}
def close_channel(self, h, v, r, s, value):
"""
Function recreating Matthew Di Ferrante's closure of a state channel in Python
Args:
h: The Keccak hash of the msg
v: The "v" parameter derived from the hashed and signed message (ethereum.utils.ecsign)
r: The "r" parameter derived from the hashed and signed message (ethereum.utils.ecsign)
s: The "s" parameter derived from the hashed and signed message (ethereum.utils.ecsign)
value: the
Returns:
Address of the message signer as a hexidecimal string
"""
# Recover the message signer's address
# v, r, and s are specific to the message that was hashed and signed.
# Changing any of these parameters produces a different address
# This also proves that h is the hashed message that was signed to produce v, r, and s
signer = ecrecover(h, v, r, s)
# Ensure that the signer is either the sender (Bob) or recipient (Alice) of the channel's ETH
if (signer != self.recipient) and (signer != self.sender):
assert False
# Ensure that that value matches the signed message
# Including the contract's hash ensures that the signed message can only be used in this channel
proof = keccak(str(self.__hash__()) + value)
if (proof != h):
assert False
# If this is the first time associate the proof with the first signer
# Doesn't matter if Alice submits her signature or Bob's signature first
# Or even if Bob submits his own signature first
if not self.signatures.get(proof):
self.signatures[proof] = signer
# Continue from here only when the second signature makes an appearance
elif (self.signatures.get(proof) != signer):
# -- psuedo-code --
# send(value, to=self.recipient)
# send(self.deposit - value, to=self.sender)
# self.destruct()
print('Success')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment