Fix keystore loader bug (#2774)

This commit is contained in:
andri lim 2024-10-24 11:07:06 +07:00 committed by GitHub
parent fc5ea1c236
commit 0d4de335df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 3 deletions

View File

@ -36,10 +36,14 @@ proc loadKeystores*(am: var AccountsManager, path: string):
createDir(path)
for filename in walkDirRec(path):
var data = Json.loadFile(filename, JsonNode)
if data.kind != JObject:
return err("expect json object of keystore data: " & filename)
if not data.hasKey("address"):
return err("no 'address' field in keystore data: " & filename)
let address = Address.fromHex(data["address"].getStr())
am.accounts[address] = NimbusAccount(keystore: data, unlocked: false)
except CatchableError as exc:
return err("loadKeystrores: " & exc.msg)
return err("loadKeystores: " & exc.msg)
ok()

View File

@ -0,0 +1 @@
{"password":"monkeyelephant","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"3ca1d5c5151fba8281f8880ece920740"},"ciphertext":"ec8b7626f3494605e4b66a889c3dbf9bae4d8fe249718d25f010441af3cf6c61","kdf":"pbkdf2","kdfparams":{"dklen":32,"c":1000000,"prf":"hmac-sha256","salt":"364a67e8bc0a782d715832946662850c"},"mac":"7001a9fb0f63db2b7538bab2a34d2103c67035f4754a3bcf4760359f530d5c21"},"id":"5098841b-9060-4ce7-baf7-c748b12d79c9","version":3}

View File

@ -0,0 +1 @@
"a string"

View File

@ -9,12 +9,12 @@
# according to those terms.
import
std/[os],
std/[os, strutils],
pkg/[unittest2],
eth/common/[base, keys],
stew/byteutils,
../nimbus/config,
../nimbus/common/[chain_config, context],
../nimbus/common/[chain_config, context, manager],
./test_helpers
proc configurationMain*() =
@ -295,5 +295,17 @@ proc configurationMain*() =
check conf.dataDir.string == defaultDataDir()
check conf.keyStore.string == "banana"
test "loadKeystores missing address":
var am = AccountsManager.init()
let res = am.loadKeystores("tests/invalid_keystore/missingaddress")
check res.isErr
check res.error.find("no 'address' field in keystore data:") == 0
test "loadKeystores not an object":
var am = AccountsManager.init()
let res = am.loadKeystores("tests/invalid_keystore/notobject")
check res.isErr
check res.error.find("expect json object of keystore data:") == 0
when isMainModule:
configurationMain()