1

I am using Infura to interact with the Ropsten testnet. I would like to send a transaction from one account to another, but the following code does not work:

w3 = Web3(Web3.HTTPProvider('InfuraUrl'))

transaction = {
    'from': 'accountAddress',
    'to': 'accountAddress',
    'value': w3.toWei(1, "ether"),
    'gas': 2000000,
    'chainId': 3
}

w3.eth.sendTransaction(transaction)

I suppose I have to use the private key obtained from Metamask. Please help if you know how to send a transaction using a local private key.

1

Infura is a public hosted node. It has no way to unlock your local accounts. Even if there was a way, it would be a security risk because anyone can connect to the same node and transfer your funds.

In order to send a transaction over Infura you will need to sign the transaction locally using web3.eth.sendRawTransaction(). The following example is from the web3.py documentation:

>>> transaction = {
...     'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
...     'value': 1000000000,
...     'gas': 2000000,
...     'gasPrice': 234567897654321,
...     'nonce': 0,
...     'chainId': 1
... }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
>>> signed = w3.eth.account.sign_transaction(transaction, key)
>>> signed.rawTransaction
HexBytes('0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428')
>>> signed.hash
HexBytes('0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60')
>>> signed.r
4487286261793418179817841024889747115779324305375823110249149479905075174044
>>> signed.s
30785525769477805655994251009256770582792548537338581640010273753578382951464
>>> signed.v
37

# When you run sendRawTransaction, you get back the hash of the transaction:
>>> w3.eth.sendRawTransaction(signed.rawTransaction)  
'0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60'
  • Thanks, It work but I don't see wallet update on the Metamask – Marta Sędziwa Sep 30 '19 at 9:28
  • I'm unsure of how to trigger an update in MetaMask, sorry. – iamdefinitelyahuman Sep 30 '19 at 10:10
0

To get access to the private key for using web3.js or pyethereum, uou need to export it from Metamask, by clicking the "..." next to the account name, choose "Account Settings" and scrolling down until you see a box labeled "Private Key". Use that as the key variable in the pyethereum example above, and be careful not to remove it afterwards and not to share it with anyone. Afterwards, you can check the transaction hash on Etherscan, e.g. etherscan.io/tx/… (except that tx hash was never submitted in the pyethereum example above, so there are no results for it).

Good luck!

0

you can try web3-hdwallet-provider to sign transactions for addresses derived from a 12-word mnemonic.

const Web3 = require('web3');
const Web3HDWalletProvider = require('web3-hdwallet-provider');

const httpProvider = new Web3.providers.HttpProvider('InfuraUrl');
const mnemonic = 'YOUR PRIVATE KEY';
const web3 = new Web3HDWalletProvider(mnemonic, httpProvider)

transaction = {
'from': 'accountAddress',
'to': 'accountAddress',
'value': w3.toWei(1, "ether"),
'gas': 2000000,
'chainId': 3}

web3.eth.sendTransaction(transaction)

Note: be careful to expose your private key, use the file path instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.