Skip to content

Commit

Permalink
fix(keyfile/rln): better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
s1fr0 committed Oct 27, 2022
1 parent 9b6850e commit 6aa7805
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ proc writeRlnCredentials*(path: string, credentials: RlnMembershipCredentials, p

# Attempts decryptions of all keyfiles with the provided password.
# If one or more credentials are successfully decrypted, the max(min(index,number_decrypted),0)-th is returned.
proc readRlnCredentials*(path: string, password: string, index: int = 0): RlnRelayResult[Option[RlnMembershipCredentials]] {.raises: [Defect, IOError, OSError].} =
proc readRlnCredentials*(path: string, password: string, index: int = 0): RlnRelayResult[Option[RlnMembershipCredentials]] =
info "Reading RLN credentials"
# With regards to printing the keys, it is purely for debugging purposes so that the user becomes explicitly aware of the current keys in use when nwaku is started.
# Note that this is only until the RLN contract being used is the one deployed on Goerli testnet.
Expand Down
49 changes: 29 additions & 20 deletions waku/v2/utils/keyfile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type
IncorrectMac = "keyfile error: `mac` verification failed"
ScryptBadParam = "keyfile error: bad scrypt's parameters"
OsError = "keyfile error: OS specific error"
IoError = "keyfile error: IO specific error"
JsonError = "keyfile error: JSON encoder/decoder error"
KeyfileDoesNotExist = "keyfile error: file does not exist"

Expand Down Expand Up @@ -508,7 +509,7 @@ proc decodeKeyFileJson*(j: JsonNode,

# Loads the file at pathname, decrypts and returns all keyfiles encrypted under password
proc loadKeyFiles*(pathname: string,
password: string): KfResult[seq[KfResult[seq[byte]]]] {.raises: [Defect, IOError].} =
password: string): KfResult[seq[KfResult[seq[byte]]]] =
## Load and decode data from file with pathname
## ``pathname``, using password string ``password``.
## The index successful decryptions is returned
Expand All @@ -520,25 +521,33 @@ proc loadKeyFiles*(pathname: string,
return err(KeyfileDoesNotExist)

# Note that lines strips the ending newline, if present
for keyfile in lines(pathname):

# We skip empty lines
if keyfile.len == 0:
continue
# We skip all lines that doesn't seem to define a json
if keyfile[0] != '{' or keyfile[^1] != '}':
continue

try:
data = json.parseJson(keyfile)
except JsonParsingError:
return err(JsonError)
except Exception: # json raises Exception
return err(OsError)

decodedKeyfile = decodeKeyFileJson(data, password)
if decodedKeyfile.isOk():
successfullyDecodedKeyfiles.add decodedKeyfile
try:
for keyfile in lines(pathname):

# We skip empty lines
if keyfile.len == 0:
continue
# We skip all lines that doesn't seem to define a json
if keyfile[0] != '{' or keyfile[^1] != '}':
continue

try:
data = json.parseJson(keyfile)
except JsonParsingError:
return err(JsonError)
except ValueError:
return err(JsonError)
except OSError:
return err(OsError)
except Exception: #parseJson raises Exception
return err(OsError)

decodedKeyfile = decodeKeyFileJson(data, password)
if decodedKeyfile.isOk():
successfullyDecodedKeyfiles.add decodedKeyfile

except IOError:
return err(IoError)

return ok(successfullyDecodedKeyfiles)

Expand Down

0 comments on commit 6aa7805

Please sign in to comment.