示例#1
0
文件: manager.py 项目: euri10/web3.py
    def request_blocking(self, method, params):
        if method == 'eth_sendTransaction':
            base_transaction = params[0]
            # create a fully signed transaction and send through the
            # `eth_sendRawTransaction` endpoint instead.
            full_transaction = self.construct_full_transaction(base_transaction)
            raw_transaction_bytes = self.sign_and_serialize_transaction(
                full_transaction,
            )
            raw_transaction_bytes_as_hex = encode_hex(raw_transaction_bytes)
            return self.request_blocking(
                'eth_sendRawTransaction', [raw_transaction_bytes_as_hex],
            )

        result = super(BaseSendRawTransactionMixin, self).request_blocking(
            method, params,
        )
        if method in self.TXN_SENDING_METHODS:
            if method == 'eth_sendRawTransaction':
                txn = rlp.decode(decode_hex(params[0]), Transaction)
                self._known_transactions[to_address(txn.sender)].add(result)
                self._known_nonces[to_address(txn.sender)].add(txn.nonce)
            else:
                txn = params[0]
                self._known_transactions[to_address(txn['from'])].add(result)
                if 'nonce' in txn:
                    self._known_nonces[to_address(txn['from'])].add(
                        to_decimal(txn['nonce'])
                    )
        return result
示例#2
0
    def request_blocking(self, method, params):
        if method == 'eth_sendTransaction':
            base_transaction = params[0]
            # create a fully signed transaction and send through the
            # `eth_sendRawTransaction` endpoint instead.
            full_transaction = self.construct_full_transaction(base_transaction)
            raw_transaction_bytes = self.sign_and_serialize_transaction(
                full_transaction,
            )
            raw_transaction_bytes_as_hex = encode_hex(raw_transaction_bytes)
            return self.request_blocking(
                'eth_sendRawTransaction', [raw_transaction_bytes_as_hex],
            )
 
        result = super(BaseSendRawTransactionMixin, self).request_blocking(
            method, params,
        )
        if method in self.TXN_SENDING_METHODS:
            if method == 'eth_sendRawTransaction':
                txn = rlp.decode(decode_hex(params[0]), Transaction)
                self._known_transactions[to_address(txn.sender)].add(result)
                self._known_nonces[to_address(txn.sender)].add(txn.nonce)
            else:
                txn = params[0]
                self._known_transactions[to_address(txn['from'])].add(result)
                if 'nonce' in txn:
                    self._known_nonces[to_address(txn['from'])].add(
                        to_decimal(txn['nonce'])
                    )
        return result
示例#3
0
 def sign_and_serialize_transaction(self, transaction):
     txn_from = to_address(transaction['from'])
     if txn_from not in self.keys:
         raise KeyError("No signing key registered for from address: {0}".format(txn_from))
     transaction = Transaction(
         nonce=to_decimal(transaction['nonce']),
         gasprice=to_decimal(transaction['gasPrice']),
         startgas=to_decimal(transaction['gas']),
         to=transaction['to'],
         value=to_decimal(transaction['value']),
         data=decode_hex(transaction['data']),
     )
     transaction.sign(self.keys[txn_from])
     assert to_address(transaction.sender) == txn_from
     return rlp.encode(transaction, Transaction)
示例#4
0
文件: manager.py 项目: euri10/web3.py
 def sign_and_serialize_transaction(self, transaction):
     txn_from = to_address(transaction['from'])
     if txn_from not in self.keys:
         raise KeyError("No signing key registered for from address: {0}".format(txn_from))
     transaction = Transaction(
         nonce=to_decimal(transaction['nonce']),
         gasprice=to_decimal(transaction['gasPrice']),
         startgas=to_decimal(transaction['gas']),
         to=transaction['to'],
         value=to_decimal(transaction['value']),
         data=decode_hex(transaction['data']),
     )
     transaction.sign(self.keys[txn_from])
     assert to_address(transaction.sender) == txn_from
     return rlp.encode(transaction, Transaction)
示例#5
0
def test_adding_signature_to_transaction(wait_for_first_block, web3,
                                         skip_if_testrpc):
    skip_if_testrpc(web3)

    transaction = {
        'nonce': hex(0),
        'to': '0xd3cda913deb6f67967b99d67acdfa1712c293601',
        'value': hex(12345),
        'gas': hex(1200300),
        'gasPrice': hex(54321),
        'data': '0x1234567890abcdef',
    }

    serialized_txn = serialize_transaction(transaction)
    signature_hex = web3.eth.sign(web3.eth.coinbase, serialized_txn)

    signed_transaction = add_signature_to_transaction(
        serialized_txn,
        decode_hex(signature_hex),
    )

    assert to_address(signed_transaction.to) == transaction['to']
    assert signed_transaction.startgas == int(transaction['gas'], 16)
    assert signed_transaction.gasprice == int(transaction['gasPrice'], 16)
    assert signed_transaction.nonce == int(transaction['nonce'], 16)
    assert signed_transaction.value == int(transaction['value'], 16)
    assert encode_hex(signed_transaction.data) == transaction['data']
    assert signed_transaction.sender == web3.eth.coinbase
def test_adding_signature_to_transaction(wait_for_first_block,
                                         web3,
                                         skip_if_testrpc):
    skip_if_testrpc(web3)

    transaction = {
        'nonce': hex(0),
        'to': '0xd3cda913deb6f67967b99d67acdfa1712c293601',
        'value': hex(12345),
        'gas': hex(1200300),
        'gasPrice': hex(54321),
        'data': '0x1234567890abcdef',
    }

    serialized_txn = serialize_transaction(transaction)
    signature_hex = web3.eth.sign(web3.eth.coinbase, serialized_txn)

    signed_transaction = add_signature_to_transaction(
        serialized_txn,
        decode_hex(signature_hex),
    )

    assert to_address(signed_transaction.to) == transaction['to']
    assert signed_transaction.startgas == int(transaction['gas'], 16)
    assert signed_transaction.gasprice == int(transaction['gasPrice'], 16)
    assert signed_transaction.nonce == int(transaction['nonce'], 16)
    assert signed_transaction.value == int(transaction['value'], 16)
    assert encode_hex(signed_transaction.data) == transaction['data']
    assert signed_transaction.sender == web3.eth.coinbase
示例#7
0
def test_transaction_serialization():
    transaction = {
        'nonce': hex(0),
        'to': '0xd3cda913deb6f67967b99d67acdfa1712c293601',
        'value': hex(12345),
        'gas': hex(1200300),
        'gasPrice': hex(54321),
        'data': '0x1234567890abcdef',
    }

    serialized_txn = serialize_transaction(transaction)
    unserialized_txn = rlp.decode(serialized_txn, UnsignedTransaction)

    assert to_address(unserialized_txn.to) == transaction['to']
    assert unserialized_txn.startgas == int(transaction['gas'], 16)
    assert unserialized_txn.gasprice == int(transaction['gasPrice'], 16)
    assert unserialized_txn.nonce == int(transaction['nonce'], 16)
    assert unserialized_txn.value == int(transaction['value'], 16)
    assert encode_hex(unserialized_txn.data) == transaction['data']
def test_transaction_serialization():
    transaction = {
        'nonce': hex(0),
        'to': '0xd3cda913deb6f67967b99d67acdfa1712c293601',
        'value': hex(12345),
        'gas': hex(1200300),
        'gasPrice': hex(54321),
        'data': '0x1234567890abcdef',
    }

    serialized_txn = serialize_transaction(transaction)
    unserialized_txn = rlp.decode(serialized_txn, UnsignedTransaction)

    assert to_address(unserialized_txn.to) == transaction['to']
    assert unserialized_txn.startgas == int(transaction['gas'], 16)
    assert unserialized_txn.gasprice == int(transaction['gasPrice'], 16)
    assert unserialized_txn.nonce == int(transaction['nonce'], 16)
    assert unserialized_txn.value == int(transaction['value'], 16)
    assert encode_hex(unserialized_txn.data) == transaction['data']
def account_public_key(account_private_key):
    from ethereum.utils import privtoaddr
    from eth_tester_client.utils import encode_address
    return to_address(encode_address(privtoaddr(account_private_key)))
示例#10
0
def test_to_address(value, expected):
    assert to_address(value) == expected
示例#11
0
 def register_private_key(self, key):
     from bitcoin import privtopub
     address = to_address(sha3(privtopub(key)[1:])[-40:])
     self.keys[address] = key
示例#12
0
def account_public_key(account_private_key):
    from ethereum.utils import privtoaddr
    return to_address(privtoaddr(account_private_key))
示例#13
0
def test_to_address(value, expected):
    assert to_address(value) == expected
def account_public_key(account_private_key):
    from ethereum.utils import privtoaddr
    from eth_tester_client.utils import encode_address
    return to_address(encode_address(privtoaddr(account_private_key)))
示例#15
0
文件: manager.py 项目: euri10/web3.py
 def register_private_key(self, key):
     from bitcoin import privtopub
     address = to_address(sha3(privtopub(key)[1:])[-40:])
     self.keys[address] = key