mirror of
https://github.com/status-im/nim-codex.git
synced 2025-01-09 18:36:29 +00:00
0ecbfcec9f
* setup and persist private key * return dht record spr * helper to remap multiaddr ip and port * set/update discovery and announce addrs * add nat and discovery IPs * allow for announce and DHT addresses separatelly * update tests * check for nat or discoveryIp * fix integration tests * misc align * don't share data dirs and and set bootstrap node * add log scope * remap announceAddrs after node start * simplify discovery initialization * make nat and disc-ip required * add log scope don't init dht spr in constructor * bump dht * dissallow `0.0.0.0` for `--nat`
51 lines
1.4 KiB
Nim
51 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 std/os
|
|
|
|
import pkg/chronicles
|
|
import pkg/questionable/results
|
|
import pkg/libp2p
|
|
|
|
import ./fileutils
|
|
import ../conf
|
|
import ../errors
|
|
import ../rng
|
|
|
|
const
|
|
SafePermissions = {UserRead, UserWrite}
|
|
|
|
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.writeFile(bytes, SafePermissions.toInt()).mapFailure(CodexKeyError)
|
|
return PrivateKey.init(bytes).mapFailure(CodexKeyError)
|
|
|
|
info "Found a network private key"
|
|
if path.getPermissionsSet().get() != SafePermissions:
|
|
warn "The network private key file is not safe, aborting"
|
|
return failure newException(
|
|
CodexKeyUnsafeError, "The network private key file is not safe")
|
|
|
|
return PrivateKey.init(
|
|
? path.readAllBytes().mapFailure(CodexKeyError))
|
|
.mapFailure(CodexKeyError)
|