2021-09-07 12:30:12 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 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
|
2021-09-07 13:45:01 +00:00
|
|
|
accounts/manager,
|
|
|
|
stew/results,
|
|
|
|
eth/keys
|
2021-09-07 12:30:12 +00:00
|
|
|
|
|
|
|
export manager
|
|
|
|
|
|
|
|
type
|
|
|
|
EthContext* = ref object
|
|
|
|
am*: AccountsManager
|
2021-09-07 13:45:01 +00:00
|
|
|
# You should only create one instance of the RNG per application / library
|
|
|
|
# Ref is used so that it can be shared between components
|
2022-07-04 07:38:02 +00:00
|
|
|
rng*: ref HmacDrbgContext
|
2021-09-07 12:30:12 +00:00
|
|
|
|
|
|
|
proc newEthContext*(): EthContext =
|
|
|
|
result = new(EthContext)
|
|
|
|
result.am = AccountsManager.init()
|
2021-09-07 13:45:01 +00:00
|
|
|
result.rng = newRng()
|
|
|
|
|
|
|
|
proc randomPrivateKey*(ctx: EthContext): PrivateKey =
|
|
|
|
random(PrivateKey, ctx.rng[])
|
|
|
|
|
|
|
|
proc randomKeyPair*(ctx: EthContext): KeyPair =
|
|
|
|
random(KeyPair, ctx.rng[])
|
|
|
|
|
|
|
|
proc hexToKeyPair*(ctx: EthContext, hexPrivateKey: string): Result[KeyPair, string] =
|
|
|
|
if hexPrivateKey.len == 0:
|
|
|
|
let privateKey = ctx.randomPrivateKey()
|
|
|
|
ok(privateKey.toKeyPair())
|
|
|
|
else:
|
|
|
|
let res = PrivateKey.fromHex(hexPrivateKey)
|
|
|
|
if res.isErr:
|
|
|
|
return err($res.error)
|
|
|
|
ok(res.get().toKeyPair())
|