def test_encode_intended_validator(primitive, hexstr, text, validator_address, expected_signable): signable_message = encode_intended_validator( validator_address, primitive, hexstr=hexstr, text=text, ) assert signable_message == expected_signable
def test_eth_valid_account_address_sign_data_with_intended_validator(acct, message_encodings): account = acct.create() signable = encode_intended_validator( account.address, **message_encodings, ) signed = account.sign_message(signable) signed_classmethod = acct.sign_message(signable, account.key) assert signed == signed_classmethod new_addr = acct.recover_message(signable, signature=signed.signature) assert new_addr == account.address
def add_value_txn(self, acct, l2nonce, balance, deposit): chainid = 100 tokenid = '0x0000000000000000000000000000000000000000' amount = int((balance + deposit) * pow(10, 18)) adjust = int(deposit * pow(10, 18)) lock = retrieve = 0 refill = 1 acstat = seller.functions.read(acct.address).call() print(f"acstat: {acstat}") l3nonce = int(('0' * 16 + hex(acstat)[2:])[-16:], 16) print(f"l3nonce: {l3nonce}") # msg = encode_abi_packed(['bytes1', 'bytes1', 'address', 'uint256', 'uint64', 'address', 'uint256', 'int256', 'int256', 'uint256', 'uint128'], # [b'\x19', b'\x00', testdata['seller']['address'], chainid, l3nonce, tokenid, amount, adjust, lock, retrieve, refill]) msg = encode_abi_packed([ 'uint256', 'uint64', 'address', 'uint256', 'int256', 'int256', 'uint256', 'uint256' ], [chainid, l3nonce, tokenid, amount, adjust, lock, retrieve, refill]) message = encode_intended_validator( validator_address=testdata['seller']['address'], primitive=msg) sig = w3.eth.account.sign_message(message, private_key=acct.key) txn = { "from": acct.address, "to": testdata['seller']['address'], "gas": hex(175000), "gasPrice": hex(pow(10, 9)), "value": hex(int((balance + deposit) * pow(10, 18))), "chainId": chainid, "nonce": l2nonce, "data": seller.encodeABI(fn_name='edit', args=[ acct.address, sig.v, bytearray.fromhex(hex(sig.r)[2:]), bytearray.fromhex(hex(sig.s)[2:]), l3nonce, adjust, lock, retrieve, refill ]), } txstr = json.dumps(txn) txmsg = encode_defunct(text=txstr) print(txmsg) txsig = w3.eth.account.sign_message( txmsg, private_key=acct.key).signature.hex() print(txsig) return txstr, txsig
def sign_score(key, validator, game_id, user, score): game_id = HexBytes(game_id) types = ['bytes32', 'address', 'uint256'] values = [game_id, user, score] encoded_values = encode_abi_packed(types, values) message = encode_intended_validator( validator_address=validator, primitive=encoded_values, ) signed = Account.sign_message(message, key) vrs = { 'v': signed['v'], 'r': HexBytes(signed['r']).hex(), 's': HexBytes(signed['s']).hex(), } return vrs
def test_sign_score(user, owner, monkeypatch): expected_signer = owner.address sig = sign_score(owner.key, CONTRACT, GAME_ID, user.address, SCORE) types = ['bytes32', 'address', 'uint256'] values = [ HexBytes(GAME_ID), user.address, SCORE, ] encoded_values = encode_abi_packed(types, values) message = encode_intended_validator( validator_address=CONTRACT, primitive=encoded_values, ) vrs = (sig['v'], sig['r'], sig['s']) actual_signer = Account.recover_message( message, vrs=vrs, ) assert actual_signer == expected_signer
def test_encode_intended_validator_invalid_address(invalid_address): with pytest.raises(ValidationError): encode_intended_validator(invalid_address, b'')