-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.py
executable file
·73 lines (67 loc) · 1.98 KB
/
transaction.py
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
#!/usr/bin/python3
# import sys
import rlp
from hexbytes import HexBytes
from web3 import Web3
from ethereum.utils import ecrecover_to_pub
from ethereum.exceptions import InvalidTransaction
class Transaction(rlp.Serializable):
'''
Base for transaction
@Object txSigned: return the raw transaction
@Object txData: return the signing data
@Object senderAddress: return the address of the sender
'''
def __init__(self, _nonce, _gasPrice, _gasLimit, _to, _value, _init, _v, _r, _s):
self.nonce = _nonce
self.gasPrice = _gasPrice
self.gasLimit = _gasLimit
self.to = _to
self.value = _value
self.init = _init
self.v = _v
self.r = _r
self.s = _s
def txSigned(self):
tx = [
self.nonce,
self.gasPrice,
self.gasLimit,
self.to,
self.value,
self.init,
self.v,
self.r,
self.s
]
result = rlp.encode(tx)
return HexBytes(result)
def txData(self):
if self.v == 27 or self.v == 28:
tx = [
self.nonce,
self.gasPrice,
self.gasLimit,
self.to,
self.value,
self.init
]
else:
tx = [
self.nonce,
self.gasPrice,
self.gasLimit,
self.to,
self.value,
self.init,
(self.v - 36) / 2 if (self.v % 2 == 0) else (self.v - 35) / 2, #Return the chain id according to EIP155
0,
0
]
return rlp.encode(tx)
def senderAddress(self):
code = Web3.sha3(self.txData())
pub = ecrecover_to_pub(code, self.v, self.r, self.s)
if pub == b'\x00' * 64:
raise InvalidTransaction("Invalid signature")
return Web3.toChecksumAddress("0x" + Web3.sha3(pub).hex()[-40::])