Creating a Scrypt Based KeyStore with Nethereum
This article explains how to create a keystore file using Nethereum.
Note
You also have the possibility to run similar code directly in your browser by using Nethereum’s playground at the following link: Key Store: Create Scrypt based KeyStore using custom params
Definition: A keystore is a JSON-encoded file that contains a single (randomly generated) private key, encrypted by a passphrase for extra security (using https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition#scrypt). Keystores are a standard way to store private keys locally to let clients such as Geth handle privateKey/signin for you. Nethereum offers a dedicated ‘keystore’ service to facilitate the creation and management of keystore files.
Required assemblies:
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
using Nethereum.KeyStore.Model;
1- Creating a Keystore File:
We first need to create an instance of KeyStoreScryptService
:
var keyStoreService = new Nethereum.KeyStore.KeyStoreScryptService();
scrypt
var scryptParams = new ScryptParams {Dklen = 32, N = 262144, R = 1, P = 8};
EthEcKey
function generates an Ethereum compliant privateKey:
var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
var password = "testPassword";
var keyStore = keyStoreService.EncryptAndGenerateKeyStore(password, ecKey.GetPrivateKeyAsBytes(), ecKey.GetPublicAddress(), scryptParams);
var json = keyStoreService.SerializeKeyStoreToJson(keyStore);
2- Decrypting a key
Extracting our private key is achieved using DecryptKeyStoreFromJson
var key = keyStoreService.DecryptKeyStoreFromJson(password, json);