Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 3841e9a

Browse files
vubvub
authored andcommitted
Bug fixes
1 parent d1da7ea commit 3841e9a

File tree

4 files changed

+64
-39
lines changed

4 files changed

+64
-39
lines changed

ethereum/blocks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from ethereum.pruning_trie import Trie
1515
from ethereum.securetrie import SecureTrie
1616
from ethereum import utils
17-
from ethereum.utils import address, int256, trie_root, hash32, to_string,
17+
from ethereum.utils import address, int256, trie_root, hash32, to_string
18+
from ethereum.utils import big_endian_to_int
1819
from ethereum import processblock
1920
from ethereum.transactions import Transaction
2021
from ethereum import bloom
@@ -990,7 +991,7 @@ def get_storage_data(self, address, index):
990991
if len(bytez) >= 32:
991992
return big_endian_to_int(bytez[-32:])
992993
else:
993-
return big_endian_to_int(bytez) * (1 << (8 * (32 - len(bytes))))
994+
return big_endian_to_int(bytez) * (1 << (8 * (32 - len(bytez))))
994995

995996
def set_storage_data(self, address, index, value):
996997
"""Set a specific item in the storage of an account.
@@ -1006,6 +1007,7 @@ def set_storage_data(self, address, index, value):
10061007
if CACHE_KEY not in self.caches:
10071008
self.caches[CACHE_KEY] = {}
10081009
self.set_and_journal('all', address, True)
1010+
assert isinstance(value, (str, bytes))
10091011
self.set_and_journal(CACHE_KEY, index, value)
10101012

10111013
def account_exists(self, address):

ethereum/specials.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# -*- coding: utf8 -*-
22
import bitcoin
33
from rlp.utils import ascii_chr
4-
from secp256k1 import PublicKey, ALL_FLAGS
4+
try:
5+
from secp256k1 import PublicKey, ALL_FLAGS
6+
except:
7+
pass
58

69
from ethereum import utils, opcodes
710
from ethereum.utils import safe_ord, decode_hex
@@ -30,26 +33,32 @@ def proc_ecrecover(ext, msg):
3033
if r >= bitcoin.N or s >= bitcoin.N or v < 27 or v > 28:
3134
return 1, msg.gas - opcodes.GECRECOVER, []
3235

33-
signature_bytes = [0] * 64
34-
msg.data.extract_copy(signature_bytes, 0, 64, 32)
35-
msg.data.extract_copy(signature_bytes, 32, 96, 32)
36-
signature = b''.join(map(ascii_chr, signature_bytes))
37-
38-
pk = PublicKey(flags=ALL_FLAGS)
3936
try:
40-
pk.public_key = pk.ecdsa_recover(
41-
message_hash,
42-
pk.ecdsa_recoverable_deserialize(
43-
signature,
44-
v - 27
45-
),
46-
raw=True
47-
)
48-
except Exception:
49-
# Recovery failed
50-
return 1, msg.gas - gas_cost, []
51-
52-
pub = pk.serialize(compressed=False)
37+
signature_bytes = [0] * 64
38+
msg.data.extract_copy(signature_bytes, 0, 64, 32)
39+
msg.data.extract_copy(signature_bytes, 32, 96, 32)
40+
signature = b''.join(map(ascii_chr, signature_bytes))
41+
42+
pk = PublicKey(flags=ALL_FLAGS)
43+
try:
44+
pk.public_key = pk.ecdsa_recover(
45+
message_hash,
46+
pk.ecdsa_recoverable_deserialize(
47+
signature,
48+
v - 27
49+
),
50+
raw=True
51+
)
52+
except Exception:
53+
# Recovery failed
54+
return 1, msg.gas - gas_cost, []
55+
56+
pub = pk.serialize(compressed=False)
57+
except:
58+
recovered_addr = bitcoin.ecdsa_raw_recover(h, (v, r, s))
59+
if recovered_addr in (False, (0, 0)):
60+
return 1, msg.gas - gas_cost, []
61+
pub = bitcoin.encode_pubkey(recovered_addr, 'bin')
5362
o = [0] * 12 + [safe_ord(x) for x in utils.sha3(pub[1:])[-20:]]
5463
return 1, msg.gas - gas_cost, o
5564

ethereum/testutils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ethereum import processblock as pb
99
import copy
1010
from ethereum.db import EphemDB
11-
from ethereum.utils import to_string, safe_ord, parse_int_or_hex
11+
from ethereum.utils import to_string, safe_ord, parse_int_or_hex, zpad
1212
from ethereum.utils import remove_0x_head, int_to_hex, normalize_address
1313
from ethereum.config import Env
1414
import json
@@ -58,7 +58,10 @@
5858

5959

6060
def normalize_hex(s):
61-
return s if len(s) > 2 else b'0x00'
61+
s = (s if len(s) > 2 else b'0x00')
62+
if len(s) < 66:
63+
s = s[:2] + b'0' * (66 - len(s)) + s[2:]
64+
return s
6265

6366

6467
def acct_standard_form(a):
@@ -154,7 +157,7 @@ def run_vm_test(params, mode, profiler=None):
154157
for k, v in h['storage'].items():
155158
blk.set_storage_data(address,
156159
utils.big_endian_to_int(decode_hex(k[2:])),
157-
utils.big_endian_to_int(decode_hex(v[2:])))
160+
zpad(decode_hex(v[2:]), 32))
158161

159162
# execute transactions
160163
sender = decode_hex(exek['caller']) # a party that originates a call
@@ -306,7 +309,7 @@ def run_state_test(params, mode):
306309
for k, v in h['storage'].items():
307310
blk.set_storage_data(address,
308311
utils.big_endian_to_int(decode_hex(k[2:])),
309-
utils.big_endian_to_int(decode_hex(v[2:])))
312+
zpad(decode_hex(v[2:]), 32))
310313

311314
for address, h in list(pre.items()):
312315
address = decode_hex(address)

ethereum/transactions.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from bitcoin import encode_pubkey, N, encode_privkey
44
from rlp.sedes import big_endian_int, binary
55
from rlp.utils import encode_hex, str_to_bytes, ascii_chr
6-
from secp256k1 import PublicKey, ALL_FLAGS, PrivateKey
6+
try:
7+
from secp256k1 import PublicKey, ALL_FLAGS, PrivateKey
8+
except:
9+
pass
710

811
from ethereum.exceptions import InvalidTransaction
912
from ethereum import bloom
@@ -82,19 +85,27 @@ def sender(self):
8285
rlpdata = rlp.encode(self, UnsignedTransaction)
8386
rawhash = utils.sha3(rlpdata)
8487

85-
pk = PublicKey(flags=ALL_FLAGS)
8688
try:
87-
pk.public_key = pk.ecdsa_recover(
88-
rawhash,
89-
pk.ecdsa_recoverable_deserialize(
90-
zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.r)), 32) + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.s)), 32),
91-
self.v - 27
92-
),
93-
raw=True
94-
)
95-
pub = pk.serialize(compressed=False)
96-
except Exception:
97-
raise InvalidTransaction("Invalid signature values (x^3+7 is non-residue)")
89+
pk = PublicKey(flags=ALL_FLAGS)
90+
using_pk = 1
91+
except:
92+
pk, using_pk = None, 0
93+
if not using_pk:
94+
recovered_addr = bitcoin.ecdsa_raw_recover(rawhash, (self.v, self.r, self.s))
95+
pub = bitcoin.encode_pubkey(recovered_addr, 'bin')
96+
else:
97+
try:
98+
pk.public_key = pk.ecdsa_recover(
99+
rawhash,
100+
pk.ecdsa_recoverable_deserialize(
101+
zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.r)), 32) + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.s)), 32),
102+
self.v - 27
103+
),
104+
raw=True
105+
)
106+
pub = pk.serialize(compressed=False)
107+
except Exception:
108+
raise InvalidTransaction("Invalid signature values (x^3+7 is non-residue)")
98109

99110
if pub[1:] == b"\x00" * 32:
100111
raise InvalidTransaction("Invalid signature (zero privkey cannot sign)")

0 commit comments

Comments
 (0)