4

I am working with https://github.com/ethereum/wiki/wiki/JSON-RPC api + curl .

And implementing custom api using this php library: https://github.com/Achse/geth-jsonrpc-php-client

below method is working fine for me for unlock before transaction:

$ curl -X POST --data '{"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0x7642b...", "password", 3600],"id":67}' http://localhost:8545

But I want to unlock account using private key not by password. So is there any way to unlock account and do transaction using private key.

2 Answers2

2

You don't need to unlock your account if you know your private key. The reason is that transactions are signed using your private key, and unlocking your account is needed to obtain your private key from your keyfile. So if you already know your private key, there is no need to unlock your account.

You can use myetherwallet.com or ethychat.io for making transactions using your private key.

If you want to make transactions using JSON-RPC and your private key, you would probably have to create a signed transaction and send this using eth_sendRawTransaction.

gisdev_p
  • 1,801
  • 1
  • 9
  • 18
  • 2
    how can I signed transaction using private key – Yogesh Karodiya Nov 06 '17 at 07:02
  • Isn't it that the private key is stored in an encrypted format, and the password is needed to decrypt it? So, even to sign the transaction, the password is needed, unless the OP has the unencrypted private key (which he did not specifically mention). – Ajoy Bhatia Nov 09 '17 at 17:04
2

you can generate a raw transaction as described here or use myetherwallet to get the signed raw transaction and then call

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":[{raw_transaction}],"id":1}'

// Result
{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
}

as explained here

besides as @yogesh has mentioned in his comment bellow Ethereumjs build for browser can be used to build the rawtransaction (in client side).

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • I am using curl + json rpc so how can I achive it ? – Yogesh Karodiya Nov 06 '17 at 14:20
  • no I don't want to use third party like myetherwallet so I am looking solution for generate raw_transaction by api – Yogesh Karodiya Nov 07 '17 at 03:30
  • in my answer i have referenced a blog post explaining how to do it using some Apis https://medium.com/blockchain-musings/how-to-create-raw-transactions-in-ethereum-part-1-1df91abdba7c – Badr Bellaj Nov 07 '17 at 09:44
  • these are js library so how can I connect with php code. – Yogesh Karodiya Nov 07 '17 at 10:09
  • https://ethereum.stackexchange.com/questions/13826/php-library-for-ethereum – Badr Bellaj Nov 07 '17 at 11:40
  • Thanks for your support @Badr I used following library for browser build https://github.com/ethereumjs/browser-builds and get hash for rawtransaction. just update your answer with this data so i can accept your answer so that other developer get benefits for it. – Yogesh Karodiya Nov 07 '17 at 11:52
  • @YogeshKarodiya great to hear that, i'll add your link – Badr Bellaj Nov 07 '17 at 12:05
  • @BadrBellaj - You can see in the medium.com post that you linked to, that the code to create the raw transaction, it has to be signed with the private key, and the private key is obtained from the keyfile by the code in the previous block, which uses the password. So, unless OP already has the unencrypted private key imported from keyfile, the password is needed to sign transaction. – Ajoy Bhatia Nov 09 '17 at 17:10
  • who assumed that the private key is encrypted? have you read it in the question? – Badr Bellaj Nov 09 '17 at 17:37
  • I may be wrong but going by OP's rep score, he seems to be new, which makes me think that he may not realize that just having the key file does not mean that you have the private key. At least, we should ask to check if OP can confirm that he has unencrypted private key. Did he say that it is unencrypted? (You are a senior member, so I did not expect the rude tone in your comment) – Ajoy Bhatia Nov 09 '17 at 23:25
  • @AjoyBhatia my friend i didn't mean to be rude,I'm sorry if it sounded that way to you. i was just wondering if i have missed something. – Badr Bellaj Nov 09 '17 at 23:52