|
1 | 1 | # -*- coding: utf8 -*-
|
2 | 2 | import bitcoin
|
3 | 3 | 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 |
5 | 8 |
|
6 | 9 | from ethereum import utils, opcodes
|
7 | 10 | from ethereum.utils import safe_ord, decode_hex
|
@@ -30,26 +33,32 @@ def proc_ecrecover(ext, msg):
|
30 | 33 | if r >= bitcoin.N or s >= bitcoin.N or v < 27 or v > 28:
|
31 | 34 | return 1, msg.gas - opcodes.GECRECOVER, []
|
32 | 35 |
|
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) |
39 | 36 | 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') |
53 | 62 | o = [0] * 12 + [safe_ord(x) for x in utils.sha3(pub[1:])[-20:]]
|
54 | 63 | return 1, msg.gas - gas_cost, o
|
55 | 64 |
|
|
0 commit comments