diff --git a/beacon_chain/spec/keystore.nim b/beacon_chain/spec/keystore.nim index ca33d6b07..5d285a8f4 100644 --- a/beacon_chain/spec/keystore.nim +++ b/beacon_chain/spec/keystore.nim @@ -6,13 +6,12 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - json, math, - stew/results, - nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, sysrand, utils], + json, math, strutils, + eth/keyfile/uuid, + stew/[results, byteutils], + nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, sysrand], ./crypto -import strutils except fromHex - export results type @@ -109,11 +108,11 @@ proc decryptKeystore*(data, passphrase: string): KsResult[seq[byte]] = crypto = ks{"crypto"}.to(Crypto[KdfPbkdf2]) kdfParams = crypto.kdf.params - salt = fromHex(kdfParams.salt) + salt = hexToSeqByte(kdfParams.salt) decKey = sha256.pbkdf2(passphrase, salt, kdfParams.c, kdfParams.dklen) - iv = fromHex(crypto.cipher.params.iv) - cipherMsg = fromHex(crypto.cipher.message) - checksumMsg = fromHex(crypto.checksum.message) + iv = hexToSeqByte(crypto.cipher.params.iv) + cipherMsg = hexToSeqByte(crypto.cipher.message) + checksumMsg = hexToSeqByte(crypto.checksum.message) else: return err "ks: unknown cipher" @@ -166,7 +165,7 @@ proc encryptKeystore*[T: KdfParams](secret: openarray[byte]; pbkdf2Params.dklen) var kdf = Kdf[KdfPbkdf2](function: "pbkdf2", params: pbkdf2Params, message: "") - kdf.params.salt = kdfSalt.toHex(lowercase=true) + kdf.params.salt = kdfSalt.toHex() else: return @@ -185,17 +184,17 @@ proc encryptKeystore*[T: KdfParams](secret: openarray[byte]; kdf: kdf, checksum: Checksum( function: "sha256", - message: sum.toHex(lowercase=true) + message: sum.toHex() ), cipher: Cipher( function: "aes-128-ctr", - params: CipherParams(iv: aesIv.toHex(lowercase=true)), - message: cipherMsg.toHex(lowercase=true) + params: CipherParams(iv: aesIv.toHex()), + message: cipherMsg.toHex() ) ), pubkey: pubkey.toHex(), path: path, - uuid: "", # TODO: uuid library? + uuid: $(uuidGenerate().tryGet()), # error handling? version: 4 ) diff --git a/tests/test_keystore.nim b/tests/test_keystore.nim index b2b3c17af..ce0f4aad1 100644 --- a/tests/test_keystore.nim +++ b/tests/test_keystore.nim @@ -9,7 +9,7 @@ import unittest, ./testutil, json, - nimcrypto/utils, + stew/byteutils, ../beacon_chain/spec/keystore from strutils import replace @@ -80,10 +80,9 @@ const const password = "testpassword" - secret = fromHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") - salt = fromHex("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") - iv = fromHex("264daa3f303d7259501c93d997d84fe6") - uuid = "64625def-3331-4eea-ab6f-782f3ed16a83" + secret = hexToSeqByte("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") + salt = hexToSeqByte("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") + iv = hexToSeqByte("264daa3f303d7259501c93d997d84fe6") suiteReport "Keystore": timedTest "Pbkdf2 decryption": @@ -94,9 +93,15 @@ suiteReport "Keystore": timedTest "Pbkdf2 encryption": let encrypt = encryptKeystore[KdfPbkdf2](secret, password, salt=salt, iv=iv, path="m/12381/60/0/0", ugly=false) - check encrypt.isOk - check encrypt.get() == pbkdf2Vector.replace(uuid, "") + + var + encryptJson = parseJson(encrypt.get()) + pbkdf2Json = parseJson(pbkdf2Vector) + encryptJson{"uuid"} = %"" + pbkdf2Json{"uuid"} = %"" + + check encryptJson == pbkdf2Json timedTest "Pbkdf2 error": check encryptKeystore[KdfPbkdf2](secret, "", salt = [byte 1]).isErr