|
9 | 9 | from ethereum import opcodes
|
10 | 10 | from ethereum import utils
|
11 | 11 | from ethereum.slogging import get_logger
|
12 |
| -from ethereum.utils import TT256, mk_contract_address, zpad, int_to_32bytearray, big_endian_to_int, ecsign, ecrecover_to_pub |
| 12 | +from ethereum.utils import TT256, mk_contract_address, zpad, int_to_32bytearray, big_endian_to_int, ecsign, ecrecover_to_pub, normalize_key |
13 | 13 |
|
14 | 14 |
|
15 | 15 | log = get_logger('eth.chain.tx')
|
@@ -57,18 +57,13 @@ def __init__(self, nonce, gasprice, startgas, to, value, data, v=0, r=0, s=0):
|
57 | 57 | self.data = None
|
58 | 58 |
|
59 | 59 | to = utils.normalize_address(to, allow_blank=True)
|
60 |
| - assert len(to) == 20 or len(to) == 0 |
| 60 | + |
61 | 61 | super(Transaction, self).__init__(nonce, gasprice, startgas, to, value, data, v, r, s)
|
62 |
| - self.logs = [] |
63 | 62 | self.network_id = None
|
64 | 63 |
|
65 | 64 | if self.gasprice >= TT256 or self.startgas >= TT256 or \
|
66 | 65 | self.value >= TT256 or self.nonce >= TT256:
|
67 | 66 | raise InvalidTransaction("Values way too high!")
|
68 |
| - if self.startgas < self.intrinsic_gas_used: |
69 |
| - raise InvalidTransaction("Startgas too low") |
70 |
| - |
71 |
| - log.debug('deserialized tx', tx=encode_hex(self.hash)[:8]) |
72 | 67 |
|
73 | 68 | @property
|
74 | 69 | def sender(self):
|
@@ -108,59 +103,42 @@ def sign(self, key, network_id=None):
|
108 | 103 |
|
109 | 104 | A potentially already existing signature would be overridden.
|
110 | 105 | """
|
111 |
| - if key in (0, '', b'\x00' * 32, '0' * 64): |
112 |
| - raise InvalidTransaction("Zero privkey cannot sign") |
113 | 106 | if network_id is None:
|
114 | 107 | rawhash = utils.sha3(rlp.encode(self, UnsignedTransaction))
|
115 | 108 | else:
|
116 | 109 | assert 1 <= network_id < 2**63 - 18
|
117 |
| - rlpdata = rlp.encode(rlp.infer_sedes(self).serialize(self)[:-3] + [big_endian_to_int(network_id), '', '']) |
| 110 | + rlpdata = rlp.encode(rlp.infer_sedes(self).serialize(self)[:-3] + [big_endian_to_int(network_id), b'', b'']) |
118 | 111 | rawhash = utils.sha3(rlpdata)
|
119 | 112 |
|
120 |
| - if len(key) == 64: |
121 |
| - # we need a binary key |
122 |
| - key = encode_privkey(key, 'bin') |
| 113 | + key = normalize_key(key) |
123 | 114 |
|
124 | 115 | self.v, self.r, self.s = ecsign(rawhash, key)
|
125 | 116 | if network_id is not None:
|
126 | 117 | self.v += 8 + network_id * 2
|
127 | 118 |
|
128 |
| - self.sender = utils.privtoaddr(key) |
| 119 | + self._sender = utils.privtoaddr(key) |
129 | 120 | return self
|
130 | 121 |
|
131 | 122 | @property
|
132 | 123 | def hash(self):
|
133 | 124 | return utils.sha3(rlp.encode(self))
|
134 | 125 |
|
135 |
| - def log_bloom(self): |
136 |
| - "returns int" |
137 |
| - bloomables = [x.bloomables() for x in self.logs] |
138 |
| - return bloom.bloom_from_list(utils.flatten(bloomables)) |
139 |
| - |
140 |
| - def log_bloom_b64(self): |
141 |
| - return bloom.b64(self.log_bloom()) |
142 |
| - |
143 | 126 | def to_dict(self):
|
144 |
| - # TODO: previous version used printers |
145 | 127 | d = {}
|
146 | 128 | for name, _ in self.__class__.fields:
|
147 | 129 | d[name] = getattr(self, name)
|
148 |
| - d['sender'] = self.sender |
149 |
| - d['hash'] = encode_hex(self.hash) |
150 |
| - return d |
151 |
| - |
152 |
| - def log_dict(self, abbrev=False): |
153 |
| - d = self.to_dict() |
154 |
| - d['sender'] = encode_hex(d['sender'] or '') |
155 |
| - d['to'] = encode_hex(d['to']) |
156 |
| - d['data'] = encode_hex(d['data']) if len(d['data']) < 2500 or not abbrev else "data<%d>" % len(d['data']) |
| 130 | + if name in ('to', 'data'): |
| 131 | + d[name] = '0x' + encode_hex(d[name]) |
| 132 | + d['sender'] = '0x' + encode_hex(self.sender) |
| 133 | + d['hash'] = '0x' + encode_hex(self.hash) |
157 | 134 | return d
|
158 | 135 |
|
159 | 136 | @property
|
160 | 137 | def intrinsic_gas_used(self):
|
161 | 138 | num_zero_bytes = str_to_bytes(self.data).count(ascii_chr(0))
|
162 | 139 | num_non_zero_bytes = len(self.data) - num_zero_bytes
|
163 | 140 | return (opcodes.GTXCOST
|
| 141 | + + (0 if self.to else opcodes.CREATE[3]) |
164 | 142 | + opcodes.GTXDATAZERO * num_zero_bytes
|
165 | 143 | + opcodes.GTXDATANONZERO * num_non_zero_bytes)
|
166 | 144 |
|
@@ -198,9 +176,3 @@ def check_low_s_homestead(self):
|
198 | 176 |
|
199 | 177 |
|
200 | 178 | UnsignedTransaction = Transaction.exclude(['v', 'r', 's'])
|
201 |
| - |
202 |
| - |
203 |
| -def contract(nonce, gasprice, startgas, endowment, code, v=0, r=0, s=0): |
204 |
| - """A contract is a special transaction without the `to` argument.""" |
205 |
| - tx = Transaction(nonce, gasprice, startgas, '', endowment, code, v, r, s) |
206 |
| - return tx |
0 commit comments