-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_channel.vy
107 lines (83 loc) · 2.73 KB
/
payment_channel.vy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# @version ^0.3.7
sender: public(address)
receiver: public(address)
ledger_entry_type: String[15]
expiry_date: public(uint256)
# 7 days
# DURATION: constant(uint256) = 7 * 24 * 60 * 60
event payment_channel_create:
sender: address
amount: uint256
receiver: address
expiry_date: uint256
event payment_channel_fund:
sender: address
amount: uint256
event payment_channel_claim:
sender: address
amount: uint256
event payment_channel_cancel:
sender: address
amount: uint256
@external
@payable
def __init__(_receiver: address, _duration: uint256):
assert _receiver != empty(address), "receiver = zero address"
self.sender = msg.sender
self.receiver = _receiver
self.ledger_entry_type = "payment channel"
self.expiry_date = block.timestamp + _duration
log payment_channel_create(msg.sender, msg.value, _receiver, self.expiry_date)
@internal
def _getHash(_amount: uint256) -> bytes32:
return keccak256(concat(
convert(self, bytes32),
convert(_amount, bytes32)
))
@external
def getHash(_amount: uint256) -> bytes32:
return self._getHash(_amount)
@internal
def _getEthSignedHash(_amount: uint256) -> bytes32:
hash: bytes32 = self._getHash(_amount)
return keccak256(
concat(
b'\x19Ethereum Signed Message:\n32',
hash
)
)
@external
def getEthSignedHash(_amount: uint256) -> bytes32:
return self._getEthSignedHash(_amount)
@internal
def _verify(_amount: uint256, _sig: Bytes[65]) -> bool:
ethSignedHash: bytes32 = self._getEthSignedHash(_amount)
r: uint256 = convert(slice(_sig, 0, 32), uint256)
s: uint256 = convert(slice(_sig, 32, 32), uint256)
v: uint256 = convert(slice(_sig, 64, 1), uint256)
return ecrecover(ethSignedHash, v, r, s) == self.sender
@external
def verify(_amount: uint256, _sig: Bytes[65]) -> bool:
return self._verify(_amount, _sig)
@nonreentrant("lock")
@external
def claim(_amount: uint256, _sig: Bytes[65]):
assert msg.sender == self.receiver, "!receiver"
assert self.balance >= _amount, "balance < payment"
assert self._verify(_amount, _sig), "invalid sig"
raw_call(self.receiver, b'\x00', value=_amount)
log payment_channel_claim(msg.sender, self.balance)
selfdestruct(self.sender)
@external
def cancel():
assert msg.sender == self.sender, "!sender"
assert block.timestamp >= self.expiry_date, "!expired"
log payment_channel_cancel(msg.sender, self.balance)
selfdestruct(self.sender)
@external
def amountInChannel() -> uint256:
return self.balance
@payable
@external
def __default__():
log payment_channel_fund(msg.sender, msg.value)