2021-07-27 11:28:05 +00:00
|
|
|
# Nimbus
|
2024-05-30 12:54:03 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-07-27 11:28:05 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Recover Address From Signature
|
|
|
|
## ==============================
|
|
|
|
##
|
|
|
|
## This module provides caching and direct versions for recovering the
|
|
|
|
## `EthAddress` from an extended signature. The caching version reduces
|
|
|
|
## calculation time for the price of maintaing it in a LRU cache.
|
|
|
|
|
|
|
|
import
|
|
|
|
../constants,
|
2022-01-18 13:05:00 +00:00
|
|
|
./utils_defs,
|
|
|
|
eth/[common, common/transaction, keys, rlp],
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/keyed_queue,
|
|
|
|
results,
|
2021-07-27 11:28:05 +00:00
|
|
|
stint
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
export
|
2022-12-02 04:39:12 +00:00
|
|
|
utils_defs, results
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
2022-01-18 13:05:00 +00:00
|
|
|
|
2021-07-27 11:28:05 +00:00
|
|
|
const
|
|
|
|
INMEMORY_SIGNATURES* = ##\
|
2022-01-18 13:05:00 +00:00
|
|
|
## Default number of recent block signatures to keep in memory
|
2021-07-27 11:28:05 +00:00
|
|
|
4096
|
|
|
|
|
|
|
|
type
|
2022-01-18 13:05:00 +00:00
|
|
|
EcKey* = ##\
|
|
|
|
## Internal key used for the LRU cache (derived from Hash256).
|
|
|
|
array[32,byte]
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
EcAddrResult* = ##\
|
|
|
|
## Typical `EthAddress` result as returned by `ecRecover()` functions.
|
|
|
|
Result[EthAddress,UtilsError]
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
EcRecover* = object
|
|
|
|
size: uint
|
|
|
|
q: KeyedQueue[EcKey,EthAddress]
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc encodePreSealed(header: BlockHeader): seq[byte] =
|
|
|
|
## Cut sigature off `extraData` header field.
|
|
|
|
if header.extraData.len < EXTRA_SEAL:
|
|
|
|
return rlp.encode(header)
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
var rlpHeader = header
|
|
|
|
rlpHeader.extraData.setLen(header.extraData.len - EXTRA_SEAL)
|
|
|
|
rlp.encode(rlpHeader)
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc hashPreSealed(header: BlockHeader): Hash256 =
|
2021-07-27 11:28:05 +00:00
|
|
|
## Returns the hash of a block prior to it being sealed.
|
2022-09-03 18:15:35 +00:00
|
|
|
keccakHash header.encodePreSealed
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc recoverImpl(rawSig: openArray[byte]; msg: Hash256): EcAddrResult =
|
2021-07-27 11:28:05 +00:00
|
|
|
## Extract account address from the last 65 bytes of the `extraData` argument
|
|
|
|
## (which is typically the bock header field with the same name.) The second
|
|
|
|
## argument `hash` is used to extract the intermediate public key. Typically,
|
|
|
|
## this would be the hash of the block header without the last 65 bytes of
|
|
|
|
## the `extraData` field reserved for the signature.
|
2022-01-18 13:05:00 +00:00
|
|
|
if rawSig.len < EXTRA_SEAL:
|
2021-07-27 11:28:05 +00:00
|
|
|
return err((errMissingSignature,""))
|
|
|
|
|
|
|
|
let sig = Signature.fromRaw(
|
2022-01-18 13:05:00 +00:00
|
|
|
rawSig.toOpenArray(rawSig.len - EXTRA_SEAL, rawSig.high))
|
2021-07-27 11:28:05 +00:00
|
|
|
if sig.isErr:
|
|
|
|
return err((errSkSigResult,$sig.error))
|
|
|
|
|
|
|
|
# Recover the public key from signature and seal hash
|
2022-04-08 04:54:11 +00:00
|
|
|
let pubKey = recover(sig.value, SkMessage(msg.data))
|
2021-07-27 11:28:05 +00:00
|
|
|
if pubKey.isErr:
|
|
|
|
return err((errSkPubKeyResult,$pubKey.error))
|
|
|
|
|
|
|
|
# Convert public key to address.
|
2022-01-18 13:05:00 +00:00
|
|
|
ok(pubKey.value.toCanonicalAddress)
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-01-18 13:05:00 +00:00
|
|
|
# Public function: straight ecRecover versions
|
2021-07-27 11:28:05 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc ecRecover*(header: BlockHeader): EcAddrResult =
|
|
|
|
## Extracts account address from the `extraData` field (last 65 bytes) of
|
|
|
|
## the argument header.
|
|
|
|
header.extraData.recoverImpl(header.hashPreSealed)
|
|
|
|
|
2021-07-27 11:28:05 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructor for caching ecRecover version
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc init*(er: var EcRecover; cacheSize = INMEMORY_SIGNATURES; initSize = 10) =
|
|
|
|
## Inialise recover cache
|
|
|
|
er.size = cacheSize.uint
|
|
|
|
er.q.init(initSize)
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc init*(T: type EcRecover;
|
|
|
|
cacheSize = INMEMORY_SIGNATURES; initSize = 10): T =
|
|
|
|
## Inialise recover cache
|
|
|
|
result.init(cacheSize, initSize)
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: miscellaneous
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
proc len*(er: var EcRecover): int =
|
|
|
|
## Returns the current number of entries in the LRU cache.
|
|
|
|
er.q.len
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-01-18 13:05:00 +00:00
|
|
|
# Public functions: caching ecRecover version
|
2021-07-27 11:28:05 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-05 22:01:18 +00:00
|
|
|
proc ecRecover*(er: var EcRecover; header: var BlockHeader): EcAddrResult =
|
2021-07-27 11:28:05 +00:00
|
|
|
## Extract account address from `extraData` field (last 65 bytes) of the
|
|
|
|
## argument header. The result is kept in a LRU cache to re-purposed for
|
|
|
|
## improved result delivery avoiding calculations.
|
2022-01-18 13:05:00 +00:00
|
|
|
let key = header.blockHash.data
|
|
|
|
block:
|
|
|
|
let rc = er.q.lruFetch(key)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2022-01-18 13:05:00 +00:00
|
|
|
return ok(rc.value)
|
|
|
|
block:
|
|
|
|
let rc = header.extraData.recoverImpl(header.hashPreSealed)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2022-01-18 13:05:00 +00:00
|
|
|
return ok(er.q.lruAppend(key, rc.value, er.size.int))
|
|
|
|
err(rc.error)
|
|
|
|
|
2024-06-05 22:01:18 +00:00
|
|
|
proc ecRecover*(er: var EcRecover; header: BlockHeader): EcAddrResult =
|
2022-01-18 13:05:00 +00:00
|
|
|
## Variant of `ecRecover()` for call-by-value header
|
|
|
|
var hdr = header
|
|
|
|
er.ecRecover(hdr)
|
|
|
|
|
2024-06-05 22:01:18 +00:00
|
|
|
proc ecRecover*(er: var EcRecover; hash: Hash256): EcAddrResult =
|
2022-01-18 13:05:00 +00:00
|
|
|
## Variant of `ecRecover()` for hash only. Will only succeed it the
|
|
|
|
## argument hash is uk the LRU queue.
|
|
|
|
let rc = er.q.lruFetch(hash.data)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2022-01-18 13:05:00 +00:00
|
|
|
return ok(rc.value)
|
|
|
|
err((errItemNotFound,""))
|
2021-07-27 11:28:05 +00:00
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Debugging
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
iterator keyItemPairs*(er: var EcRecover): (EcKey,EthAddress) =
|
2022-01-18 13:05:00 +00:00
|
|
|
var rc = er.q.first
|
2022-04-08 04:54:11 +00:00
|
|
|
while rc.isOk:
|
2022-01-18 13:05:00 +00:00
|
|
|
yield (rc.value.key, rc.value.data)
|
|
|
|
rc = er.q.next(rc.value.key)
|
2021-07-27 11:28:05 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|