I'm currently developing an application on top of Ethereum's JSON RPC. I would like to be able to unlock and lock the wallet as needed, similar to how Bitcoin does it. However, it seems that at the moment it's only possible to unlock a geth wallet with the console through personal.unlockAccount. I'm wondering - is that the only way to unlock a geth wallet, or is there some way to do it through some other means, ideally with JSON RPC?
2 Answers
It is safer to use the Go Ethereum - Management APIs - personal_sendTransaction API to unlock your account AND send your transaction at the same time:
personal_sendTransaction
Validate the given passphrase and submit transaction.
The transaction is the same argument as for eth_sendTransaction and contains the from address. If the passphrase can be used to decrypt the private key belogging to tx.from the transaction is verified, signed and send onto the network. The account is not unlocked globally in the node and cannot be used in other RPC calls.
Client Method invocation Console personal.signAndSendTransaction(tx, passphrase) RPC {"method": "personal_sendTransaction", "params": [tx, string]}
Note, prior to Geth 1.5, please use personal_signAndSendTransaction as that was the original introductory name and only later renamed to the current final version.
Examples
var tx = {from: "0x391694e7e0b0cce554cb130d723a9d27458f9298", to: "0xafa3f8684e54059998bc3a7b0d2b0da075154d66", value: web3.toWei(1.23, "ether")} undefined personal.sendTransaction(tx, "passphrase") 0x8474441674cdd47b35b875fd1a530b800b51a5264b9975fb21129eeb8c18582f
Your account is only unlocked for the specified transaction.
You will have to enable the personal API over the RPC interface using the geth
API option:
geth --rpc --rpcapi "eth,net,web3,personal" [other option] console
You may want to read the following links about the safety of allowing the personal API over RPC:
- Can I unlock an account using JSON-RPC?
- How can I make new account by JSON-RPC?
- How to reduce the chances of your Ethereum wallet getting hacked?
Update 21/07/2016 - Responding to comment below
I searched the geth
github repository for personal_sendTransaction
and could not find the text. I however found internal/web3ext/web3ext.go#L435-L440:
new web3._extend.Method({
name: 'signAndSendTransaction',
call: 'personal_signAndSendTransaction',
params: 2,
inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
})
Like you said, the documentation seems outdated.
-
Hmm, I tried your method and it didn't work - I got an error of
The method personal_sendTransaction does not exist/is not available
. However, I did manage to usepersonal_listAccounts
andpersonal_unlockAccount
- I'm not entirely sure what would cause those to work, but not the sendTransaction... Jul 21, 2016 at 12:28 -
Hmm, it looks like
personal_signAndSendTransaction
is working - the Maangement APIs document seems to be outdated... Jul 21, 2016 at 12:33 -
1
Yes, using the "personal_unlockAccount" API call
{
'jsonrpc':'2.0',
'method':'personal_unlockAccount',
'params':[address, password, seconds],
'id':1
};