web3 contains the eth object - web3.eth.
Definition
The actual encoding and decoding of the file remains largely unchanged from version 1, except that the crypto algorithm is no longer fixed to AES-128-CBC (AES-128-CTR is now the minimal requirement). Most of the meanings/algorithm are similar to version 1, exceptmac, which is given as the SHA3 (keccak-256) of the concatenations of the second-leftmost 16 bytes of the derived key together with the full ciphertext.
Secret key files are stored directly in ~/.web3/keystore (for Unix-like systems) and ~/AppData/Web3/keystore (for Windows). They may be named anything, but a good convention is <uuid>.json, where <uuid> is the 128-bit UUID given to the secret key (a privacy-preserving proxy for the secret key’s address).
All such files have an associated password. To derive a given .json file’s secret key, first derive the file’s encryption key; this is done through taking the file’s password and passing it through a key derivation function as described by the kdf key. KDF-dependent static and dynamic parameters to the KDF function are described in kdfparams key.
PBKDF2 must be supported by all minimally-compliant implementations, denoted though:
kdf:pbkdf2
prf: Must behmac-sha256(may be extended in the future);c: number of iterations;salt: salt passed to PBKDF;dklen: length for the derived key. Must be >= 32.
ciphertext key’s contents, i.e.:
++ is the concatenation operator)
This value should be compared to the contents of the mac key; if they are different, an alternative password should be requested (or the operation cancelled).
After the file’s key has been verified, the cipher text (the ciphertext key in the file) may be decrypted using the symmetric encryption algorithm specified by the cipher key and parameterised through the cipherparams key. If the derived key size and the algorithm’s key size are mismatched, the zero padded, rightmost bytes of the derived key should be used as the key to the algorithm.
All minimally-compliant implementations must support the AES-128-CTR algorithm, denoted through:
cipher: aes-128-ctr
iv: 128-bit initialisation vector for the cipher.
DK[0..15]
The creation/encryption of a secret key should be essentially the reverse of these instructions. Make sure the uuid, salt and iv are actually random.
In addition to the version field, which should act as a “hard” identifier of version, implementations may also use minorversion to track smaller, non-breaking changes to the format.
Test vectors
Details:Address:008aeeda4d805471df9b2a5b0f38a0c3bcba786bICAP:XE542A5PZHH8PYIZUBEJEO0MFWRAPPIL67UUID:3198bc9c-6672-5ab3-d9954942343ae5b6Password:testpasswordSecret:7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d
PBKDF2-SHA-256
Test vector usingAES-128-CTR and PBKDF2-SHA-256:
File contents of ~/.web3/keystore/3198bc9c-6672-5ab3-d9954942343ae5b6.json:
Derived key: f06d69cdc7da0faffb1008270bca38f5e31891a3a773950e6d0fea48a7188551
MAC Body: e31891a3a773950e6d0fea48a71885515318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46
MAC: 517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2
Cipher key: f06d69cdc7da0faffb1008270bca38f5
Scrypt
Test vector using AES-128-CTR and Scrypt:Derived key: fac192ceb5fd772906bea3e118a69e8bbb5cc24229e20d8766fd298291bba6bd
MAC Body: bb5cc24229e20d8766fd298291bba6bdd172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c
MAC: 2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097
Cipher key: fac192ceb5fd772906bea3e118a69e8b
ALTERATIONS FROM VERSION 1
This version fixes several inconsistencies with the version 1 published here↗. In brief these are:- Capitalisation is unjustified and inconsistent (scrypt lowercase, Kdf mixed-case, MAC uppercase).
- Address unnecessary and compromises privacy.
Saltis intrinsically a parameter of the key derivation function and deserves to be associated with it, not with the crypto in general.- SaltLen unnecessary (just derive it from Salt).
- The key derivation function is given, yet the crypto algorithm is hard specified.
Versionis intrinsically numeric yet is a string (structured versioning would be possible with a string, but can be considered out of scope for a rarely changing configuration file format).KDFandcipherare notionally sibling concepts yet are organised differently.MACis calculated through a whitespace agnostic piece of data(!)

