12

If I have the plaintext private key, -how- can I generate a geth-compatible keystore file using Node.js?

I think I need to encrypt it using the scrypt algorithm by supplying a password, but I don't know how to do this. Any -preferrably browserifiable- examples would be highly appreciated.

Thanks,

5
  • 3
    Simplest is to import the key to Geth otherwise see ethereum.stackexchange.com/questions/1825/…
    – eth
    Jan 6, 2017 at 2:12
  • @eth Thanks! But I forgot to mention I want to do this in Node.js, without Geth. Updated the question accordingly.
    – jeff
    Jan 6, 2017 at 2:47
  • I thought that was the case that's why I commented instead of answering :) Yes, it's a good idea to make questions more specific. From the anatomy link you have the Web3 Secret Storage Definition and that might be enough to keep you going for a bit. (I'd answer if I knew more.)
    – eth
    Jan 6, 2017 at 2:57
  • 2
    There's a Python library for what you want to do, if it helps. Jan 6, 2017 at 6:31
  • You could also do in the desktop version of MyCrypto.com Sep 30, 2018 at 16:42

1 Answer 1

14

geth-compatible keystore file can be created in Node using ethereumjs-wallet library:

> var Wallet = require('ethereumjs-wallet');
> var key = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex');
> var wallet = Wallet.fromPrivateKey(key);
> wallet.toV3String('password');
'{"version":3,"id":"467233bf-45ec-423b-9548-bdc4a42aa099","address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"ciphertext":"17886b7ff355219dd20900543b9592fcd4dc6fe7d8f776f1a4d1c63993112181","cipherparams":{"iv":"434e4e71d2013a2d84e86a6e89efbb0b"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"7a785ab75fa906734788d85ff43a2c8e704af41881dd50a2d52abe08092f07ec","n":262144,"r":8,"p":1},"mac":"98d9a76960dcef22a5fd28a6bf47e5c68a71b30bcf353eccbf5a6555abec78a1"}}'

You can also specify additional options to control kdf, cipher and other wallet params. See here.

1
  • 4
    Nice. I've used geth account import privatekeyfile to produce the following JSON {"address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"cipher":"aes-128-ctr","ciphertext":"ff22d876d416cb97876dabfee6b6d00645de2d8dd5fca3b46f82112b1a3cffcd","cipherparams":{"iv":"798fcf346ea18c4c3c9e848118c94527"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"bcc9bdc5a24f37ea56269017325b7bda67813199e017d566d7c274722a29d2b6"},"mac":"57efcea80420e75ed4087f20ac0e37481959a3a8a884e4c4a42bcb4331962dfc"},"id":"f162e70f-4b7c-425b-ba84-3041edfc292a","version":3} Jan 9, 2017 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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