nim-codex/codex/utils/keyutils.nim
Jaremy Creechley 7efa9177df
Bump deps take2 (#492)
* extra utilities and tweaks
* add atlas lock
* update ignores
* break build into it's own script
* update url rules
* base off codexdht's
* compile fixes for Nim 1.6.14
* update submodules
* convert mapFailure to procs to work around type resolution issues
* add toml parser for multiaddress
* change error type on keyutils
* bump nimbus build to use 1.6.14
* update gitignore
* adding new deps submodules
* bump nim ci version
* even more fixes
* more libp2p changes
* update keys
* fix eventually function
* adding coverage test file
* move coverage to build.nims
* use nimcache/coverage
* move libp2p import for tests into helper.nim
* remove named bin
* bug fixes for networkpeers (from Dmitriy)

---------

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
2023-08-01 16:47:57 -07:00

45 lines
1.4 KiB
Nim

## Nim-Codex
## Copyright (c) 2022 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import pkg/upraises
push: {.upraises: [].}
import pkg/chronicles
import pkg/questionable/results
import pkg/libp2p/crypto/crypto
import ./fileutils
import ../errors
import ../rng
export crypto
type
CodexKeyError = object of CodexError
CodexKeyUnsafeError = object of CodexKeyError
proc setupKey*(path: string): ?!PrivateKey =
if not path.fileAccessible({AccessFlags.Find}):
info "Creating a private key and saving it"
let
res = ? PrivateKey.random(Rng.instance()[]).mapFailure(CodexKeyError)
bytes = ? res.getBytes().mapFailure(CodexKeyError)
? path.secureWriteFile(bytes).mapFailure(CodexKeyError)
return PrivateKey.init(bytes).mapFailure(CodexKeyError)
info "Found a network private key"
if not ? checkSecureFile(path).mapFailure(CodexKeyError):
warn "The network private key file is not safe, aborting"
return failure newException(
CodexKeyUnsafeError, "The network private key file is not safe")
let kb = ? path.readAllBytes().mapFailure(CodexKeyError)
return PrivateKey.init(kb).mapFailure(CodexKeyError)