Beispiel #1
0
    def signTransaction(self, transaction_dict, private_key):
        '''
        @param private_key in bytes, str, or int.
            In Python 2, a bytes, unicode or str object will be interpreted as hexstr
            In Python 3, only a str object will be interpreted as hexstr
        '''
        assert isinstance(transaction_dict, Mapping)

        account = self.privateKeyToAccount(private_key)

        # sign transaction
        (
            v,
            r,
            s,
            transaction_hash,
            rlp_encoded,
        ) = sign_transaction_dict(account._key_obj, transaction_dict)

        return AttributeDict({
            'rawTransaction':
            HexBytes(binascii.hexlify(rlp_encoded)),
            'hash':
            HexBytes(binascii.hexlify(transaction_hash)),
            'r':
            HexBytes(r),
            's':
            HexBytes(s),
            'v':
            v,
        })
Beispiel #2
0
 def test_eth_getTransactionReceipt_mined(self, web3, block_with_txn, mined_txn_hash):
     receipt = web3_offline_signing.eth.getTransactionReceipt(mined_txn_hash)
     assert is_dict(receipt)
     assert receipt['blockNumber'] == block_with_txn['number']
     assert receipt['blockHash'] == block_with_txn['hash']
     assert receipt['transactionIndex'] == 0
     assert receipt['transactionHash'] == HexBytes(mined_txn_hash)
Beispiel #3
0
 def recoverTransaction(self, serialized_transaction):
     txn_bytes = HexBytes(serialized_transaction)
     txn = Transaction.from_bytes(txn_bytes)
     msg_hash = hash_of_signed_transaction(txn)
     if sys.version_info.major < 3:
         msg_hash = to_hex(msg_hash)
     return self.recover(msg_hash, vrs=vrs_from(txn))
Beispiel #4
0
 def recover(self, msghash, vrs=None, signature=None):
     hash_bytes = HexBytes(msghash)
     if vrs is not None:
         v, r, s = map(hexstr_if_str(to_int), vrs)
         v_standard = to_standard_v(v)
         signature_obj = self._keys.Signature(vrs=(v_standard, r, s))
     elif signature is not None:
         signature_bytes = HexBytes(signature)
         signature_bytes_standard = to_standard_signature_bytes(
             signature_bytes)
         signature_obj = self._keys.Signature(
             signature_bytes=signature_bytes_standard)
     else:
         raise TypeError(
             "You must supply the vrs tuple or the signature bytes")
     pubkey = signature_obj.recover_public_key_from_msg_hash(hash_bytes)
     return pubkey.to_checksum_address()
Beispiel #5
0
 def privateKeyToAccount(self, private_key):
     key_bytes = HexBytes(private_key)
     try:
         key_obj = self._keys.PrivateKey(key_bytes)
         return LocalAccount(key_obj, self)
     except ValidationError as original_exception:
         raise_from(
             ValueError(
                 "The private key must be exactly 32 bytes long, instead of "
                 "%d bytes." % len(key_bytes)), original_exception)
Beispiel #6
0
def to_hexbytes(num_bytes, val):
    if isinstance(val, str):
        result = HexBytes(val)
    elif isinstance(val, bytes):
        if len(val) == num_bytes:
            result = HexBytes(val)
        else:
            # some providers return values with hex as bytes, like b'0xEFFF' :(
            hexstr = val.decode('utf-8')
            if not is_hex(hexstr):
                raise ValueError("Cannot convert %r into a %d byte argument" %
                                 (hexstr, num_bytes))
            result = HexBytes(hexstr)
    else:
        raise TypeError("Cannot convert %r to HexBytes" % val)

    if len(result) != num_bytes:
        raise ValueError("The value %r is %d bytes, but should be %d" %
                         (result, len(result), num_bytes))
    return result
Beispiel #7
0
    def test_eth_getTransactionReceipt_with_log_entry(self,
                                                      web3,
                                                      block_with_txn_with_log,
                                                      emitter_contract,
                                                      txn_hash_with_log):
        receipt = web3_offline_signing.eth.getTransactionReceipt(txn_hash_with_log)
        assert is_dict(receipt)
        assert receipt['blockNumber'] == block_with_txn_with_log['number']
        assert receipt['blockHash'] == block_with_txn_with_log['hash']
        assert receipt['transactionIndex'] == 0
        assert receipt['transactionHash'] == HexBytes(txn_hash_with_log)

        assert len(receipt['logs']) == 1
        log_entry = receipt['logs'][0]

        assert log_entry['blockNumber'] == block_with_txn_with_log['number']
        assert log_entry['blockHash'] == block_with_txn_with_log['hash']
        assert log_entry['logIndex'] == 0
        assert is_same_address(log_entry['address'], emitter_contract.address)
        assert log_entry['transactionIndex'] == 0
        assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
Beispiel #8
0
 def sign(self,
          message=None,
          private_key=None,
          message_hexstr=None,
          message_text=None):
     '''
     @param private_key in bytes, str, or int.
         In Python 2, a bytes, unicode or str object will be interpreted as hexstr
         In Python 3, only a str object will be interpreted as hexstr
     '''
     msg_bytes = to_bytes(message, hexstr=message_hexstr, text=message_text)
     msg_hash = self.hashMessage(msg_bytes)
     key_bytes = HexBytes(private_key)
     key = self._keys.PrivateKey(key_bytes)
     (v, r, s, eth_signature_bytes) = sign_message_hash(key, msg_hash)
     return AttributeDict({
         'message': HexBytes(msg_bytes),
         'messageHash': msg_hash,
         'r': HexBytes(r),
         's': HexBytes(s),
         'v': v,
         'signature': HexBytes(eth_signature_bytes),
     })
Beispiel #9
0
 def encrypt(private_key, password):
     key_bytes = HexBytes(private_key)
     password_bytes = text_if_str(to_bytes, password)
     assert len(key_bytes) == 32
     return create_keyfile_json(key_bytes, password_bytes)
Beispiel #10
0
 def test_eth_getTransactionByBlockNumberAndIndex(self, web3, block_with_txn, mined_txn_hash):
     transaction = web3_offline_signing.eth.getTransactionFromBlock(block_with_txn['number'], 0)
     assert is_dict(transaction)
     assert transaction['hash'] == HexBytes(mined_txn_hash)
Beispiel #11
0
 def test_eth_getTransactionByHash(self, web3, mined_txn_hash):
     transaction = web3_offline_signing.eth.getTransaction(mined_txn_hash)
     assert is_dict(transaction)
     assert transaction['hash'] == HexBytes(mined_txn_hash)
Beispiel #12
0
 def test_eth_sendRawTransaction(self, web3, funded_account_for_raw_txn):
     txn_hash = web3_offline_signing.eth.sendRawTransaction(
         '0xf8648085174876e8008252089439eeed73fb1d3855e90cbd42f348b3d7b340aaa601801ba0ec1295f00936acd0c2cb90ab2cdaacb8bf5e11b3d9957833595aca9ceedb7aada05dfc8937baec0e26029057abd3a1ef8c505dca2cdc07ffacb046d090d2bea06a'  # noqa: E501
     )
     expected = HexBytes('0x1f80f8ab5f12a45be218f76404bda64d37270a6f4f86ededd0eb599f80548c13')
     assert txn_hash == expected