2018-03-02 11:57:43 +01:00
|
|
|
# Nim Eth-keys
|
2018-02-13 19:20:27 +01:00
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
2018-03-02 11:57:43 +01: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.
|
2018-02-13 19:20:27 +01:00
|
|
|
|
2018-03-21 14:11:48 +01:00
|
|
|
import ./private/conversion_bytes
|
|
|
|
export toHex, hexToByteArrayBE, hexToSeqByteBE
|
2018-02-14 19:50:36 +01:00
|
|
|
|
2018-02-13 19:20:27 +01:00
|
|
|
|
2018-03-21 14:11:48 +01:00
|
|
|
# Note: Fields are intentionally kept private
|
2018-02-13 19:20:27 +01:00
|
|
|
type
|
|
|
|
PublicKey* = object
|
2018-03-21 14:11:48 +01:00
|
|
|
Fraw_key: array[64, byte]
|
2018-02-13 19:20:27 +01:00
|
|
|
|
|
|
|
PrivateKey* = object
|
2018-03-21 14:11:48 +01:00
|
|
|
Fraw_key: array[32, byte]
|
|
|
|
Fpublic_key: PublicKey
|
2018-02-13 19:20:27 +01:00
|
|
|
|
2018-02-14 19:50:36 +01:00
|
|
|
Signature* {.packed.}= object
|
2018-03-21 14:11:48 +01:00
|
|
|
Fr: array[32, byte]
|
|
|
|
Fs: array[32, byte]
|
|
|
|
Fv: range[0.byte .. 1.byte]
|
|
|
|
|
|
|
|
|
|
|
|
# "Public" accessors, only exposed to internal modules
|
|
|
|
|
|
|
|
template genAccessors(name: untyped, fieldType, objType: typedesc): untyped =
|
|
|
|
# Access
|
|
|
|
proc name*(obj: objType): fieldType {.noSideEffect, inline, noInit.} =
|
|
|
|
obj.`F name`
|
|
|
|
|
|
|
|
# Assignement
|
|
|
|
proc `name=`*(obj: var objType, value: fieldType): fieldType {.noSideEffect, inline.} =
|
|
|
|
obj.`F name` = value
|
|
|
|
|
|
|
|
# Mutable
|
|
|
|
proc `name`*(obj: var objType): var fieldType {.noSideEffect, inline.} =
|
|
|
|
obj.`F name`
|
|
|
|
|
|
|
|
genAccessors(raw_key, array[64, byte], PublicKey)
|
|
|
|
genAccessors(raw_key, array[32, byte], PrivateKey)
|
|
|
|
genAccessors(public_key, PublicKey, PrivateKey)
|
|
|
|
genAccessors(s, array[32, byte], Signature)
|
|
|
|
genAccessors(r, array[32, byte], Signature)
|
|
|
|
genAccessors(v, range[0.byte .. 1.byte], Signature)
|