Created
January 11, 2019 07:15
-
-
Save boneyard93501/e205f3c8ad8adc913ab401bc4a588b31 to your computer and use it in GitHub Desktop.
eth signature-recovery equivaency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# note to self | |
import requests | |
from eth_account.messages import defunct_hash_message | |
from hexbytes import HexBytes | |
from pymongo import MongoClient | |
from web3 import Web3 | |
def personal_sign(w3, tx_hash: HexBytes, acct_addr: str, password: str) -> str: | |
''' | |
parity RPC call | |
''' | |
nonce = w3.eth.getTransactionCount(acct_addr) | |
d = {"method": "personal_sign", "params": [w3.toHex(tx_hash), acct_addr, password], "id": nonce, "jsonrpc": "2.0"} | |
res = requests.post("http://127.0.0.1:8545", json=d).json()['result'] | |
return res | |
def recover_address(w3, message, signature): | |
message_hash = defunct_hash_message(text=message) | |
address = w3.eth.account.recoverHash(message_hash, signature=signature) | |
return address | |
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545')) | |
m_client = MongoClient('mongodb://127.0.0.1:27017') | |
test_acct = m_client['your-db']['accounts'].find_one({}) | |
print(test_acct) | |
w3.personal.unlockAccount(test_acct['address'], test_acct['password']) | |
s = w3.eth.sign(test_acct['address'], text='test msg') | |
print(s) | |
rc = recover_address(w3, 'test msg', s) | |
print(rc, rc == test_acct['address']) | |
s = personal_sign(w3, 'test msg'.encode('utf-8'), test_acct['address'], test_acct['password']) | |
print(s) | |
rc = recover_address(w3, 'test msg', s) | |
print(rc, rc == test_acct['address']) | |
h = defunct_hash_message(text='test msg') | |
signed_message = w3.eth.account.signHash(h, private_key=test_acct['private-key']) | |
rc = recover_address(w3, 'test msg', signed_message.signature) | |
print(rc, rc == test_acct['address']) | |
tx_hash = w3.soliditySha3(['uint16'], [500]) | |
print(tx_hash) | |
signed_message = w3.eth.account.signHash(tx_hash, private_key=test_acct['private-key']) | |
rc = w3.eth.account.recoverHash(tx_hash, signature=signed_message.signature) | |
print(rc, rc == test_acct['address']) | |
s = personal_sign(w3, tx_hash, test_acct['address'], test_acct['password']) | |
rc = w3.eth.account.recoverHash(tx_hash, signature=signed_message.signature) | |
print(rc, rc == test_acct['address']) | |
w3.personal.unlockAccount(test_acct['address'], test_acct['password']) | |
s = w3.eth.sign(test_acct['address'], data=tx_hash) | |
rc = w3.eth.account.recoverHash(tx_hash, signature=signed_message.signature) | |
print(rc, rc == test_acct['address']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment