[Ethereum] How to generate a keystore (UTC) file from the raw private key

encryptionkeystorenodejsprivate-key

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,

Best Answer

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.

Related Topic